|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Apply symbol_mappings from objdiff.json to symbols.txt, then remove them.""" |
| 3 | + |
| 4 | +import json |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +ROOT = Path(__file__).resolve().parent.parent |
| 9 | +OBJDIFF_PATH = ROOT / "objdiff.json" |
| 10 | +SYMBOLS_PATH = ROOT / "config" / "GM8E01_00" / "symbols.txt" |
| 11 | + |
| 12 | + |
| 13 | +def main(): |
| 14 | + with open(OBJDIFF_PATH, "r") as f: |
| 15 | + objdiff = json.load(f) |
| 16 | + |
| 17 | + # Collect all mappings: old_name -> new_name |
| 18 | + mappings = {} |
| 19 | + units_with_mappings = [] |
| 20 | + for unit in objdiff.get("units", []): |
| 21 | + sm = unit.get("symbol_mappings") |
| 22 | + if sm: |
| 23 | + units_with_mappings.append(unit["name"]) |
| 24 | + for old, new in sm.items(): |
| 25 | + if old in mappings and mappings[old] != new: |
| 26 | + print( |
| 27 | + f"WARNING: conflicting mapping for '{old}':\n" |
| 28 | + f" '{mappings[old]}' vs '{new}'", |
| 29 | + file=sys.stderr, |
| 30 | + ) |
| 31 | + mappings[old] = new |
| 32 | + |
| 33 | + if not mappings: |
| 34 | + print("No symbol_mappings found in objdiff.json.") |
| 35 | + return |
| 36 | + |
| 37 | + print(f"Found {len(mappings)} mapping(s) in {len(units_with_mappings)} unit(s):") |
| 38 | + for name in units_with_mappings: |
| 39 | + print(f" {name}") |
| 40 | + |
| 41 | + # Apply to symbols.txt |
| 42 | + with open(SYMBOLS_PATH, "r") as f: |
| 43 | + lines = f.readlines() |
| 44 | + |
| 45 | + applied = set() |
| 46 | + new_lines = [] |
| 47 | + for line in lines: |
| 48 | + replaced = False |
| 49 | + for old, new in mappings.items(): |
| 50 | + prefix = old + " = " |
| 51 | + if line.startswith(prefix): |
| 52 | + new_lines.append(new + " = " + line[len(prefix):]) |
| 53 | + applied.add(old) |
| 54 | + replaced = True |
| 55 | + break |
| 56 | + if not replaced: |
| 57 | + new_lines.append(line) |
| 58 | + |
| 59 | + not_found = set(mappings.keys()) - applied |
| 60 | + if not_found: |
| 61 | + print(f"\nWARNING: {len(not_found)} mapping(s) not found in symbols.txt:", |
| 62 | + file=sys.stderr) |
| 63 | + for name in sorted(not_found): |
| 64 | + print(f" {name}", file=sys.stderr) |
| 65 | + |
| 66 | + with open(SYMBOLS_PATH, "w") as f: |
| 67 | + f.writelines(new_lines) |
| 68 | + |
| 69 | + print(f"\nApplied {len(applied)} rename(s) to {SYMBOLS_PATH.relative_to(ROOT)}") |
| 70 | + |
| 71 | + # Remove symbol_mappings from objdiff.json |
| 72 | + for unit in objdiff.get("units", []): |
| 73 | + if unit.get("symbol_mappings"): |
| 74 | + unit["symbol_mappings"] = None |
| 75 | + |
| 76 | + with open(OBJDIFF_PATH, "w") as f: |
| 77 | + json.dump(objdiff, f, indent=2) |
| 78 | + f.write("\n") |
| 79 | + |
| 80 | + print(f"Cleared symbol_mappings from {OBJDIFF_PATH.relative_to(ROOT)}") |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments