2023-10-04 14:34:56 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
###
|
|
|
|
# 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
|
|
|
|
###
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import re
|
2024-06-10 04:09:01 +00:00
|
|
|
from typing import List
|
2023-10-04 14:34:56 +00:00
|
|
|
|
|
|
|
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")
|
2024-11-24 12:18:14 +00:00
|
|
|
include_dirs: List[str] = [] # Set with -I flag
|
2023-10-04 14:34:56 +00:00
|
|
|
|
2024-10-28 04:03:08 +00:00
|
|
|
include_pattern = re.compile(r'^#\s*include\s*[<"](.+?)[>"]')
|
2024-08-27 14:48:50 +00:00
|
|
|
guard_pattern = re.compile(r"^#\s*ifndef\s+(.*)$")
|
2024-11-24 12:18:14 +00:00
|
|
|
once_pattern = re.compile(r"^#\s*pragma\s+once$")
|
2023-10-04 14:34:56 +00:00
|
|
|
|
|
|
|
defines = set()
|
2024-11-24 12:18:14 +00:00
|
|
|
deps = []
|
2023-10-04 14:34:56 +00:00
|
|
|
|
2024-06-10 04:09:01 +00:00
|
|
|
|
2024-11-24 12:18:14 +00:00
|
|
|
def import_h_file(in_file: str, r_path: str) -> str:
|
2023-10-04 14:34:56 +00:00
|
|
|
rel_path = os.path.join(root_dir, r_path, in_file)
|
|
|
|
if os.path.exists(rel_path):
|
2024-11-24 12:18:14 +00:00
|
|
|
return import_c_file(rel_path)
|
2024-06-10 04:09:01 +00:00
|
|
|
for include_dir in include_dirs:
|
|
|
|
inc_path = os.path.join(include_dir, in_file)
|
|
|
|
if os.path.exists(inc_path):
|
2024-11-24 12:18:14 +00:00
|
|
|
return import_c_file(inc_path)
|
2023-10-04 14:34:56 +00:00
|
|
|
else:
|
2024-06-10 04:09:01 +00:00
|
|
|
print("Failed to locate", in_file)
|
|
|
|
return ""
|
|
|
|
|
2023-10-04 14:34:56 +00:00
|
|
|
|
2024-11-24 12:18:14 +00:00
|
|
|
def import_c_file(in_file: str) -> str:
|
2023-10-04 14:34:56 +00:00
|
|
|
in_file = os.path.relpath(in_file, root_dir)
|
2024-06-10 04:09:01 +00:00
|
|
|
deps.append(in_file)
|
|
|
|
out_text = ""
|
2023-10-04 14:34:56 +00:00
|
|
|
|
|
|
|
try:
|
2024-06-10 04:09:01 +00:00
|
|
|
with open(in_file, encoding="utf-8") as file:
|
2024-11-24 12:18:14 +00:00
|
|
|
out_text += process_file(in_file, list(file))
|
2023-10-04 14:34:56 +00:00
|
|
|
except Exception:
|
2024-06-10 04:09:01 +00:00
|
|
|
with open(in_file) as file:
|
2024-11-24 12:18:14 +00:00
|
|
|
out_text += process_file(in_file, list(file))
|
2023-10-04 14:34:56 +00:00
|
|
|
return out_text
|
|
|
|
|
2024-06-10 04:09:01 +00:00
|
|
|
|
2024-11-24 12:18:14 +00:00
|
|
|
def process_file(in_file: str, lines: List[str]) -> str:
|
2024-06-10 04:09:01 +00:00
|
|
|
out_text = ""
|
2023-10-04 14:34:56 +00:00
|
|
|
for idx, line in enumerate(lines):
|
2024-06-10 04:09:01 +00:00
|
|
|
if idx == 0:
|
2024-11-24 12:18:14 +00:00
|
|
|
guard_match = guard_pattern.match(line.strip())
|
2024-06-10 04:09:01 +00:00
|
|
|
if guard_match:
|
|
|
|
if guard_match[1] in defines:
|
|
|
|
break
|
|
|
|
defines.add(guard_match[1])
|
2024-11-24 12:18:14 +00:00
|
|
|
else:
|
|
|
|
once_match = once_pattern.match(line.strip())
|
|
|
|
if once_match:
|
|
|
|
if in_file in defines:
|
|
|
|
break
|
|
|
|
defines.add(in_file)
|
2024-06-10 04:09:01 +00:00
|
|
|
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'
|
2024-11-24 12:18:14 +00:00
|
|
|
out_text += import_h_file(include_match[1], os.path.dirname(in_file))
|
2024-06-10 04:09:01 +00:00
|
|
|
out_text += f'/* end "{include_match[1]}" */\n'
|
|
|
|
else:
|
|
|
|
out_text += line
|
2023-10-04 14:34:56 +00:00
|
|
|
|
|
|
|
return out_text
|
|
|
|
|
2024-06-10 04:09:01 +00:00
|
|
|
|
|
|
|
def sanitize_path(path: str) -> str:
|
|
|
|
return path.replace("\\", "/").replace(" ", "\\ ")
|
|
|
|
|
|
|
|
|
2023-10-04 14:34:56 +00:00
|
|
|
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""",
|
|
|
|
)
|
2024-06-10 04:09:01 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-o",
|
|
|
|
"--output",
|
|
|
|
help="""Output file""",
|
|
|
|
default="ctx.c",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--depfile",
|
|
|
|
help="""Dependency file""",
|
|
|
|
)
|
2024-11-24 12:18:14 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-I",
|
|
|
|
"--include",
|
|
|
|
help="""Include directory""",
|
|
|
|
action="append",
|
|
|
|
)
|
2023-10-04 14:34:56 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-11-24 12:18:14 +00:00
|
|
|
if args.include is None:
|
|
|
|
exit("No include directories specified")
|
|
|
|
global include_dirs
|
|
|
|
include_dirs = args.include
|
|
|
|
output = import_c_file(args.c_file)
|
2023-10-04 14:34:56 +00:00
|
|
|
|
2024-06-10 04:09:01 +00:00
|
|
|
with open(os.path.join(root_dir, args.output), "w", encoding="utf-8") as f:
|
2023-10-04 14:34:56 +00:00
|
|
|
f.write(output)
|
|
|
|
|
2024-06-10 04:09:01 +00:00
|
|
|
if args.depfile:
|
|
|
|
with open(os.path.join(root_dir, args.depfile), "w", encoding="utf-8") as f:
|
|
|
|
f.write(sanitize_path(args.output) + ":")
|
|
|
|
for dep in deps:
|
|
|
|
path = sanitize_path(dep)
|
|
|
|
f.write(f" \\\n\t{path}")
|
|
|
|
|
2023-10-04 14:34:56 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-06-10 04:09:01 +00:00
|
|
|
main()
|