Redid the VN Scene Parser
This commit is contained in:
100
src/dawntools/util/CodeGen.hpp
Normal file
100
src/dawntools/util/CodeGen.hpp
Normal file
@@ -0,0 +1,100 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnsharedlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct ClassGenInfo {
|
||||
std::vector<std::string> includes;
|
||||
std::string clazz = "Unknown";
|
||||
std::string extend = "";
|
||||
std::string constructorArgs = "";
|
||||
std::string extendArgs = "";
|
||||
|
||||
std::vector<std::string> protectedCode;
|
||||
std::vector<std::string> protectedProperties;
|
||||
|
||||
std::vector<std::string> publicCode;
|
||||
std::vector<std::string> publicProperties;
|
||||
};
|
||||
|
||||
struct MethodGenInfo {
|
||||
std::string name;
|
||||
std::string type = "void";
|
||||
std::vector<std::string> body;
|
||||
std::string args = "";
|
||||
bool_t isStatic = false;
|
||||
bool_t isOverride = false;
|
||||
};
|
||||
|
||||
class CodeGen {
|
||||
protected:
|
||||
static void line(
|
||||
std::vector<std::string> *out,
|
||||
std::string contents,
|
||||
std::string tabs
|
||||
) {
|
||||
out->push_back(tabs + contents);
|
||||
}
|
||||
|
||||
static void lines(
|
||||
std::vector<std::string> *out,
|
||||
std::vector<std::string> lines,
|
||||
std::string tabs
|
||||
) {
|
||||
auto itLine = lines.begin();
|
||||
while(itLine != lines.end()) {
|
||||
line(out, *itLine, tabs);
|
||||
++itLine;
|
||||
}
|
||||
}
|
||||
|
||||
static void classGen(
|
||||
std::vector<std::string> *out,
|
||||
struct ClassGenInfo info
|
||||
) {
|
||||
std::vector<std::string> buffer;
|
||||
|
||||
line(out, "#pragma once", "");
|
||||
line(out, "", "");
|
||||
if(info.includes.size() > 0) {
|
||||
lines(out, info.includes, "");
|
||||
line(out, "", "");
|
||||
}
|
||||
line(out, "namespace Dawn {", "");
|
||||
line(out, "class " + info.clazz + (info.extend.size() == 0 ? "{" : " : public " + info.extend + " {" ), " ");
|
||||
if(info.protectedCode.size() > 0) {
|
||||
line(out, "protected:", " ");
|
||||
lines(out, info.protectedProperties, " ");
|
||||
line(out, "", " ");
|
||||
lines(out, info.protectedCode, " ");
|
||||
}
|
||||
|
||||
if(info.publicCode.size() > 0 || info.constructorArgs.size() > 0) {
|
||||
line(out, "public:", " ");
|
||||
lines(out, info.publicProperties, " ");
|
||||
line(out, "", " ");
|
||||
line(out, info.clazz + "(" + info.constructorArgs + ")" + (info.extend.size() > 0 ? " : " + info.extend + "(" + info.extendArgs + ")" : "") + " {", " ");
|
||||
line(out, "}", " ");
|
||||
if(info.publicCode.size() > 0) {
|
||||
line(out, "", " ");
|
||||
lines(out, info.publicCode, " ");
|
||||
}
|
||||
}
|
||||
line(out, "};", " ");
|
||||
line(out, "}", "");
|
||||
}
|
||||
|
||||
static void methodGen(
|
||||
std::vector<std::string> *out,
|
||||
struct MethodGenInfo info
|
||||
) {
|
||||
line(out, info.type + " " + info.name + "(" + info.args + ") " + ( info.isOverride ? "override" : "" ) + "{", "");
|
||||
lines(out, info.body, " ");
|
||||
line(out, "}", "");
|
||||
}
|
||||
};
|
||||
}
|
@@ -63,13 +63,13 @@ bool_t File::readString(std::string *out) {
|
||||
out->clear();
|
||||
|
||||
size_t i = 0;
|
||||
char buffer[FILE_BUFFER_SIZE + 1];
|
||||
char buffer[FILE_BUFFER_SIZE + 1];// +1 for null term
|
||||
while(i != this->length) {
|
||||
size_t amt = mathMin<size_t>(FILE_BUFFER_SIZE, (this->length - i));
|
||||
auto amtRead = fread(buffer, sizeof(char), amt, this->file);
|
||||
if(amtRead != amt) return false;
|
||||
i += amtRead;
|
||||
buffer[amtRead + 1] = '\0';
|
||||
buffer[amtRead] = '\0';
|
||||
out->append(buffer);
|
||||
}
|
||||
|
||||
|
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