Redid the VN Scene Parser
This commit is contained in:
72
src/dawntools/util/XmlParser.hpp
Normal file
72
src/dawntools/util/XmlParser.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "util/Xml.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<typename T>
|
||||
class XmlParser {
|
||||
protected:
|
||||
virtual std::vector<std::string> getRequiredAttributes() = 0;
|
||||
virtual std::map<std::string, std::string> getOptionalAttributes() = 0;
|
||||
virtual int32_t onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
T *output,
|
||||
std::string *error
|
||||
) = 0;
|
||||
|
||||
public:
|
||||
static std::string parseDuration(std::string duration) {
|
||||
std::string dur = duration;
|
||||
if(dur.find('.') == std::string::npos) dur += ".0";
|
||||
return dur + "f";
|
||||
}
|
||||
|
||||
static std::string parseEase(std::string e) {
|
||||
if(e == "out-quad") return "&easeOutQuad";
|
||||
if(e == "linear") return "&easeLinear";
|
||||
return "";
|
||||
}
|
||||
|
||||
int32_t parse(Xml *xml, T *output, std::string *error) {
|
||||
std::map<std::string, std::string> values;
|
||||
|
||||
// First get the required attributes
|
||||
auto required = this->getRequiredAttributes();
|
||||
auto itRequired = required.begin();
|
||||
while(itRequired != required.end()) {
|
||||
auto s = *itRequired;
|
||||
auto attr = xml->attributes.find(s);
|
||||
if(attr == xml->attributes.end()) {
|
||||
std::cout << "Missing required attribute \"" << s << "\"" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
values[s] = attr->second;
|
||||
++itRequired;
|
||||
}
|
||||
|
||||
// Now get the optional attributes
|
||||
auto optional = this->getOptionalAttributes();
|
||||
auto itOptional = optional.begin();
|
||||
while(itOptional != optional.end()) {
|
||||
auto key = itOptional->first;
|
||||
auto defaultValue = itOptional->second;
|
||||
|
||||
auto attr = xml->attributes.find(key);
|
||||
if(attr == xml->attributes.end()) {
|
||||
values[key] = defaultValue;
|
||||
} else {
|
||||
values[key] = attr->second;
|
||||
}
|
||||
++itOptional;
|
||||
}
|
||||
|
||||
// Now send to parser
|
||||
return this->onParse(xml, values, output, error);
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user