mirror of
https://github.com/zeldaret/oot.git
synced 2024-11-11 03:39:59 +00:00
add "word replace" behavior, cleanup (#892)
This commit is contained in:
parent
6ce382bc8d
commit
e34cb58640
1 changed files with 66 additions and 31 deletions
|
@ -3,7 +3,18 @@
|
||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
animdict ={
|
# all occurrences of keys will be replaced by associated value
|
||||||
|
simpleReplace = {
|
||||||
|
"ACTORTYPE":"ACTORCAT",
|
||||||
|
"DistToLink":"DistToPlayer",
|
||||||
|
"HitItem":"HitInfo",
|
||||||
|
}
|
||||||
|
|
||||||
|
# all occurrences of keys will be replaced by associated value,
|
||||||
|
# if the occurence is the whole word
|
||||||
|
# for example, if there is a space before and an open parenthesis after,
|
||||||
|
# like for a function call: ` func_8002E4B4(`
|
||||||
|
wordReplace = {
|
||||||
"Actor_SetHeight":"Actor_SetFocus",
|
"Actor_SetHeight":"Actor_SetFocus",
|
||||||
"func_8002E4B4":"Actor_UpdateBgCheckInfo",
|
"func_8002E4B4":"Actor_UpdateBgCheckInfo",
|
||||||
"func_8002BDB0":"Actor_SetFeetPos",
|
"func_8002BDB0":"Actor_SetFeetPos",
|
||||||
|
@ -29,7 +40,6 @@ animdict ={
|
||||||
"actor.wallPolySource":"actor.wallBgId",
|
"actor.wallPolySource":"actor.wallBgId",
|
||||||
"actor.floorPolySource":"actor.floorBgId",
|
"actor.floorPolySource":"actor.floorBgId",
|
||||||
"actor.wallPolyRot":"actor.wallYaw",
|
"actor.wallPolyRot":"actor.wallYaw",
|
||||||
"DistToLink":"DistToPlayer",
|
|
||||||
"yawTowardsLink":"yawTowardsPlayer",
|
"yawTowardsLink":"yawTowardsPlayer",
|
||||||
"colChkInfo.unk_10":"colChkInfo.cylRadius",
|
"colChkInfo.unk_10":"colChkInfo.cylRadius",
|
||||||
"colChkInfo.unk_12":"colChkInfo.cylHeight",
|
"colChkInfo.unk_12":"colChkInfo.cylHeight",
|
||||||
|
@ -49,17 +59,15 @@ animdict ={
|
||||||
"Actor_ChangeType":"Actor_ChangeCategory",
|
"Actor_ChangeType":"Actor_ChangeCategory",
|
||||||
"ActorShadow_DrawFunc_Squiggly":"ActorShadow_DrawHorse",
|
"ActorShadow_DrawFunc_Squiggly":"ActorShadow_DrawHorse",
|
||||||
"ActorShadow_DrawFunc_":"ActorShadow_Draw",
|
"ActorShadow_DrawFunc_":"ActorShadow_Draw",
|
||||||
"ACTORTYPE":"ACTORCAT",
|
|
||||||
"actor.type":"actor.category",
|
"actor.type":"actor.category",
|
||||||
".body.":".info.",
|
".body.":".info.",
|
||||||
"HitItem":"HitInfo",
|
|
||||||
"bumper.unk_06":"bumper.hitPos",
|
"bumper.unk_06":"bumper.hitPos",
|
||||||
"base.list":"base.elements",
|
"base.list":"base.elements",
|
||||||
"toucher.flags":"toucher.dmgFlags",
|
"toucher.flags":"toucher.dmgFlags",
|
||||||
"bumper.flags":"bumper.dmgFlags",
|
"bumper.flags":"bumper.dmgFlags",
|
||||||
"maskA ":"ocFlags1 ",
|
"maskA ":"ocFlags1 ",
|
||||||
"maskB ":"ocFlags2 ",
|
"maskB ":"ocFlags2 ",
|
||||||
"base.type":"base.colType",
|
".base.type":".base.colType",
|
||||||
"COLTYPE_UNK11":"COLTYPE_HARD",
|
"COLTYPE_UNK11":"COLTYPE_HARD",
|
||||||
"COLTYPE_UNK12":"COLTYPE_WOOD",
|
"COLTYPE_UNK12":"COLTYPE_WOOD",
|
||||||
"COLTYPE_UNK13":"COLTYPE_TREE",
|
"COLTYPE_UNK13":"COLTYPE_TREE",
|
||||||
|
@ -93,47 +101,74 @@ animdict ={
|
||||||
"func_800339B8": "Actor_TestFloorInDirection",
|
"func_800339B8": "Actor_TestFloorInDirection",
|
||||||
}
|
}
|
||||||
|
|
||||||
def replace_anim(file):
|
# [a-zA-Z0-9_]
|
||||||
with open(file,'r',encoding = 'utf-8') as infile:
|
def is_word_char(c):
|
||||||
|
return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or (c >= '0' and c <= '9') or c == '_'
|
||||||
|
|
||||||
|
def replace_single(file):
|
||||||
|
with open(file, 'r', encoding = 'utf-8') as infile:
|
||||||
srcdata = infile.read()
|
srcdata = infile.read()
|
||||||
|
|
||||||
funcs = list(animdict.keys())
|
changesCount = 0
|
||||||
fixes = 0
|
|
||||||
for func in funcs:
|
|
||||||
newfunc = animdict.get(func)
|
|
||||||
if(newfunc is None):
|
|
||||||
print("How did this happen?")
|
|
||||||
return -1
|
|
||||||
if(func in srcdata):
|
|
||||||
fixes += 1
|
|
||||||
print(func)
|
|
||||||
srcdata = srcdata.replace(func, newfunc)
|
|
||||||
|
|
||||||
if(fixes > 0):
|
for old, new in simpleReplace.items():
|
||||||
print('Changed', fixes,'entr' + ('y' if fixes == 1 else 'ies') + ' in',file)
|
# replace `old` with `new`
|
||||||
|
if old in srcdata:
|
||||||
|
changesCount += 1
|
||||||
|
print(old, "->", new)
|
||||||
|
srcdata = srcdata.replace(old, new)
|
||||||
|
|
||||||
|
for old, new in wordReplace.items():
|
||||||
|
# replace `old` with `new` if the occurence of `old` is the whole word
|
||||||
|
oldStartIdx = srcdata.find(old)
|
||||||
|
if oldStartIdx >= 0:
|
||||||
|
old_start_as_word = is_word_char(old[0])
|
||||||
|
old_end_as_word = is_word_char(old[-1])
|
||||||
|
replaceCount = 0
|
||||||
|
while oldStartIdx >= 0:
|
||||||
|
replace = True
|
||||||
|
if old_start_as_word:
|
||||||
|
if oldStartIdx == 0:
|
||||||
|
pass
|
||||||
|
elif is_word_char(srcdata[oldStartIdx-1]):
|
||||||
|
replace = False
|
||||||
|
if old_end_as_word:
|
||||||
|
oldEndIdx = oldStartIdx + len(old)
|
||||||
|
if oldEndIdx >= len(srcdata):
|
||||||
|
pass
|
||||||
|
elif is_word_char(srcdata[oldEndIdx]):
|
||||||
|
replace = False
|
||||||
|
if replace:
|
||||||
|
srcdata = srcdata[:oldStartIdx] + new + srcdata[oldEndIdx:]
|
||||||
|
replaceCount += 1
|
||||||
|
oldStartIdx = srcdata.find(old, oldStartIdx + len(new))
|
||||||
|
if replaceCount > 0:
|
||||||
|
changesCount += 1
|
||||||
|
print(old, "->", new)
|
||||||
|
|
||||||
|
if changesCount > 0:
|
||||||
|
print('Changed', changesCount, 'entry' if changesCount == 1 else 'entries', 'in', file)
|
||||||
with open(file, 'w', encoding = 'utf-8', newline = '\n') as outfile:
|
with open(file, 'w', encoding = 'utf-8', newline = '\n') as outfile:
|
||||||
outfile.write(srcdata)
|
outfile.write(srcdata)
|
||||||
return 1
|
|
||||||
|
|
||||||
def replace_anim_all(repo):
|
def replace_all(repo):
|
||||||
for subdir, dirs, files in os.walk(repo + os.sep + 'src'):
|
for subdir, dirs, files in os.walk(repo + os.sep + 'src'):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
if(filename.endswith('.c')):
|
if filename.endswith('.c'):
|
||||||
file = subdir + os.sep + filename
|
file = subdir + os.sep + filename
|
||||||
replace_anim(file)
|
replace_single(file)
|
||||||
for subdir, dirs, files in os.walk(repo + os.sep + 'asm' + os.sep + 'non_matchings'):
|
for subdir, dirs, files in os.walk(repo + os.sep + 'asm' + os.sep + 'non_matchings'):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
if(filename.endswith('.s')):
|
if filename.endswith('.s'):
|
||||||
file = subdir + os.sep + filename
|
file = subdir + os.sep + filename
|
||||||
replace_anim(file)
|
replace_single(file)
|
||||||
return 1
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Apply function renames to a file')
|
parser = argparse.ArgumentParser(description='Apply function renames to a file')
|
||||||
parser.add_argument('file', help="source file to be processed. use . to process the whole repo", default = None)
|
parser.add_argument('file', help="source file to be processed. use . to process the whole repo")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if(args.file == '.'):
|
if args.file == '.':
|
||||||
replace_anim_all(os.curdir)
|
replace_all(os.curdir)
|
||||||
else:
|
else:
|
||||||
replace_anim(args.file)
|
replace_single(args.file)
|
||||||
|
|
Loading…
Reference in a new issue