Skip to content

Instantly share code, notes, and snippets.

@Dragorn421
Created January 18, 2022 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dragorn421/df88e4b7e25dae4fe2c195d0d635cc35 to your computer and use it in GitHub Desktop.
Save Dragorn421/df88e4b7e25dae4fe2c195d0d635cc35 to your computer and use it in GitHub Desktop.
import os
import os.path
import re
replace_ignored = []
flags = dict()
def get_flag(index, mask):
assert mask > 0
shift = 0
v = mask
while v & 1 == 0:
shift += 1
v >>= 1
assert v == 1
assert shift < 16
flag_value = (index << 4) | shift
flag = f"ITEMGETINF_{flag_value:02X}"
flags[flag_value] = flag
return flag
pat_get = re.compile(r"gSaveContext\.itemGetInf\[([^\]]+)\]\s*&\s*([^\s\)]+)")
def repl_get(match_object):
index_str = match_object.group(1)
mask_str = match_object.group(2)
index = int(index_str, 0)
mask = int(mask_str, 0)
flag = get_flag(index, mask)
return f"GET_ITEMGETINF({flag})"
pat_set = re.compile(r"gSaveContext\.itemGetInf\[([^\]]+)\]\s*\|=\s*([^\s\);]+)")
def repl_set(match_object):
index_str = match_object.group(1)
mask_str = match_object.group(2)
try:
index = int(index_str, 0)
mask = int(mask_str, 0)
except ValueError:
g = match_object.group(0)
replace_ignored.append(g)
return g
flag = get_flag(index, mask)
return f"SET_ITEMGETINF({flag})"
for dirpath, dirnames, filenames in os.walk("src"):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
with open(filepath) as f:
contents = f.read()
new_contents = contents
replace_ignored = []
new_contents = pat_get.sub(repl_get, new_contents)
new_contents = pat_set.sub(repl_set, new_contents)
if replace_ignored:
print(filepath, replace_ignored)
if new_contents != contents:
print(filepath)
with open(filepath, "w") as f:
f.write(new_contents)
print(flags)
print("".join(f"#define {flag} 0x{flag_value:02X}\n" for flag_value, flag in sorted(flags.items(), key=lambda item: item[0])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment