Skip to content

Commit

Permalink
Add path XML parser (HarbourMasters#4115)
Browse files Browse the repository at this point in the history
* Add path XML parser

* Update SetPathwaysFactory.cpp
  • Loading branch information
Pepe20129 authored May 10, 2024
1 parent 844413b commit 08ad16e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
55 changes: 55 additions & 0 deletions soh/soh/resource/importer/PathFactory.cpp
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -35,6 +36,60 @@ std::shared_ptr<Ship::IResource> ResourceFactoryBinaryPathV0::ReadResource(std::
path->pathData.push_back(pathDataEntry);
}

if (CVarGetInteger(CVAR_DEVELOPER_TOOLS("ResourceLogging"), 0)) {
LogPathAsXML(path);
}

return path;
}

std::shared_ptr<Ship::IResource> ResourceFactoryXMLPathV0::ReadResource(std::shared_ptr<Ship::File> file) {
if (!FileHasValidFormatAndReader(file)) {
return nullptr;
}

auto path = std::make_shared<Path>(file->InitData);
auto reader = std::get<std::shared_ptr<tinyxml2::XMLDocument>>(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<Vec3s> 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
6 changes: 6 additions & 0 deletions soh/soh/resource/importer/PathFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

#include "Resource.h"
#include "ResourceFactoryBinary.h"
#include "ResourceFactoryXML.h"

namespace SOH {
class ResourceFactoryBinaryPathV0 : public Ship::ResourceFactoryBinary {
public:
std::shared_ptr<Ship::IResource> ReadResource(std::shared_ptr<Ship::File> file) override;
};

class ResourceFactoryXMLPathV0 : public Ship::ResourceFactoryXML {
public:
std::shared_ptr<Ship::IResource> ReadResource(std::shared_ptr<Ship::File> file) override;
};
} // namespace SOH
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SetPathwaysFactory::ReadResource(std::shared_ptr<Ship::ResourceInitData> initDat
std::string pathFileName = reader->ReadString();
auto path = std::static_pointer_cast<Path>(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)) {
Expand Down
33 changes: 33 additions & 0 deletions soh/soh/resource/logging/PathLogger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "soh/resource/type/Path.h"
#include "spdlog/spdlog.h"

namespace SOH {
void LogPathAsXML(std::shared_ptr<Ship::IResource> resource) {
std::shared_ptr<Path> path = std::static_pointer_cast<Path>(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());
}
}
6 changes: 6 additions & 0 deletions soh/soh/resource/logging/PathLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Resource.h"
#include "soh/OTRGlobals.h"

namespace SOH {
void LogPathAsXML(std::shared_ptr<Ship::IResource> resource);
}

0 comments on commit 08ad16e

Please sign in to comment.