Files
IAEngine/Src/ConfigData/imp/cpp/ConfigData.cpp
Isuru Samarathunga 67cb23d589 Fixes
2025-11-10 09:49:22 +05:30

95 lines
3.3 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <ConfigData/ConfigData.hpp>
#include <IACore/File.hpp>
#include <filesystem>
namespace ia::iae
{
ConfigData::ConfigData(IN pugi::xml_document &&document, IN CONST String &type, IN CONST String &name,
IN pugi::xml_node propertiesNode, IN Vector<pugi::xml_node> &&childNodes)
: m_document(IA_MOVE(document)), m_type(type), m_name(name), m_propertiesNode(propertiesNode),
m_childNodes(IA_MOVE(childNodes))
{
}
RefPtr<ConfigData> ConfigData::LoadFromFile(IN CONST String &path)
{
const auto result = LoadFromMemory(File::ReadToString(path));
result->m_filePath = std::filesystem::canonical(path.c_str()).string().c_str();
return result;
}
RefPtr<ConfigData> ConfigData::LoadFromMemory(IN CONST String &data)
{
pugi::xml_document doc;
if (!doc.load_string(data.c_str()))
return nullptr;
if (std::distance(doc.children().begin(), doc.children().end()) != 1)
return nullptr;
const auto rootNode = *doc.children().begin();
// Verify Engine Version
{
const auto engineInfoNode = rootNode.child("IAEngine");
if (!engineInfoNode)
return nullptr;
const auto engineVersionNode = engineInfoNode.child("EngineVersion");
if (!engineVersionNode)
return nullptr;
const auto engineVersion = IA_PARSE_VERSION_STRING(engineVersionNode.text().as_string());
if (engineVersion > ENGINE_VERSION)
{
CFG_LOG_WARN("XML data file was created with a newer version of IAEngine, skipping..");
return nullptr;
}
}
const auto propertiesNode = rootNode.child("Properties");
if (!propertiesNode)
return nullptr;
Vector<pugi::xml_node> childNodes;
for (auto &c : rootNode.children())
{
if ((!strcmp(c.name(), "Properties")) || (!strcmp(c.name(), "IAEngine")))
continue;
childNodes.pushBack(c);
}
const auto nameAttr = rootNode.attribute("name");
return MakeRefPtr<ConfigData>(IA_MOVE(doc), rootNode.name(), nameAttr ? nameAttr.as_string() : "",
propertiesNode, IA_MOVE(childNodes));
}
VOID ConfigData::WriteChangesToDisk()
{
if (m_filePath.empty())
return;
m_document.save_file(m_filePath.c_str());
}
pugi::xml_node ConfigData::Property(IN CONST String &name)
{
return m_propertiesNode.child(name.c_str());
}
} // namespace ia::iae