2021-12-05 17:54:36 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "BrickDatabase.h"
|
|
|
|
#include "Game.h"
|
2022-11-01 18:21:26 +00:00
|
|
|
#include "AssetManager.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::vector<Brick> BrickDatabase::emptyCache{};
|
2021-12-05 17:54:36 +00:00
|
|
|
BrickDatabase* BrickDatabase::m_Address = nullptr;
|
|
|
|
|
|
|
|
BrickDatabase::BrickDatabase() = default;
|
|
|
|
BrickDatabase::~BrickDatabase() = default;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::vector<Brick>& BrickDatabase::GetBricks(const std::string& lxfmlPath) {
|
|
|
|
const auto cached = m_Cache.find(lxfmlPath);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (cached != m_Cache.end()) {
|
|
|
|
return cached->second;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-11-06 00:09:26 +00:00
|
|
|
AssetMemoryBuffer buffer = Game::assetManager->GetFileAsBuffer((lxfmlPath).c_str());
|
2022-11-10 18:59:31 +00:00
|
|
|
|
|
|
|
if (!buffer.m_Success) {
|
|
|
|
return emptyCache;
|
|
|
|
}
|
|
|
|
|
2022-11-01 18:21:26 +00:00
|
|
|
std::istream file(&buffer);
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!file.good()) {
|
|
|
|
return emptyCache;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
std::stringstream data;
|
|
|
|
data << file.rdbuf();
|
|
|
|
if (data.str().empty()) {
|
2022-11-01 18:21:26 +00:00
|
|
|
buffer.close();
|
2021-12-05 17:54:36 +00:00
|
|
|
return emptyCache;
|
|
|
|
}
|
|
|
|
|
2022-11-01 18:21:26 +00:00
|
|
|
buffer.close();
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* doc = new tinyxml2::XMLDocument();
|
|
|
|
if (doc->Parse(data.str().c_str(), data.str().size()) != 0) {
|
|
|
|
delete doc;
|
|
|
|
return emptyCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Brick> parts;
|
|
|
|
|
|
|
|
auto* lxfml = doc->FirstChildElement("LXFML");
|
|
|
|
auto* bricks = lxfml->FirstChildElement("Bricks");
|
|
|
|
std::string searchTerm = "Brick";
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!bricks) {
|
|
|
|
searchTerm = "Part";
|
|
|
|
bricks = lxfml->FirstChildElement("Scene")->FirstChildElement("Model")->FirstChildElement("Group");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!bricks) {
|
2022-07-28 13:39:57 +00:00
|
|
|
return emptyCache;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* currentBrick = bricks->FirstChildElement(searchTerm.c_str());
|
|
|
|
while (currentBrick != nullptr) {
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* part = currentBrick->FirstChildElement("Part");
|
2021-12-05 17:54:36 +00:00
|
|
|
if (part == nullptr) part = currentBrick;
|
|
|
|
|
|
|
|
if (part->Attribute("designID") != nullptr) {
|
2022-07-28 13:39:57 +00:00
|
|
|
Brick brick{ static_cast<uint32_t>(part->IntAttribute("designID")) };
|
|
|
|
|
|
|
|
// Depends on the file, some don't specify a list but just a single material
|
|
|
|
const auto* materialList = part->Attribute("materials");
|
|
|
|
const auto* materialID = part->Attribute("materialID");
|
|
|
|
|
|
|
|
if (materialList != nullptr) {
|
|
|
|
std::string materialString(materialList);
|
|
|
|
const auto materials = GeneralUtils::SplitString(materialString, ',');
|
|
|
|
|
|
|
|
if (!materials.empty()) {
|
|
|
|
brick.materialID = std::stoi(materials[0]);
|
|
|
|
} else {
|
|
|
|
brick.materialID = 0;
|
|
|
|
}
|
|
|
|
} else if (materialID != nullptr) {
|
|
|
|
brick.materialID = std::stoi(materialID);
|
|
|
|
} else {
|
|
|
|
brick.materialID = 0; // This is bad, makes it so the minigame can't be played
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
parts.push_back(brick);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
currentBrick = currentBrick->NextSiblingElement(searchTerm.c_str());
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Cache[lxfmlPath] = parts;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
delete doc;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return m_Cache[lxfmlPath];
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|