diff --git a/src/engine/courses/Course.cpp b/src/engine/courses/Course.cpp index 340fdf1bc..3cccf0477 100644 --- a/src/engine/courses/Course.cpp +++ b/src/engine/courses/Course.cpp @@ -31,7 +31,6 @@ Course::Course() { // Props.CourseLength = "567m"; // Props.Cup = FLOWER_CUP; // Props.CupIndex = 3; - Props.TrackSectionsPtr = NULL; Props.LakituTowType = (s32) OLakitu::LakituTowType::NORMAL; Props.AIBehaviour = D_0D008F28; Props.AIMaximumSeparation = 50.0f; @@ -91,7 +90,7 @@ void Course::Load(Vtx* vtx, Gfx* gfx) { void Course::LoadO2R(std::string trackPath) { if (!trackPath.empty()) { - Props.TrackSectionsPtr = (trackPath + "/data_track_sections").c_str(); + TrackSectionsPtr = (trackPath + "/data_track_sections"); std::string path_file = (trackPath + "/data_paths").c_str(); @@ -126,14 +125,16 @@ void Course::LoadO2R(std::string trackPath) { void Course::Load() { // Load from O2R - if (Props.TrackSectionsPtr != NULL || Props.TrackSectionsPtr[0] != '\0') { + if (!TrackSectionsPtr.empty()) { - auto res = std::dynamic_pointer_cast(ResourceLoad(Props.TrackSectionsPtr)); + //auto res = std::dynamic_pointer_cast(ResourceLoad(TrackSectionsPtr.c_str())); - if (res != nullptr) { - std::vector sections = res->TrackSectionsList; + TrackSectionsO2R* sections = (TrackSectionsO2R*) LOAD_ASSET_RAW(TrackSectionsPtr.c_str()); + size_t size = ResourceGetSizeByName(TrackSectionsPtr.c_str()); - ParseCourseSections(sections); + if (sections != nullptr) { + Course::Init(); + ParseCourseSections(sections, size); } return; } @@ -184,24 +185,25 @@ void Course::Load() { } // C++ version of parse_course_displaylists() -void Course::ParseCourseSections(std::vector sections) { - for (auto& section : sections) { - if (section.flags & 0x8000) { +void Course::ParseCourseSections(TrackSectionsO2R* sections, size_t size) { + for (size_t i = 0; i < (size / sizeof(TrackSectionsO2R)); i++) { + if (sections[i].flags & 0x8000) { D_8015F59C = 1; } else { D_8015F59C = 0; } - if (section.flags & 0x2000) { + if (sections[i].flags & 0x2000) { D_8015F5A0 = 1; } else { D_8015F5A0 = 0; } - if (section.flags & 0x4000) { + if (sections[i].flags & 0x4000) { D_8015F5A4 = 1; } else { D_8015F5A4 = 0; } - generate_collision_mesh((Gfx*)LOAD_ASSET_RAW(section.addr), section.surfaceType, section.sectionId); + printf("LOADING DL %s\n", sections[i].addr.c_str()); + generate_collision_mesh((Gfx*)LOAD_ASSET_RAW(sections[i].addr.c_str()), sections[i].surfaceType, sections[i].sectionId); } } @@ -302,7 +304,7 @@ void Course::Waypoints(Player* player, int8_t playerId) { } void Course::Render(struct UnkStruct_800DC5EC* arg0) { - //if (Props.TrackSectionsPtr) { + //if (!TrackSectionsPtr.empty()) { // gSPDisplayList(gDisplayListHead++, (Gfx*)LOAD_ASSET_RAW(Props.TrackModel)); //} } diff --git a/src/engine/courses/Course.h b/src/engine/courses/Course.h index cefe0d4dd..cd25ec29b 100644 --- a/src/engine/courses/Course.h +++ b/src/engine/courses/Course.h @@ -78,7 +78,6 @@ typedef struct Properties { const course_texture *textures; enum MusicSeq Sequence; float WaterLevel; // Used for effects, and Lakitu pick up height. Not necessarily the visual water model height. - const char* TrackSectionsPtr; #ifdef __cplusplus nlohmann::json to_json() const { @@ -234,16 +233,17 @@ public: const course_texture* textures = nullptr; bool bSpawnFinishline = true; std::optional FinishlineSpawnPoint; + std::string TrackSectionsPtr; virtual ~Course() = default; explicit Course(); virtual void LoadO2R(std::string trackPath); // Load custom track from o2r - virtual void Load(); // Decompress and load stock courses or from o2r but Props.TrackSectionsPtr must be set. + virtual void Load(); // Decompress and load stock courses or from o2r but TrackSectionsPtr must be set. virtual void Load(Vtx* vtx, Gfx *gfx); // Load custom track from code. Load must be overridden and then call to this base class method impl. virtual void LoadTextures(); - virtual void ParseCourseSections(std::vector sections); + virtual void ParseCourseSections(TrackSectionsO2R* sections, size_t size); /** * @brief BeginPlay This function is called once at the start of gameplay. diff --git a/src/port/resource/importers/TrackSectionsFactory.cpp b/src/port/resource/importers/TrackSectionsFactory.cpp index b9c15878d..3c4bd9e12 100644 --- a/src/port/resource/importers/TrackSectionsFactory.cpp +++ b/src/port/resource/importers/TrackSectionsFactory.cpp @@ -49,14 +49,19 @@ ResourceFactoryXMLTrackSectionsV0::ReadResource(std::shared_ptr file std::get>(file->Reader)->FirstChildElement()->FirstChildElement(); while (child != nullptr) { - TrackSectionsO2R data; - // Convert n64 addr to native addr - data.addr = child->Attribute("gfx_path"); - data.surfaceType = child->IntAttribute("surface"); - data.sectionId = child->IntAttribute("section"); - data.flags = child->IntAttribute("flags"); + std::string childName = child->Name(); - section->TrackSectionsList.push_back(data); + if (childName == "Section") { + TrackSectionsO2R data; + // Convert n64 addr to native addr + data.addr = std::string(child->Attribute("gfx_path")); + data.surfaceType = child->IntAttribute("surface"); + data.sectionId = child->IntAttribute("section"); + data.flags = child->IntAttribute("flags"); + + section->TrackSectionsList.push_back(data); + } + child = child->NextSiblingElement(); } return section; diff --git a/src/port/resource/importers/TrackSectionsFactory.h b/src/port/resource/importers/TrackSectionsFactory.h index f0853682e..0f1891043 100644 --- a/src/port/resource/importers/TrackSectionsFactory.h +++ b/src/port/resource/importers/TrackSectionsFactory.h @@ -2,6 +2,7 @@ #include "Resource.h" #include "ResourceFactoryBinary.h" +#include "resource/ResourceFactoryXML.h" namespace MK64 { class ResourceFactoryBinaryTrackSectionsV0 : public Ship::ResourceFactoryBinary { @@ -10,7 +11,7 @@ class ResourceFactoryBinaryTrackSectionsV0 : public Ship::ResourceFactoryBinary std::shared_ptr initData) override; }; -class ResourceFactoryXMLTrackSectionsV0 : public Ship::ResourceFactoryBinary { +class ResourceFactoryXMLTrackSectionsV0 : public Ship::ResourceFactoryXML { public: std::shared_ptr ReadResource(std::shared_ptr file, std::shared_ptr initData) override; diff --git a/src/port/resource/type/TrackSections.cpp b/src/port/resource/type/TrackSections.cpp index dcbdb4323..9d0ac27f8 100644 --- a/src/port/resource/type/TrackSections.cpp +++ b/src/port/resource/type/TrackSections.cpp @@ -21,6 +21,6 @@ TrackSectionsO2R* TrackSectionsO2RClass::GetPointer() { } size_t TrackSectionsO2RClass::GetPointerSize() { - return TrackSectionsList.size() *sizeof(TrackSectionsO2R); + return TrackSectionsList.size() * sizeof(TrackSectionsO2R); } } // namespace MK64 diff --git a/src/port/resource/type/TrackSections.h b/src/port/resource/type/TrackSections.h index 17e3bc105..32182022f 100644 --- a/src/port/resource/type/TrackSections.h +++ b/src/port/resource/type/TrackSections.h @@ -12,7 +12,7 @@ typedef struct { } TrackSectionsI; typedef struct { - const char* addr; + std::string addr; uint8_t surfaceType; uint8_t sectionId; uint16_t flags;