diff --git a/pre_edited_cs/__pyinstaller/__init__.py b/__pyinstaller/__init__.py similarity index 100% rename from pre_edited_cs/__pyinstaller/__init__.py rename to __pyinstaller/__init__.py diff --git a/__pyinstaller/hook-caver.py b/__pyinstaller/hook-caver.py new file mode 100644 index 0000000..40f586e --- /dev/null +++ b/__pyinstaller/hook-caver.py @@ -0,0 +1,5 @@ +from PyInstaller.utils.hooks import collect_data_files + +# https://pyinstaller.readthedocs.io/en/stable/hooks.html#provide-hooks-with-package + +datas = collect_data_files('caver', excludes=['__pyinstaller']) \ No newline at end of file diff --git a/pre_edited_cs/__pyinstaller/hook-pre-edited-cs.py b/__pyinstaller/hook-pre-edited-cs.py similarity index 100% rename from pre_edited_cs/__pyinstaller/hook-pre-edited-cs.py rename to __pyinstaller/hook-pre-edited-cs.py diff --git a/caver/patcher.py b/caver/patcher.py index 079f271..871387f 100644 --- a/caver/patcher.py +++ b/caver/patcher.py @@ -4,6 +4,7 @@ from lupa import LuaRuntime import logging import shutil import re +import sys import pre_edited_cs @@ -13,19 +14,22 @@ CSVERSION = 3 class CaverException(Exception): pass +def get_path() -> Path: + if getattr(sys, "frozen", False): + file_dir = Path(getattr(sys, "_MEIPASS")) + else: + file_dir = Path(__file__).parent + return file_dir + def patch_files(patch_data: dict, output_dir: Path, progress_update: Callable[[str, float], None]): - progress_update("Copying base files...", 0.0) + progress_update("Copying base files...", -1) ensure_base_files_exist(output_dir) mapcount = len(patch_data["maps"].keys()) - lua_file = Path(__file__).parent.joinpath("tsc_file.lua").read_text() - try: - TscFile = LuaRuntime().execute(lua_file) - except Exception as e: - print(lua_file) - raise e - + lua_file = get_path().joinpath("tsc_file.lua").read_text() + TscFile = LuaRuntime().execute(lua_file) + for i, (mapname, mapdata) in enumerate(patch_data["maps"].items()): progress_update(f"Patching {mapname}...", i/mapcount) patch_map(mapname, mapdata, TscFile, output_dir) @@ -43,14 +47,18 @@ def ensure_base_files_exist(output_dir: Path): keep_existing_files = version.exists() and int(version.read_text()) >= CSVERSION def should_ignore(path: str, names: list[str]): - base = ["__init__.py", "__pycache__", "ScriptSource"] + base = ["__init__.py", "__pycache__", "ScriptSource", "__pyinstaller"] if keep_existing_files: p = Path(path) base.extend([p.joinpath(name) for name in names if p.joinpath(name).exists() and p.joinpath(name).is_file()]) return base try: - shutil.copytree(internal_copy, output_dir, ignore=should_ignore, dirs_exist_ok=True) + shutil.copytree(internal_copy.joinpath("data"), output_dir.joinpath("data"), ignore=should_ignore, dirs_exist_ok=True) + root_files = ["Doukutsu.exe", "DoConfig.exe", "Config.dat"] + root_files = [f for f in root_files if not f in should_ignore(str(output_dir), root_files)] + for f in root_files: + shutil.copyfile(internal_copy.joinpath(f), output_dir.joinpath(f)) except shutil.Error: raise CaverException("Error copying base files. Ensure the directory is not read-only, and that Doukutsu.exe is closed.") output_dir.joinpath("data", "Plaintext").mkdir(exist_ok=True) @@ -119,19 +127,3 @@ def create_hint_script(text: str, facepic: bool, ending: str) -> str: line3 += text[split:split+msgbox_limit] return f"