Skip to content

Commit

Permalink
New versatile write_or_delete_if_found function
Browse files Browse the repository at this point in the history
Rewrite
  • Loading branch information
Lilaa3 committed Sep 27, 2024
1 parent d2d16dd commit 82f7e22
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 169 deletions.
3 changes: 2 additions & 1 deletion fast64_internal/sm64/sm64_anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
headerFile.write("extern const struct Animation *const " + animsName + "[];\n")
headerFile.close()

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

# write to table.inc.c
tableFilePath = os.path.join(animDirPath, "table.inc.c")
Expand Down Expand Up @@ -277,6 +277,7 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
groupName,
Path(dirPath),
dirName,
levelName,
[Path("anims/data.inc.c"), Path("anims/table.inc.c")],
[Path("anim_header.h")],
)
Expand Down
23 changes: 15 additions & 8 deletions fast64_internal/sm64/sm64_collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
insertableBinaryTypes,
defaultExtendSegment4,
)
from .sm64_utility import export_rom_checks, update_actor_includes
from .sm64_utility import export_rom_checks, to_include_descriptor, update_actor_includes, write_or_delete_if_found
from .sm64_objects import SM64_Area, start_process_sm64_objects
from .sm64_level_parser import parseLevelAtPointer
from .sm64_rom_tweaks import ExtendBank0x04
Expand All @@ -24,7 +24,6 @@
get64bitAlignedAddr,
prop_split,
getExportDir,
deleteIfFound,
duplicateHierarchy,
cleanupDuplicatedObjects,
writeInsertableFile,
Expand Down Expand Up @@ -334,15 +333,23 @@ def exportCollisionC(
data_includes = [Path("collision.inc.c")]
if writeRoomsFile:
data_includes.append(Path("rooms.inc.c"))
update_actor_includes(headerType, groupName, Path(dirPath), name, data_includes, [Path("collision_header.h")])
update_actor_includes(
headerType, groupName, Path(dirPath), name, levelName, data_includes, [Path("collision_header.h")]
)
if not writeRoomsFile: # TODO: Could be done better
if headerType == "Actor":
groupPathC = os.path.join(dirPath, groupName + ".c")
deleteIfFound(groupPathC, '\n#include "' + name + '/rooms.inc.c"')
group_path_c = Path(dirPath) / f"{groupName}.c"
write_or_delete_if_found(group_path_c, to_remove=[to_include_descriptor(Path(name) / "rooms.inc.c")])
elif headerType == "Level":
groupPathC = os.path.join(dirPath, "leveldata.c")
deleteIfFound(groupPathC, '#include "levels/' + levelName + "/" + name + '/rooms.inc.c"')
deleteIfFound(groupPathC, '#include "rooms.inc.c"')
group_path_c = Path(dirPath) / "leveldata.c"
write_or_delete_if_found(
group_path_c,
to_remove=[
to_include_descriptor(
Path(name) / "rooms.inc.c", Path("levels") / levelName / name / "rooms.inc.c"
),
],
)

return cDefine

Expand Down
31 changes: 20 additions & 11 deletions fast64_internal/sm64/sm64_f3d_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
update_world_default_rendermode,
)
from .sm64_texscroll import modifyTexScrollFiles, modifyTexScrollHeadersGroup
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, writeMaterialHeaders
from .sm64_utility import (
END_IF_FOOTER,
ModifyFoundDescriptor,
export_rom_checks,
starSelectWarning,
update_actor_includes,
write_or_delete_if_found,
write_material_headers,
)
from .sm64_level_parser import parseLevelAtPointer
from .sm64_rom_tweaks import ExtendBank0x04
from typing import Tuple, Union, Iterable
Expand Down Expand Up @@ -62,7 +70,6 @@
applyRotation,
toAlnum,
checkIfPathExists,
writeIfNotFound,
overwriteData,
getExportDir,
writeMaterialFiles,
Expand Down Expand Up @@ -196,7 +203,9 @@ def exportTexRectToC(dirPath, texProp, texDir, savePNG, name, exportToProject, p
overwriteData("const\s*u8\s*", textures[0].name, data, seg2CPath, None, False)

# Append texture declaration to segment2.h
writeIfNotFound(seg2HPath, declaration, "#endif")
write_or_delete_if_found(
Path(seg2HPath), ModifyFoundDescriptor(declaration), path_must_exist=True, footer=END_IF_FOOTER
)

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

update_actor_includes(headerType, groupName, Path(dirPath), name, ["model.inc.c"], ["header.h"])
update_actor_includes(headerType, groupName, Path(dirPath), name, levelName, ["model.inc.c"], ["header.h"])
fileStatus = None
if not customExport:
if headerType == "Actor":
if DLFormat != DLFormat.Static: # Change this
writeMaterialHeaders(
basePath,
'"actors/' + toAlnum(name) + '/material.inc.c"',
'"actors/' + toAlnum(name) + '/material.inc.h"',
write_material_headers(
Path(basePath),
Path("actors") / toAlnum(name) / "material.inc.c",
Path("actors") / toAlnum(name) / "material.inc.h",
)

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

elif headerType == "Level":
if DLFormat != DLFormat.Static: # Change this
writeMaterialHeaders(
write_material_headers(
basePath,
'"levels/' + levelName + "/" + toAlnum(name) + '/material.inc.c"',
'"levels/' + levelName + "/" + toAlnum(name) + '/material.inc.h"',
Path("levels") / levelName / toAlnum(name) / "material.inc.c",
Path("levels") / levelName / toAlnum(name) / "material.inc.h",
)

texscrollIncludeC = '#include "levels/' + levelName + "/" + name + '/texscroll.inc.c"'
Expand Down
13 changes: 7 additions & 6 deletions fast64_internal/sm64/sm64_geolayout_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .sm64_texscroll import modifyTexScrollFiles, modifyTexScrollHeadersGroup
from .sm64_level_parser import parseLevelAtPointer
from .sm64_rom_tweaks import ExtendBank0x04
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, writeMaterialHeaders
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, write_material_headers

from ..utility import (
PluginError,
Expand Down Expand Up @@ -658,12 +658,12 @@ def saveGeolayoutC(
geoData = geolayoutGraph.to_c()

if headerType == "Actor":
matCInclude = '"actors/' + dirName + '/material.inc.c"'
matHInclude = '"actors/' + dirName + '/material.inc.h"'
matCInclude = Path("actors") / dirName / "material.inc.c"
matHInclude = Path("actors") / dirName / "material.inc.h"
headerInclude = '#include "actors/' + dirName + '/geo_header.h"'
else:
matCInclude = '"levels/' + levelName + "/" + dirName + '/material.inc.c"'
matHInclude = '"levels/' + levelName + "/" + dirName + '/material.inc.h"'
matCInclude = Path("levels") / levelName / dirName / "material.inc.c"
matHInclude = Path("levels") / levelName / dirName / "material.inc.h"
headerInclude = '#include "levels/' + levelName + "/" + dirName + '/geo_header.h"'

modifyTexScrollFiles(exportDir, geoDirPath, scrollData)
Expand Down Expand Up @@ -714,6 +714,7 @@ def saveGeolayoutC(
groupName,
Path(dirPath),
dirName,
levelName,
[Path("model.inc.c")],
[Path("geo_header.h")],
[Path("geo.inc.c")],
Expand Down Expand Up @@ -791,7 +792,7 @@ def saveGeolayoutC(
)

if DLFormat != DLFormat.Static: # Change this
writeMaterialHeaders(exportDir, matCInclude, matHInclude)
write_material_headers(Path(exportDir), matCInclude, matHInclude)

return staticData.header, fileStatus

Expand Down
35 changes: 24 additions & 11 deletions fast64_internal/sm64/sm64_level_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
from .sm64_f3d_writer import SM64Model, SM64GfxFormatter
from .sm64_geolayout_writer import setRooms, convertObjectToGeolayout
from .sm64_f3d_writer import modifyTexScrollFiles, modifyTexScrollHeadersGroup
from .sm64_utility import cameraWarning, starSelectWarning, write_includes, writeMaterialHeaders
from .sm64_utility import (
cameraWarning,
starSelectWarning,
to_include_descriptor,
write_includes,
write_or_delete_if_found,
write_material_headers,
)

from ..utility import (
PluginError,
Expand All @@ -22,7 +29,6 @@
restoreHiddenState,
overwriteData,
selectSingleObject,
deleteIfFound,
applyBasicTweaks,
applyRotation,
raisePluginError,
Expand Down Expand Up @@ -999,10 +1005,10 @@ def include_proto(file_name, new_line_first=False):
if not customExport:
if DLFormat != DLFormat.Static:
# Write material headers
writeMaterialHeaders(
exportDir,
f'"levels/{level_name}/material.inc.c"',
f'"levels/{level_name}/material.inc.h"',
write_material_headers(
Path(exportDir),
Path("levels") / level_name / "material.inc.c",
Path("levels") / level_name / "material.inc.c",
)

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

# Write level data
write_includes(Path(geoPath), ['"geo.inc.c"'])
write_includes(Path(levelDataPath), ['"leveldata.inc.c"'])
write_includes(Path(headerPath), ['"header.inc.h"'], before_endif=True)
write_includes(Path(geoPath), [Path("geo.inc.c")])
write_includes(Path(levelDataPath), [Path("leveldata.inc.c")])
write_includes(Path(headerPath), [Path("header.inc.h")], before_endif=True)

old_include = to_include_descriptor(Path("levels") / level_name / "texture_include.inc.c")
if fModel.texturesSavedLastExport == 0:
textureIncludePath = os.path.join(level_dir, "texture_include.inc.c")
if os.path.exists(textureIncludePath):
os.remove(textureIncludePath)
# This one is for backwards compatibility purposes
deleteIfFound(os.path.join(level_dir, "texture.inc.c"), include_proto("texture_include.inc.c"))
write_or_delete_if_found(
Path(level_dir) / "texture.inc.c",
to_remove=[old_include],
)

# This one is for backwards compatibility purposes
deleteIfFound(levelDataPath, include_proto("texture_include.inc.c"))
write_or_delete_if_found(
Path(levelDataPath),
to_remove=[old_include],
)

texscrollIncludeC = include_proto("texscroll.inc.c")
texscrollIncludeH = include_proto("texscroll.inc.h")
Expand Down
9 changes: 7 additions & 2 deletions fast64_internal/sm64/sm64_texscroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os, re, bpy
from ..utility import PluginError, getDataFromFile, saveDataToFile, CScrollData, CData
from .c_templates.tile_scroll import tile_scroll_c, tile_scroll_h
from .sm64_utility import getMemoryCFilePath, write_includes
from .sm64_utility import END_IF_FOOTER, ModifyFoundDescriptor, getMemoryCFilePath, write_or_delete_if_found

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

# Add extern definition of segment table
write_includes(Path(baseDir) / "src/game/memory.h", externs=["uintptr_t sSegmentROMTable[32]"], before_endif=True)
write_or_delete_if_found(
Path(baseDir) / "src/game/memory.h",
[ModifyFoundDescriptor("uintptr_t sSegmentROMTable[32];", r"uintptr_t\h*sSegmentROMTable\[.*?\]\h?;")],
path_must_exist=True,
footer=END_IF_FOOTER,
)


def writeScrollTextureCall(path, include, callString):
Expand Down
Loading

0 comments on commit 82f7e22

Please sign in to comment.