From 3a1282dd714a85bbe2f28ff9b513c9defffb6fd4 Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Wed, 25 Jan 2023 23:15:49 -0800 Subject: [PATCH 1/6] tp.py: fix exception handler --- tools/tp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tp.py b/tools/tp.py index 19c0869937..74f5b2ad9e 100644 --- a/tools/tp.py +++ b/tools/tp.py @@ -317,7 +317,7 @@ def setup(debug: bool, game_path: Path, tools_path: Path): os.chdir(str(game_path.absolute())) extract_game_assets.extract("../" + str(iso)) os.chdir(previous_dir) - except e as Exception: + except Exception as e: LOG.error(f"failure:") LOG.error(e) sys.exit(1) From 9ac12694738c26c62ace64156e3d874dde77e555 Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Wed, 25 Jan 2023 23:17:45 -0800 Subject: [PATCH 2/6] libarc/arc.py: explicitly specify utf-8 encoding when opening text files --- tools/libarc/arc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/libarc/arc.py b/tools/libarc/arc.py index 6c5e927bed..48c39c6ba3 100644 --- a/tools/libarc/arc.py +++ b/tools/libarc/arc.py @@ -249,7 +249,7 @@ def extract_to_directory(directory, data, write_function): fileDataLines = files_data.splitlines() # fileDataLines.sort(key=lambda x : int(x.split(":")[0])) - filesFile = open("_files.txt", "w") + filesFile = open("_files.txt", "w", encoding="utf-8") for line in fileDataLines: filesFile.write(line + "\n") os.chdir(cwd) @@ -384,7 +384,7 @@ def parseDirForPack( def convert_dir_to_arc(sourceDir, convertFunction): # print("Converting "+str(sourceDir)) - fileData = open(sourceDir / "_files.txt", "r").read() + fileData = open(sourceDir / "_files.txt", "r", encoding="utf-8").read() fileDataLinesFull = fileData.splitlines() # fileDataLinesFull.sort(key=lambda x : int(x.split(":")[0])) From e786c7dd3a29767474117067ccb04290a982519f Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Wed, 25 Jan 2023 23:31:11 -0800 Subject: [PATCH 3/6] libarc/arc.py: Check if directories already exist before creating --- tools/libarc/arc.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/libarc/arc.py b/tools/libarc/arc.py index 48c39c6ba3..f57ea2ae53 100644 --- a/tools/libarc/arc.py +++ b/tools/libarc/arc.py @@ -194,7 +194,9 @@ def read(buffer) -> RARC: def extract_node(node, arcData, write_function, parentDir, dirNames) -> str: - os.mkdir(Path(parentDir) / node.name) + nodeDir = Path(parentDir) / node.name + if not os.path.exists(nodeDir): + os.mkdir(nodeDir) for i in range(node.directory_index, node.directory_count + node.directory_index): dir = arcData._directories[i] dirNames[i] = str(Path(parentDir) / Path(node.name)) + "/" + dir.name @@ -219,7 +221,8 @@ def extract_node(node, arcData, write_function, parentDir, dirNames) -> str: def extract_to_directory(directory, data, write_function): print("Extracting " + str(directory)) - os.mkdir(directory) + if not os.path.exists(directory): + os.mkdir(directory) arcData = read(data) cwd = os.getcwd() os.chdir(directory) From 10ebe01a42ebaae7664d19f8a8bd8ae1346727d1 Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Thu, 26 Jan 2023 00:56:58 -0800 Subject: [PATCH 4/6] libarc/arc.py: use unix paths for _files.txt --- tools/libarc/arc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/libarc/arc.py b/tools/libarc/arc.py index f57ea2ae53..ce895ba989 100644 --- a/tools/libarc/arc.py +++ b/tools/libarc/arc.py @@ -8,7 +8,7 @@ import struct import os import ctypes -from pathlib import Path +from pathlib import Path, PurePosixPath from dataclasses import dataclass, field from typing import List, Dict @@ -199,7 +199,7 @@ def extract_node(node, arcData, write_function, parentDir, dirNames) -> str: os.mkdir(nodeDir) for i in range(node.directory_index, node.directory_count + node.directory_index): dir = arcData._directories[i] - dirNames[i] = str(Path(parentDir) / Path(node.name)) + "/" + dir.name + dirNames[i] = str(PurePosixPath(parentDir) / PurePosixPath(node.name)) + "/" + dir.name if type(dir) == Folder and dir.name != "." and dir.name != "..": for j, node2 in enumerate(arcData._nodes): if dir.data_offset == j: @@ -213,7 +213,7 @@ def extract_node(node, arcData, write_function, parentDir, dirNames) -> str: break elif type(dir) == File: dirNames[i] = write_function( - Path(parentDir) / Path(node.name) / dir.name, dir.data + PurePosixPath(parentDir) / PurePosixPath(node.name) / dir.name, dir.data ) return dirNames From 8ccb2d4776c879a393c5bfb634e2c989b26a82b0 Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Thu, 26 Jan 2023 21:26:40 -0800 Subject: [PATCH 5/6] transform-dep.py: refactor --- tools/transform-dep.py | 84 +++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/tools/transform-dep.py b/tools/transform-dep.py index 412e3bd61d..5c5f7e7b69 100755 --- a/tools/transform-dep.py +++ b/tools/transform-dep.py @@ -3,57 +3,67 @@ import argparse import os from platform import uname +from typing import List if os.name != 'nt': wineprefix = os.environ.get('WINEPREFIX', os.path.join(os.environ['HOME'], '.wine')) winedevices = os.path.join(wineprefix, 'dosdevices') + def in_wsl() -> bool: # wsl1 has Microsoft, wsl2 has microsoft-standard release = uname().release return 'microsoft-standard' in release or 'Microsoft' in release -def import_d_file(in_file) -> str: - out_lines = [] - with open(in_file) as file: - for idx, line in enumerate(file): - if idx == 0: - if line.endswith(' \\\n'): - out_lines.append(line[:-3].replace('\\', '/') + " \\\n") - else: - out_lines.append(line.replace('\\', '/')) +def convert_path(path: str) -> str: + # lowercase drive letter + path = path[0].lower() + path[1:] + if os.name == 'nt': + return path.replace('\\', '/') + elif path[0] == 'z': + # shortcut for z: + return path[2:].replace('\\', '/') + elif in_wsl(): + if path.startswith(r'\\wsl'): + # first part could be wsl$ or wsl.localhost + pos = path.find('\\', 2) + pos = path.find('\\', pos + 1) + path = path[pos:] + return path.replace('\\', '/') + else: + path = path[0:1] + path[2:] + return os.path.join('/mnt', path.replace('\\', '/')) + else: + # use $WINEPREFIX/dosdevices to resolve path + return os.path.realpath(os.path.join(winedevices, path.replace('\\', '/'))) + + +def import_d_file(in_file: str) -> str: + out_lines: List[str] = [] + + with open(in_file, 'r') as file: + it = iter(file) + line = next(it) + if line.endswith(' \\\n'): + out_lines.append(line[:-3].replace('\\', '/') + " \\\n") + else: + out_lines.append(line.replace('\\', '/')) + + for line in it: + suffix = '' + if line.endswith(' \\\n'): + suffix = ' \\' + path = line.lstrip()[:-3] else: - suffix = '' - if line.endswith(' \\\n'): - suffix = ' \\' - path = line.lstrip()[:-3] - else: - path = line.strip() - # lowercase drive letter - path = path[0].lower() + path[1:] - if os.name == 'nt': - path = path.replace('\\', '/') - elif path[0] == 'z': - # shortcut for z: - path = path[2:].replace('\\', '/') - elif in_wsl(): - if path.startswith(r'\\wsl'): - # first part could be wsl$ or wsl.localhost - pos = path.find('\\', 2) - pos = path.find('\\', pos + 1) - path = path[pos:] - path = path.replace('\\', '/') - else: - path = path[0:1] + path[2:] - path = os.path.join('/mnt', path.replace('\\', '/')) - else: - # use $WINEPREFIX/dosdevices to resolve path - path = os.path.realpath(os.path.join(winedevices, path.replace('\\', '/'))) - out_lines.append(f"\t{path}{suffix}\n") + path = line.strip() + + path = convert_path(path) + out_lines.append(f"\t{path}{suffix}\n") return ''.join(out_lines) + def main(): parser = argparse.ArgumentParser( description="""Transform a .d file from Wine paths to normal paths""" @@ -75,4 +85,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() From b63d35d9cf0d13e03ed4582908ce7b66b07dcf9e Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Thu, 26 Jan 2023 21:30:17 -0800 Subject: [PATCH 6/6] transform-dep.py: fake the -MP compiler option --- tools/transform-dep.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/transform-dep.py b/tools/transform-dep.py index 5c5f7e7b69..0e2d4d0531 100755 --- a/tools/transform-dep.py +++ b/tools/transform-dep.py @@ -50,6 +50,7 @@ def import_d_file(in_file: str) -> str: else: out_lines.append(line.replace('\\', '/')) + headers: List[str] = [] for line in it: suffix = '' if line.endswith(' \\\n'): @@ -59,7 +60,10 @@ def import_d_file(in_file: str) -> str: path = line.strip() path = convert_path(path) + headers.append(path) out_lines.append(f"\t{path}{suffix}\n") + # the metrowerks compiler doesn't support -MP + out_lines.extend([f'{header}:\n' for header in headers]) return ''.join(out_lines)