from tools import * from pathlib import Path from tempfile import TemporaryDirectory import subprocess import shutil import os import datetime from tools import convert_zh_to_tw, import_translation_json # ========== 配置区域 ========== IMPORTLANG = input("🟢 語言是?(留空則為zh_CN):") # 語言選項 if len(IMPORTLANG) == 0: IMPORTLANG = "zh_CN" VERSION = "v" + input("🟢 版本號是?(留空則為當日日期):") # 版本號 if len(VERSION) == 1: VERSION = "build-in-" + str(datetime.date.today()) WEBLATE_PATH = os.path.abspath("../psoutertale-weblate") SOURCE_DIR = Path("../Petrichor-PSTVL").resolve() # ============================== # WTGIIIR協定 # Wasneet: That's Great If It Is Run-able協定 os.chdir("translate") print(f"🟡「切換目錄」 進入翻譯專案目錄:{os.getcwd()}") os.system(f"cd {WEBLATE_PATH} && git pull") print("\n🔵「文本同步」 從Weblate拉取最新譯文中...") print("\n🔵「文件準備」 處理LUA文本模板開始:") for lua_file in src_text_path.glob("*.lua"): print(f"🟢 處理模板:{lua_file.stem}") print(" ▸ 克隆簡體中文譯文中...") for lang in ["zh_CN"]: shutil.copy( f"{WEBLATE_PATH}/petrichor/{lua_file.stem}/{lang}.json", f"strings/{lua_file.stem}/{lang}.json", ) print(" ▸ 生成繁體中文譯文中...") convert_zh_to_tw( f"strings/{lua_file.stem}/zh_CN.json", f"strings/{lua_file.stem}/zh_TW.json" ) print(" ▸ 編譯最後的語言文件...") import_translation_json( lua_file, f"strings/{lua_file.stem}/en_US.json", f"strings/{lua_file.stem}/{IMPORTLANG}.json", f"text/{IMPORTLANG}/{lua_file.stem}.lua", ) PROJECT_ROOT = SOURCE_DIR LANG_IMAGES = SOURCE_DIR / "images" / IMPORTLANG BUILD_OUTPUT = SOURCE_DIR / "translate" / "gamebuild" / f"Petrichor-{VERSION}.love" src_text_path = PROJECT_ROOT / "translate" / "src" print(f"🟢「初始化」 Hello!\n 我現在開始裝箱 {IMPORTLANG} 版 Petrichor!") print(f" ▸ 版本號是:{VERSION}") print(f" ▸ Weblate路徑是:{WEBLATE_PATH}") print(f" ▸ 專案根目錄是:{PROJECT_ROOT}\n") # 7z校驗 print(f"\n🟢「開始裝箱」 準備為 {VERSION} 打包囉!") print("\n🔵「環境校驗」 來讓我找找你的7-zip...") if shutil.which("7z") is None: print("🔴「報錯」 快點去安裝7-zip!沒有這個我怎麼幫你裝箱嘛!") print("🔴「提示」 官方網址:https://www.7-zip.org/") exit(1) else: print("🟢「好耶」 你電腦裡有7-zip耶,太棒了!") # 繁瑣的校驗 if not LANG_IMAGES.exists(): print(f"🔴「報錯」 找不到這個語言圖片資料夾:{LANG_IMAGES}") print("🔴「提示」 你看看你路徑有沒有填錯?是不是打錯字了?") exit(1) else: image_count = len(list(LANG_IMAGES.glob("**/*"))) // 2 print(f"🟢 找到了 {image_count} 張圖片,看起來都在!") print("🟡「提示」 遊戲文件裝箱中...") original_cwd = os.getcwd() with TemporaryDirectory() as tmpdir: tmp_path = Path(tmpdir) print(f"\n🔵「準備臨時空間」 建個臨時檔案夾{tmp_path}沒意見吧") print("🔵「準備文件」 開始複製專案主體檔案...") shutil.copytree( SOURCE_DIR, tmp_path, ignore=shutil.ignore_patterns( 'translate', # 把translate操作間目錄忽略 'images', '*.py', '*.pyc', '__pycache__', '.git' ), dirs_exist_ok=True ) file_count = len(list(tmp_path.glob("**/*"))) // 2 print(f"🟢 專案文件複製完成,一共大約 {file_count} 個文件~") print("\n🔵「資源替換」 複製語言圖片中...") # 圖片多言語替換 shutil.copytree( LANG_IMAGES, tmp_path / "images", dirs_exist_ok=True ) print("🟢 圖片替換完畢,準備壓縮!") print("\n🔵「壓縮階段」 開始使用7-zip打包成.love檔案...") os.chdir(tmp_path) # 讓7z閉嘴 command = f'7z a -tzip "{BUILD_OUTPUT}" .' result = subprocess.run( command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) exit_code = result.returncode os.chdir(original_cwd) if result.returncode == 0: size_kb = BUILD_OUTPUT.stat().st_size // 1024 print(f"\n🎉「裝箱完成」 成功打包!我放在{BUILD_OUTPUT}咯") print(f"📏 檔案大小為:約 {size_kb} KB,應該沒問題!") else: if result.returncode == 2: print(f"🔴「錯誤」 裝箱時檔案被佔用了,遊戲是不是開著呀?") else: print(f"🔴「錯誤」 打包失敗!錯誤碼:{result.returncode}") exit(1) print("\n🔵「清理臨時資料」 收拾乾淨囉~") print("🟢「完成」 一切搞定,可以關掉這個視窗囉!辛苦啦~")