Skip to content

Commit 82f7e22

Browse files
committed
New versatile write_or_delete_if_found function
Rewrite
1 parent d2d16dd commit 82f7e22

File tree

8 files changed

+248
-169
lines changed

8 files changed

+248
-169
lines changed

fast64_internal/sm64/sm64_anim.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
234234
headerFile.write("extern const struct Animation *const " + animsName + "[];\n")
235235
headerFile.close()
236236

237-
write_includes(Path(animDirPath) / "data.inc.c", [f'"{animFileName}"'])
237+
write_includes(Path(animDirPath) / "data.inc.c", [Path(animFileName)])
238238

239239
# write to table.inc.c
240240
tableFilePath = os.path.join(animDirPath, "table.inc.c")
@@ -277,6 +277,7 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
277277
groupName,
278278
Path(dirPath),
279279
dirName,
280+
levelName,
280281
[Path("anims/data.inc.c"), Path("anims/table.inc.c")],
281282
[Path("anim_header.h")],
282283
)

fast64_internal/sm64/sm64_collision.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
insertableBinaryTypes,
1010
defaultExtendSegment4,
1111
)
12-
from .sm64_utility import export_rom_checks, update_actor_includes
12+
from .sm64_utility import export_rom_checks, to_include_descriptor, update_actor_includes, write_or_delete_if_found
1313
from .sm64_objects import SM64_Area, start_process_sm64_objects
1414
from .sm64_level_parser import parseLevelAtPointer
1515
from .sm64_rom_tweaks import ExtendBank0x04
@@ -24,7 +24,6 @@
2424
get64bitAlignedAddr,
2525
prop_split,
2626
getExportDir,
27-
deleteIfFound,
2827
duplicateHierarchy,
2928
cleanupDuplicatedObjects,
3029
writeInsertableFile,
@@ -334,15 +333,23 @@ def exportCollisionC(
334333
data_includes = [Path("collision.inc.c")]
335334
if writeRoomsFile:
336335
data_includes.append(Path("rooms.inc.c"))
337-
update_actor_includes(headerType, groupName, Path(dirPath), name, data_includes, [Path("collision_header.h")])
336+
update_actor_includes(
337+
headerType, groupName, Path(dirPath), name, levelName, data_includes, [Path("collision_header.h")]
338+
)
338339
if not writeRoomsFile: # TODO: Could be done better
339340
if headerType == "Actor":
340-
groupPathC = os.path.join(dirPath, groupName + ".c")
341-
deleteIfFound(groupPathC, '\n#include "' + name + '/rooms.inc.c"')
341+
group_path_c = Path(dirPath) / f"{groupName}.c"
342+
write_or_delete_if_found(group_path_c, to_remove=[to_include_descriptor(Path(name) / "rooms.inc.c")])
342343
elif headerType == "Level":
343-
groupPathC = os.path.join(dirPath, "leveldata.c")
344-
deleteIfFound(groupPathC, '#include "levels/' + levelName + "/" + name + '/rooms.inc.c"')
345-
deleteIfFound(groupPathC, '#include "rooms.inc.c"')
344+
group_path_c = Path(dirPath) / "leveldata.c"
345+
write_or_delete_if_found(
346+
group_path_c,
347+
to_remove=[
348+
to_include_descriptor(
349+
Path(name) / "rooms.inc.c", Path("levels") / levelName / name / "rooms.inc.c"
350+
),
351+
],
352+
)
346353

347354
return cDefine
348355

fast64_internal/sm64/sm64_f3d_writer.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
update_world_default_rendermode,
1616
)
1717
from .sm64_texscroll import modifyTexScrollFiles, modifyTexScrollHeadersGroup
18-
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, writeMaterialHeaders
18+
from .sm64_utility import (
19+
END_IF_FOOTER,
20+
ModifyFoundDescriptor,
21+
export_rom_checks,
22+
starSelectWarning,
23+
update_actor_includes,
24+
write_or_delete_if_found,
25+
write_material_headers,
26+
)
1927
from .sm64_level_parser import parseLevelAtPointer
2028
from .sm64_rom_tweaks import ExtendBank0x04
2129
from typing import Tuple, Union, Iterable
@@ -62,7 +70,6 @@
6270
applyRotation,
6371
toAlnum,
6472
checkIfPathExists,
65-
writeIfNotFound,
6673
overwriteData,
6774
getExportDir,
6875
writeMaterialFiles,
@@ -196,7 +203,9 @@ def exportTexRectToC(dirPath, texProp, texDir, savePNG, name, exportToProject, p
196203
overwriteData("const\s*u8\s*", textures[0].name, data, seg2CPath, None, False)
197204

198205
# Append texture declaration to segment2.h
199-
writeIfNotFound(seg2HPath, declaration, "#endif")
206+
write_or_delete_if_found(
207+
Path(seg2HPath), ModifyFoundDescriptor(declaration), path_must_exist=True, footer=END_IF_FOOTER
208+
)
200209

201210
# Write/Overwrite function to hud.c
202211
overwriteData("void\s*", fTexRect.name, code, hudPath, projectExportData[1], True)
@@ -425,15 +434,15 @@ def sm64ExportF3DtoC(
425434
cDefFile.write(staticData.header)
426435
cDefFile.close()
427436

428-
update_actor_includes(headerType, groupName, Path(dirPath), name, ["model.inc.c"], ["header.h"])
437+
update_actor_includes(headerType, groupName, Path(dirPath), name, levelName, ["model.inc.c"], ["header.h"])
429438
fileStatus = None
430439
if not customExport:
431440
if headerType == "Actor":
432441
if DLFormat != DLFormat.Static: # Change this
433-
writeMaterialHeaders(
434-
basePath,
435-
'"actors/' + toAlnum(name) + '/material.inc.c"',
436-
'"actors/' + toAlnum(name) + '/material.inc.h"',
442+
write_material_headers(
443+
Path(basePath),
444+
Path("actors") / toAlnum(name) / "material.inc.c",
445+
Path("actors") / toAlnum(name) / "material.inc.h",
437446
)
438447

439448
texscrollIncludeC = '#include "actors/' + name + '/texscroll.inc.c"'
@@ -443,10 +452,10 @@ def sm64ExportF3DtoC(
443452

444453
elif headerType == "Level":
445454
if DLFormat != DLFormat.Static: # Change this
446-
writeMaterialHeaders(
455+
write_material_headers(
447456
basePath,
448-
'"levels/' + levelName + "/" + toAlnum(name) + '/material.inc.c"',
449-
'"levels/' + levelName + "/" + toAlnum(name) + '/material.inc.h"',
457+
Path("levels") / levelName / toAlnum(name) / "material.inc.c",
458+
Path("levels") / levelName / toAlnum(name) / "material.inc.h",
450459
)
451460

452461
texscrollIncludeC = '#include "levels/' + levelName + "/" + name + '/texscroll.inc.c"'

fast64_internal/sm64/sm64_geolayout_writer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .sm64_texscroll import modifyTexScrollFiles, modifyTexScrollHeadersGroup
1515
from .sm64_level_parser import parseLevelAtPointer
1616
from .sm64_rom_tweaks import ExtendBank0x04
17-
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, writeMaterialHeaders
17+
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, write_material_headers
1818

1919
from ..utility import (
2020
PluginError,
@@ -658,12 +658,12 @@ def saveGeolayoutC(
658658
geoData = geolayoutGraph.to_c()
659659

660660
if headerType == "Actor":
661-
matCInclude = '"actors/' + dirName + '/material.inc.c"'
662-
matHInclude = '"actors/' + dirName + '/material.inc.h"'
661+
matCInclude = Path("actors") / dirName / "material.inc.c"
662+
matHInclude = Path("actors") / dirName / "material.inc.h"
663663
headerInclude = '#include "actors/' + dirName + '/geo_header.h"'
664664
else:
665-
matCInclude = '"levels/' + levelName + "/" + dirName + '/material.inc.c"'
666-
matHInclude = '"levels/' + levelName + "/" + dirName + '/material.inc.h"'
665+
matCInclude = Path("levels") / levelName / dirName / "material.inc.c"
666+
matHInclude = Path("levels") / levelName / dirName / "material.inc.h"
667667
headerInclude = '#include "levels/' + levelName + "/" + dirName + '/geo_header.h"'
668668

669669
modifyTexScrollFiles(exportDir, geoDirPath, scrollData)
@@ -714,6 +714,7 @@ def saveGeolayoutC(
714714
groupName,
715715
Path(dirPath),
716716
dirName,
717+
levelName,
717718
[Path("model.inc.c")],
718719
[Path("geo_header.h")],
719720
[Path("geo.inc.c")],
@@ -791,7 +792,7 @@ def saveGeolayoutC(
791792
)
792793

793794
if DLFormat != DLFormat.Static: # Change this
794-
writeMaterialHeaders(exportDir, matCInclude, matHInclude)
795+
write_material_headers(Path(exportDir), matCInclude, matHInclude)
795796

796797
return staticData.header, fileStatus
797798

fast64_internal/sm64/sm64_level_writer.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
from .sm64_f3d_writer import SM64Model, SM64GfxFormatter
1313
from .sm64_geolayout_writer import setRooms, convertObjectToGeolayout
1414
from .sm64_f3d_writer import modifyTexScrollFiles, modifyTexScrollHeadersGroup
15-
from .sm64_utility import cameraWarning, starSelectWarning, write_includes, writeMaterialHeaders
15+
from .sm64_utility import (
16+
cameraWarning,
17+
starSelectWarning,
18+
to_include_descriptor,
19+
write_includes,
20+
write_or_delete_if_found,
21+
write_material_headers,
22+
)
1623

1724
from ..utility import (
1825
PluginError,
@@ -22,7 +29,6 @@
2229
restoreHiddenState,
2330
overwriteData,
2431
selectSingleObject,
25-
deleteIfFound,
2632
applyBasicTweaks,
2733
applyRotation,
2834
raisePluginError,
@@ -999,10 +1005,10 @@ def include_proto(file_name, new_line_first=False):
9991005
if not customExport:
10001006
if DLFormat != DLFormat.Static:
10011007
# Write material headers
1002-
writeMaterialHeaders(
1003-
exportDir,
1004-
f'"levels/{level_name}/material.inc.c"',
1005-
f'"levels/{level_name}/material.inc.h"',
1008+
write_material_headers(
1009+
Path(exportDir),
1010+
Path("levels") / level_name / "material.inc.c",
1011+
Path("levels") / level_name / "material.inc.c",
10061012
)
10071013

10081014
# Export camera triggers
@@ -1073,19 +1079,26 @@ def include_proto(file_name, new_line_first=False):
10731079
createHeaderFile(level_name, headerPath)
10741080

10751081
# Write level data
1076-
write_includes(Path(geoPath), ['"geo.inc.c"'])
1077-
write_includes(Path(levelDataPath), ['"leveldata.inc.c"'])
1078-
write_includes(Path(headerPath), ['"header.inc.h"'], before_endif=True)
1082+
write_includes(Path(geoPath), [Path("geo.inc.c")])
1083+
write_includes(Path(levelDataPath), [Path("leveldata.inc.c")])
1084+
write_includes(Path(headerPath), [Path("header.inc.h")], before_endif=True)
10791085

1086+
old_include = to_include_descriptor(Path("levels") / level_name / "texture_include.inc.c")
10801087
if fModel.texturesSavedLastExport == 0:
10811088
textureIncludePath = os.path.join(level_dir, "texture_include.inc.c")
10821089
if os.path.exists(textureIncludePath):
10831090
os.remove(textureIncludePath)
10841091
# This one is for backwards compatibility purposes
1085-
deleteIfFound(os.path.join(level_dir, "texture.inc.c"), include_proto("texture_include.inc.c"))
1092+
write_or_delete_if_found(
1093+
Path(level_dir) / "texture.inc.c",
1094+
to_remove=[old_include],
1095+
)
10861096

10871097
# This one is for backwards compatibility purposes
1088-
deleteIfFound(levelDataPath, include_proto("texture_include.inc.c"))
1098+
write_or_delete_if_found(
1099+
Path(levelDataPath),
1100+
to_remove=[old_include],
1101+
)
10891102

10901103
texscrollIncludeC = include_proto("texscroll.inc.c")
10911104
texscrollIncludeH = include_proto("texscroll.inc.h")

fast64_internal/sm64/sm64_texscroll.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os, re, bpy
33
from ..utility import PluginError, getDataFromFile, saveDataToFile, CScrollData, CData
44
from .c_templates.tile_scroll import tile_scroll_c, tile_scroll_h
5-
from .sm64_utility import getMemoryCFilePath, write_includes
5+
from .sm64_utility import END_IF_FOOTER, ModifyFoundDescriptor, getMemoryCFilePath, write_or_delete_if_found
66

77
# This is for writing framework for scroll code.
88
# Actual scroll code found in f3d_gbi.py (FVertexScrollData)
@@ -79,7 +79,12 @@ def writeSegmentROMTable(baseDir):
7979
memFile.close()
8080

8181
# Add extern definition of segment table
82-
write_includes(Path(baseDir) / "src/game/memory.h", externs=["uintptr_t sSegmentROMTable[32]"], before_endif=True)
82+
write_or_delete_if_found(
83+
Path(baseDir) / "src/game/memory.h",
84+
[ModifyFoundDescriptor("uintptr_t sSegmentROMTable[32];", r"uintptr_t\h*sSegmentROMTable\[.*?\]\h?;")],
85+
path_must_exist=True,
86+
footer=END_IF_FOOTER,
87+
)
8388

8489

8590
def writeScrollTextureCall(path, include, callString):

0 commit comments

Comments
 (0)