From 031d6823a1b3f028230c8d5e380d06237bcf3e66 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 15 Mar 2020 19:32:00 +0100 Subject: [PATCH] mostly pathing improvements, mostly to benefit linux --- MultiClient.py | 2 -- MultiMystery.py | 3 +-- Patch.py | 5 +++-- Utils.py | 44 ++++++++++++++++++++++++++++++++------------ 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/MultiClient.py b/MultiClient.py index a81e6cab..ef18cbf0 100644 --- a/MultiClient.py +++ b/MultiClient.py @@ -8,8 +8,6 @@ import atexit import sys import os -if __name__ == "__main__": - os.chdir(os.path.split(sys.argv[0])[0]) # set to local folder, so that options yamls can be found exit_func = atexit.register(input, "Press enter to close.") diff --git a/MultiMystery.py b/MultiMystery.py index 1cdaccdb..52007c03 100644 --- a/MultiMystery.py +++ b/MultiMystery.py @@ -1,5 +1,4 @@ __author__ = "Berserker55" # you can find me on the ALTTP Randomizer Discord -__version__ = 1.7 """ This script launches a Multiplayer "Multiworld" Mystery Game @@ -24,7 +23,7 @@ def feedback(text:str): if __name__ == "__main__": try: - print(f"{__author__}'s MultiMystery Launcher V{__version__}") + print(f"{__author__}'s MultiMystery Launcher") import ModuleUpdate ModuleUpdate.update() diff --git a/Patch.py b/Patch.py index 6ebc161a..63102a3b 100644 --- a/Patch.py +++ b/Patch.py @@ -14,9 +14,10 @@ base_rom_bytes = None def get_base_rom_bytes() -> bytes: global base_rom_bytes if not base_rom_bytes: - with open("host.yaml") as f: - options = Utils.parse_yaml(f.read()) + options = Utils.get_options() file_name = options["general_options"]["rom_file"] + if not os.path.exists(file_name): + file_name = Utils.local_path(file_name) base_rom_bytes = bytes(read_rom(open(file_name, "rb"))) basemd5 = hashlib.md5() diff --git a/Utils.py b/Utils.py index 94023dff..ae024869 100644 --- a/Utils.py +++ b/Utils.py @@ -46,36 +46,37 @@ def is_bundled(): return getattr(sys, 'frozen', False) def local_path(path): - if local_path.cached_path is not None: + if local_path.cached_path: return os.path.join(local_path.cached_path, path) - if is_bundled() and hasattr(sys, "_MEIPASS"): + elif is_bundled() and hasattr(sys, "_MEIPASS"): # we are running in a PyInstaller bundle - local_path.cached_path = sys._MEIPASS # pylint: disable=protected-access,no-member - elif is_bundled(): - #probably cxFreeze - local_path.cached_path = os.path.dirname(sys.argv[0]) + local_path.cached_path = sys._MEIPASS # pylint: disable=protected-access,no-member + else: # we are running in a normal Python environment + # or cx_Freeze local_path.cached_path = os.path.dirname(os.path.abspath(sys.argv[0])) + return os.path.join(local_path.cached_path, path) local_path.cached_path = None def output_path(path): - if output_path.cached_path is not None: + if output_path.cached_path: return os.path.join(output_path.cached_path, path) - if not is_bundled(): + if not is_bundled() and not hasattr(sys, "_MEIPASS"): + # this should trigger if it's cx_freeze bundling output_path.cached_path = '.' return os.path.join(output_path.cached_path, path) else: - # has been packaged, so cannot use CWD for output. + # has been PyInstaller packaged, so cannot use CWD for output. if sys.platform == 'win32': - #windows + # windows import ctypes.wintypes - CSIDL_PERSONAL = 5 # My Documents - SHGFP_TYPE_CURRENT = 0 # Get current, not default value + CSIDL_PERSONAL = 5 # My Documents + SHGFP_TYPE_CURRENT = 0 # Get current, not default value buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH) ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf) @@ -166,3 +167,22 @@ def get_public_ipv4() -> str: logging.exception(e) pass # we could be offline, in a local game, so no point in erroring out return ip + + +_options = None + + +def get_options() -> dict: + global _options + if _options: + return _options + else: + locations = ("options.yaml", "host.yaml", + local_path("options.yaml"), local_path("host.yaml")) + + for location in locations: + if os.path.exists(location): + with open(location) as f: + _options = parse_yaml(f.read()) + return _options + raise FileNotFoundError(f"Could not find {locations[0]} to load options.")