diff --git a/tools/libarc/arc.py b/tools/libarc/arc.py index 6c5e927bed1..ce895ba989f 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 @@ -194,10 +194,12 @@ 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 + 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: @@ -211,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 @@ -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) @@ -249,7 +252,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 +387,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])) diff --git a/tools/tp.py b/tools/tp.py index 19c0869937d..74f5b2ad9e3 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) diff --git a/tools/transform-dep.py b/tools/transform-dep.py index 412e3bd61d2..0e2d4d05318 100755 --- a/tools/transform-dep.py +++ b/tools/transform-dep.py @@ -3,57 +3,71 @@ 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('\\', '/')) + + headers: List[str] = [] + 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) + 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) + def main(): parser = argparse.ArgumentParser( description="""Transform a .d file from Wine paths to normal paths""" @@ -75,4 +89,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main()