Skip to content

Commit 2dec391

Browse files
committed
handle multiple possible cwds
1 parent 91639cc commit 2dec391

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

prepare_release.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from collections.abc import Iterator
99
from functools import cache
1010
from io import BytesIO
11-
from os import path
1211
from pathlib import Path
1312
from zipfile import ZIP_DEFLATED, ZipFile
1413

@@ -20,6 +19,9 @@
2019
ZIP_SETTINGS_FOLDER = ZIP_MODS_FOLDER / "settings"
2120
ZIP_EXECUTABLE_FOLDER = Path("Binaries")
2221
ZIP_PLUGINS_FOLDER = ZIP_EXECUTABLE_FOLDER / "Plugins"
22+
ZIP_PROXY_INIT_SCRIPT_FOLDERS = [
23+
ZIP_EXECUTABLE_FOLDER / ZIP_MODS_FOLDER,
24+
]
2325

2426
# The base CMake directories - these need the preset added after
2527
BUILD_DIR_BASE = THIS_FOLDER / "out" / "build"
@@ -36,6 +38,7 @@
3638

3739
# And there are a few extra files which we want which aren't matched by the above
3840
INIT_SCRIPT = MODS_FOLDER / "__main__.py"
41+
PROXY_INIT_SCRIPT = MODS_FOLDER / "proxy__main__.py"
3942
SETTINGS_GITIGNORE = MODS_FOLDER / "settings" / ".gitignore"
4043
STUBS_DIR = THIS_FOLDER / "libs" / "pyunrealsdk" / "stubs"
4144
STUBS_LICENSE = THIS_FOLDER / "libs" / "pyunrealsdk" / "LICENSE"
@@ -217,10 +220,17 @@ def zip_config_file(zip_file: ZipFile) -> None:
217220
Args:
218221
zip_file: The zip file to add the config file to.
219222
"""
220-
# Path.relative_to doesn't work when where's no common base, need to use os.path
221-
# While the file goes in the plugins folder, this path is relative to *the executable*
222-
init_script_path = path.relpath(ZIP_MODS_FOLDER / INIT_SCRIPT.name, ZIP_EXECUTABLE_FOLDER)
223-
pyexec_root = path.relpath(ZIP_MODS_FOLDER, ZIP_EXECUTABLE_FOLDER)
223+
# When launching via Steam, the CWD is (sometimes) `<steam>\Borderlands`. When launching via Mod
224+
# Organiser, or by running the exe directly, it's `Borderlands\Binaries`.
225+
# Stick with Steam as the default, since if we're not using Steam, the path this checks will
226+
# still be inside the game folder.
227+
init_script_path = str(ZIP_MODS_FOLDER / INIT_SCRIPT.name)
228+
pyexec_root = str(ZIP_MODS_FOLDER)
229+
230+
# Copy the proxy script to all the spots the relative path might otherwise end up
231+
for path in ZIP_PROXY_INIT_SCRIPT_FOLDERS:
232+
zip_file.write(PROXY_INIT_SCRIPT, path / INIT_SCRIPT.name)
233+
zip_file.writestr(str(path / "Wrong folder, you cannot place sdk mods here!.txt"), "")
224234

225235
version_number = tomllib.loads(MANAGER_PYPROJECT.read_text())["project"]["version"]
226236
git_version = get_git_repo_version()

src/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def get_all_mod_folders() -> Sequence[Path]:
102102
Path(x) for x in unrealsdk.config.get("mod_manager", {}).get("extra_folders", [])
103103
]
104104

105-
return [Path(__file__).parent, *extra_folders]
105+
return [Path(__file__).resolve().parent, *extra_folders]
106106

107107

108108
@cache
@@ -225,7 +225,6 @@ def find_mods_to_import(all_mod_folders: Sequence[Path]) -> Collection[ModInfo]:
225225
mod_info = ModInfo(entry.name, entry)
226226

227227
elif entry.is_file() and validate_file_in_mods_folder(entry):
228-
# Files are never legacy mods
229228
mod_info = ModInfo(entry.stem, entry)
230229
else:
231230
continue

src/proxy__main__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# When launching via Steam, the CWD is (sometimes) `<steam>\Borderlands`. When launching via Mod
2+
# Organiser, or by running the exe directly, it's `Borderlands\Binaries`. This means we can't put a
3+
# single location in the `unrealsdk.toml`.
4+
# This file is copied into a few different places by the release script, and attempts to normalize
5+
# the path to the expected location
6+
7+
import importlib.util
8+
import sys
9+
from pathlib import Path
10+
11+
from unrealsdk import logging
12+
13+
binaries_dir = Path(sys.executable).resolve().parent
14+
real_init_script = binaries_dir.parent / "sdk_mods" / "__main__.py"
15+
16+
logging.misc(f"Redirecting init script to: {real_init_script}")
17+
18+
# Use importlib to completely replace ourselves as the root module
19+
spec = importlib.util.spec_from_file_location("__main__", real_init_script)
20+
assert spec is not None and spec.loader is not None
21+
22+
module = importlib.util.module_from_spec(spec)
23+
sys.modules["__main__"] = module
24+
spec.loader.exec_module(module)

0 commit comments

Comments
 (0)