Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path XML parser #4115

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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);
}
Loading