80 lines
2.2 KiB
C++
80 lines
2.2 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "SceneItemComponentParser.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
std::vector<std::string> SceneItemComponentParser::getRequiredAttributes() {
|
|
return { };
|
|
}
|
|
|
|
std::map<std::string, std::string> SceneItemComponentParser::getOptionalAttributes() {
|
|
return { { "ref", "" } };
|
|
}
|
|
|
|
int32_t SceneItemComponentParser::onParse(
|
|
Xml *node,
|
|
std::map<std::string, std::string> values,
|
|
struct SceneItemComponent *out,
|
|
std::string *error
|
|
) {
|
|
// Check if values contains key "ref" and has a string of size more than 0
|
|
if(values.find("ref") != values.end() && values["ref"].size() > 0) {
|
|
out->ref = values["ref"];
|
|
}
|
|
|
|
// Get the ruleset.
|
|
auto ruleset = out->registry->getRuleset(node->node);
|
|
if(ruleset.name.size() == 0) {
|
|
*error = "Unknown prefab node type: " + node->node;
|
|
return 1;
|
|
}
|
|
|
|
out->include = ruleset.include;
|
|
out->type = node->node;
|
|
|
|
// Define parser types here.
|
|
|
|
// Iterate all the optional attributes and store within the out if present
|
|
auto itOptional = ruleset.optional.begin();
|
|
while(itOptional != ruleset.optional.end()) {
|
|
// Determine the parser to use
|
|
auto name = itOptional->first;
|
|
auto type = itOptional->second;
|
|
|
|
std::function<std::string(std::string, std::string*)> parser = rawParser;
|
|
try {
|
|
parser = parserFromTypeName(type);
|
|
} catch(std::string err) {
|
|
if(name[0] == '*') {
|
|
name = name.substr(1, name.size());
|
|
parser = rawParser;
|
|
} else {
|
|
*error = "Unknown parser for type: " + type + "::" + name;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if(node->attributes.find(name) != node->attributes.end()) {
|
|
auto raw = node->attributes[name];
|
|
out->values[name] = parser(raw, error);
|
|
if(error->size() != 0) return 1;
|
|
}
|
|
++itOptional;
|
|
}
|
|
|
|
auto itInnerXml = ruleset.innerXml.begin();
|
|
while(itInnerXml != ruleset.innerXml.end()) {
|
|
auto name = itInnerXml->first;
|
|
auto type = itInnerXml->second;
|
|
|
|
out->values[name] = stringParser(node->innerXml, error);
|
|
if(error->size() != 0) return 1;
|
|
++itInnerXml;
|
|
}
|
|
|
|
return 0;
|
|
} |