def decrypt_archive_rom(cls, archive, path, file=None): print("decrypt_archive_rom", path) result = {} f = archive.open(path) data = f.read(len("AMIROMTYPE1")) out_data = b"" sha1 = hashlib.sha1() if data != b"AMIROMTYPE1": # not encrypted, write raw data sha1.update(data) out_data += data data = f.read() sha1.update(data) out_data += data result["data"] = out_data result["sha1"] = sha1.hexdigest() cls.patch_rom(result) if file is not None: file.write(result["data"]) return result key_path = archive.join(archive.dirname(path), "rom.key") key_archive = Archive(key_path) try: f2 = key_archive.open(key_path) except Exception: raise Exception("did not find rom.key to decrypt ROM with") print("using key file", key_path) # if not os.path.exists(key_file): # raise Exception("did not find rom.key to decrypt ROM with") key_data = f2.read() f2.close() while True: data = f.read(len(key_data)) if not data: break dec = [] for i in range(len(data)): dec.append(data[i] ^ key_data[i]) dec_data = bytes(dec) # if file is not None: # file.write(dec_data) out_data += dec_data if sha1 is not None: sha1.update(dec_data) result["data"] = out_data result["sha1"] = sha1.hexdigest() cls.patch_rom(result) if file is not None: file.write(result["data"]) return result
def checksum(self, path): print("[CHECKSUM]", repr(path)) archive = Archive(path) if os.path.exists(path): size = os.path.getsize(path) if size == 0: # Either a real 0-byte file or a device node on a BSD # system (could be large). To reliably get the size we could # use ioctl, but we simply return the checksum for a 0-byte # file in either case. return ZERO_SHA1 s = hashlib.sha1() f = archive.open(path) while True: data = f.read(65536) if not data: break s.update(data) return s.hexdigest()
def calculate_whdload_args(archive_path: str) -> str: """ This function, as it is currently written, only works if there is an .info with the same name as the .slave file. In theory, they could be different since the .info file contains a slave=... tool type. """ archive = Archive(archive_path) slave_args = {} lower_to_name = {} for path in archive.list_files(): path_lower = path.lower() if path_lower.rsplit("#/", 1)[1] == "s/startup-sequence": logging.info("[WHDLOAD] Found Startup-Sequence, assuming " "non-WHDLoad archive") return "" lower_to_name[path_lower] = path for path in lower_to_name.values(): name = os.path.basename(path) name_lower = name.lower() if name_lower.endswith(".info"): try: args = read_whdload_args_from_info_stream(archive.open(path)) args = strip_whdload_slave_prefix(args) except Exception as e: logging.warning("[WHDLOAD] WARNING: Error reading args: %s", repr(e)) else: if args: archive_name = path.rsplit("#/", 1)[1] logging.debug("[WHDLOAD] %s => %s".format( archive_name, " ".join(args))) slave_args[archive_name] = args if len(slave_args) == 0: return "" if len(slave_args) > 1: logging.debug("[WHDLOAD] Multiple WHDLoad icons found") for icon, args in slave_args.items(): if icon.lower() in primary_icons: logging.debug("[WHDLOAD] Choosing %s as primary icon", icon) return fix_whdload_args(args) raise Exception("Multiple slaves found") return fix_whdload_args(slave_args.popitem()[1])
def download_game_file_archive(self, url): print("\ndownload_game_file_archive", url) archive_path = Downloader.cache_file_from_url(url) archive = Archive(archive_path) archive_files = archive.list_files() print(archive_files) for name in archive_files: print(name) ifs = archive.open(name) data = ifs.read() Downloader.cache_data(data) if len(archive_files) == 0: # might not be an archive then with open(archive_path, "rb") as f: data = f.read() Downloader.cache_data(data) # the downloaded archive is no longer needed, now that we have # extracted all the files os.remove(archive_path) print("\n")
def run_config_or_game(cls): config_path = None archive_path = None floppy_image_paths = [] cdrom_image_paths = [] config_uuid = None floppy_extensions = (".adf", ".ipf", ".dms", ".adz") cdrom_extensions = (".cue", ".iso") archive_extensions = (".zip", ".lha") # FIXME: replace argument "parsing" with use of argparse module # at some point last_arg = sys.argv[-1] file_ext = os.path.splitext(last_arg)[-1].lower() if file_ext == ".fs-uae": config_path = last_arg elif file_ext in archive_extensions: archive_path = last_arg # elif file_ext in floppy_extensions: # floppy_image_paths = [last_arg] elif is_uuid(last_arg): config_uuid = last_arg.lower() for arg in sys.argv[1:]: if not arg.startswith("--"): _, ext = os.path.splitext(arg) if ext in floppy_extensions: floppy_image_paths.append(arg) elif ext in cdrom_extensions: cdrom_image_paths.append(arg) if config_path: print("[STARTUP] Config path given:", config_path) if not os.path.exists(config_path): print("[STARTUP] Config path does not exist", file=sys.stderr) return True LauncherConfig.load_file(config_path) fsgs.config.add_from_argv() return cls.run_config_directly() if archive_path: print("[STARTUP] Archive path given:", archive_path) if not os.path.exists(archive_path): print("[STARTUP] Archive path does not exist", file=sys.stderr) return True archive = Archive(os.path.realpath(archive_path)) archive_name = os.path.basename(archive_path) # We want to exclude pure directory entries when checking for # archives with only floppies. arc_files = [ x for x in archive.list_files() if not x.endswith("/") ] if all( map(lambda f: f.lower().endswith(floppy_extensions), arc_files)): print("[STARTUP] Archive contains floppy disk images only") floppy_image_paths = arc_files else: if cls.auto_detect_game: # FIXME: Could also do this for floppy file archives. archive_util = ArchiveUtil(archive_path) archive_uuid = archive_util.create_variant_uuid() print("[STARTUP] Try auto-detecting variant, uuid =", archive_uuid) if fsgs.load_game_variant(archive_uuid): print("[STARTUP] Auto-detected variant", archive_uuid) print("[STARTUP] Adding archive files to file index") for archive_file in archive.list_files(): stream = archive.open(archive_file) data = stream.read() size = len(data) sha1 = hashlib.sha1(data).hexdigest() FileDatabase.add_static_file(archive_file, size=size, sha1=sha1) fsgs.config.add_from_argv() fsgs.config.set("__config_name", archive_name) LauncherConfig.post_load_values(fsgs.config) return cls.run_config_directly() values = whdload.generate_config_for_archive(archive_path) values["hard_drive_0"] = archive_path values.update(fsgs.config.config_from_argv()) # archive_name, archive_ext = os.path.splitext(archive_name) values["__config_name"] = archive_name return cls.run_config_directly_with_values(values) if floppy_image_paths: enum_paths = tuple(enumerate(floppy_image_paths)) values = {} values.update(fsgs.config.config_from_argv()) max_drives = int(values.get("floppy_drive_count", "4")) values.update({ "floppy_drive_{0}".format(k): v for k, v in enum_paths[:max_drives] }) values.update( {"floppy_image_{0}".format(k): v for k, v in enum_paths[:20]}) # FIXME: Generate a better config name for save dir? values["__config_name"] = "Default" return cls.run_config_directly_with_values(values) if cdrom_image_paths: enum_paths = tuple(enumerate(cdrom_image_paths)) values = {"amiga_model": "CD32"} values.update(fsgs.config.config_from_argv()) max_drives = int(values.get("cdrom_drive_count", "1")) values.update({ "cdrom_drive_{0}".format(k): v for k, v in enum_paths[:max_drives] }) values.update( {"cdrom_image_{0}".format(k): v for k, v in enum_paths[:20]}) # FIXME: Generate a better config name for save dir? values["__config_name"] = "Default" return cls.run_config_directly_with_values(values) if config_uuid: print("[STARTUP] Config uuid given:", config_uuid) variant_uuid = config_uuid # values = fsgs.game.set_from_variant_uuid(variant_uuid) if fsgs.load_game_variant(variant_uuid): print("[STARTUP] Loaded variant") else: print("[STARTUP] Could not load variant, try to load game") game_uuid = config_uuid variant_uuid = fsgs.find_preferred_game_variant(game_uuid) print("[STARTUP] Preferred variant:", variant_uuid) fsgs.load_game_variant(variant_uuid) fsgs.config.add_from_argv() LauncherConfig.post_load_values(fsgs.config) return cls.run_config_directly()
def run_config_or_game(cls): config_path = None archive_path = None floppy_image_paths = [] cdrom_image_paths = [] config_uuid = None floppy_extensions = (".adf", ".ipf", ".dms", ".adz") cdrom_extensions = (".cue", ".iso") archive_extensions = (".zip", ".lha") # FIXME: replace argument "parsing" with use of argparse module # at some point last_arg = sys.argv[-1] file_ext = os.path.splitext(last_arg)[-1].lower() if file_ext == ".fs-uae": config_path = last_arg elif file_ext in archive_extensions: archive_path = last_arg # elif file_ext in floppy_extensions: # floppy_image_paths = [last_arg] elif is_uuid(last_arg): config_uuid = last_arg.lower() for arg in sys.argv[1:]: if not arg.startswith("--"): _, ext = os.path.splitext(arg) if ext in floppy_extensions: floppy_image_paths.append(arg) elif ext in cdrom_extensions: cdrom_image_paths.append(arg) if config_path: print("[STARTUP] Config path given:", config_path) if not os.path.exists(config_path): print("[STARTUP] Config path does not exist", file=sys.stderr) return True LauncherConfig.load_file(config_path) fsgs.config.add_from_argv() return cls.run_config_directly() if archive_path: print("[STARTUP] Archive path given:", archive_path) if not os.path.exists(archive_path): print("[STARTUP] Archive path does not exist", file=sys.stderr) return True archive = Archive(os.path.realpath(archive_path)) archive_name = os.path.basename(archive_path) # We want to exclude pure directory entries when checking for # archives with only floppies. arc_files = [ x for x in archive.list_files() if not x.endswith("/") ] if all( map(lambda f: f.lower().endswith(floppy_extensions), arc_files) ): print("[STARTUP] Archive contains floppy disk images only") floppy_image_paths = arc_files else: if cls.auto_detect_game: # FIXME: Could also do this for floppy file archives. archive_util = ArchiveUtil(archive_path) archive_uuid = archive_util.create_variant_uuid() print( "[STARTUP] Try auto-detecting variant, uuid =", archive_uuid, ) if fsgs.load_game_variant(archive_uuid): print("[STARTUP] Auto-detected variant", archive_uuid) print("[STARTUP] Adding archive files to file index") for archive_file in archive.list_files(): stream = archive.open(archive_file) data = stream.read() size = len(data) sha1 = hashlib.sha1(data).hexdigest() FileDatabase.add_static_file( archive_file, size=size, sha1=sha1 ) fsgs.config.add_from_argv() fsgs.config.set("__config_name", archive_name) LauncherConfig.post_load_values(fsgs.config) return cls.run_config_directly() values = whdload.generate_config_for_archive(archive_path) values["hard_drive_0"] = archive_path values.update(fsgs.config.config_from_argv()) # archive_name, archive_ext = os.path.splitext(archive_name) values["__config_name"] = archive_name return cls.run_config_directly_with_values(values) if floppy_image_paths: enum_paths = tuple(enumerate(floppy_image_paths)) values = {} values.update(fsgs.config.config_from_argv()) max_drives = int(values.get("floppy_drive_count", "4")) values.update( { "floppy_drive_{0}".format(k): v for k, v in enum_paths[:max_drives] } ) values.update( {"floppy_image_{0}".format(k): v for k, v in enum_paths[:20]} ) # FIXME: Generate a better config name for save dir? values["__config_name"] = "Default" return cls.run_config_directly_with_values(values) if cdrom_image_paths: enum_paths = tuple(enumerate(cdrom_image_paths)) values = {"amiga_model": "CD32"} values.update(fsgs.config.config_from_argv()) max_drives = int(values.get("cdrom_drive_count", "1")) values.update( { "cdrom_drive_{0}".format(k): v for k, v in enum_paths[:max_drives] } ) values.update( {"cdrom_image_{0}".format(k): v for k, v in enum_paths[:20]} ) # FIXME: Generate a better config name for save dir? values["__config_name"] = "Default" return cls.run_config_directly_with_values(values) if config_uuid: print("[STARTUP] Config uuid given:", config_uuid) variant_uuid = config_uuid # values = fsgs.game.set_from_variant_uuid(variant_uuid) if fsgs.load_game_variant(variant_uuid): print("[STARTUP] Loaded variant") else: print("[STARTUP] Could not load variant, try to load game") game_uuid = config_uuid variant_uuid = fsgs.find_preferred_game_variant(game_uuid) print("[STARTUP] Preferred variant:", variant_uuid) fsgs.load_game_variant(variant_uuid) fsgs.config.add_from_argv() LauncherConfig.post_load_values(fsgs.config) return cls.run_config_directly()