Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SM64] Smarter and standardized includes #449

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
39 changes: 14 additions & 25 deletions fast64_internal/sm64/sm64_anim.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import bpy, os, copy, shutil, mathutils, math
from bpy.utils import register_class, unregister_class
from ..panels import SM64_Panel
Expand All @@ -14,7 +15,6 @@
decodeSegmentedAddr,
getExportDir,
toAlnum,
writeIfNotFound,
get64bitAlignedAddr,
writeInsertableFile,
getFrameInterval,
Expand Down Expand Up @@ -46,7 +46,7 @@
marioAnimations,
)

from .sm64_utility import export_rom_checks, import_rom_checks
from .sm64_utility import export_rom_checks, import_rom_checks, update_actor_includes, write_includes

sm64_anim_types = {"ROTATE", "TRANSLATE"}

Expand Down Expand Up @@ -234,12 +234,7 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
headerFile.write("extern const struct Animation *const " + animsName + "[];\n")
headerFile.close()

# write to data.inc.c
dataFilePath = os.path.join(animDirPath, "data.inc.c")
if not os.path.exists(dataFilePath):
dataFile = open(dataFilePath, "w", newline="\n")
dataFile.close()
writeIfNotFound(dataFilePath, '#include "' + animFileName + '"\n', "")
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 @@ -275,23 +270,17 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
with open(tableFilePath, "w") as f:
f.write(stringData)

if not customExport:
if headerType == "Actor":
groupPathC = os.path.join(dirPath, groupName + ".c")
groupPathH = os.path.join(dirPath, groupName + ".h")

writeIfNotFound(groupPathC, '\n#include "' + dirName + '/anims/data.inc.c"', "")
writeIfNotFound(groupPathC, '\n#include "' + dirName + '/anims/table.inc.c"', "")
writeIfNotFound(groupPathH, '\n#include "' + dirName + '/anim_header.h"', "#endif")
elif headerType == "Level":
groupPathC = os.path.join(dirPath, "leveldata.c")
groupPathH = os.path.join(dirPath, "header.h")

writeIfNotFound(groupPathC, '\n#include "levels/' + levelName + "/" + dirName + '/anims/data.inc.c"', "")
writeIfNotFound(groupPathC, '\n#include "levels/' + levelName + "/" + dirName + '/anims/table.inc.c"', "")
writeIfNotFound(
groupPathH, '\n#include "levels/' + levelName + "/" + dirName + '/anim_header.h"', "\n#endif"
)
if customExport:
headerType = "Custom"
update_actor_includes(
headerType,
groupName,
Path(dirPath),
dirName,
levelName,
[Path("anims", "data.inc.c"), Path("anims", "table.inc.c")],
[Path("anim_header.h")],
)


