summaryrefslogtreecommitdiff
path: root/src/factories/mk64/TrackSections.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/factories/mk64/TrackSections.cpp')
-rw-r--r--src/factories/mk64/TrackSections.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/factories/mk64/TrackSections.cpp b/src/factories/mk64/TrackSections.cpp
new file mode 100644
index 0000000..6707950
--- /dev/null
+++ b/src/factories/mk64/TrackSections.cpp
@@ -0,0 +1,91 @@
+#include "TrackSections.h"
+
+#include "Companion.h"
+#include "utils/Decompressor.h"
+
+#define NUM(x) std::dec << std::setfill(' ') << std::setw(6) << x
+#define COL(c) "0x" << std::hex << std::setw(2) << std::setfill('0') << c
+
+void MK64::TrackSectionsHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) {
+ const auto symbol = GetSafeNode(node, "symbol", entryName);
+
+ if(Companion::Instance->IsOTRMode()){
+ write << "static const char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
+ return;
+ }
+
+ write << "extern TrackSections " << symbol << "[];\n";
+}
+
+void MK64::TrackSectionsCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
+ auto sections = std::static_pointer_cast<TrackSectionsData>(raw)->mSecs;
+ const auto symbol = GetSafeNode(node, "symbol", entryName);
+ const auto offset = GetSafeNode<uint32_t>(node, "offset");
+
+ if (Companion::Instance->IsDebug()) {
+ write << "// 0x" << std::hex << std::uppercase << offset << "\n";
+ }
+
+ write << "TrackSections " << symbol << "[] = {\n";
+
+ for (int i = 0; i < sections.size(); i++) {
+ auto entry = sections[i];
+
+ auto addr = entry.addr;
+ auto surf = entry.surfaceType;
+ auto sect = entry.sectionId;
+ auto flags = entry.flags;
+
+ if(i <= sections.size() - 1) {
+ write << fourSpaceTab;
+ }
+
+ // { addr, surface, section, flags },
+ write << "{" << NUM(addr) << ", " << NUM(surf) << ", " << NUM(sect) << ", " << NUM(flags) << "},\n";
+ }
+
+ if (Companion::Instance->IsDebug()) {
+ write << fourSpaceTab << "// 0x" << std::hex << std::uppercase << (offset + (sizeof(TrackSections) * sections.size())) << "\n";
+ }
+
+ write << "};\n\n";
+}
+
+void MK64::TrackSectionsBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
+ auto sections = std::static_pointer_cast<TrackSectionsData>(raw);
+ auto writer = LUS::BinaryWriter();
+
+ WriteHeader(writer, LUS::ResourceType::TrackSection, 0);
+ writer.Write((uint32_t) sections->mSecs.size());
+ for(auto entry : sections->mSecs) {
+ writer.Write(entry.addr);
+ writer.Write(entry.surfaceType);
+ writer.Write(entry.sectionId);
+ writer.Write(entry.flags);
+ }
+
+ writer.Finish(write);
+}
+
+std::optional<std::shared_ptr<IParsedData>> MK64::TrackSectionsFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
+ auto count = GetSafeNode<size_t>(node, "count");
+
+ auto [_, segment] = Decompressor::AutoDecode(node, buffer);
+ LUS::BinaryReader reader(segment.data, count * sizeof(MK64::TrackSections));
+
+ reader.SetEndianness(LUS::Endianness::Big);
+ std::vector<MK64::TrackSections> sections;
+
+ for(size_t i = 0; i < count; i++) {
+ auto addr = reader.ReadUInt32();
+ auto surf = reader.ReadInt8();
+ auto sect = reader.ReadInt8();
+ auto flags = reader.ReadUInt16();
+
+ sections.push_back(MK64::TrackSections({
+ addr, surf, sect, flags,
+ }));
+ }
+
+ return std::make_shared<TrackSectionsData>(sections);
+} \ No newline at end of file