Skip to content

Commit

Permalink
as_posix fix and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilaa3 committed Oct 11, 2024
1 parent 1a98922 commit 05a1ed0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 35 deletions.
4 changes: 2 additions & 2 deletions 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", [Path(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 @@ -278,7 +278,7 @@ def exportAnimationC(armatureObj, loopAnim, dirPath, dirName, groupName, customE
Path(dirPath),
dirName,
levelName,
[Path("anims/data.inc.c"), Path("anims/table.inc.c")],
[Path("anims", "data.inc.c"), Path("anims", "table.inc.c")],
[Path("anim_header.h")],
)

Expand Down
10 changes: 4 additions & 6 deletions fast64_internal/sm64/sm64_collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,14 @@ def exportCollisionC(
)
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")])
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"
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"
),
to_include_descriptor(Path(name, "rooms.inc.c"), Path("levels", levelName, name, "rooms.inc.c")),
],
)

Expand Down
8 changes: 4 additions & 4 deletions fast64_internal/sm64/sm64_f3d_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ def sm64ExportF3DtoC(
if DLFormat != DLFormat.Static: # Change this
write_material_headers(
Path(basePath),
Path("actors") / toAlnum(name) / "material.inc.c",
Path("actors") / toAlnum(name) / "material.inc.h",
Path("actors", toAlnum(name), "material.inc.c"),
Path("actors", toAlnum(name), "material.inc.h"),
)

texscrollIncludeC = '#include "actors/' + name + '/texscroll.inc.c"'
Expand All @@ -454,8 +454,8 @@ def sm64ExportF3DtoC(
if DLFormat != DLFormat.Static: # Change this
write_material_headers(
basePath,
Path("levels") / levelName / toAlnum(name) / "material.inc.c",
Path("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
8 changes: 4 additions & 4 deletions fast64_internal/sm64/sm64_geolayout_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,12 @@ def saveGeolayoutC(
geoData = geolayoutGraph.to_c()

if headerType == "Actor":
matCInclude = Path("actors") / dirName / "material.inc.c"
matHInclude = Path("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 = Path("levels") / levelName / dirName / "material.inc.c"
matHInclude = Path("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
13 changes: 5 additions & 8 deletions fast64_internal/sm64/sm64_level_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def include_proto(file_name, new_line_first=False):
def write_include(path: Path, include: Path, before_endif=False):
return write_or_delete_if_found(
path,
[to_include_descriptor(include, Path("levels") / level_name / include)],
[to_include_descriptor(include, Path("levels", level_name, include))],
path_must_exist=True,
footer=END_IF_FOOTER if before_endif else None,
)
Expand Down Expand Up @@ -1015,8 +1015,8 @@ def write_include(path: Path, include: Path, before_endif=False):
# Write material headers
write_material_headers(
Path(exportDir),
Path("levels") / level_name / "material.inc.c",
Path("levels") / level_name / "material.inc.c",
Path("levels", level_name, "material.inc.c"),
Path("levels", level_name, "material.inc.h"),
)

# Export camera triggers
Expand Down Expand Up @@ -1091,16 +1091,13 @@ def write_include(path: Path, include: Path, before_endif=False):
write_include(Path(levelDataPath), Path("leveldata.inc.c"))
write_include(Path(headerPath), Path("header.inc.h"), before_endif=True)

old_include = to_include_descriptor(Path("levels") / level_name / "texture_include.inc.c")
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
write_or_delete_if_found(
Path(level_dir) / "texture.inc.c",
to_remove=[old_include],
)
write_or_delete_if_found(Path(level_dir, "texture.inc.c"), to_remove=[old_include])

# This one is for backwards compatibility purposes
write_or_delete_if_found(Path(levelDataPath), to_remove=[old_include])
Expand Down
2 changes: 1 addition & 1 deletion fast64_internal/sm64/sm64_texscroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def writeSegmentROMTable(baseDir):

# Add extern definition of segment table
write_or_delete_if_found(
Path(baseDir) / "src/game/memory.h",
Path(baseDir, "src", "game", "memory.h"),
[
ModifyFoundDescriptor(
"extern uintptr_t sSegmentROMTable[32];", r"extern\h*uintptr_t\h*sSegmentROMTable\[.*?\]\h*?;"
Expand Down
27 changes: 18 additions & 9 deletions fast64_internal/sm64/sm64_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
import bpy
from bpy.types import UILayout

from ..utility import PluginError, filepath_checks, run_and_draw_errors, multilineLabel, prop_split, COMMENT_PATTERN
from ..utility import (
filepath_checks,
run_and_draw_errors,
multilineLabel,
prop_split,
as_posix,
PluginError,
COMMENT_PATTERN,
)
from .sm64_function_map import func_map


Expand Down Expand Up @@ -233,7 +241,7 @@ def find_descriptors(
else:
if footer is not None and error_if_no_footer:
raise PluginError(f"Footer {footer.string} does not exist.")
footer_pos = len(text)
footer_pos = len(commentless)

found_matches: dict[ModifyFoundDescriptor, list[DescriptorMatch]] = {}
for descriptor in descriptors:
Expand Down Expand Up @@ -291,13 +299,14 @@ def write_or_delete_if_found(
footer_pos -= diff

additions = ""
if text[footer_pos - 1] not in {"\n", "\r"}: # add new line if not there
additions += "\n"
for descriptor in to_add:
if descriptor in found_matches:
continue
print(f"Adding {descriptor.string} in {str(path)}")
additions += f"{descriptor.string}\n"
changed = True
print(text[:footer_pos])
text = text[:footer_pos] + additions + text[footer_pos:]

if changed or create_new:
Expand All @@ -308,10 +317,10 @@ def write_or_delete_if_found(

def to_include_descriptor(include: Path, *alternatives: Path):
base_regex = r'\n?#\h*?include\h*?"{0}"'
regex = base_regex.format(include.as_posix())
regex = base_regex.format(as_posix(include))
for alternative in alternatives:
regex += f"|{base_regex.format(alternative.as_posix())}"
return ModifyFoundDescriptor(f'#include "{include.as_posix()}"', regex)
regex += f"|{base_regex.format(as_posix(alternative))}"
return ModifyFoundDescriptor(f'#include "{as_posix(include)}"', regex)


END_IF_FOOTER = ModifyFoundDescriptor("#endif", r"#\h*?endif")
Expand Down Expand Up @@ -363,13 +372,13 @@ def write_includes_with_alternate(path: Path, includes: Optional[list[Path]], be
if header_type == "Level":
path_and_alternates = [
[
Path(dir_name) / include,
Path("levels") / level_name / (dir_name) / include, # backwards compatability
Path(dir_name, include),
Path("levels", level_name, dir_name, include), # backwards compatability
]
for include in includes
]
else:
path_and_alternates = [[Path(dir_name) / include] for include in includes]
path_and_alternates = [[Path(dir_name, include)] for include in includes]
return write_or_delete_if_found(
path,
[to_include_descriptor(*paths) for paths in path_and_alternates],
Expand Down
6 changes: 5 additions & 1 deletion fast64_internal/utility.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bpy, random, string, os, math, traceback, re, os, mathutils, ast, operator
import bpy, random, string, os, math, traceback, re, os, mathutils, ast, operator, pathlib
from math import pi, ceil, degrees, radians, copysign
from mathutils import *
from .utility_anim import *
Expand Down Expand Up @@ -1857,3 +1857,7 @@ def create_or_get_world(scene: Scene) -> World:
WORLD_WARNING_COUNT = 0
print(f'No world in this file, creating world named "Fast64".')
return bpy.data.worlds.new("Fast64")


def as_posix(path: pathlib.Path) -> str:
return path.as_posix().replace("\\", "/") # Windows path sometimes still has backslashes?

0 comments on commit 05a1ed0

Please sign in to comment.