def exportAnimationBinary(romfile, exportRange, armatureObj, DMAAddresses, segmentData, isDMA, loopAnim):
Expand Down
48 changes: 20 additions & 28 deletions fast64_internal/sm64/sm64_collision.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import bpy, shutil, os, math, mathutils
from bpy.utils import register_class, unregister_class
from io import BytesIO
Expand All @@ -8,7 +9,7 @@
insertableBinaryTypes,
defaultExtendSegment4,
)
from .sm64_utility import export_rom_checks
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 @@ -23,8 +24,6 @@
get64bitAlignedAddr,
prop_split,
getExportDir,
writeIfNotFound,
deleteIfFound,
duplicateHierarchy,
cleanupDuplicatedObjects,
writeInsertableFile,
Expand Down Expand Up @@ -331,31 +330,24 @@ def exportCollisionC(
cDefFile.write(cDefine)
cDefFile.close()

if headerType == "Actor":
# Write to group files
if groupName == "" or groupName is None:
raise PluginError("Actor header type chosen but group name not provided.")

groupPathC = os.path.join(dirPath, groupName + ".c")
groupPathH = os.path.join(dirPath, groupName + ".h")

writeIfNotFound(groupPathC, '\n#include "' + name + '/collision.inc.c"', "")
if writeRoomsFile:
writeIfNotFound(groupPathC, '\n#include "' + name + '/rooms.inc.c"', "")
else:
deleteIfFound(groupPathC, '\n#include "' + name + '/rooms.inc.c"')
writeIfNotFound(groupPathH, '\n#include "' + name + '/collision_header.h"', "\n#endif")

elif headerType == "Level":
groupPathC = os.path.join(dirPath, "leveldata.c")
groupPathH = os.path.join(dirPath, "header.h")

writeIfNotFound(groupPathC, '\n#include "levels/' + levelName + "/" + name + '/collision.inc.c"', "")
if writeRoomsFile:
writeIfNotFound(groupPathC, '\n#include "levels/' + levelName + "/" + name + '/rooms.inc.c"', "")
else:
deleteIfFound(groupPathC, '\n#include "levels/' + levelName + "/" + name + '/rooms.inc.c"')
writeIfNotFound(groupPathH, '\n#include "levels/' + levelName + "/" + name + '/collision_header.h"', "\n#endif")
data_includes = [Path("collision.inc.c")]
if writeRoomsFile:
data_includes.append(Path("rooms.inc.c"))
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":
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":
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
50 changes: 21 additions & 29 deletions fast64_internal/sm64/sm64_f3d_writer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import shutil, copy, bpy, re, os
from io import BytesIO
from math import ceil, log, radians
Expand All @@ -14,7 +15,15 @@
update_world_default_rendermode,
)
from .sm64_texscroll import modifyTexScrollFiles, modifyTexScrollHeadersGroup
from .sm64_utility import export_rom_checks, starSelectWarning
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 @@ -61,11 +70,9 @@
applyRotation,
toAlnum,
checkIfPathExists,
writeIfNotFound,
overwriteData,
getExportDir,
writeMaterialFiles,
writeMaterialHeaders,
get64bitAlignedAddr,
writeInsertableFile,
getPathAndLevel,
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,24 +434,15 @@ def sm64ExportF3DtoC(
cDefFile.write(staticData.header)
cDefFile.close()

update_actor_includes(headerType, groupName, Path(dirPath), name, levelName, ["model.inc.c"], ["header.h"])
fileStatus = None
if not customExport:
if headerType == "Actor":
# Write to group files
if groupName == "" or groupName is None:
raise PluginError("Actor header type chosen but group name not provided.")

groupPathC = os.path.join(dirPath, groupName + ".c")
groupPathH = os.path.join(dirPath, groupName + ".h")

writeIfNotFound(groupPathC, '\n#include "' + toAlnum(name) + '/model.inc.c"', "")
writeIfNotFound(groupPathH, '\n#include "' + toAlnum(name) + '/header.h"', "\n#endif")

if DLFormat != DLFormat.Static: # Change this
writeMaterialHeaders(
basePath,
'#include "actors/' + toAlnum(name) + '/material.inc.c"',
'#include "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 @@ -451,19 +451,11 @@ def sm64ExportF3DtoC(
texscrollGroupInclude = '#include "actors/' + groupName + '.h"'

elif headerType == "Level":
groupPathC = os.path.join(dirPath, "leveldata.c")
groupPathH = os.path.join(dirPath, "header.h")

writeIfNotFound(groupPathC, '\n#include "levels/' + levelName + "/" + toAlnum(name) + '/model.inc.c"', "")
writeIfNotFound(
groupPathH, '\n#include "levels/' + levelName + "/" + toAlnum(name) + '/header.h"', "\n#endif"
)

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

texscrollIncludeC = '#include "levels/' + levelName + "/" + name + '/texscroll.inc.c"'
Expand Down
70 changes: 17 additions & 53 deletions fast64_internal/sm64/sm64_geolayout_writer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from pathlib import Path

import bpy, mathutils, math, copy, os, shutil, re
from bpy.utils import register_class, unregister_class
Expand All @@ -13,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
from .sm64_utility import export_rom_checks, starSelectWarning, update_actor_includes, write_material_headers

from ..utility import (
PluginError,
Expand All @@ -26,10 +27,8 @@
getExportDir,
toAlnum,
writeMaterialFiles,
writeIfNotFound,
get64bitAlignedAddr,
encodeSegmentedAddr,
writeMaterialHeaders,
writeInsertableFile,
bytesToHex,
checkSM64EmptyUsesGeoLayout,
Expand Down Expand Up @@ -659,38 +658,12 @@ def saveGeolayoutC(
geoData = geolayoutGraph.to_c()

if headerType == "Actor":
matCInclude = '#include "actors/' + dirName + '/material.inc.c"'
matHInclude = '#include "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"'

if not customExport:
# Group name checking, before anything is exported to prevent invalid state on error.
if groupName == "" or groupName is None:
raise PluginError("Actor header type chosen but group name not provided.")

groupPathC = os.path.join(dirPath, groupName + ".c")
groupPathGeoC = os.path.join(dirPath, groupName + "_geo.c")
groupPathH = os.path.join(dirPath, groupName + ".h")

if not os.path.exists(groupPathC):
raise PluginError(
groupPathC + ' not found.\n Most likely issue is that "' + groupName + '" is an invalid group name.'
)
elif not os.path.exists(groupPathGeoC):
raise PluginError(
groupPathGeoC
+ ' not found.\n Most likely issue is that "'
+ groupName
+ '" is an invalid group name.'
)
elif not os.path.exists(groupPathH):
raise PluginError(
groupPathH + ' not found.\n Most likely issue is that "' + groupName + '" is an invalid group name.'
)

else:
matCInclude = '#include "levels/' + levelName + "/" + dirName + '/material.inc.c"'
matHInclude = '#include "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 @@ -736,6 +709,16 @@ def saveGeolayoutC(
cDefFile.close()

fileStatus = None
update_actor_includes(
headerType,
groupName,
Path(dirPath),
dirName,
levelName,
[Path("model.inc.c")],
[Path("geo_header.h")],
[Path("geo.inc.c")],
)
if not customExport:
if headerType == "Actor":
if dirName == "star" and bpy.context.scene.replaceStarRefs:
Expand Down Expand Up @@ -787,31 +770,12 @@ def saveGeolayoutC(
appendSecondaryGeolayout(geoDirPath, 'bully', 'bully_boss', 'GEO_SCALE(0x00, 0x2000), GEO_NODE_OPEN(),')
"""

# Write to group files
groupPathC = os.path.join(dirPath, groupName + ".c")
groupPathGeoC = os.path.join(dirPath, groupName + "_geo.c")
groupPathH = os.path.join(dirPath, groupName + ".h")

writeIfNotFound(groupPathC, '\n#include "' + dirName + '/model.inc.c"', "")
writeIfNotFound(groupPathGeoC, '\n#include "' + dirName + '/geo.inc.c"', "")
writeIfNotFound(groupPathH, '\n#include "' + dirName + '/geo_header.h"', "\n#endif")

texscrollIncludeC = '#include "actors/' + dirName + '/texscroll.inc.c"'
texscrollIncludeH = '#include "actors/' + dirName + '/texscroll.inc.h"'
texscrollGroup = groupName
texscrollGroupInclude = '#include "actors/' + groupName + '.h"'

elif headerType == "Level":
groupPathC = os.path.join(dirPath, "leveldata.c")
groupPathGeoC = os.path.join(dirPath, "geo.c")
groupPathH = os.path.join(dirPath, "header.h")

writeIfNotFound(groupPathC, '\n#include "levels/' + levelName + "/" + dirName + '/model.inc.c"', "")
writeIfNotFound(groupPathGeoC, '\n#include "levels/' + levelName + "/" + dirName + '/geo.inc.c"', "")
writeIfNotFound(
groupPathH, '\n#include "levels/' + levelName + "/" + dirName + '/geo_header.h"', "\n#endif"
)

texscrollIncludeC = '#include "levels/' + levelName + "/" + dirName + '/texscroll.inc.c"'
texscrollIncludeH = '#include "levels/' + levelName + "/" + dirName + '/texscroll.inc.h"'
texscrollGroup = levelName
Expand All @@ -828,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
Loading