Add path XML parser (#4115)

* Add path XML parser

* Update SetPathwaysFactory.cpp
This commit is contained in:
Pepe20129 2024-05-10 07:17:50 +02:00 committed by GitHub
parent 844413b391
commit 08ad16e750
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 101 additions and 0 deletions

View File

@ -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<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

View File

@ -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

View File

@ -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)) {

View File

@ -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());
}
}

View File

@ -0,0 +1,6 @@
#include "Resource.h"
#include "soh/OTRGlobals.h"
namespace SOH {
void LogPathAsXML(std::shared_ptr<Ship::IResource> resource);
}