import csv import pathlib import struct ROOT = pathlib.Path(r"K:/ghidra/Crusader_Decomp") TARGETS = {189, 190, 191, 272, 273, 283, 285} TARGET_COMPARE_CLASSES = {"NPCTRIG", "COR_BOOT", "REE_BOOT", "SFXTRIG"} def find_all(haystack: bytes, needle: bytes) -> list[int]: offsets: list[int] = [] start = 0 while True: found = haystack.find(needle, start) if found < 0: return offsets offsets.append(found) start = found + 1 def lcp(left: bytes, right: bytes) -> int: count = 0 limit = min(len(left), len(right)) while count < limit and left[count] == right[count]: count += 1 return count def lcs(left: bytes, right: bytes) -> int: count = 0 limit = min(len(left), len(right)) while count < limit and left[-1 - count] == right[-1 - count]: count += 1 return count rows = list( csv.DictReader( (ROOT / "USECODE/EUSECODE_extracted/class_event_index.tsv").open("r", encoding="utf-8"), delimiter="\t", ) ) rows_by_entry: dict[int, list[dict[str, object]]] = {} for row in rows: entry_index = int(row["entry_index"]) if entry_index not in TARGETS: continue if not row["derived_body_start"]: continue rows_by_entry.setdefault(entry_index, []).append( { "class_name": row["class_name_hint"], "slot": int(row["slot"], 0), "event_name_hint": row["event_name_hint"], "body_start": int(row["derived_body_start"], 0), "body_end": int(row["derived_body_end"], 0), } ) chunk_files: dict[int, pathlib.Path] = {} for chunk_path in (ROOT / "USECODE/EUSECODE_extracted/chunks").glob("chunk_*.bin"): entry_index = int(chunk_path.name.split("_")[1]) if entry_index in rows_by_entry: chunk_files[entry_index] = chunk_path bodies: dict[tuple[str, int], bytes] = {} for entry_index in sorted(rows_by_entry): chunk_path = chunk_files[entry_index] data = chunk_path.read_bytes() class_name = str(rows_by_entry[entry_index][0]["class_name"]) print(f"ENTRY {entry_index} {class_name} FILE {chunk_path.name}") for row in sorted(rows_by_entry[entry_index], key=lambda item: int(item["body_start"])): body = data[int(row["body_start"]):int(row["body_end"])] class_name = str(row["class_name"]) slot = int(row["slot"]) bodies[(class_name, slot)] = body hits_0410_16 = find_all(body, struct.pack(" {right_key[0]}:0x{right_key[1]:02X} len={right_len} prefix={prefix} suffix={suffix} total={total}" )