Tool refactoring
This commit is contained in:
38
archive/tools/tools/languagegen/CMakeLists.txt
Normal file
38
archive/tools/tools/languagegen/CMakeLists.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Texture Build Tool
|
||||
project(languagegen VERSION 2.0)
|
||||
add_executable(languagegen)
|
||||
|
||||
|
||||
# Sources
|
||||
target_sources(languagegen
|
||||
PRIVATE
|
||||
${DAWN_SHARED_SOURCES}
|
||||
${DAWN_TOOL_SOURCES}
|
||||
LanguageGen.cpp
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(languagegen
|
||||
PUBLIC
|
||||
${DAWN_SHARED_INCLUDES}
|
||||
${DAWN_TOOL_INCLUDES}
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Definitions
|
||||
target_compile_definitions(languagegen
|
||||
PUBLIC
|
||||
DAWN_TOOL_INSTANCE=LanguageGen
|
||||
DAWN_TOOL_HEADER="LanguageGen.hpp"
|
||||
)
|
||||
|
||||
# Libraries
|
||||
target_link_libraries(languagegen
|
||||
PUBLIC
|
||||
${DAWN_BUILD_HOST_LIBS}
|
||||
)
|
152
archive/tools/tools/languagegen/LanguageGen.cpp
Normal file
152
archive/tools/tools/languagegen/LanguageGen.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "LanguageGen.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> LanguageParser::getRequiredAttributes() {
|
||||
return std::vector<std::string>{ "key" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> LanguageParser::getOptionalAttributes() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t LanguageParser::onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct LanguageString *out,
|
||||
std::string *error
|
||||
) {
|
||||
out->key = values["key"];
|
||||
out->text = node->value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::vector<std::string> LanguageGroupParser::getRequiredAttributes() {
|
||||
return std::vector<std::string>{ "key" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> LanguageGroupParser::getOptionalAttributes() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t LanguageGroupParser::onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct LanguageGroup *out,
|
||||
std::string *error
|
||||
) {
|
||||
std::string key = values["key"];
|
||||
out->key += key;
|
||||
|
||||
auto it = node->children.begin();
|
||||
int32_t ret;
|
||||
while(it != node->children.end()) {
|
||||
auto c = *it;
|
||||
if(c->node == "string") {
|
||||
struct LanguageString string;
|
||||
ret = (LanguageParser()).parse(c, &string, error);
|
||||
if(ret != 0) return ret;
|
||||
|
||||
string.lang = out->lang;
|
||||
string.key = out->key + "." + string.key;
|
||||
out->strings.push_back(string);
|
||||
|
||||
} else if(c->node == "group") {
|
||||
struct LanguageGroup group;
|
||||
group.key += key + ".";
|
||||
group.lang = out->lang;
|
||||
|
||||
ret = (LanguageGroupParser()).parse(c, &group, error);
|
||||
if(ret != 0) return ret;
|
||||
vectorAppend(&out->strings, group.strings);
|
||||
|
||||
}
|
||||
++it;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::vector<std::string> LanguageRootParser::getRequiredAttributes() {
|
||||
return std::vector<std::string>{ "lang" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> LanguageRootParser::getOptionalAttributes() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t LanguageRootParser::onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct LanguageRoot *out,
|
||||
std::string *error
|
||||
) {
|
||||
int32_t ret;
|
||||
out->lang = values["lang"];
|
||||
|
||||
auto it = node->children.begin();
|
||||
while(it != node->children.end()) {
|
||||
auto c = *it;
|
||||
if(c->node == "string") {
|
||||
struct LanguageString string;
|
||||
ret = (LanguageParser()).parse(c, &string, error);
|
||||
if(ret != 0) return ret;
|
||||
string.lang = out->lang;
|
||||
out->strings.push_back(string);
|
||||
|
||||
} else if(c->node == "group") {
|
||||
struct LanguageGroup group;
|
||||
group.lang = out->lang;
|
||||
ret = (LanguageGroupParser()).parse(c, &group, error);
|
||||
if(ret != 0) return ret;
|
||||
vectorAppend(&out->strings, group.strings);
|
||||
|
||||
}
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::vector<std::string> LanguageGen::getRequiredFlags() {
|
||||
return std::vector<std::string>{ "input", "output" };
|
||||
}
|
||||
|
||||
int32_t LanguageGen::start() {
|
||||
auto fileIn = File(flags["input"]);
|
||||
std::string buffer;
|
||||
if(!fileIn.readString(&buffer)) {
|
||||
std::cout << "Failed to open/read input file " << fileIn.filename << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
auto xml = Xml::load(buffer);
|
||||
std::string error;
|
||||
struct LanguageRoot root;
|
||||
|
||||
auto ret = (LanguageRootParser()).parse(&xml, &root, &error);
|
||||
if(ret != 0) {
|
||||
std::cout << error << std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Now dump out the language strings to be picked up later.
|
||||
ret = languageSaveStrings(flags["output"], root.strings);
|
||||
if(ret != 0) return ret;
|
||||
|
||||
return 0;
|
||||
}
|
67
archive/tools/tools/languagegen/LanguageGen.hpp
Normal file
67
archive/tools/tools/languagegen/LanguageGen.hpp
Normal file
@ -0,0 +1,67 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "util/DawnTool.hpp"
|
||||
#include "util/File.hpp"
|
||||
#include "util/XmlParser.hpp"
|
||||
#include "util/Language.cpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct LanguageGroup {
|
||||
std::vector<struct LanguageString> strings;
|
||||
std::string key;
|
||||
std::string lang;
|
||||
};
|
||||
|
||||
struct LanguageRoot {
|
||||
std::string lang;
|
||||
std::vector<struct LanguageString> strings;
|
||||
};
|
||||
|
||||
class LanguageParser : public XmlParser<struct LanguageString> {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredAttributes() override;
|
||||
std::map<std::string, std::string> getOptionalAttributes() override;
|
||||
int32_t onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct LanguageString *out,
|
||||
std::string *error
|
||||
) override;
|
||||
};
|
||||
|
||||
class LanguageGroupParser : public XmlParser<struct LanguageGroup> {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredAttributes() override;
|
||||
std::map<std::string, std::string> getOptionalAttributes() override;
|
||||
int32_t onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct LanguageGroup *out,
|
||||
std::string *error
|
||||
) override;
|
||||
};
|
||||
|
||||
class LanguageRootParser : public XmlParser<struct LanguageRoot> {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredAttributes() override;
|
||||
std::map<std::string, std::string> getOptionalAttributes() override;
|
||||
int32_t onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct LanguageRoot *out,
|
||||
std::string *error
|
||||
) override;
|
||||
};
|
||||
|
||||
class LanguageGen : public DawnTool {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredFlags() override;
|
||||
|
||||
public:
|
||||
int32_t start() override;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user