Skip to content

Commit 51d7257

Browse files
committed
flags_fp_conv: only convert int->Q for C literals
should skip over ints that are part of a string (eg field name) `chara.field_48 = 4096` should give `chara.field_48 = Q12(1.0f)` could add something to round the result automatically, since we know the precision for each Q format, but need to check if that would work properly
1 parent ea2f3ae commit 51d7257

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

tools/flags_fp_conv.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/usr/bin/env python3
22

3-
# Converts `g_SavegamePtr->eventFlags_168[X] & Y` expressions to `Savegame_EventFlagGet(Z)`
4-
# Also converts Q12 integers (0x7CCC, -0x1444, 0xFFFE0DDD, 4096) into float equivalents (may need to be rounded afterward)
3+
# Converts `g_SavegamePtr->eventFlags_168[X]` expressions to `Savegame_EventFlagGet(Z)` / `Savegame_EventFlagSet` / `Savegame_EventFlagClear`
4+
# Also converts Q format integers (0x7CCC, -0x1444, 0xFFFE0DDD, 4096) into float equivalents
5+
# Q12 is used by default, but can be specified by including it in the line (`4096 Q8` or `Q8(0x1337)` to convert to Q8)
6+
# May need to round the result afterward
57
# (mostly GPT written)
68

79
import re
10+
import readline
811

912
# ---------- EventFlags converter ----------
1013
def _parse_mask(mask_str):
@@ -129,7 +132,8 @@ def process_fp_text(text):
129132
text = re.sub(r'\bQ\d+\b', '', text, count=1)
130133

131134
def convert_value(match):
132-
value_str = match.group(0)
135+
prefix = match.group(1) or "" # may be None if start-of-string
136+
value_str = match.group(2)
133137

134138
if value_str.startswith(("0x", "-0x", "+0x")):
135139
val = int(value_str, 16)
@@ -142,16 +146,17 @@ def convert_value(match):
142146
else:
143147
val = int(value_str, 10)
144148

145-
return f"Q{qval}({val / (1 << qval)}f)"
149+
return f"{prefix}Q{qval}({val / (1 << qval)}f)"
146150

147-
pattern = re.compile(r'[-+]?0x[0-9A-Fa-f]+|[-+]?\d+')
151+
# prefix can be: start-of-string, space, or a punctuation
152+
pattern = re.compile(r'(^|[\s,(=:{\[])([-+]?0x[0-9A-Fa-f]+|[-+]?\d+)')
148153
return pattern.sub(convert_value, text)
149154

150155
# ---------- Unified REPL ----------
151156
if __name__ == "__main__":
152-
print("Enter expressions like '(g_SavegamePtr->eventFlags_168[2] & 8)' or '(g_SavegamePtr->eventFlags_168[2] & (1 << 3))'.")
157+
print("Enter expressions like 'g_SavegamePtr->eventFlags_168[2] & 8' or 'g_SavegamePtr->eventFlags_168[2] |= (1 << 3))'.")
153158
print("Or enter a line containing decimal/hexadecimal Q12 numbers (0x7CCC, -0x1444, 4096, 0xFFFE0DDD) to convert to float")
154-
print("(change to other Q** formats by including Q format in the text, `chara.field_48 = Q8(-0x1999)'")
159+
print("(change to other Q** formats by including Q format in the text, 'chara.field_48 = Q8(-0x1999)'")
155160
print()
156161
print("Type 'exit' to quit.")
157162
while True:

0 commit comments

Comments
 (0)