diff --git a/fast64_internal/sm64/sm64_anim.py b/fast64_internal/sm64/sm64_anim.py index 263e6b29a..64cdbfecb 100644 --- a/fast64_internal/sm64/sm64_anim.py +++ b/fast64_internal/sm64/sm64_anim.py @@ -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") @@ -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")], ) diff --git a/fast64_internal/sm64/sm64_collision.py b/fast64_internal/sm64/sm64_collision.py index 5f342bd27..f164088c9 100644 --- a/fast64_internal/sm64/sm64_collision.py +++ b/fast64_internal/sm64/sm64_collision.py @@ -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")), ], ) diff --git a/fast64_internal/sm64/sm64_f3d_writer.py b/fast64_internal/sm64/sm64_f3d_writer.py index e93b2231f..7eddc5afb 100644 --- a/fast64_internal/sm64/sm64_f3d_writer.py +++ b/fast64_internal/sm64/sm64_f3d_writer.py @@ -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"' @@ -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"' diff --git a/fast64_internal/sm64/sm64_geolayout_writer.py b/fast64_internal/sm64/sm64_geolayout_writer.py index 3c8038612..8195c209b 100644 --- a/fast64_internal/sm64/sm64_geolayout_writer.py +++ b/fast64_internal/sm64/sm64_geolayout_writer.py @@ -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) diff --git a/fast64_internal/sm64/sm64_level_writer.py b/fast64_internal/sm64/sm64_level_writer.py index e890ba8f3..2f6154367 100644 --- a/fast64_internal/sm64/sm64_level_writer.py +++ b/fast64_internal/sm64/sm64_level_writer.py @@ -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, ) @@ -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 @@ -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]) diff --git a/fast64_internal/sm64/sm64_texscroll.py b/fast64_internal/sm64/sm64_texscroll.py index 5fa1dc453..c39de7fd0 100644 --- a/fast64_internal/sm64/sm64_texscroll.py +++ b/fast64_internal/sm64/sm64_texscroll.py @@ -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*?;" diff --git a/fast64_internal/sm64/sm64_utility.py b/fast64_internal/sm64/sm64_utility.py index 923e742f4..112e97a9e 100644 --- a/fast64_internal/sm64/sm64_utility.py +++ b/fast64_internal/sm64/sm64_utility.py @@ -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 @@ -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: @@ -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: @@ -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") @@ -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], diff --git a/fast64_internal/utility.py b/fast64_internal/utility.py index 3da649478..71e639c01 100644 --- a/fast64_internal/utility.py +++ b/fast64_internal/utility.py @@ -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 * @@ -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?