// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "PrefabComponentParser.hpp" using namespace Dawn; std::vector PrefabComponentParser::getRequiredAttributes() { return { }; } std::map PrefabComponentParser::getOptionalAttributes() { return { { "ref", "" } }; } int32_t PrefabComponentParser::onParse( Xml *node, std::map values, struct PrefabComponent *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 parser = rawParser; if(type.find("string") != std::string::npos) { parser = stringParser; } else if(type.find("float") != std::string::npos) { parser = floatParser; } else if(type.find("Color") != std::string::npos) { parser = colorParser; } else if(type.find("vec2") != std::string::npos) { parser = vec2Parser; } else if(type.find("vec3") != std::string::npos) { parser = vec3Parser; } else if(type.find("vec4") != std::string::npos) { parser = vec4Parser; } else if(type == "int32_t" || type == "int") { parser = intParser; } else if(type == "bool_t") { parser = boolParser; } else if(type.starts_with("enum")) { parser = rawParser; } else if(type.find("*") == (type.size() - 1)) { type = type.substr(0, type.size() - 1); parser = rawParser; } else 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; } return 0; }