2023-05-04 11:03:02 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2021-12-16 01:27:26 +01:00
|
|
|
import sym_info
|
2021-03-24 09:39:43 +01:00
|
|
|
from pathlib import Path
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
_script_lib = None
|
2023-07-29 19:03:17 +02:00
|
|
|
|
|
|
|
|
2021-12-30 15:22:44 +01:00
|
|
|
def script_lib(offset=0):
|
2020-10-31 03:28:18 +01:00
|
|
|
global _script_lib
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
if not _script_lib:
|
|
|
|
_script_lib = {}
|
2020-10-19 23:58:57 +02:00
|
|
|
|
|
|
|
from os import path
|
|
|
|
import re
|
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
# star rod database
|
|
|
|
"""
|
2020-10-19 23:58:57 +02:00
|
|
|
LIB_LINE_RE = re.compile(r"\s+:\s+")
|
|
|
|
NAME_RE = re.compile(r"({[^}]*})?\s*([a-zA-Z0-9_]+)")
|
|
|
|
for filename in Path(path.dirname(__file__), "star-rod", "database").rglob("*.lib"):
|
|
|
|
with open(filename, "r") as file:
|
|
|
|
for line in file.readlines():
|
|
|
|
parts = LIB_LINE_RE.split(line)
|
|
|
|
if len(parts) >= 3:
|
|
|
|
try:
|
|
|
|
kind = parts[0]
|
|
|
|
vaddr = int(parts[1].split(", ")[0], 16)
|
|
|
|
if name := NAME_RE.match(parts[2]):
|
|
|
|
name = name.group(2)
|
2020-10-31 03:28:18 +01:00
|
|
|
_script_lib[vaddr] = name
|
2020-10-19 23:58:57 +02:00
|
|
|
except:
|
|
|
|
pass
|
2020-10-31 03:28:18 +01:00
|
|
|
"""
|
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
repo_root = Path(__file__).resolve().parent.parent
|
|
|
|
symbols = Path(repo_root / "ver" / "current" / "symbol_addrs.txt")
|
|
|
|
with open(symbols, "r") as file:
|
2020-10-31 03:28:18 +01:00
|
|
|
for line in file.readlines():
|
2020-11-08 11:28:08 +01:00
|
|
|
s = [s.strip() for s in line.split("=", 1)]
|
2020-10-31 03:28:18 +01:00
|
|
|
name = s[0]
|
2021-03-24 09:39:43 +01:00
|
|
|
vaddr = int(s[1].split(";")[0].split(" ")[0], 16)
|
|
|
|
|
|
|
|
raddr = "0xFFFFFFFF"
|
|
|
|
if "rom:" in line:
|
2023-07-29 19:03:17 +02:00
|
|
|
raddr = line.split("rom:", 1)[1]
|
2021-03-24 09:39:43 +01:00
|
|
|
if " " in raddr:
|
2023-07-29 19:03:17 +02:00
|
|
|
raddr = raddr.split(" ", 1)[0]
|
2021-03-24 09:39:43 +01:00
|
|
|
raddr = raddr.strip()
|
|
|
|
|
|
|
|
if vaddr not in _script_lib:
|
|
|
|
_script_lib[vaddr] = []
|
|
|
|
_script_lib[vaddr].append([int(raddr, 16), name])
|
|
|
|
|
2021-12-28 18:11:07 +01:00
|
|
|
"""
|
2021-04-03 19:21:49 +02:00
|
|
|
# Sort the symbols for each vram address by the difference
|
2021-03-24 09:39:43 +01:00
|
|
|
# between their rom address and the offset passed in.
|
|
|
|
# If offset - rom address goes below 0, it's part of the
|
|
|
|
# previous file, so treat it as min priority, same as a default.
|
|
|
|
# After sorting, the first rom address and name should be the best candidate.
|
|
|
|
for k in _script_lib.keys():
|
|
|
|
for i,entry in enumerate(_script_lib[k]):
|
|
|
|
diff = offset - entry[0]
|
|
|
|
entry[0] = 0xFFFFFFFF if diff < 0 else diff
|
|
|
|
_script_lib[k][i][0] = entry[0]
|
|
|
|
_script_lib[k] = sorted(_script_lib[k], key=lambda x: x[0])
|
2021-12-28 18:11:07 +01:00
|
|
|
"""
|
2020-10-31 03:28:18 +01:00
|
|
|
return _script_lib
|
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2021-12-28 18:11:07 +01:00
|
|
|
def extend_symbol_map(a, b):
|
|
|
|
for k in b:
|
|
|
|
if k not in a:
|
|
|
|
a[k] = b[k]
|
|
|
|
else:
|
|
|
|
a[k] += b[k]
|
|
|
|
|
|
|
|
return a
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-07-12 14:06:00 +02:00
|
|
|
def round_fixed(f: float) -> float:
|
|
|
|
g = f * 100.0
|
|
|
|
whole = round(g)
|
2023-07-29 19:03:17 +02:00
|
|
|
if abs(g - whole) <= 100.0 / 1024.0:
|
2021-07-12 14:06:00 +02:00
|
|
|
f = whole / 100.0
|
|
|
|
return f
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-12-28 18:11:07 +01:00
|
|
|
def find_symbol_in_overlay(symbol_map, overlay_rom_addr, symbol_ram_addr):
|
|
|
|
if not symbol_ram_addr in symbol_map:
|
|
|
|
return None
|
|
|
|
|
|
|
|
lowest_delta = None
|
|
|
|
lowest_symbol_name = None
|
|
|
|
|
|
|
|
for rom_addr, symbol_name in symbol_map[symbol_ram_addr]:
|
|
|
|
delta = rom_addr - overlay_rom_addr
|
|
|
|
|
|
|
|
if delta >= 0 and (lowest_delta is None or delta <= lowest_delta):
|
|
|
|
lowest_delta = delta
|
|
|
|
lowest_symbol_name = symbol_name
|
|
|
|
|
|
|
|
if lowest_symbol_name:
|
|
|
|
return lowest_symbol_name
|
|
|
|
|
|
|
|
return symbol_map[symbol_ram_addr][0][1]
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2022-01-01 14:08:04 +01:00
|
|
|
def browse_header(valid_enums, enums):
|
|
|
|
# enums
|
2023-07-29 19:03:17 +02:00
|
|
|
for i, line in enumerate(enums):
|
2022-01-01 14:08:04 +01:00
|
|
|
if (line.startswith("enum ")) or (line.startswith("typedef enum ")):
|
2023-07-29 19:03:17 +02:00
|
|
|
enum_name = line.split("enum ", 1)[1].split(" {", 1)[0]
|
2022-01-01 14:08:04 +01:00
|
|
|
if enum_name in valid_enums:
|
|
|
|
CONSTANTS[enum_name] = {}
|
|
|
|
last_num = -1
|
|
|
|
i += 1
|
|
|
|
while "}" not in enums[i]:
|
|
|
|
if not enums[i]:
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
if "//" in enums[i]:
|
2023-07-29 19:03:17 +02:00
|
|
|
name = enums[i].split("//", 1)[0].strip()
|
2022-01-01 14:08:04 +01:00
|
|
|
else:
|
|
|
|
name = enums[i].strip()
|
2023-07-29 19:03:17 +02:00
|
|
|
val = last_num + 1
|
2022-01-01 14:08:04 +01:00
|
|
|
if "=" in name:
|
|
|
|
name, val = name.split(" = ")
|
|
|
|
val = int(val[:-1], 0)
|
2022-01-03 10:54:01 +01:00
|
|
|
if val > 0x80000000:
|
2022-01-01 14:08:04 +01:00
|
|
|
val -= 0x100000000
|
|
|
|
else:
|
|
|
|
name = name[:-1]
|
|
|
|
name = name.strip()
|
2023-07-29 19:03:17 +02:00
|
|
|
# print("\"" + name + "\"", "===", val)
|
2022-01-01 14:08:04 +01:00
|
|
|
|
|
|
|
CONSTANTS[enum_name][val] = name.strip()
|
|
|
|
i += 1
|
|
|
|
last_num = val
|
|
|
|
return
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-03-30 05:39:17 +02:00
|
|
|
# Grab CONSTANTS from the include/ folder to save manual work
|
|
|
|
CONSTANTS = {}
|
2022-01-06 17:32:57 +01:00
|
|
|
SWITCH_TYPES = []
|
2023-07-29 19:03:17 +02:00
|
|
|
LOCAL_WORDS = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
2021-03-30 05:39:17 +02:00
|
|
|
SAVE_VARS = set()
|
2023-07-29 19:03:17 +02:00
|
|
|
|
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
def get_constants():
|
2021-03-30 05:39:17 +02:00
|
|
|
global CONSTANTS
|
|
|
|
global VALID_SAVE_VARS
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
valid_enums = {
|
|
|
|
"StoryProgress",
|
|
|
|
"ItemIDs",
|
|
|
|
"PlayerAnims",
|
|
|
|
"ActorType",
|
|
|
|
"ActorIDs",
|
|
|
|
"Events",
|
|
|
|
"SoundIDs",
|
|
|
|
"SongIDs",
|
|
|
|
"Locations",
|
|
|
|
"AmbientSounds",
|
|
|
|
"NpcIDs",
|
|
|
|
"Emotes",
|
|
|
|
"NpcFlags",
|
|
|
|
"Statuses",
|
|
|
|
"Elements",
|
|
|
|
"DamageTypes",
|
|
|
|
"ElementImmunityFlags",
|
|
|
|
"HitResults",
|
|
|
|
"ActorFlags",
|
|
|
|
"ActorPartFlags",
|
|
|
|
"ActorEventFlags",
|
|
|
|
"ElementFlags",
|
|
|
|
"EncounterTriggers",
|
|
|
|
"Abilities",
|
|
|
|
"EasingType",
|
|
|
|
"DecorationIDs",
|
|
|
|
"HitResults",
|
|
|
|
"Phases",
|
|
|
|
"ItemSpawnModes",
|
|
|
|
"ActionStates",
|
|
|
|
"Triggers",
|
|
|
|
"Buttons",
|
|
|
|
"ActionCommand",
|
|
|
|
"MoveIDs",
|
|
|
|
"BattleStatusFlags1",
|
|
|
|
"BattleStatusFlags2",
|
|
|
|
"BtlCameraPreset",
|
|
|
|
"EffectID",
|
|
|
|
"StatusFlags",
|
|
|
|
}
|
2021-03-24 18:30:25 +01:00
|
|
|
for enum in valid_enums:
|
2021-03-30 05:39:17 +02:00
|
|
|
CONSTANTS[enum] = {}
|
2021-04-03 19:21:49 +02:00
|
|
|
CONSTANTS["NPC_SPRITE"] = {}
|
2021-04-08 19:42:36 +02:00
|
|
|
CONSTANTS["MAP_NPCS"] = {}
|
2021-03-30 05:39:17 +02:00
|
|
|
|
2022-09-08 14:12:26 +02:00
|
|
|
[SAVE_VARS.add(x) for x in ["GB_WorldLocation", "GB_StoryProgress"]]
|
2021-03-24 09:39:43 +01:00
|
|
|
|
|
|
|
include_path = Path(Path(__file__).resolve().parent.parent / "include")
|
2021-03-29 02:05:56 +02:00
|
|
|
enums = Path(include_path / "enums.h").read_text().splitlines()
|
2022-01-01 14:08:04 +01:00
|
|
|
browse_header(valid_enums, enums)
|
2022-01-06 17:32:57 +01:00
|
|
|
enums = Path(include_path / "effects.h").read_text().splitlines()
|
|
|
|
browse_header(valid_enums, enums)
|
|
|
|
|
2022-01-01 14:08:04 +01:00
|
|
|
include_path = Path(Path(__file__).resolve().parent.parent / "src" / "battle")
|
|
|
|
enums = Path(include_path / "battle.h").read_text().splitlines()
|
|
|
|
browse_header(valid_enums, enums)
|
2021-03-24 09:39:43 +01:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
"""
|
2021-04-03 19:21:49 +02:00
|
|
|
# defines
|
2021-03-29 02:05:56 +02:00
|
|
|
for line in enums:
|
2021-03-24 18:30:25 +01:00
|
|
|
this_enum = ""
|
2021-03-29 02:05:56 +02:00
|
|
|
for enum in valid_defines:
|
2021-03-24 18:30:25 +01:00
|
|
|
if f"#define {enum}_" in line:
|
|
|
|
this_enum = enum
|
|
|
|
break;
|
|
|
|
if this_enum:
|
2021-03-24 09:39:43 +01:00
|
|
|
name = line.split(" ",2)[1]
|
|
|
|
id_ = line.split("0x", 1)[1]
|
|
|
|
if " " in id_:
|
|
|
|
id_ = id_.split(" ",1)[0]
|
2021-03-30 05:39:17 +02:00
|
|
|
CONSTANTS[this_enum][int(id_, 16)] = name
|
2023-07-29 19:03:17 +02:00
|
|
|
"""
|
2021-03-29 02:05:56 +02:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
# exit()
|
2021-04-03 19:21:49 +02:00
|
|
|
# sprites
|
2023-07-29 19:03:17 +02:00
|
|
|
sprite_path = Path(
|
|
|
|
Path(__file__).resolve().parent.parent / "ver" / "current" / "build" / "include" / "sprite" / "npc"
|
|
|
|
)
|
2021-04-03 19:21:49 +02:00
|
|
|
for file in sprite_path.iterdir():
|
|
|
|
fd = file.read_text()
|
|
|
|
for line in fd.splitlines():
|
2021-10-25 13:44:55 +02:00
|
|
|
if "#define NPC_ANIM_" not in line:
|
2021-04-03 19:21:49 +02:00
|
|
|
continue
|
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
header_file_name = file.parts[-1]
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
name = line.split(" ", 2)[1]
|
2021-10-25 13:44:55 +02:00
|
|
|
value = int(line.split("0x", 1)[1], base=16)
|
2021-04-03 19:21:49 +02:00
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
CONSTANTS["NPC_SPRITE"][value] = name
|
|
|
|
CONSTANTS["NPC_SPRITE"][str(value) + ".h"] = header_file_name
|
2021-04-03 19:21:49 +02:00
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
return
|
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2021-04-08 19:42:36 +02:00
|
|
|
def make_anim_macro(self, sprite, palette, anim):
|
2021-04-29 19:09:30 +02:00
|
|
|
if sprite == 0xFF and palette == 0xFF and anim == 0xFF:
|
|
|
|
return "-1"
|
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
value = (sprite << 16) | (palette << 8) | anim
|
|
|
|
|
|
|
|
if value in CONSTANTS["NPC_SPRITE"]:
|
2023-07-29 19:03:17 +02:00
|
|
|
self.INCLUDES_NEEDED["sprites"].add(CONSTANTS["NPC_SPRITE"][str(value) + ".h"])
|
|
|
|
return CONSTANTS["NPC_SPRITE"][value]
|
2021-04-08 19:42:36 +02:00
|
|
|
else:
|
2021-10-25 13:44:55 +02:00
|
|
|
return f"0x{sprite:02X}{palette:02X}{anim:02X}"
|
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
def remove_evt_ptr(s):
|
|
|
|
if s.startswith("EVT_PTR("):
|
|
|
|
return s[8:-1]
|
|
|
|
else:
|
|
|
|
return s
|
2021-04-08 19:42:36 +02:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2022-01-06 17:32:57 +01:00
|
|
|
def make_flags(argNum, flags, new_args):
|
|
|
|
enabled = []
|
|
|
|
for x in range(32):
|
|
|
|
flag = argNum & (1 << x)
|
|
|
|
if flag:
|
|
|
|
if flag in CONSTANTS[flags]:
|
|
|
|
enabled.append(CONSTANTS[flags][flag])
|
|
|
|
else:
|
|
|
|
enabled.append(f"0x{flag:08X}")
|
|
|
|
if not enabled:
|
|
|
|
enabled.append(f"0")
|
|
|
|
new_args.append(" | ".join(enabled))
|
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2021-04-03 19:21:49 +02:00
|
|
|
def fix_args(self, func, args, info):
|
2021-03-30 05:39:17 +02:00
|
|
|
global CONSTANTS
|
2021-04-22 05:19:31 +02:00
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
new_args = []
|
2021-04-08 19:42:36 +02:00
|
|
|
args = args.split(", ")
|
2023-07-29 19:03:17 +02:00
|
|
|
for i, arg in enumerate(args):
|
|
|
|
if (remove_evt_ptr(arg).startswith("D_B")) or (i == 0 and func == "MakeEntity" and arg.startswith("D_")):
|
2021-10-25 13:44:55 +02:00
|
|
|
if func == "MakeEntity":
|
|
|
|
arg = "MAKE_ENTITY_END"
|
|
|
|
else:
|
|
|
|
arg = "0x" + remove_evt_ptr(arg)[2:]
|
2022-01-06 17:32:57 +01:00
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
if "0x" in arg and int(remove_evt_ptr(arg), 16) >= 0xF0000000:
|
|
|
|
arg = f"{int(remove_evt_ptr(arg), 16) - 0x100000000}"
|
2022-01-06 17:32:57 +01:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
if i in info or (i + 1 == len(args) and -1 in info):
|
2022-01-06 17:32:57 +01:00
|
|
|
if arg.startswith("LW"):
|
|
|
|
argNum = trim_lw(arg)
|
|
|
|
LOCAL_WORDS[int(argNum)] = info[i]
|
|
|
|
new_args.append(f"{arg}")
|
2023-07-29 19:03:17 +02:00
|
|
|
# print(LOCAL_WORDS)
|
2022-01-06 17:32:57 +01:00
|
|
|
continue
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
if i + 1 == len(args) and -1 in info:
|
2021-04-29 19:09:30 +02:00
|
|
|
i = -1
|
2021-04-03 19:21:49 +02:00
|
|
|
if "_" in arg:
|
|
|
|
new_args.append(f"{arg}")
|
|
|
|
continue
|
2021-12-28 18:11:07 +01:00
|
|
|
try:
|
|
|
|
argNum = int(arg, 0)
|
|
|
|
except ValueError:
|
|
|
|
new_args.append(f"{arg}")
|
|
|
|
continue
|
2021-04-03 19:21:49 +02:00
|
|
|
|
|
|
|
if info[i] == "Bool":
|
|
|
|
new_args.append(f"{'TRUE' if argNum == True else 'FALSE'}")
|
2021-10-25 13:44:55 +02:00
|
|
|
elif info[i] == "Hex" and argNum > 0:
|
2021-04-03 19:21:49 +02:00
|
|
|
new_args.append(f"0x{argNum:08X}")
|
|
|
|
elif info[i] == "CustomAnim":
|
2022-01-16 18:22:23 +01:00
|
|
|
if argNum != -1:
|
|
|
|
try:
|
2023-07-29 19:03:17 +02:00
|
|
|
value = argNum & 0x00FFFFFF
|
2022-01-16 18:22:23 +01:00
|
|
|
|
|
|
|
if func == "SetAnimation" and int(new_args[1], 10) == 0:
|
|
|
|
call = f"{CONSTANTS['PlayerAnims'][argNum]}"
|
|
|
|
elif value in CONSTANTS["NPC_SPRITE"]:
|
2023-07-29 19:03:17 +02:00
|
|
|
self.INCLUDES_NEEDED["sprites"].add(CONSTANTS["NPC_SPRITE"][str(value) + ".h"])
|
|
|
|
call = CONSTANTS["NPC_SPRITE"][value]
|
2022-01-16 18:22:23 +01:00
|
|
|
else:
|
|
|
|
call = f"{argNum:06X}"
|
|
|
|
except ValueError:
|
2023-07-29 19:03:17 +02:00
|
|
|
call = f"0x{argNum:06X}"
|
2022-01-16 18:22:23 +01:00
|
|
|
except KeyError:
|
2023-07-29 19:03:17 +02:00
|
|
|
call = f"0x{argNum:06X}"
|
2022-01-16 18:22:23 +01:00
|
|
|
else:
|
|
|
|
call = "-1"
|
2021-04-03 19:21:49 +02:00
|
|
|
new_args.append(call)
|
|
|
|
elif info[i] == "CustomMsg":
|
|
|
|
type_ = (argNum & 0xFF0000) >> 16
|
2023-07-29 19:03:17 +02:00
|
|
|
num_ = (argNum & 0xFFFF) >> 0
|
2021-04-03 19:21:49 +02:00
|
|
|
new_args.append(f"MESSAGE_ID(0x{type_:02X}, 0x{num_:04X})")
|
2021-04-08 19:42:36 +02:00
|
|
|
elif info[i] == "NpcFlags":
|
2022-01-06 17:32:57 +01:00
|
|
|
make_flags(argNum, "NpcFlags", new_args)
|
|
|
|
elif info[i] == "DamageTypes":
|
|
|
|
make_flags(argNum, "DamageTypes", new_args)
|
|
|
|
elif info[i] == "ActorPartFlags":
|
|
|
|
make_flags(argNum, "ActorPartFlags", new_args)
|
|
|
|
elif info[i] == "ActorFlags":
|
|
|
|
make_flags(argNum, "ActorFlags", new_args)
|
|
|
|
elif info[i] == "BattleStatusFlags1":
|
|
|
|
make_flags(argNum, "BattleStatusFlags1", new_args)
|
|
|
|
elif info[i] == "BattleStatusFlags2":
|
|
|
|
make_flags(argNum, "BattleStatusFlags2", new_args)
|
|
|
|
elif info[i] == "StatusFlags":
|
|
|
|
make_flags(argNum, "StatusFlags", new_args)
|
2021-04-08 19:42:36 +02:00
|
|
|
elif info[i] == "NpcIDs":
|
|
|
|
if argNum >= 0:
|
2021-10-25 13:44:55 +02:00
|
|
|
if argNum in CONSTANTS["MAP_NPCS"]:
|
|
|
|
new_args.append(CONSTANTS["MAP_NPCS"][argNum])
|
|
|
|
else:
|
|
|
|
new_args.append(str(argNum))
|
2021-04-08 19:42:36 +02:00
|
|
|
else:
|
|
|
|
new_args.append(CONSTANTS["NpcIDs"][argNum])
|
2021-12-24 14:29:05 +01:00
|
|
|
elif info[i] == "SoundIDs":
|
|
|
|
if argNum in CONSTANTS["SoundIDs"]:
|
|
|
|
new_args.append(CONSTANTS["SoundIDs"][argNum])
|
|
|
|
else:
|
|
|
|
new_args.append("0x%X" % argNum)
|
|
|
|
elif info[i] == "StoryProgress":
|
|
|
|
print(info[i])
|
|
|
|
if argNum in CONSTANTS["StoryProgress"]:
|
|
|
|
new_args.append(CONSTANTS["StoryProgress"][argNum])
|
|
|
|
else:
|
|
|
|
new_args.append(str(argNum))
|
2021-03-24 09:39:43 +01:00
|
|
|
else:
|
2022-01-06 17:32:57 +01:00
|
|
|
try:
|
|
|
|
new_args.append(f"{CONSTANTS[info[i]][argNum]}")
|
|
|
|
except KeyError:
|
|
|
|
if not (info[i] == "NpcIDs" and argNum > 0):
|
2023-07-29 19:03:17 +02:00
|
|
|
print(
|
|
|
|
f"0x{argNum:X} was not found within {info[i]} constants for function {func} arg {i}, add it."
|
|
|
|
)
|
2021-04-22 05:19:31 +02:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
if info[i] == "ItemIDs" and argNum < 0:
|
2022-01-06 17:32:57 +01:00
|
|
|
new_args.append(f"{int(argNum)}")
|
|
|
|
else:
|
2023-07-29 19:03:17 +02:00
|
|
|
# Print the unknowns in hex
|
2022-01-06 17:32:57 +01:00
|
|
|
new_args.append(self.var(argNum))
|
2021-04-29 19:09:30 +02:00
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
else:
|
|
|
|
new_args.append(f"{arg}")
|
|
|
|
return ", ".join(new_args)
|
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2021-03-24 18:30:25 +01:00
|
|
|
replace_funcs = {
|
2023-07-29 19:03:17 +02:00
|
|
|
"ActorExists": {0: "ActorIDs", 1: "Bool"},
|
|
|
|
"ActorSpeak": {0: "CustomMsg", 1: "ActorIDs", 3: "CustomAnim", 4: "CustomAnim"},
|
|
|
|
"AddActorDecoration": {0: "ActorIDs"},
|
|
|
|
"AddActorVar": {0: "ActorIDs"},
|
|
|
|
"AddKeyItem": {0: "ItemIDs"},
|
|
|
|
"AddGoalPos": {0: "ActorIDs"},
|
|
|
|
"BattleCamTargetActor": {0: "ActorIDs"},
|
|
|
|
"BindHandleEvent": {0: "ActorIDs"},
|
|
|
|
"BindIdle": {0: "ActorIDs"},
|
2023-08-21 20:07:08 +02:00
|
|
|
"BindHandlePhase": {0: "ActorIDs"},
|
2023-07-29 19:03:17 +02:00
|
|
|
"BindNpcAI": {0: "NpcIDs"},
|
|
|
|
"BindNpcDefeat": {0: "NpcIDs"},
|
|
|
|
"BindNpcIdle": {0: "NpcIDs"},
|
|
|
|
"BindNpcInteract": {0: "NpcIDs"},
|
|
|
|
"BindTakeTurn": {0: "ActorIDs"},
|
|
|
|
"CheckButtonDown": {0: "Hex", 1: "Bool"},
|
|
|
|
"ContinueSpeech": {1: "CustomAnim", 2: "CustomAnim", 4: "CustomMsg"},
|
|
|
|
"CopyBuffs": {0: "ActorIDs", 1: "ActorIDs"},
|
|
|
|
"CopyStatusEffects": {0: "ActorIDs", 1: "ActorIDs"},
|
2023-10-14 21:43:12 +02:00
|
|
|
"CountTargets": {0: "ActorIDs"},
|
2023-07-29 19:03:17 +02:00
|
|
|
"DisablePlayerInput": {0: "Bool"},
|
|
|
|
"DisablePlayerPhysics": {0: "Bool"},
|
|
|
|
"DispatchDamagePlayerEvent": {1: "Events"},
|
|
|
|
"DispatchEvent": {0: "ActorIDs", 1: "Events"},
|
|
|
|
"EnableActorBlur": {0: "ActorIDs"},
|
|
|
|
"EnableActorGlow": {0: "ActorIDs", 1: "Bool"},
|
|
|
|
"EnableIdleScript": {0: "ActorIDs"},
|
|
|
|
"EnableNpcShadow": {0: "NpcIDs", 1: "Bool"},
|
|
|
|
"EndActorSpeech": {0: "ActorIDs", 2: "CustomAnim", 3: "CustomAnim"},
|
|
|
|
"EndSpeech": {1: "CustomAnim", 2: "CustomAnim"},
|
|
|
|
"EnemyDamageTarget": {
|
|
|
|
0: "ActorIDs",
|
|
|
|
1: "HitResults",
|
|
|
|
2: "DamageTypes",
|
|
|
|
4: "StatusFlags",
|
|
|
|
6: "BattleStatusFlags1",
|
|
|
|
},
|
|
|
|
"EnemyTestTarget": {
|
|
|
|
0: "ActorIDs",
|
|
|
|
1: "HitResults",
|
|
|
|
2: "DamageTypes",
|
|
|
|
3: "StatusFlags",
|
|
|
|
5: "BattleStatusFlags1",
|
|
|
|
},
|
|
|
|
"FallToGoal": {0: "ActorIDs"},
|
|
|
|
"FindKeyItem": {0: "ItemIDs"},
|
|
|
|
"FlyPartTo": {0: "ActorIDs"},
|
|
|
|
"FlyToGoal": {0: "ActorIDs"},
|
|
|
|
"ForceHomePos": {0: "ActorIDs"},
|
2023-08-21 20:07:08 +02:00
|
|
|
"GetPartAnimNotify": {0: "ActorIDs"},
|
2023-07-29 19:03:17 +02:00
|
|
|
"SetActorPaletteEffect": {0: "ActorIDs"},
|
|
|
|
"SetActorPaletteSwapParams": {0: "ActorIDs"},
|
2023-10-02 02:12:53 +02:00
|
|
|
"EnableActorPaletteEffects": {0: "ActorIDs"},
|
2023-07-29 19:03:17 +02:00
|
|
|
"HideHealthBar": {0: "ActorIDs"},
|
|
|
|
"func_8027D434": {0: "ActorIDs"},
|
|
|
|
"SetProjectileTargetOffset": {0: "ActorIDs"},
|
|
|
|
"GetInstigatorValue": {0: "ActorIDs"},
|
|
|
|
"SetNpcFoldParams": {0: "NpcIDs"},
|
|
|
|
"SetNpcFoldFlags": {0: "NpcIDs"},
|
|
|
|
"UpdatePlayerFold": {0: "PlayerAnims"},
|
|
|
|
"GetAnimation": {0: "ActorIDs", 2: "CustomAnim"},
|
|
|
|
"GetActorFlags": {0: "ActorIDs", 1: "ActorFlags"},
|
|
|
|
"GetActorHP": {0: "ActorIDs"},
|
|
|
|
"GetActorPos": {0: "ActorIDs"},
|
|
|
|
"GetActorSize": {0: "ActorIDs"},
|
|
|
|
"GetActorVar": {0: "ActorIDs"},
|
|
|
|
"GetBattleFlags": {0: "BattleStatusFlags1"},
|
|
|
|
"GetBattlePhase": {0: "Phases"},
|
|
|
|
"GetDistanceToGoal": {0: "ActorIDs"},
|
|
|
|
"GetEnemyMaxHP": {0: "ActorIDs"},
|
|
|
|
"GetGoalPos": {0: "ActorIDs"},
|
|
|
|
"GetHomePos": {0: "ActorIDs"},
|
|
|
|
"GetIdleGoal": {0: "ActorIDs"},
|
|
|
|
"GetIndexFromHome": {0: "ActorIDs"},
|
|
|
|
"GetIndexFromPos": {0: "ActorIDs"},
|
|
|
|
"GetItemPower": {0: "ItemIDs"},
|
|
|
|
"GetLastDamage": {0: "ActorIDs"},
|
|
|
|
"GetLastElement": {0: "DamageTypes"},
|
|
|
|
"GetLastEvent": {0: "ActorIDs", 1: "Events"},
|
|
|
|
"GetPartOffset": {0: "ActorIDs"},
|
|
|
|
"GetPartPos": {0: "ActorIDs"},
|
|
|
|
"GetPartRotation": {0: "ActorIDs"},
|
|
|
|
"GetNpcPos": {0: "NpcIDs"},
|
|
|
|
"GetOriginalActorType": {0: "ActorIDs", 1: "ActorType"},
|
|
|
|
"GetStatusFlags": {0: "ActorIDs", 1: "StatusFlags"},
|
|
|
|
"HidePlayerShadow": {0: "Bool"},
|
|
|
|
"HPBarToCurrent": {0: "ActorIDs"},
|
|
|
|
"HPBarToHome": {0: "ActorIDs"},
|
|
|
|
"IdleFlyToGoal": {0: "ActorIDs"},
|
|
|
|
"IdleJumpToGoal": {0: "ActorIDs"},
|
|
|
|
"IdleRunToGoal": {0: "ActorIDs"},
|
|
|
|
"InterpNpcYaw": {0: "NpcIDs"},
|
|
|
|
"JumpPartTo": {0: "ActorIDs"},
|
|
|
|
"JumpToGoal": {0: "ActorIDs", 2: "Bool", 3: "Bool", 4: "Bool"},
|
|
|
|
"JumpWithBounce": {0: "ActorIDs"},
|
|
|
|
"LandJump": {0: "ActorIDs"},
|
|
|
|
"LoadActionCommand": {0: "ActionCommand"},
|
|
|
|
"MakeEntity": {0: "Hex"},
|
|
|
|
"MakeItemEntity": {0: "ItemIDs"},
|
|
|
|
"ModifyColliderFlags": {2: "Hex"},
|
|
|
|
"NpcFaceNpc": {0: "NpcIDs", 1: "NpcIDs"},
|
|
|
|
"NpcFacePlayer": {0: "NpcIDs"},
|
|
|
|
"NpcJump0": {0: "NpcIDs"},
|
|
|
|
"NpcJump1": {0: "NpcIDs"},
|
|
|
|
"NpcMoveTo": {0: "NpcIDs"},
|
|
|
|
"PlayAmbientSounds": {0: "AmbientSounds"},
|
|
|
|
"PlayEffect": {0: "EffectID"},
|
|
|
|
"PlayLoopingSoundAtActor": {0: "ActorIDs", 2: "SoundIDs"},
|
|
|
|
"PlaySound": {0: "SoundIDs"},
|
|
|
|
"PlaySoundAt": {0: "SoundIDs"},
|
|
|
|
"PlaySoundAtActor": {0: "ActorIDs", 1: "SoundIDs"},
|
|
|
|
"PlaySoundAtModel": {1: "SoundIDs"},
|
|
|
|
"PlaySoundAtNpc": {0: "NpcIDs", 1: "SoundIDs"},
|
|
|
|
"PlaySoundAtPart": {0: "ActorIDs", 2: "SoundIDs"},
|
|
|
|
"RemoveActor": {0: "ActorIDs"},
|
|
|
|
"RemoveActorDecoration": {0: "ActorIDs"},
|
|
|
|
"RemoveNpc": {0: "NpcIDs"},
|
|
|
|
"ResetActorSounds": {0: "ActorIDs"},
|
|
|
|
"ResetAllActorSounds": {0: "ActorIDs"},
|
|
|
|
"RunToGoal": {0: "ActorIDs", 2: "Bool"},
|
|
|
|
"SetActorDispOffset": {0: "ActorIDs"},
|
|
|
|
"SetActorFlagBits": {0: "ActorIDs", 1: "ActorFlags"},
|
|
|
|
"SetActorIdleSpeed": {0: "ActorIDs"},
|
|
|
|
"SetActorIdleJumpGravity": {0: "ActorIDs"},
|
|
|
|
"SetActorJumpGravity": {0: "ActorIDs"},
|
|
|
|
"SetActorPos": {0: "ActorIDs"},
|
|
|
|
"SetActorRotation": {0: "ActorIDs"},
|
|
|
|
"SetActorRotationOffset": {0: "ActorIDs"},
|
|
|
|
"SetActorScale": {0: "ActorIDs"},
|
|
|
|
"SetActorSize": {0: "ActorIDs"},
|
|
|
|
"SetActorSounds": {0: "ActorIDs"},
|
|
|
|
"SetActorSpeed": {0: "ActorIDs"},
|
|
|
|
"SetActorType": {0: "ActorIDs", 1: "ActorType"},
|
|
|
|
"SetActorVar": {0: "ActorIDs"},
|
|
|
|
"SetActorYaw": {0: "ActorIDs"},
|
|
|
|
"SetAnimation": {0: "ActorIDs", 2: "CustomAnim"},
|
|
|
|
"SetAnimationRate": {0: "ActorIDs"},
|
|
|
|
"SetBattleFlagBits": {0: "BattleStatusFlags1"},
|
|
|
|
"SetBattleFlagBits2": {0: "BattleStatusFlags2"},
|
|
|
|
"SetDefenseTable": {0: "ActorIDs"},
|
|
|
|
"SetEnemyHP": {0: "ActorIDs"},
|
|
|
|
"SetEnemyTargetOffset": {0: "ActorIDs"},
|
|
|
|
"SetGoalPos": {0: "ActorIDs"},
|
|
|
|
"SetGoalToFirstTarget": {0: "ActorIDs"},
|
|
|
|
"SetGoalToHome": {0: "ActorIDs"},
|
|
|
|
"SetGoalToIndex": {0: "ActorIDs"},
|
|
|
|
"SetGoalToTarget": {0: "ActorIDs"},
|
|
|
|
"SetHomePos": {0: "ActorIDs"},
|
|
|
|
"SetIdleAnimations": {0: "ActorIDs"},
|
|
|
|
"SetIdleGoal": {0: "ActorIDs"},
|
|
|
|
"SetIdleGoalToHome": {0: "ActorIDs"},
|
|
|
|
"SetJumpAnimations": {
|
|
|
|
0: "ActorIDs",
|
|
|
|
2: "PlayerAnims",
|
|
|
|
3: "PlayerAnims",
|
|
|
|
4: "PlayerAnims",
|
|
|
|
},
|
|
|
|
"SetMusicTrack": {1: "SongIDs"},
|
|
|
|
"SetNpcAnimation": {0: "NpcIDs", 1: "CustomAnim"},
|
|
|
|
"SetNpcAux": {0: "NpcIDs"},
|
|
|
|
"SetNpcFlagBits": {0: "NpcIDs", 1: "NpcFlags", 2: "Bool"},
|
|
|
|
"SetNpcJumpscale": {0: "NpcIDs"},
|
|
|
|
"SetNpcPos": {0: "NpcIDs"},
|
|
|
|
"SetNpcRotation": {0: "NpcIDs"},
|
|
|
|
"SetNpcScale": {0: "NpcIDs"},
|
|
|
|
"SetNpcSpeed": {0: "NpcIDs"},
|
|
|
|
"SetNpcSprite": {1: "Hex"},
|
|
|
|
"SetNpcYaw": {0: "NpcIDs"},
|
|
|
|
"SetOwnerID": {0: "ActorIDs"},
|
|
|
|
"SetPartAlpha": {0: "ActorIDs"},
|
|
|
|
"SetPartDispOffset": {0: "ActorIDs"},
|
|
|
|
"SetPartEventBits": {0: "ActorIDs", 2: "ActorEventFlags"},
|
|
|
|
"SetPartFlags": {0: "ActorIDs", 2: "ActorPartFlags"},
|
|
|
|
"SetPartFlagBits": {0: "ActorIDs", 2: "ActorPartFlags"},
|
|
|
|
"SetPartJumpGravity": {0: "ActorIDs"},
|
|
|
|
"SetPartMoveSpeed": {0: "ActorIDs"},
|
|
|
|
"SetPartPos": {0: "ActorIDs"},
|
|
|
|
"SetPartRotation": {0: "ActorIDs"},
|
|
|
|
"SetPartRotationOffset": {0: "ActorIDs"},
|
|
|
|
"SetPartScale": {0: "ActorIDs"},
|
|
|
|
"SetPartSize": {0: "ActorIDs"},
|
|
|
|
"SetPartSounds": {0: "ActorIDs"},
|
|
|
|
"SetPartTargetFlagBits": {0: "ActorIDs"},
|
|
|
|
"SetPartYaw": {0: "ActorIDs"},
|
|
|
|
"SetPlayerAnimation": {0: "PlayerAnims"},
|
|
|
|
"SetSelfEnemyFlagBits": {0: "NpcFlags", 1: "Bool"},
|
|
|
|
# "SetSelfVar" :{1:"Bool"}, # apparently this was a bool in some scripts but it passes non-0/1 values, including negatives
|
|
|
|
"SetSpriteShading": {0: "CustomAnim"},
|
|
|
|
"SetStatusTable": {0: "ActorIDs"},
|
|
|
|
"SetTargetActor": {0: "ActorIDs", 1: "ActorIDs"},
|
|
|
|
"SetTargetOffset": {0: "ActorIDs"},
|
|
|
|
"ShowChoice": {0: "CustomMsg"},
|
|
|
|
"ShowEmote": {1: "Emotes"},
|
|
|
|
"ShowMessageAtScreenPos": {0: "CustomMsg"},
|
|
|
|
"ShowMessageAtWorldPos": {0: "CustomMsg"},
|
|
|
|
"SpeakToPlayer": {0: "NpcIDs", 1: "CustomAnim", 2: "CustomAnim", -1: "CustomMsg"},
|
|
|
|
"StopLoopingSoundAtActor": {0: "ActorIDs"},
|
|
|
|
"SwitchMessage": {0: "CustomMsg"},
|
|
|
|
"UseBattleCamPreset": {0: "BtlCameraPreset"},
|
|
|
|
"UseIdleAnimation": {0: "ActorIDs", 1: "Bool"},
|
|
|
|
"WasStatusInflicted": {0: "ActorIDs", 1: "Bool"},
|
2021-03-24 18:30:25 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2022-01-06 17:32:57 +01:00
|
|
|
def trim_lw(arg):
|
2023-07-29 19:03:17 +02:00
|
|
|
return arg[arg.find("(") + 1 : arg.find(")")]
|
2022-01-06 17:32:57 +01:00
|
|
|
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2021-04-03 19:21:49 +02:00
|
|
|
def replace_constants(self, func, args):
|
2021-03-24 18:30:25 +01:00
|
|
|
global replace_funcs
|
2022-01-06 17:32:57 +01:00
|
|
|
global LOCAL_WORDS
|
|
|
|
|
|
|
|
new_args = []
|
|
|
|
new_args = args.split(", ")
|
|
|
|
for i, new_arg in enumerate(new_args):
|
|
|
|
if new_arg.startswith("LW"):
|
|
|
|
new_arg = trim_lw(new_arg)
|
|
|
|
LOCAL_WORDS[int(new_arg)] = 0
|
2021-03-30 05:39:17 +02:00
|
|
|
|
2021-03-24 18:30:25 +01:00
|
|
|
if func in replace_funcs:
|
2021-04-03 19:21:49 +02:00
|
|
|
return fix_args(self, func, args, replace_funcs[func])
|
2022-01-06 17:32:57 +01:00
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
return args
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2022-01-06 17:32:57 +01:00
|
|
|
def fix_1_arg(self, lw, arg, format):
|
|
|
|
if lw:
|
|
|
|
lw = int(trim_lw(lw))
|
|
|
|
if LOCAL_WORDS[lw]:
|
2023-07-29 19:03:17 +02:00
|
|
|
info = {0: LOCAL_WORDS[lw]}
|
2022-01-06 17:32:57 +01:00
|
|
|
args = f"{arg}"
|
|
|
|
return fix_args(self, 0, args, info)
|
|
|
|
|
|
|
|
if format:
|
2023-07-29 19:03:17 +02:00
|
|
|
info = {0: format}
|
2022-01-06 17:32:57 +01:00
|
|
|
args = f"{arg}"
|
|
|
|
return fix_args(self, 0, args, info)
|
|
|
|
|
|
|
|
return arg
|
2021-12-23 13:16:12 +01:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
class ScriptDisassembler:
|
2023-07-29 19:03:17 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
bytes,
|
|
|
|
script_name="script",
|
|
|
|
symbol_map={},
|
|
|
|
romstart=0,
|
|
|
|
INCLUDES_NEEDED={"forward": [], "sprites": set(), "npcs": []},
|
|
|
|
INCLUDED={"functions": set(), "includes": set()},
|
|
|
|
prelude=True,
|
|
|
|
transform_symbol_name=None,
|
|
|
|
use_script_lib=True,
|
|
|
|
):
|
2020-10-31 03:28:18 +01:00
|
|
|
self.bytes = bytes
|
|
|
|
self.script_name = script_name
|
2021-10-25 13:44:55 +02:00
|
|
|
self.prelude = prelude
|
2021-03-24 09:39:43 +01:00
|
|
|
|
2021-12-30 15:22:44 +01:00
|
|
|
if use_script_lib:
|
|
|
|
self.symbol_map = extend_symbol_map(symbol_map, script_lib(self.bytes.tell()))
|
|
|
|
else:
|
|
|
|
self.symbol_map = symbol_map
|
|
|
|
|
2021-04-03 19:21:49 +02:00
|
|
|
self.romstart = romstart
|
2021-12-28 18:11:07 +01:00
|
|
|
self.transform_symbol_name = transform_symbol_name
|
2021-04-03 19:21:49 +02:00
|
|
|
self.INCLUDES_NEEDED = INCLUDES_NEEDED
|
|
|
|
self.INCLUDED = INCLUDED
|
2020-10-31 03:28:18 +01:00
|
|
|
|
|
|
|
self.out = ""
|
|
|
|
self.prefix = ""
|
|
|
|
|
|
|
|
self.indent = 1
|
|
|
|
self.indent_used = False
|
|
|
|
|
|
|
|
self.done = False
|
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
self.start_pos = self.bytes.tell()
|
|
|
|
self.end_pos = 0
|
|
|
|
self.instructions = 0
|
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
def disassemble(self):
|
|
|
|
while True:
|
|
|
|
opcode = self.read_word()
|
|
|
|
argc = self.read_word()
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
# print(f"Op {opcode:X}, argc {argc}")
|
2021-04-03 19:21:49 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
if opcode > 0xFF or argc > 0xFF:
|
2021-12-28 18:11:07 +01:00
|
|
|
raise Exception(f"script '{self.script_name}' is malformed (opcode {opcode:X}, argc {argc:X})")
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
argv = []
|
|
|
|
for i in range(0, argc):
|
|
|
|
argv.append(self.read_word())
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
# print(argv)
|
2021-04-03 19:21:49 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
self.disassemble_command(opcode, argc, argv)
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2021-03-24 09:39:43 +01:00
|
|
|
self.instructions += 1
|
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
if self.done:
|
2021-03-24 09:39:43 +01:00
|
|
|
self.end_pos = self.bytes.tell()
|
2020-10-31 03:28:18 +01:00
|
|
|
return self.prefix + self.out
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2020-11-08 15:57:58 +01:00
|
|
|
def write(self, line):
|
2023-07-29 19:03:17 +02:00
|
|
|
if self.indent < 0:
|
|
|
|
self.indent = 0
|
|
|
|
if self.indent > 1:
|
|
|
|
self.indent_used = True
|
2020-10-31 03:28:18 +01:00
|
|
|
|
|
|
|
self.out += " " * self.indent
|
|
|
|
self.out += line
|
2020-11-08 15:57:58 +01:00
|
|
|
|
|
|
|
def write_line(self, line):
|
|
|
|
self.write(line)
|
2020-10-31 03:28:18 +01:00
|
|
|
self.out += "\n"
|
|
|
|
|
|
|
|
def prefix_line(self, line):
|
|
|
|
self.prefix += line
|
|
|
|
self.prefix += "\n"
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
def var(self, arg, prefer_hex=False, use_evt_ptr=True):
|
2020-10-31 03:28:18 +01:00
|
|
|
if arg in self.symbol_map:
|
2021-10-25 13:44:55 +02:00
|
|
|
s = self.symbol_map[arg][0][1]
|
2022-09-11 18:23:47 +02:00
|
|
|
return f"EVT_PTR({s})" if use_evt_ptr else s
|
2020-10-21 04:10:13 +02:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
v = arg - 2**32 # convert to s32
|
2020-10-19 23:58:57 +02:00
|
|
|
if v > -250000000:
|
2023-07-29 19:03:17 +02:00
|
|
|
if v <= -220000000:
|
|
|
|
return f"EVT_FLOAT({round_fixed((v + 230000000) / 1024)})"
|
|
|
|
elif v <= -200000000:
|
|
|
|
return f"ArrayFlag({v + 210000000})"
|
|
|
|
elif v <= -180000000:
|
|
|
|
return f"ArrayVar({v + 190000000})"
|
|
|
|
elif v <= -160000000:
|
|
|
|
return f"GameByte({v + 170000000})"
|
|
|
|
elif v <= -140000000:
|
|
|
|
return f"AreaByte({v + 150000000})"
|
|
|
|
elif v <= -120000000:
|
|
|
|
return f"GameFlag({v + 130000000})"
|
|
|
|
elif v <= -100000000:
|
|
|
|
return f"AreaFlag({v + 110000000})"
|
|
|
|
elif v <= -80000000:
|
|
|
|
return f"MapFlag({v + 90000000})"
|
|
|
|
elif v <= -60000000:
|
|
|
|
return f"LocalFlag({v + 70000000})"
|
|
|
|
elif v <= -40000000:
|
|
|
|
return f"MapVar({v + 50000000})"
|
|
|
|
elif v <= -20000000:
|
|
|
|
return f"LocalVar({v + 30000000})"
|
2020-10-19 23:58:57 +02:00
|
|
|
|
|
|
|
if arg == 0xFFFFFFFF:
|
|
|
|
return "-1"
|
2021-04-03 19:21:49 +02:00
|
|
|
elif (arg & 0xFF000000) == 0x80000000:
|
2020-10-19 23:58:57 +02:00
|
|
|
return f"0x{arg:X}"
|
2021-04-03 19:21:49 +02:00
|
|
|
elif arg >= 0x80000000:
|
|
|
|
return f"{arg - 0x100000000}"
|
2021-10-25 13:44:55 +02:00
|
|
|
elif prefer_hex and arg > 0:
|
|
|
|
return f"0x{arg:X}"
|
2020-10-19 23:58:57 +02:00
|
|
|
else:
|
|
|
|
return f"{arg}"
|
|
|
|
|
2021-04-03 19:21:49 +02:00
|
|
|
def replace_star_rod_function_name(self, name):
|
2023-07-29 19:03:17 +02:00
|
|
|
vram = int(name.split("_", 1)[1], 16)
|
2021-04-08 19:42:36 +02:00
|
|
|
name = "N(" + name.replace("function", "func") + f"_{(vram - 0x80240000)+self.romstart:X}" + ")"
|
2021-04-03 19:21:49 +02:00
|
|
|
return name
|
|
|
|
|
2021-04-29 19:09:30 +02:00
|
|
|
def replace_star_rod_prefix(self, addr, isArg=False):
|
|
|
|
if type(addr) is str:
|
|
|
|
return addr
|
2021-04-28 10:58:21 +02:00
|
|
|
if addr > 0x80000000 and addr in self.symbol_map:
|
2021-12-28 18:11:07 +01:00
|
|
|
name = find_symbol_in_overlay(self.symbol_map, self.romstart, addr)
|
|
|
|
if self.transform_symbol_name:
|
|
|
|
name = self.transform_symbol_name(name)
|
2021-04-03 19:21:49 +02:00
|
|
|
toReplace = True
|
|
|
|
suffix = ""
|
2021-10-25 13:44:55 +02:00
|
|
|
if False and name.startswith("N(func_"):
|
2021-04-03 19:21:49 +02:00
|
|
|
prefix = "ApiStatus "
|
|
|
|
name = self.replace_star_rod_function_name(name[2:-1])
|
2021-08-22 23:50:10 +02:00
|
|
|
suffix = "(Evt* script, s32 isInitialCall)"
|
2021-04-08 19:42:36 +02:00
|
|
|
elif name[2:-1] in self.INCLUDED["includes"]:
|
|
|
|
prefix = "ApiStatus "
|
2021-08-22 23:50:10 +02:00
|
|
|
suffix = "(Evt* script, s32 isInitialCall)"
|
2021-04-08 19:42:36 +02:00
|
|
|
elif name.startswith("N(npcAISettings_"):
|
2022-09-08 14:12:26 +02:00
|
|
|
prefix = "MobileAISettings "
|
2021-04-08 19:42:36 +02:00
|
|
|
elif name.startswith("N(npcSettings_"):
|
2021-04-03 19:21:49 +02:00
|
|
|
prefix = "NpcSettings "
|
2021-04-08 19:42:36 +02:00
|
|
|
elif name.startswith("N(npcGroup_"):
|
2023-02-14 01:46:48 +01:00
|
|
|
prefix = "NpcData "
|
2021-04-08 19:42:36 +02:00
|
|
|
elif name.startswith("N(entryList_"):
|
|
|
|
prefix = "EntryList "
|
|
|
|
elif name.startswith("N(npcGroupList_"):
|
2021-04-03 19:21:49 +02:00
|
|
|
prefix = "NpcGroupList "
|
2021-04-08 19:42:36 +02:00
|
|
|
elif name.startswith("N("):
|
2022-01-16 14:28:09 +01:00
|
|
|
prefix = "EvtScript "
|
2021-04-03 19:21:49 +02:00
|
|
|
else:
|
|
|
|
toReplace = False
|
|
|
|
|
|
|
|
if toReplace:
|
|
|
|
if name not in self.INCLUDED["functions"]:
|
|
|
|
self.INCLUDES_NEEDED["forward"].append(prefix + name + suffix + ";")
|
2021-04-08 19:42:36 +02:00
|
|
|
self.INCLUDED["functions"].add(name)
|
2021-04-29 19:09:30 +02:00
|
|
|
return name
|
2023-07-29 19:03:17 +02:00
|
|
|
elif not isArg or name.startswith('"'):
|
2021-04-29 19:09:30 +02:00
|
|
|
return name
|
|
|
|
else:
|
|
|
|
return str(addr)
|
2021-04-03 19:21:49 +02:00
|
|
|
return addr
|
|
|
|
|
2021-04-29 19:09:30 +02:00
|
|
|
def addr_ref(self, addr, isArg=False):
|
2020-10-31 03:28:18 +01:00
|
|
|
if addr in self.symbol_map:
|
2021-04-29 19:09:30 +02:00
|
|
|
return self.replace_star_rod_prefix(addr, isArg)
|
2023-07-29 19:03:17 +02:00
|
|
|
return self.var(addr) # f"0x{addr:08X}"
|
2020-10-21 04:10:13 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
def trigger(self, trigger):
|
2023-07-29 19:03:17 +02:00
|
|
|
if trigger == 0x00000040:
|
|
|
|
trigger = "TRIGGER_WALL_PUSH"
|
|
|
|
if trigger == 0x00000080:
|
|
|
|
trigger = "TRIGGER_FLOOR_TOUCH"
|
|
|
|
if trigger == 0x00000100:
|
|
|
|
trigger = "TRIGGER_WALL_PRESS_A"
|
|
|
|
if trigger == 0x00000200:
|
|
|
|
trigger = "TRIGGER_FLOOR_JUMP"
|
|
|
|
if trigger == 0x00000400:
|
|
|
|
trigger = "TRIGGER_WALL_TOUCH"
|
|
|
|
if trigger == 0x00000800:
|
|
|
|
trigger = "TRIGGER_FLOOR_PRESS_A"
|
|
|
|
if trigger == 0x00001000:
|
|
|
|
trigger = "TRIGGER_WALL_HAMMER"
|
|
|
|
if trigger == 0x00010000:
|
|
|
|
trigger = "TRIGGER_GAME_FLAG_SET"
|
|
|
|
if trigger == 0x00020000:
|
|
|
|
trigger = "TRIGGER_AREA_FLAG_SET"
|
|
|
|
if trigger == 0x00040000:
|
|
|
|
trigger = "TRIGGER_CEILING_TOUCH"
|
|
|
|
if trigger == 0x00080000:
|
|
|
|
trigger = "TRIGGER_FLOOR_ABOVE"
|
|
|
|
if trigger == 0x00100000:
|
|
|
|
trigger = "TRIGGER_POINT_BOMB"
|
2020-11-08 20:07:10 +01:00
|
|
|
return f"0x{trigger:X}" if type(trigger) is int else trigger
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
def read_word(self):
|
|
|
|
return int.from_bytes(self.bytes.read(4), byteorder="big")
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
def disassemble_command(self, opcode, argc, argv):
|
2020-10-19 23:58:57 +02:00
|
|
|
if opcode == 0x01:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line("EVT_END")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
if self.prelude:
|
2022-01-13 07:26:42 +01:00
|
|
|
try:
|
2023-07-29 19:03:17 +02:00
|
|
|
self.prefix_line(
|
|
|
|
f"EvtScript D_{self.script_name - info[0] + info[2]:08X}_{self.script_name:06X} = {{"
|
|
|
|
)
|
2022-01-13 07:26:42 +01:00
|
|
|
self.write_line("};")
|
|
|
|
except:
|
2022-01-16 14:28:09 +01:00
|
|
|
self.prefix_line(f"EvtScript {self.script_name} = {{")
|
2022-01-13 07:26:42 +01:00
|
|
|
self.write_line("};")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.done = True
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x02:
|
|
|
|
self.write_line(f"EVT_RETURN")
|
|
|
|
elif opcode == 0x03:
|
|
|
|
self.write_line(f"EVT_LABEL({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x04:
|
|
|
|
self.write_line(f"EVT_GOTO({self.var(argv[0])})")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x05:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_LOOP({self.var(argv[0])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x06:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line("EVT_END_LOOP")
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x07:
|
|
|
|
self.write_line(f"EVT_BREAK_LOOP")
|
|
|
|
elif opcode == 0x08:
|
|
|
|
self.write_line(f"EVT_WAIT({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x09:
|
|
|
|
self.write_line(f"EVT_WAIT_SECS({self.var(argv[0])})")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x0A:
|
2022-01-06 17:32:57 +01:00
|
|
|
if self.var(argv[0]).startswith("LW"):
|
|
|
|
args_str = fix_1_arg(self, self.var(argv[0]), self.var(argv[1]), 0)
|
|
|
|
else:
|
|
|
|
args_str = self.var(argv[1])
|
|
|
|
self.write_line(f"EVT_IF_EQ({self.var(argv[0])}, {args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x0B:
|
2022-01-06 17:32:57 +01:00
|
|
|
if self.var(argv[0]).startswith("LW"):
|
|
|
|
args_str = fix_1_arg(self, self.var(argv[0]), self.var(argv[1]), 0)
|
|
|
|
else:
|
|
|
|
args_str = self.var(argv[1])
|
|
|
|
self.write_line(f"EVT_IF_NE({self.var(argv[0])}, {args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x0C:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_IF_LT({self.var(argv[0])}, {self.var(argv[1])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x0D:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_IF_GT({self.var(argv[0])}, {self.var(argv[1])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x0E:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_IF_LE({self.var(argv[0])}, {self.var(argv[1])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x0F:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_IF_GE({self.var(argv[0])}, {self.var(argv[1])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x10:
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, self.var(argv[0]), self.var(argv[1]), "Hex")
|
|
|
|
self.write_line(f"EVT_IF_FLAG({self.var(argv[0])}, {args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x11:
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, self.var(argv[0]), self.var(argv[1]), "Hex")
|
|
|
|
self.write_line(f"EVT_IF_NOT_FLAG({self.var(argv[0])}, {args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
|
|
|
elif opcode == 0x12:
|
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_ELSE")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x13:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_END_IF")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x14:
|
2022-01-06 17:32:57 +01:00
|
|
|
new_arg = trim_lw(self.var(argv[0]))
|
|
|
|
SWITCH_TYPES.append(LOCAL_WORDS[int(new_arg)])
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_SWITCH({self.var(argv[0])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 2
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x15:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_SWITCH_CONST(0x{argv[0]:X})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 2
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x16:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, 0, self.var(argv[0]), SWITCH_TYPES[-1])
|
|
|
|
self.write_line(f"EVT_CASE_EQ({args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x17:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, 0, self.var(argv[0]), SWITCH_TYPES[-1])
|
|
|
|
self.write_line(f"EVT_CASE_NE({args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x18:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_CASE_LT({self.var(argv[0])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x19:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_CASE_GT({self.var(argv[0])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x1A:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_CASE_LE({self.var(argv[0])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x1B:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_CASE_GE({self.var(argv[0])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x1C:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_CASE_DEFAULT")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x1D:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, 0, self.var(argv[0]), SWITCH_TYPES[-1])
|
|
|
|
self.write_line(f"EVT_CASE_OR_EQ({args_str})")
|
2020-11-08 20:07:10 +01:00
|
|
|
self.indent += 1
|
|
|
|
elif opcode == 0x1E:
|
|
|
|
self.indent -= 1
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, 0, self.var(argv[0]), SWITCH_TYPES[-1])
|
|
|
|
self.write_line(f"EVT_CASE_AND_EQ({args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x1F:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2022-01-06 17:32:57 +01:00
|
|
|
args_str = fix_1_arg(self, 0, self.var(argv[0]), SWITCH_TYPES[-1])
|
|
|
|
self.write_line(f"EVT_CASE_FLAG({args_str})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x20:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_END_CASE_GROUP")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x21:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_CASE_RANGE({self.var(argv[0])}, {self.var(argv[1])})")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x22:
|
|
|
|
self.write_line(f"EVT_BREAK_SWITCH")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x23:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 2
|
2022-01-06 17:32:57 +01:00
|
|
|
del SWITCH_TYPES[-1]
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_END_SWITCH")
|
2022-01-06 17:32:57 +01:00
|
|
|
elif opcode == 0x24:
|
|
|
|
if self.var(argv[0]).startswith("LW"):
|
|
|
|
new_arg = trim_lw(self.var(argv[0]))
|
|
|
|
if self.var(argv[1]).startswith("LW"):
|
2023-07-29 19:03:17 +02:00
|
|
|
from_arg = trim_lw(self.var(argv[1])) # Carry type info of LW if being set from an LW
|
2022-01-06 17:32:57 +01:00
|
|
|
else:
|
2023-07-29 19:03:17 +02:00
|
|
|
from_arg = 0 # If a constant, we no longer know the type
|
2022-01-06 17:32:57 +01:00
|
|
|
LOCAL_WORDS[int(new_arg)] = LOCAL_WORDS[int(from_arg)]
|
|
|
|
|
|
|
|
self.write_line(f"EVT_SET({self.var(argv[0])}, {self.var(argv[1])})")
|
2021-10-25 13:44:55 +02:00
|
|
|
elif opcode == 0x25:
|
|
|
|
argNum = argv[1]
|
2023-07-29 19:03:17 +02:00
|
|
|
sprite = (argNum & 0xFF0000) >> 16
|
|
|
|
palette = (argNum & 0xFF00) >> 8
|
|
|
|
anim = (argNum & 0xFF) >> 0
|
2021-10-25 13:44:55 +02:00
|
|
|
if sprite > 0:
|
|
|
|
value = make_anim_macro(self, sprite, palette, anim)
|
2022-01-06 17:32:57 +01:00
|
|
|
elif argNum > 100:
|
2021-10-25 13:44:55 +02:00
|
|
|
value = f"0x{argNum:08X}"
|
2022-01-06 17:32:57 +01:00
|
|
|
else:
|
|
|
|
value = f"{argNum}"
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_SET_CONST({self.var(argv[0])}, {value})")
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x26:
|
|
|
|
self.write_line(f"EVT_SETF({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x27:
|
|
|
|
self.write_line(f"EVT_ADD({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x28:
|
|
|
|
self.write_line(f"EVT_SUB({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x29:
|
|
|
|
self.write_line(f"EVT_MUL({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x2A:
|
|
|
|
self.write_line(f"EVT_DIV({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x2B:
|
|
|
|
self.write_line(f"EVT_MOD({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x2C:
|
|
|
|
self.write_line(f"EVT_ADDF({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x2D:
|
|
|
|
self.write_line(f"EVT_SUBF({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x2E:
|
|
|
|
self.write_line(f"EVT_MULF({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x2F:
|
|
|
|
self.write_line(f"EVT_DIVF({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x30:
|
|
|
|
self.write_line(f"EVT_USE_BUF({self.var(argv[0])})")
|
2021-10-25 13:44:55 +02:00
|
|
|
elif opcode == 0x31 or opcode == 0x32 or opcode == 0x33 or opcode == 0x34:
|
|
|
|
args = [*map(self.var, argv)]
|
|
|
|
self.write_line(f"EVT_BUF_READ{opcode - 0x30}({', '.join(args)})")
|
2020-11-08 20:07:10 +01:00
|
|
|
elif opcode == 0x35:
|
2021-10-25 13:44:55 +02:00
|
|
|
args = [*map(self.var, argv)]
|
|
|
|
self.write_line(f"EVT_BUF_PEEK({', '.join(args)})")
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x36:
|
|
|
|
self.write_line(f"EVT_USE_FBUF({self.var(argv[0])})")
|
2021-10-25 13:44:55 +02:00
|
|
|
elif opcode == 0x37 or opcode == 0x38 or opcode == 0x39 or opcode == 0x3A:
|
|
|
|
args = [*map(self.var, argv)]
|
|
|
|
self.write_line(f"EVT_FBUF_READ{opcode - 0x36}({', '.join(args)})")
|
2020-11-08 20:07:10 +01:00
|
|
|
elif opcode == 0x3B:
|
2021-10-25 13:44:55 +02:00
|
|
|
args = [*map(self.var, argv)]
|
|
|
|
self.write_line(f"EVT_FBUF_PEEK({', '.join(args)})")
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x3C:
|
|
|
|
self.write_line(f"EVT_USE_ARRAY({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x3D:
|
|
|
|
self.write_line(f"EVT_USE_FLAG_ARRAY({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x3E:
|
|
|
|
self.write_line(f"EVT_MALLOC_ARRAY({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x3F:
|
|
|
|
self.write_line(f"EVT_BITWISE_AND({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x40:
|
|
|
|
self.write_line(f"EVT_BITWISE_AND_CONST({self.var(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x41:
|
|
|
|
self.write_line(f"EVT_BITWISE_OR({self.var(argv[0])}, 0x{argv[1]:X})")
|
|
|
|
elif opcode == 0x42:
|
|
|
|
self.write_line(f"EVT_BITWISE_OR_CONST({self.var(argv[0])}, 0x{argv[1]:X})")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x43:
|
2021-10-25 13:44:55 +02:00
|
|
|
func = self.addr_ref(argv[0])
|
|
|
|
args = [self.var(a, use_evt_ptr=True) for a in argv[1:]]
|
2023-07-29 19:03:17 +02:00
|
|
|
args_str = ", ".join(args)
|
2021-10-25 13:44:55 +02:00
|
|
|
args_str = replace_constants(self, func, args_str)
|
|
|
|
if func.startswith("evt_"):
|
|
|
|
# use func-specific macro
|
|
|
|
self.write_line(f"{func}({args_str})")
|
2021-12-24 14:29:05 +01:00
|
|
|
# Since the map ascii for map transitions is global, and several of them share the same RAM address,
|
|
|
|
# or ar not migrated, we have to create a placeholder
|
|
|
|
elif func == "GotoMap" or func == "GotoMapSpecial":
|
|
|
|
args = [self.var(a, use_evt_ptr=True) for a in argv[2:]]
|
2023-07-29 19:03:17 +02:00
|
|
|
args_str = ", ".join(args)
|
2021-12-24 14:29:05 +01:00
|
|
|
self.write_line(f"EVT_CALL({func}, EVT_PTR(UNK_STR_{argv[1]:X}), {args_str})")
|
2021-10-25 13:44:55 +02:00
|
|
|
elif args_str:
|
|
|
|
self.write_line(f"EVT_CALL({func}, {args_str})")
|
|
|
|
else:
|
2023-07-29 19:03:17 +02:00
|
|
|
self.write_line(f"EVT_CALL({func})") # no args
|
|
|
|
elif opcode == 0x44:
|
|
|
|
self.write_line(f"EVT_EXEC({self.addr_ref(argv[0])})")
|
|
|
|
elif opcode == 0x45:
|
|
|
|
self.write_line(f"EVT_EXEC_GET_TID({self.addr_ref(argv[0])}, {self.var(argv[1])})")
|
|
|
|
elif opcode == 0x46:
|
|
|
|
self.write_line(f"EVT_EXEC_WAIT({self.addr_ref(argv[0])})")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x47:
|
2023-07-29 19:03:17 +02:00
|
|
|
args = [
|
|
|
|
self.addr_ref(argv[0]),
|
|
|
|
self.trigger(argv[1]),
|
|
|
|
self.collider_id(argv[2]),
|
|
|
|
*map(self.var, argv[3:]),
|
|
|
|
]
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_BIND_TRIGGER({', '.join(args)})")
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x48:
|
|
|
|
self.write_line(f"EVT_UNBIND")
|
|
|
|
elif opcode == 0x49:
|
|
|
|
self.write_line(f"EVT_KILL_THREAD({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x4A:
|
|
|
|
self.write_line(f"EVT_JUMP({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x4B:
|
|
|
|
self.write_line(f"EVT_SET_PRIORITY({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x4C:
|
|
|
|
self.write_line(f"EVT_SET_TIMESCALE({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x4D:
|
|
|
|
self.write_line(f"EVT_SET_GROUP({self.var(argv[0])})")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x4E:
|
2023-07-29 19:03:17 +02:00
|
|
|
args = [
|
|
|
|
self.addr_ref(argv[0]),
|
|
|
|
self.trigger(argv[1]),
|
|
|
|
self.collider_id(argv[2]),
|
|
|
|
*map(self.var, argv[3:]),
|
|
|
|
]
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"EVT_BIND_PADLOCK({', '.join(args)})")
|
2023-07-29 19:03:17 +02:00
|
|
|
elif opcode == 0x4F:
|
|
|
|
self.write_line(f"EVT_SUSPEND_GROUP({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x50:
|
|
|
|
self.write_line(f"EVT_RESUME_GROUP({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x51:
|
|
|
|
self.write_line(f"EVT_SUSPEND_OTHERS({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x52:
|
|
|
|
self.write_line(f"EVT_RESUME_OTHERS({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x53:
|
|
|
|
self.write_line(f"EVT_SUSPEND_THREAD({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x54:
|
|
|
|
self.write_line(f"EVT_RESUME_THREAD({self.var(argv[0])})")
|
|
|
|
elif opcode == 0x55:
|
|
|
|
self.write_line(f"EVT_IS_THREAD_RUNNING({self.var(argv[0])}, {self.var(argv[1])})")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x56:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line("EVT_THREAD")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x57:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line("EVT_END_THREAD")
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x58:
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line("EVT_CHILD_THREAD")
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent += 1
|
2020-10-19 23:58:57 +02:00
|
|
|
elif opcode == 0x59:
|
2020-10-31 03:28:18 +01:00
|
|
|
self.indent -= 1
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line("EVT_END_CHILD_THREAD")
|
2020-10-19 23:58:57 +02:00
|
|
|
else:
|
|
|
|
# unknown opcode
|
|
|
|
argv_str = ""
|
|
|
|
for arg in argv:
|
|
|
|
argv_str += ", "
|
|
|
|
argv_str += f"0x{arg:X}"
|
2021-10-25 13:44:55 +02:00
|
|
|
self.write_line(f"0x{opcode:02X}{argv_str}),")
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-10-25 13:44:55 +02:00
|
|
|
def collider_id(self, arg):
|
|
|
|
if arg >= 0x4000 and arg <= 0x5000:
|
|
|
|
return f"EVT_ENTITY_INDEX({arg - 0x4000})"
|
|
|
|
else:
|
|
|
|
return self.var(arg)
|
2023-07-29 19:03:17 +02:00
|
|
|
|
|
|
|
|
2020-10-31 03:28:18 +01:00
|
|
|
class UnsupportedScript(Exception):
|
|
|
|
pass
|
2023-07-29 19:03:17 +02:00
|
|
|
|
|
|
|
|
2020-10-19 23:58:57 +02:00
|
|
|
if __name__ == "__main__":
|
2021-03-29 02:05:56 +02:00
|
|
|
import argparse
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-03-29 02:05:56 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("file", type=str, help="File to dissassemble from")
|
2021-12-16 01:27:26 +01:00
|
|
|
parser.add_argument("offset", help="Offset to start dissassembling from")
|
2023-07-29 19:03:17 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-end",
|
|
|
|
"-e",
|
|
|
|
"--e",
|
|
|
|
type=lambda x: int(x, 16),
|
|
|
|
default=0,
|
|
|
|
dest="end",
|
|
|
|
required=False,
|
|
|
|
help="End offset to stop dissassembling from.\nOnly used as a way to find valid scripts.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-vram",
|
|
|
|
"-v",
|
|
|
|
"--v",
|
|
|
|
type=lambda x: int(x, 16),
|
|
|
|
default=0,
|
|
|
|
dest="vram",
|
|
|
|
required=False,
|
|
|
|
help="VRAM start will be tracked and used for the script output name",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-si",
|
|
|
|
"--si",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
dest="si",
|
|
|
|
required=False,
|
|
|
|
help="Force si script output",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-blob",
|
|
|
|
"--b",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
dest="blob",
|
|
|
|
required=False,
|
|
|
|
help="If there is a blob of scripts.",
|
|
|
|
)
|
2021-03-29 02:05:56 +02:00
|
|
|
args = parser.parse_args()
|
2021-04-03 19:21:49 +02:00
|
|
|
vram_base = args.vram
|
2021-03-29 02:05:56 +02:00
|
|
|
get_constants()
|
2021-04-29 19:09:30 +02:00
|
|
|
INCLUDED = {}
|
|
|
|
INCLUDED["functions"] = set()
|
|
|
|
INCLUDED["includes"] = set()
|
|
|
|
INCLUDES_NEEDED = {}
|
|
|
|
INCLUDES_NEEDED["include"] = []
|
|
|
|
INCLUDES_NEEDED["forward"] = []
|
|
|
|
INCLUDES_NEEDED["npcs"] = {}
|
|
|
|
INCLUDES_NEEDED["sprites"] = set()
|
2021-12-16 01:27:26 +01:00
|
|
|
try:
|
|
|
|
offset = int(args.offset, 0)
|
|
|
|
except ValueError:
|
|
|
|
info = sym_info.search_symbol(args.offset)
|
|
|
|
if info is None:
|
|
|
|
print(f"{args.offset} is not a valid symbol name")
|
|
|
|
exit(1)
|
|
|
|
offset = info[0]
|
|
|
|
if args.end > offset:
|
2021-03-30 05:39:17 +02:00
|
|
|
# Search the given memory range and report scripts
|
2021-03-29 02:05:56 +02:00
|
|
|
with open(args.file, "rb") as f:
|
2021-04-03 19:21:49 +02:00
|
|
|
gap = False
|
|
|
|
first_print = False
|
2021-12-16 01:27:26 +01:00
|
|
|
while offset < args.end:
|
|
|
|
f.seek(offset)
|
2021-12-24 14:29:05 +01:00
|
|
|
script = ScriptDisassembler(f, args.offset, {}, 0x978DE0, INCLUDES_NEEDED, INCLUDED)
|
2021-03-29 02:05:56 +02:00
|
|
|
try:
|
|
|
|
script_text = script.disassemble()
|
2021-10-25 13:44:55 +02:00
|
|
|
if script.instructions > 1 and "_EVT_CMD" not in script_text:
|
2021-04-03 19:21:49 +02:00
|
|
|
if gap and first_print:
|
2023-07-29 19:03:17 +02:00
|
|
|
potential_struct_sizes = {
|
|
|
|
"NpcData": 0x1F0,
|
|
|
|
"MobileAISettings": 0x30,
|
|
|
|
"NpcSettings": 0x2C,
|
|
|
|
"NpcGroupList": 0xC,
|
|
|
|
}
|
2021-12-16 01:27:26 +01:00
|
|
|
gap_size = offset - gap_start
|
2021-04-03 19:21:49 +02:00
|
|
|
potential_struct = "Unknown data"
|
|
|
|
potential_count = 1
|
2023-07-29 19:03:17 +02:00
|
|
|
for k, v in potential_struct_sizes.items():
|
2021-04-03 19:21:49 +02:00
|
|
|
if gap_size % v == 0:
|
|
|
|
potential_struct = k
|
|
|
|
potential_count = gap_size // v
|
2023-07-29 19:03:17 +02:00
|
|
|
print(
|
|
|
|
f"========== 0x{gap_size:X} byte gap ({potential_count} {potential_struct}?) 0x{gap_start:X} - 0x{offset:X} =========="
|
|
|
|
)
|
2021-04-03 19:21:49 +02:00
|
|
|
print()
|
|
|
|
gap = False
|
2023-07-29 19:03:17 +02:00
|
|
|
# print(f"EvtScript read from 0x{script.start_pos:X} to 0x{script.end_pos:X} "
|
2021-04-03 19:21:49 +02:00
|
|
|
# f"(0x{script.end_pos - script.start_pos:X} bytes, {script.instructions} instructions)")
|
2023-07-29 19:03:17 +02:00
|
|
|
# print()
|
2021-04-03 19:21:49 +02:00
|
|
|
vram = f"{args.vram:X}_" if vram_base > 0 else f""
|
2023-07-29 19:03:17 +02:00
|
|
|
script_text = script_text.replace(
|
|
|
|
"EvtScript script = SCRIPT({",
|
|
|
|
f"EvtScript N(D_{vram}{offset:X}) = " + "SCRIPT({",
|
|
|
|
)
|
2021-03-30 05:39:17 +02:00
|
|
|
print(script_text, end="")
|
|
|
|
print()
|
2023-07-29 19:03:17 +02:00
|
|
|
# print(f"Valid script found at 0x{offset:X}")
|
2021-12-16 01:27:26 +01:00
|
|
|
args.vram += script.end_pos - offset
|
|
|
|
offset = script.end_pos
|
2021-04-03 19:21:49 +02:00
|
|
|
first_print = True
|
2021-03-29 02:05:56 +02:00
|
|
|
else:
|
2021-04-03 19:21:49 +02:00
|
|
|
if not gap:
|
2021-12-16 01:27:26 +01:00
|
|
|
gap_start = offset
|
2021-04-03 19:21:49 +02:00
|
|
|
gap = True
|
2021-12-16 01:27:26 +01:00
|
|
|
offset += 4
|
2021-04-03 19:21:49 +02:00
|
|
|
args.vram += 4
|
2021-03-29 02:05:56 +02:00
|
|
|
except Exception:
|
2021-04-03 19:21:49 +02:00
|
|
|
if not gap:
|
2021-12-16 01:27:26 +01:00
|
|
|
gap_start = offset
|
2021-04-03 19:21:49 +02:00
|
|
|
gap = True
|
2021-12-16 01:27:26 +01:00
|
|
|
offset += 4
|
2021-04-03 19:21:49 +02:00
|
|
|
args.vram += 4
|
2021-03-29 02:05:56 +02:00
|
|
|
else:
|
|
|
|
with open(args.file, "rb") as f:
|
2021-12-16 01:27:26 +01:00
|
|
|
f.seek(offset)
|
2022-01-08 06:47:52 +01:00
|
|
|
loffset = args.offset
|
|
|
|
looping = 1
|
|
|
|
while looping:
|
|
|
|
try:
|
|
|
|
script = ScriptDisassembler(f, loffset, {}, 0x978DE0, INCLUDES_NEEDED, INCLUDED)
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2022-01-08 06:47:52 +01:00
|
|
|
if args.si:
|
2023-07-29 19:03:17 +02:00
|
|
|
print(
|
|
|
|
ScriptDisassembler(f, loffset, {}, 0x978DE0, INCLUDES_NEEDED, INCLUDED).disassemble(),
|
|
|
|
end="",
|
|
|
|
)
|
2022-01-08 06:47:52 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
script_text = script.disassemble()
|
2020-10-19 23:58:57 +02:00
|
|
|
|
2022-01-16 14:28:09 +01:00
|
|
|
# print(f"EvtScript read from 0x{script.start_pos:X} to 0x{script.end_pos:X} "
|
2022-01-08 06:47:52 +01:00
|
|
|
# f"(0x{script.end_pos - script.start_pos:X} bytes, {script.instructions} instructions)")
|
|
|
|
print()
|
|
|
|
print(script_text, end="")
|
2020-10-31 03:28:18 +01:00
|
|
|
|
2022-01-08 06:47:52 +01:00
|
|
|
except UnsupportedScript:
|
|
|
|
f.seek(offset)
|
|
|
|
print(ScriptDisassembler(f).disassemble(), end="")
|
|
|
|
break
|
2022-09-28 22:52:12 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2021-04-29 19:09:30 +02:00
|
|
|
|
2022-01-08 06:47:52 +01:00
|
|
|
loffset = script.end_pos
|
2023-07-29 19:03:17 +02:00
|
|
|
LOCAL_WORDS = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
2022-01-08 06:47:52 +01:00
|
|
|
looping = args.blob
|
|
|
|
try:
|
|
|
|
loffset = _script_lib[loffset - info[0] + info[2]][0][1]
|
|
|
|
except:
|
|
|
|
pass
|