From 12dd50ef7778219eaba918253ee29cc67404655b Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 6 Feb 2023 22:43:39 -0800 Subject: [PATCH] New tools refactor first pass --- src/dawntools/audio/audiogen/CMakeLists.txt | 2 +- src/dawntools/audio/audiogen/main.cpp | 2 +- .../display/texturegen/CMakeLists.txt | 4 +- src/dawntools/display/texturegen/main.cpp | 4 +- .../display/tilesetgen/CMakeLists.txt | 4 +- src/dawntools/display/tilesetgen/main.cpp | 4 +- .../display/truetypegen/CMakeLists.txt | 4 +- src/dawntools/display/truetypegen/main.cpp | 4 +- src/dawntools/display/uigen/CMakeLists.txt | 4 +- src/dawntools/display/uigen/main.cpp | 4 +- .../locale/languagegen/CMakeLists.txt | 9 +- .../languagegen/{main.cpp => LanguageGen.cpp} | 156 ++++++++---------- .../locale/languagegen/LanguageGen.hpp | 34 ++++ src/dawntools/util/DawnTool.cpp | 14 ++ src/dawntools/util/DawnTool.hpp | 27 +++ src/dawntools/{utils => util}/csv.cpp | 0 src/dawntools/{utils => util}/csv.hpp | 0 src/dawntools/{utils => util}/file.cpp | 4 + src/dawntools/{utils => util}/file.hpp | 4 +- src/dawntools/{utils => util}/image.cpp | 0 src/dawntools/{utils => util}/image.hpp | 0 src/dawntools/{utils => util}/string.hpp | 0 src/dawntools/{utils => util}/xml.cpp | 0 src/dawntools/{utils => util}/xml.hpp | 0 .../visualnovel/vnscenegen/CMakeLists.txt | 4 +- src/dawntools/visualnovel/vnscenegen/main.cpp | 4 +- 26 files changed, 181 insertions(+), 111 deletions(-) rename src/dawntools/locale/languagegen/{main.cpp => LanguageGen.cpp} (78%) create mode 100644 src/dawntools/locale/languagegen/LanguageGen.hpp create mode 100644 src/dawntools/util/DawnTool.cpp create mode 100644 src/dawntools/util/DawnTool.hpp rename src/dawntools/{utils => util}/csv.cpp (100%) rename src/dawntools/{utils => util}/csv.hpp (100%) rename src/dawntools/{utils => util}/file.cpp (93%) rename src/dawntools/{utils => util}/file.hpp (93%) rename src/dawntools/{utils => util}/image.cpp (100%) rename src/dawntools/{utils => util}/image.hpp (100%) rename src/dawntools/{utils => util}/string.hpp (100%) rename src/dawntools/{utils => util}/xml.cpp (100%) rename src/dawntools/{utils => util}/xml.hpp (100%) diff --git a/src/dawntools/audio/audiogen/CMakeLists.txt b/src/dawntools/audio/audiogen/CMakeLists.txt index 058a7e09..df35f8ba 100644 --- a/src/dawntools/audio/audiogen/CMakeLists.txt +++ b/src/dawntools/audio/audiogen/CMakeLists.txt @@ -9,7 +9,7 @@ add_executable(audiogen) target_sources(audiogen PRIVATE main.cpp - ../../utils/file.cpp + ../../util/file.cpp ) target_include_directories(audiogen PUBLIC diff --git a/src/dawntools/audio/audiogen/main.cpp b/src/dawntools/audio/audiogen/main.cpp index aa9eaaef..b6aebfb9 100644 --- a/src/dawntools/audio/audiogen/main.cpp +++ b/src/dawntools/audio/audiogen/main.cpp @@ -4,7 +4,7 @@ // https://opensource.org/licenses/MIT #include "dawnsharedlibs.hpp" -#include "../../utils/file.hpp" +#include "../../util/file.hpp" #include "AudioFile.h" int main(int argc, char *argv[]) { diff --git a/src/dawntools/display/texturegen/CMakeLists.txt b/src/dawntools/display/texturegen/CMakeLists.txt index 9b984bee..276b572a 100644 --- a/src/dawntools/display/texturegen/CMakeLists.txt +++ b/src/dawntools/display/texturegen/CMakeLists.txt @@ -9,8 +9,8 @@ add_executable(texturegen) target_sources(texturegen PRIVATE main.cpp - ../../utils/file.cpp - ../../utils/image.cpp + ../../util/file.cpp + ../../util/image.cpp ) target_include_directories(texturegen PUBLIC diff --git a/src/dawntools/display/texturegen/main.cpp b/src/dawntools/display/texturegen/main.cpp index 2ba4c120..e8fc8cfc 100644 --- a/src/dawntools/display/texturegen/main.cpp +++ b/src/dawntools/display/texturegen/main.cpp @@ -6,8 +6,8 @@ */ #include "dawnsharedlibs.hpp" -#include "../../utils/file.hpp" -#include "../../utils/image.hpp" +#include "../../util/file.hpp" +#include "../../util/image.hpp" int main(int argc, char *argv[]) { FILE *file; diff --git a/src/dawntools/display/tilesetgen/CMakeLists.txt b/src/dawntools/display/tilesetgen/CMakeLists.txt index 68cdbdd6..7b1c26e0 100644 --- a/src/dawntools/display/tilesetgen/CMakeLists.txt +++ b/src/dawntools/display/tilesetgen/CMakeLists.txt @@ -9,8 +9,8 @@ add_executable(tilesetgen) target_sources(tilesetgen PRIVATE main.cpp - ../../utils/file.cpp - ../../utils/image.cpp + ../../util/file.cpp + ../../util/image.cpp ) target_include_directories(tilesetgen PUBLIC diff --git a/src/dawntools/display/tilesetgen/main.cpp b/src/dawntools/display/tilesetgen/main.cpp index 9fa18d80..351d4d1f 100644 --- a/src/dawntools/display/tilesetgen/main.cpp +++ b/src/dawntools/display/tilesetgen/main.cpp @@ -6,8 +6,8 @@ */ #include "dawnsharedlibs.hpp" -#include "../../utils/file.hpp" -#include "../../utils/image.hpp" +#include "../../util/file.hpp" +#include "../../util/image.hpp" int main(int argc, char *argv[]) { char *in; diff --git a/src/dawntools/display/truetypegen/CMakeLists.txt b/src/dawntools/display/truetypegen/CMakeLists.txt index cd2c7d92..082844ff 100644 --- a/src/dawntools/display/truetypegen/CMakeLists.txt +++ b/src/dawntools/display/truetypegen/CMakeLists.txt @@ -9,8 +9,8 @@ add_executable(truetypegen) target_sources(truetypegen PRIVATE main.cpp - ../../utils/file.cpp - ../../utils/image.cpp + ../../util/file.cpp + ../../util/image.cpp ) target_include_directories(truetypegen PUBLIC diff --git a/src/dawntools/display/truetypegen/main.cpp b/src/dawntools/display/truetypegen/main.cpp index 5923d4b1..af177b4f 100644 --- a/src/dawntools/display/truetypegen/main.cpp +++ b/src/dawntools/display/truetypegen/main.cpp @@ -6,8 +6,8 @@ */ #include "dawnsharedlibs.hpp" -#include "../../utils/file.hpp" -#include "../../utils/image.hpp" +#include "../../util/file.hpp" +#include "../../util/image.hpp" #ifndef STB_TRUETYPE_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION #include diff --git a/src/dawntools/display/uigen/CMakeLists.txt b/src/dawntools/display/uigen/CMakeLists.txt index d2504dc0..3fcd0242 100644 --- a/src/dawntools/display/uigen/CMakeLists.txt +++ b/src/dawntools/display/uigen/CMakeLists.txt @@ -9,8 +9,8 @@ add_executable(uigen) target_sources(uigen PRIVATE main.cpp - ../../utils/file.cpp - ../../utils/xml.cpp + ../../util/file.cpp + ../../util/xml.cpp ) target_include_directories(uigen PUBLIC diff --git a/src/dawntools/display/uigen/main.cpp b/src/dawntools/display/uigen/main.cpp index 77959320..d49e783a 100644 --- a/src/dawntools/display/uigen/main.cpp +++ b/src/dawntools/display/uigen/main.cpp @@ -5,8 +5,8 @@ * https://opensource.org/licenses/MIT */ -#include "../../utils/xml.hpp" -#include "../../utils/file.hpp" +#include "../../util/xml.hpp" +#include "../../util/file.hpp" #include #include diff --git a/src/dawntools/locale/languagegen/CMakeLists.txt b/src/dawntools/locale/languagegen/CMakeLists.txt index 253d344c..1589d8e0 100644 --- a/src/dawntools/locale/languagegen/CMakeLists.txt +++ b/src/dawntools/locale/languagegen/CMakeLists.txt @@ -8,10 +8,11 @@ project(languagegen VERSION 2.0) add_executable(languagegen) target_sources(languagegen PRIVATE - main.cpp - ../../utils/file.cpp - ../../utils/csv.cpp - ../../utils/xml.cpp + LanguageGen.cpp + ../../util/DawnTool.cpp + ../../util/file.cpp + ../../util/csv.cpp + ../../util/xml.cpp ) target_include_directories(languagegen PUBLIC diff --git a/src/dawntools/locale/languagegen/main.cpp b/src/dawntools/locale/languagegen/LanguageGen.cpp similarity index 78% rename from src/dawntools/locale/languagegen/main.cpp rename to src/dawntools/locale/languagegen/LanguageGen.cpp index b5fa6c48..9b44ec96 100644 --- a/src/dawntools/locale/languagegen/main.cpp +++ b/src/dawntools/locale/languagegen/LanguageGen.cpp @@ -5,85 +5,23 @@ * https://opensource.org/licenses/MIT */ -#include "dawnsharedlibs.hpp" -#include "../../utils/file.hpp" -#include "../../utils/csv.hpp" -#include "../../utils/xml.hpp" -#include -#include -#include -#include -#include +#include "LanguageGen.hpp" -struct LanguageString { - std::string key; - std::string value; -}; +using namespace Dawn; -int32_t parseString( - xml_t *stringNode, - std::string key, - std::map> *strings -) { - auto attrLang = xmlGetAttributeByName(stringNode, "lang"); - if(attrLang == -1) { - std::cout << "String is missing lang parameter." << std::endl; - return -1; - } - - std::string lang(stringNode->attributeDatas[attrLang]); - struct LanguageString str; - str.key = key; - str.value = std::string(stringNode->value); - - auto existing = (*strings).find(lang); - if(existing == (*strings).end()) { - (*strings).insert(std::make_pair(lang, std::vector())); - } - (*strings)[lang].push_back(str); - return 0; +LanguageGen::LanguageGen(const int argc, const char *argv[]) : + DawnTool(argc, argv) +{ } -int32_t parseGroup( - xml_t *groupNode, - std::string key, - std::map> *strings -) { - int32_t ret; - - auto attrKey = xmlGetAttributeByName(groupNode, "key"); - if(attrKey == -1) { - std::cout << "Group node is missing key" << std::endl; - return 1; - } - - if(key.size() > 0) key += "."; - key += std::string(groupNode->attributeDatas[attrKey]); - - for(int32_t i = 0; i < groupNode->childrenCount; i++) { - auto c = groupNode->children + i; - if(std::string(c->node) == "string") { - ret = parseString(c, key, strings); - if(ret != 0) return ret; - } else if(std::string(c->node) == "group") { - ret = parseGroup(c, key, strings); - if(ret != 0) return ret; - } - } - - return 0; -} - -int main(int argc, char *argv[]) { - if(argc != 3) { +int32_t LanguageGen::start() { + if(this->args.size() != 3) { std::cout << "Invalid number of arguments provided to language gen!" << std::endl; return 1; } - char *fileInName = argv[1]; - fileNormalizeSlashes(fileInName); - - FILE *fileIn = fopen(fileInName, "rb"); + auto fileInName = fileNormalizeSlashesNew(this->args[1]); + FILE *fileIn = fopen(fileInName.c_str(), "rb"); if(fileIn == NULL) { std::cout << "Failed to open input file " << fileInName << std::endl; return 1; @@ -124,7 +62,7 @@ int main(int argc, char *argv[]) { for(int32_t i = 0; i < xml.childrenCount; i++) { auto c = xml.children + i; if(std::string(c->node) == "group") { - auto ret = parseGroup(c, "", &strings); + auto ret = this->parseGroup(c, "", &strings); if(ret != 0) { xmlDispose(&xml); return ret; @@ -170,20 +108,12 @@ int main(int argc, char *argv[]) { it2++; } - std::string filenameOut(argv[2]); + std::string filenameOut = this->args[2]; filenameOut += "/language_" + it->first + ".language"; + filenameOut = fileNormalizeSlashesNew(filenameOut); - char *filenameOutC = (char *)malloc(sizeof(char) * (filenameOut.size() + 1)); - if(filenameOutC == NULL) { - std::cout << "Failed to allocate filename memory" << std::endl; - return 1; - } - strcpy(filenameOutC, filenameOut.c_str()); - fileNormalizeSlashes(filenameOutC); - - fileMkdirp(filenameOutC); - FILE *fileOut = fopen(filenameOutC, "wb"); - free(filenameOutC); + fileMkdirp((char *)filenameOut.c_str()); + FILE *fileOut = fopen(filenameOut.c_str(), "wb"); if(fileOut == NULL) { std::cout << "Failed to create output file " << filenameOut << std::endl; return 1; @@ -211,4 +141,62 @@ int main(int argc, char *argv[]) { } return 0; +} + +int32_t LanguageGen::parseString( + xml_t *stringNode, + std::string key, + std::map> *strings +) { + auto attrLang = xmlGetAttributeByName(stringNode, "lang"); + if(attrLang == -1) { + std::cout << "String is missing lang parameter." << std::endl; + return -1; + } + + std::string lang(stringNode->attributeDatas[attrLang]); + struct LanguageString str; + str.key = key; + str.value = std::string(stringNode->value); + + auto existing = (*strings).find(lang); + if(existing == (*strings).end()) { + (*strings).insert(std::make_pair(lang, std::vector())); + } + (*strings)[lang].push_back(str); + return 0; +} + +int32_t LanguageGen::parseGroup( + xml_t *groupNode, + std::string key, + std::map> *strings +) { + int32_t ret; + + auto attrKey = xmlGetAttributeByName(groupNode, "key"); + if(attrKey == -1) { + std::cout << "Group node is missing key" << std::endl; + return 1; + } + + if(key.size() > 0) key += "."; + key += std::string(groupNode->attributeDatas[attrKey]); + + for(int32_t i = 0; i < groupNode->childrenCount; i++) { + auto c = groupNode->children + i; + if(std::string(c->node) == "string") { + ret = this->parseString(c, key, strings); + if(ret != 0) return ret; + } else if(std::string(c->node) == "group") { + ret = this->parseGroup(c, key, strings); + if(ret != 0) return ret; + } + } + + return 0; +} + +int main(const int argc, const char *argv[]) { + return (LanguageGen(argc, argv)).start(); } \ No newline at end of file diff --git a/src/dawntools/locale/languagegen/LanguageGen.hpp b/src/dawntools/locale/languagegen/LanguageGen.hpp new file mode 100644 index 00000000..d0331471 --- /dev/null +++ b/src/dawntools/locale/languagegen/LanguageGen.hpp @@ -0,0 +1,34 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "util/DawnTool.hpp" + +namespace Dawn { + struct LanguageString { + std::string key; + std::string value; + }; + + class LanguageGen : public DawnTool { + protected: + int32_t parseGroup( + xml_t *node, + std::string key, + std::map> *strings + ); + + int32_t parseString( + xml_t *node, + std::string key, + std::map> *strings + ); + + public: + LanguageGen(const int argc, const char *argv[]); + + int32_t start() override; + }; +} \ No newline at end of file diff --git a/src/dawntools/util/DawnTool.cpp b/src/dawntools/util/DawnTool.cpp new file mode 100644 index 00000000..31415177 --- /dev/null +++ b/src/dawntools/util/DawnTool.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "DawnTool.hpp" + +using namespace Dawn; + +DawnTool::DawnTool(const int argc, const char *argv[]) { + for(int32_t i = 0; i < argc; i++) { + this->args.push_back(std::string(argv[i])); + } +} \ No newline at end of file diff --git a/src/dawntools/util/DawnTool.hpp b/src/dawntools/util/DawnTool.hpp new file mode 100644 index 00000000..f58025c8 --- /dev/null +++ b/src/dawntools/util/DawnTool.hpp @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnsharedlibs.hpp" +#include "../../util/file.hpp" +#include "../../util/csv.hpp" +#include "../../util/xml.hpp" +#include +#include +#include +#include +#include + +namespace Dawn { + class DawnTool { + protected: + std::vector args; + + public: + DawnTool(const int argc, const char *argv[]); + + virtual int32_t start() = 0; + }; +} \ No newline at end of file diff --git a/src/dawntools/utils/csv.cpp b/src/dawntools/util/csv.cpp similarity index 100% rename from src/dawntools/utils/csv.cpp rename to src/dawntools/util/csv.cpp diff --git a/src/dawntools/utils/csv.hpp b/src/dawntools/util/csv.hpp similarity index 100% rename from src/dawntools/utils/csv.hpp rename to src/dawntools/util/csv.hpp diff --git a/src/dawntools/utils/file.cpp b/src/dawntools/util/file.cpp similarity index 93% rename from src/dawntools/utils/file.cpp rename to src/dawntools/util/file.cpp index 984a0cea..6c98383f 100644 --- a/src/dawntools/utils/file.cpp +++ b/src/dawntools/util/file.cpp @@ -192,4 +192,8 @@ bool fileListChildren( *count = i; return true; #endif +} + +std::string fileNormalizeSlashesNew(std::string str) { + return str; } \ No newline at end of file diff --git a/src/dawntools/utils/file.hpp b/src/dawntools/util/file.hpp similarity index 93% rename from src/dawntools/utils/file.hpp rename to src/dawntools/util/file.hpp index 67e21157..8430ea58 100644 --- a/src/dawntools/utils/file.hpp +++ b/src/dawntools/util/file.hpp @@ -72,4 +72,6 @@ int32_t readAhead( int32_t skipAhead( char *bufferIn, int32_t start, char *needles, int32_t needleCount -); \ No newline at end of file +); + +std::string fileNormalizeSlashesNew(std::string str); \ No newline at end of file diff --git a/src/dawntools/utils/image.cpp b/src/dawntools/util/image.cpp similarity index 100% rename from src/dawntools/utils/image.cpp rename to src/dawntools/util/image.cpp diff --git a/src/dawntools/utils/image.hpp b/src/dawntools/util/image.hpp similarity index 100% rename from src/dawntools/utils/image.hpp rename to src/dawntools/util/image.hpp diff --git a/src/dawntools/utils/string.hpp b/src/dawntools/util/string.hpp similarity index 100% rename from src/dawntools/utils/string.hpp rename to src/dawntools/util/string.hpp diff --git a/src/dawntools/utils/xml.cpp b/src/dawntools/util/xml.cpp similarity index 100% rename from src/dawntools/utils/xml.cpp rename to src/dawntools/util/xml.cpp diff --git a/src/dawntools/utils/xml.hpp b/src/dawntools/util/xml.hpp similarity index 100% rename from src/dawntools/utils/xml.hpp rename to src/dawntools/util/xml.hpp diff --git a/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt b/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt index 53f96ddf..f102ff0a 100644 --- a/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt +++ b/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt @@ -9,8 +9,8 @@ add_executable(vnscenegen) target_sources(vnscenegen PRIVATE main.cpp - ../../utils/file.cpp - ../../utils/xml.cpp + ../../util/file.cpp + ../../util/xml.cpp ) target_include_directories(vnscenegen PUBLIC diff --git a/src/dawntools/visualnovel/vnscenegen/main.cpp b/src/dawntools/visualnovel/vnscenegen/main.cpp index 66e06a69..0efe69fb 100644 --- a/src/dawntools/visualnovel/vnscenegen/main.cpp +++ b/src/dawntools/visualnovel/vnscenegen/main.cpp @@ -3,8 +3,8 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -#include "../../utils/file.hpp" -#include "../../utils/xml.hpp" +#include "../../util/file.hpp" +#include "../../util/xml.hpp" #include #include