mm/tools/eventinfconvert.py
Anghelo Carvajal ba4368d0e8
Macros for weekEventReg and eventInf (#984)
* make macros

* script

* Use the script on the whole codebase

* fix script

* Rename GET to CHECk

* More replacements

* Update weekeventflags on schedule scripts

* eventinf defines

* eventinfconvert

* run the scripts on the codebase

* format

* RACE_FLAGS

* format

* remove redundant parenthesis

* format

* add macros to permuter_settings

* comments on WEEKEVENTREG_

* format

* CHECK_WEEKEVENTREG_ALT

* don't automatically generate the macros on the script to allow naming them

* format D_801C66D0 a bit

* add TODOs

* Update src/overlays/actors/ovl_Boss_03/z_boss_03.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* Update src/overlays/actors/ovl_En_Baisen/z_en_baisen.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* Update src/overlays/actors/ovl_Boss_06/z_boss_06.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* Update src/overlays/actors/ovl_Elf_Msg6/z_elf_msg6.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* Update src/overlays/actors/ovl_En_Raf/z_en_raf.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* review

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* new files

* format

* weekeventreg

* review

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* review

Co-authored-by: EllipticEllipsis <elliptic.ellipsis@gmail.com>

* update

* format

* review

Co-authored-by: EllipticEllipsis <elliptic.ellipsis@gmail.com>

* flags

* a

* eventinf stuff

* format

* EnGakufu

* flags

* run the script

* format

* whoops

* cleanup

* fix

* update weekeventregs

* format

* format

* review

* format

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>
Co-authored-by: EllipticEllipsis <elliptic.ellipsis@gmail.com>
2022-11-05 15:48:36 -03:00

92 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import math
import re
def isValid(index: int, mask: int) -> bool:
if index < 0 or index > 7:
return False
if mask != (1 << int(math.log2(mask))):
return False
return True
def getFlagMacro(index: int, mask: int) -> str:
return f"EVENTINF_{index}{int(math.log2(mask))}"
def getCheckMacro(index: int, mask: int) -> str:
return f"CHECK_EVENTINF({getFlagMacro(index, mask)})"
def getSetMacro(index: int, mask: int) -> str:
return f"SET_EVENTINF({getFlagMacro(index, mask)})"
def getClearMacro(index: int, mask: int) -> str:
return f"CLEAR_EVENTINF({getFlagMacro(index, mask)})"
NUMBER_PATTERN = r"(0[xX])?[0-9a-fA-F]+"
def applyChange(fileContents: str, compiledRegex: re.Pattern, callback) -> str:
parsedContents = ""
match = compiledRegex.search(fileContents)
while match:
index = int(match.group("index"), 0)
mask = int(match.group("mask"), 0)
start, end = match.span()
parsedContents += fileContents[:start]
if isValid(index, mask):
parsedContents += callback(index, mask)
else:
parsedContents += fileContents[start:end]
fileContents = fileContents[end:]
match = compiledRegex.search(fileContents)
parsedContents += fileContents
return parsedContents
def updateCheck(fileContents: str) -> str:
checkRegex = re.compile(rf"gSaveContext.eventInf\[(?P<index>{NUMBER_PATTERN})\]\s*\&\s*(?P<mask>{NUMBER_PATTERN})")
return applyChange(fileContents, checkRegex, getCheckMacro)
def updateSet(fileContents: str) -> str:
setRegex = re.compile(rf"gSaveContext.eventInf\[(?P<index>{NUMBER_PATTERN})\]\s*\|=\s*(?P<mask>{NUMBER_PATTERN})")
return applyChange(fileContents, setRegex, getSetMacro)
def updateClear(fileContents: str) -> str:
clearRegex = re.compile(rf"gSaveContext.eventInf\[(?P<index>{NUMBER_PATTERN})\]\s*\&=\s*(\(u8\))?~(?P<mask>{NUMBER_PATTERN})")
return applyChange(fileContents, clearRegex, getClearMacro)
def read_file(filename):
with open(filename) as src_file:
return src_file.read()
def write_file(filename, contents):
with open(filename, "w") as f:
f.write(contents)
def main():
parser = argparse.ArgumentParser(description="Converts a eventInf access to a macro")
parser.add_argument("filename", help="Replace every occurrence of numeric eventInf on this file to the corresponding macro")
args = parser.parse_args()
fileContents = read_file(args.filename)
parsedContents = updateCheck(fileContents)
parsedContents = updateSet(parsedContents)
parsedContents = updateClear(parsedContents)
if fileContents != parsedContents:
write_file(args.filename, parsedContents)
if __name__ == "__main__":
main()