From 08ad16e75046c2a1d8424b56571980775dc0296a Mon Sep 17 00:00:00 2001 From: Pepe20129 <72659707+Pepe20129@users.noreply.github.com> Date: Fri, 10 May 2024 07:17:50 +0200 Subject: [PATCH] Add path XML parser (#4115) * Add path XML parser * Update SetPathwaysFactory.cpp --- soh/soh/resource/importer/PathFactory.cpp | 55 +++++++++++++++++++ soh/soh/resource/importer/PathFactory.h | 6 ++ .../scenecommand/SetPathwaysFactory.cpp | 1 + soh/soh/resource/logging/PathLogger.cpp | 33 +++++++++++ soh/soh/resource/logging/PathLogger.h | 6 ++ 5 files changed, 101 insertions(+) create mode 100644 soh/soh/resource/logging/PathLogger.cpp create mode 100644 soh/soh/resource/logging/PathLogger.h diff --git a/soh/soh/resource/importer/PathFactory.cpp b/soh/soh/resource/importer/PathFactory.cpp index 00807b75afa..f0f92be0221 100644 --- a/soh/soh/resource/importer/PathFactory.cpp +++ b/soh/soh/resource/importer/PathFactory.cpp @@ -1,5 +1,6 @@ #include "soh/resource/importer/PathFactory.h" #include "soh/resource/type/Path.h" +#include "soh/resource/logging/PathLogger.h" #include "spdlog/spdlog.h" namespace SOH { @@ -35,6 +36,60 @@ std::shared_ptr ResourceFactoryBinaryPathV0::ReadResource(std:: path->pathData.push_back(pathDataEntry); } + if (CVarGetInteger(CVAR_DEVELOPER_TOOLS("ResourceLogging"), 0)) { + LogPathAsXML(path); + } + return path; } + +std::shared_ptr ResourceFactoryXMLPathV0::ReadResource(std::shared_ptr file) { + if (!FileHasValidFormatAndReader(file)) { + return nullptr; + } + + auto path = std::make_shared(file->InitData); + auto reader = std::get>(file->Reader); + + auto pathElement = reader->RootElement(); + + //path->numPaths = pathElement->IntAttribute("NumPaths"); + //path->paths.reserve(path->numPaths); + + auto pathDataElement = pathElement->FirstChildElement(); + + while (pathDataElement != nullptr) { + std::vector points; + //uint32_t pointCount = pathDataElement->IntAttribute("NumPoints"); + //points.reserve(pointCount); + + auto pointElement = pathDataElement->FirstChildElement(); + + while (pointElement != nullptr) { + Vec3s point; + point.x = pointElement->IntAttribute("X"); + point.y = pointElement->IntAttribute("Y"); + point.z = pointElement->IntAttribute("Z"); + + points.push_back(point); + + pointElement = pointElement->NextSiblingElement(); + } + + PathData pathDataEntry; + //pathDataEntry.count = pointCount; + pathDataEntry.count = points.size(); + + path->paths.push_back(points); + pathDataEntry.points = path->paths.back().data(); + + path->pathData.push_back(pathDataEntry); + + pathDataElement = pathDataElement->NextSiblingElement(); + } + + path->numPaths = path->paths.size(); + + return path; +}; } // namespace SOH diff --git a/soh/soh/resource/importer/PathFactory.h b/soh/soh/resource/importer/PathFactory.h index ddbee772df4..9dd73b11025 100644 --- a/soh/soh/resource/importer/PathFactory.h +++ b/soh/soh/resource/importer/PathFactory.h @@ -2,10 +2,16 @@ #include "Resource.h" #include "ResourceFactoryBinary.h" +#include "ResourceFactoryXML.h" namespace SOH { class ResourceFactoryBinaryPathV0 : public Ship::ResourceFactoryBinary { public: std::shared_ptr ReadResource(std::shared_ptr file) override; }; + +class ResourceFactoryXMLPathV0 : public Ship::ResourceFactoryXML { + public: + std::shared_ptr ReadResource(std::shared_ptr file) override; +}; } // namespace SOH diff --git a/soh/soh/resource/importer/scenecommand/SetPathwaysFactory.cpp b/soh/soh/resource/importer/scenecommand/SetPathwaysFactory.cpp index 65af2e0de31..ed368123536 100644 --- a/soh/soh/resource/importer/scenecommand/SetPathwaysFactory.cpp +++ b/soh/soh/resource/importer/scenecommand/SetPathwaysFactory.cpp @@ -17,6 +17,7 @@ SetPathwaysFactory::ReadResource(std::shared_ptr initDat std::string pathFileName = reader->ReadString(); auto path = std::static_pointer_cast(Ship::Context::GetInstance()->GetResourceManager()->LoadResourceProcess(pathFileName.c_str())); setPathways->paths.push_back(path->GetPointer()); + setPathways->pathFileNames.push_back(pathFileName); } if (CVarGetInteger(CVAR_DEVELOPER_TOOLS("ResourceLogging"), 0)) { diff --git a/soh/soh/resource/logging/PathLogger.cpp b/soh/soh/resource/logging/PathLogger.cpp new file mode 100644 index 00000000000..5c216c9ff96 --- /dev/null +++ b/soh/soh/resource/logging/PathLogger.cpp @@ -0,0 +1,33 @@ +#include "soh/resource/type/Path.h" +#include "spdlog/spdlog.h" + +namespace SOH { + void LogPathAsXML(std::shared_ptr resource) { + std::shared_ptr path = std::static_pointer_cast(resource); + + tinyxml2::XMLDocument doc; + tinyxml2::XMLElement* root = doc.NewElement("Path"); + doc.InsertFirstChild(root); + + for (size_t i = 0; i < path->paths.size(); i += 1) { + tinyxml2::XMLElement* pathData = doc.NewElement("PathData"); + + for (size_t j = 0; j < path->paths[i].size(); j += 1) { + tinyxml2::XMLElement* pathPoint = doc.NewElement("PathPoint"); + + pathPoint->SetAttribute("X", path->paths[i][j].x); + pathPoint->SetAttribute("Y", path->paths[i][j].y); + pathPoint->SetAttribute("Z", path->paths[i][j].z); + + pathData->InsertEndChild(pathPoint); + } + + root->InsertEndChild(pathData); + } + + tinyxml2::XMLPrinter printer; + doc.Accept(&printer); + + SPDLOG_INFO("{}: {}", resource->GetInitData()->Path, printer.CStr()); + } +} \ No newline at end of file diff --git a/soh/soh/resource/logging/PathLogger.h b/soh/soh/resource/logging/PathLogger.h new file mode 100644 index 00000000000..a5e42aeaf85 --- /dev/null +++ b/soh/soh/resource/logging/PathLogger.h @@ -0,0 +1,6 @@ +#include "Resource.h" +#include "soh/OTRGlobals.h" + +namespace SOH { + void LogPathAsXML(std::shared_ptr resource); +} \ No newline at end of file