2022-04-10 00:17:06 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
###
|
|
|
|
# Generates a ctx.c file, usable for "Context" on https://decomp.me.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# python3 tools/decompctx.py src/file.cpp
|
|
|
|
#
|
|
|
|
# If changes are made, please submit a PR to
|
|
|
|
# https://github.com/encounter/dtk-template
|
|
|
|
###
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
|
|
|
|
src_dir = os.path.join(root_dir, "src")
|
2023-10-12 11:10:49 +00:00
|
|
|
include_dirs = [
|
|
|
|
os.path.join(root_dir, "include"),
|
|
|
|
os.path.join(root_dir, "libc"),
|
|
|
|
]
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
include_pattern = re.compile(r'^#include\s*[<"](.+?)[>"]$')
|
2023-10-08 11:41:43 +00:00
|
|
|
guard_pattern = re.compile(r'^#ifndef\s+(.*)$')
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
defines = set()
|
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
def import_h_file(in_file: str, r_path: str) -> str:
|
2022-04-10 00:17:06 +00:00
|
|
|
rel_path = os.path.join(root_dir, r_path, in_file)
|
|
|
|
if os.path.exists(rel_path):
|
2023-10-12 11:10:49 +00:00
|
|
|
return import_c_file(rel_path)
|
|
|
|
for include_dir in include_dirs:
|
|
|
|
inc_path = os.path.join(include_dir, in_file)
|
|
|
|
if os.path.exists(inc_path):
|
|
|
|
return import_c_file(inc_path)
|
2022-04-10 00:17:06 +00:00
|
|
|
else:
|
2023-10-12 11:10:49 +00:00
|
|
|
print("Failed to locate", in_file)
|
|
|
|
return ""
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
def import_c_file(in_file) -> str:
|
|
|
|
in_file = os.path.relpath(in_file, root_dir)
|
2023-10-08 11:41:43 +00:00
|
|
|
out_text = ''
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(in_file, encoding="utf-8") as file:
|
|
|
|
out_text += process_file(in_file, list(file))
|
|
|
|
except Exception:
|
|
|
|
with open(in_file) as file:
|
|
|
|
out_text += process_file(in_file, list(file))
|
|
|
|
return out_text
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
def process_file(in_file: str, lines) -> str:
|
|
|
|
out_text = ''
|
|
|
|
for idx, line in enumerate(lines):
|
|
|
|
guard_match = guard_pattern.match(line.strip())
|
|
|
|
if idx == 0:
|
|
|
|
if guard_match:
|
|
|
|
if guard_match[1] in defines:
|
|
|
|
break
|
|
|
|
defines.add(guard_match[1])
|
|
|
|
print("Processing file", in_file)
|
|
|
|
include_match = include_pattern.match(line.strip())
|
|
|
|
if include_match and not include_match[1].endswith(".s"):
|
|
|
|
out_text += f"/* \"{in_file}\" line {idx} \"{include_match[1]}\" */\n"
|
|
|
|
out_text += import_h_file(include_match[1], os.path.dirname(in_file))
|
|
|
|
out_text += f"/* end \"{include_match[1]}\" */\n"
|
|
|
|
else:
|
|
|
|
out_text += line
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
return out_text
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="""Create a context file which can be used for decomp.me"""
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"c_file",
|
|
|
|
help="""File from which to create context""",
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
output = import_c_file(args.c_file)
|
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
with open(os.path.join(root_dir, "ctx.c"), "w", encoding="utf-8") as f:
|
2022-04-10 00:17:06 +00:00
|
|
|
f.write(output)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-10-12 11:10:49 +00:00
|
|
|
main()
|