mostly pathing improvements, mostly to benefit linux

This commit is contained in:
Fabian Dill 2020-03-15 19:32:00 +01:00
parent 2a05b9cd72
commit 031d6823a1
4 changed files with 36 additions and 18 deletions

View File

@ -8,8 +8,6 @@ import atexit
import sys import sys
import os 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.") exit_func = atexit.register(input, "Press enter to close.")

View File

@ -1,5 +1,4 @@
__author__ = "Berserker55" # you can find me on the ALTTP Randomizer Discord __author__ = "Berserker55" # you can find me on the ALTTP Randomizer Discord
__version__ = 1.7
""" """
This script launches a Multiplayer "Multiworld" Mystery Game This script launches a Multiplayer "Multiworld" Mystery Game
@ -24,7 +23,7 @@ def feedback(text:str):
if __name__ == "__main__": if __name__ == "__main__":
try: try:
print(f"{__author__}'s MultiMystery Launcher V{__version__}") print(f"{__author__}'s MultiMystery Launcher")
import ModuleUpdate import ModuleUpdate
ModuleUpdate.update() ModuleUpdate.update()

View File

@ -14,9 +14,10 @@ base_rom_bytes = None
def get_base_rom_bytes() -> bytes: def get_base_rom_bytes() -> bytes:
global base_rom_bytes global base_rom_bytes
if not base_rom_bytes: if not base_rom_bytes:
with open("host.yaml") as f: options = Utils.get_options()
options = Utils.parse_yaml(f.read())
file_name = options["general_options"]["rom_file"] 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"))) base_rom_bytes = bytes(read_rom(open(file_name, "rb")))
basemd5 = hashlib.md5() basemd5 = hashlib.md5()

View File

@ -46,36 +46,37 @@ def is_bundled():
return getattr(sys, 'frozen', False) return getattr(sys, 'frozen', False)
def local_path(path): 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) 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 # we are running in a PyInstaller bundle
local_path.cached_path = sys._MEIPASS # pylint: disable=protected-access,no-member 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])
else: else:
# we are running in a normal Python environment # we are running in a normal Python environment
# or cx_Freeze
local_path.cached_path = os.path.dirname(os.path.abspath(sys.argv[0])) local_path.cached_path = os.path.dirname(os.path.abspath(sys.argv[0]))
return os.path.join(local_path.cached_path, path) return os.path.join(local_path.cached_path, path)
local_path.cached_path = None local_path.cached_path = None
def output_path(path): 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) 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 = '.' output_path.cached_path = '.'
return os.path.join(output_path.cached_path, path) return os.path.join(output_path.cached_path, path)
else: else:
# has been packaged, so cannot use CWD for output. # has been PyInstaller packaged, so cannot use CWD for output.
if sys.platform == 'win32': if sys.platform == 'win32':
#windows # windows
import ctypes.wintypes import ctypes.wintypes
CSIDL_PERSONAL = 5 # My Documents CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH) buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf) ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
@ -166,3 +167,22 @@ def get_public_ipv4() -> str:
logging.exception(e) logging.exception(e)
pass # we could be offline, in a local game, so no point in erroring out pass # we could be offline, in a local game, so no point in erroring out
return ip 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.")