From 7c2072ee23679ae4dd45a3034bb62b96126f02c7 Mon Sep 17 00:00:00 2001 From: Ethan Roseman Date: Thu, 18 Jun 2020 21:12:58 -0400 Subject: [PATCH] Improved mips_to_c context script (#214) * Improved context script * .gitignore --- .gitignore | 1 + tools/{gen_mips_to_c_context.py => m2ctx.py} | 52 +++++++++++--------- 2 files changed, 31 insertions(+), 22 deletions(-) rename tools/{gen_mips_to_c_context.py => m2ctx.py} (62%) mode change 100644 => 100755 diff --git a/.gitignore b/.gitignore index 17d9dc84cc..2df8a40b25 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ tools/overlayhelpers/batchdisasm/mipsdisasm/* tools/disasm/output/* tools/asmsplitter/asm/* tools/asmsplitter/c/* +ctx.c # Assets *.rgba32.png diff --git a/tools/gen_mips_to_c_context.py b/tools/m2ctx.py old mode 100644 new mode 100755 similarity index 62% rename from tools/gen_mips_to_c_context.py rename to tools/m2ctx.py index c73c62deda..e4e0af1a80 --- a/tools/gen_mips_to_c_context.py +++ b/tools/m2ctx.py @@ -3,28 +3,13 @@ import os import sys import subprocess +from pathlib import Path script_dir = os.path.dirname(os.path.realpath(__file__)) root_dir = script_dir + "/../" -build_dir = root_dir + "build/" src_dir = root_dir + "src/" -def import_c_file(in_file): - in_file = os.path.relpath(in_file, root_dir) - cpp_command = ["cpp", "-P", "-Iinclude", "-Isrc", "-undef", "-D__sgi", "-D_LANGUAGE_C", - "-DNON_MATCHING", "-D_Static_assert(x, y)=", "-D__attribute__(x)=", in_file] - try: - return subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8") - except subprocess.CalledProcessError: - print( - "Failed to preprocess input file, when running command:\n" - + cpp_command, - file=sys.stderr, - ) - sys.exit(1) - - def get_c_dir(dirname): for root, dirs, files in os.walk(src_dir): for directory in dirs: @@ -38,16 +23,39 @@ def get_c_file(directory): if file.endswith(".c") and "data" not in file: return file +def import_c_file(in_file): + in_file = os.path.relpath(in_file, root_dir) + cpp_command = ["cpp", "-P", "-Iinclude", "-Isrc", "-undef", "-D__sgi", "-D_LANGUAGE_C", + "-DNON_MATCHING", "-D_Static_assert(x, y)=", "-D__attribute__(x)=", in_file] + try: + return subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8") + except subprocess.CalledProcessError: + print( + "Failed to preprocess input file, when running command:\n" + + cpp_command, + file=sys.stderr, + ) + sys.exit(1) def main(): - this_dir = os.getcwd().split("/")[-1] - c_dir_path = get_c_dir(this_dir) - c_file = get_c_file(c_dir_path) - c_file_path = os.path.join(c_dir_path, c_file) - + if len(sys.argv) > 1: + arg = sys.argv[1] + if arg == "-h" or arg == "--help": + sys.exit("Usage: ./m2ctx.py path/to/file.c\n" \ + "or ./m2ctx.py (from an actor or gamestate's asm dir)\n" \ + "Output will be saved in oot/ctx.c") + c_file_path = Path.cwd() / sys.argv[1] + else: + this_dir = Path.cwd() + c_dir_path = get_c_dir(this_dir.name) + if c_dir_path is None: + sys.exit("Cannot find appropriate c file dir. In argumentless mode, run this script from the c file's corresponding asm dir.") + c_file = get_c_file(c_dir_path) + c_file_path = os.path.join(c_dir_path, c_file) + output = import_c_file(c_file_path) - with open(os.path.join(build_dir, "ctx.c"), "w") as f: + with open(os.path.join(root_dir, "ctx.c"), "w") as f: f.write(output)