Moved C++ tools out
This commit is contained in:
60
archive/dawntools/prefabtool/CMakeLists.txt
Normal file
60
archive/dawntools/prefabtool/CMakeLists.txt
Normal file
@ -0,0 +1,60 @@
|
||||
# Copyright (c) 2023 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Texture Build Tool
|
||||
project(prefabtool VERSION 1.0)
|
||||
add_executable(prefabtool)
|
||||
|
||||
# Sources
|
||||
target_sources(prefabtool
|
||||
PRIVATE
|
||||
${DAWN_SHARED_SOURCES}
|
||||
${DAWN_TOOL_SOURCES}
|
||||
PrefabTool.cpp
|
||||
PrefabGen.cpp
|
||||
PrefabParser.cpp
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(prefabtool
|
||||
PUBLIC
|
||||
${DAWN_SHARED_INCLUDES}
|
||||
${DAWN_TOOL_INCLUDES}
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Definitions
|
||||
target_compile_definitions(prefabtool
|
||||
PUBLIC
|
||||
${DAWN_SHARED_DEFINITIONS}
|
||||
DAWN_TOOL_INSTANCE=PrefabTool
|
||||
DAWN_TOOL_HEADER="PrefabTool.hpp"
|
||||
)
|
||||
|
||||
# Libraries
|
||||
target_link_libraries(prefabtool
|
||||
PUBLIC
|
||||
${DAWN_BUILD_HOST_LIBS}
|
||||
)
|
||||
|
||||
# Tool Function
|
||||
function(tool_prefab in)
|
||||
set(DEPS "")
|
||||
if(DAWN_BUILD_TOOLS)
|
||||
set(DEPS prefabtool)
|
||||
endif()
|
||||
|
||||
STRING(REGEX REPLACE "[\.|\\|\/|\:]" "-" prefab_name ${in})
|
||||
add_custom_target(prefab_${prefab_name}
|
||||
COMMAND prefabtool --input="${in}" --output="${DAWN_GENERATED_DIR}/generatedprefabs" --sources="${DAWN_SOURCES_DIR}"
|
||||
COMMENT "Generating prefab from ${in}"
|
||||
DEPENDS ${DEPS}
|
||||
)
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${DAWN_GENERATED_DIR}/generatedprefabs
|
||||
)
|
||||
add_dependencies(${DAWN_TARGET_NAME} prefab_${prefab_name})
|
||||
endfunction()
|
65
archive/dawntools/prefabtool/PrefabGen.cpp
Normal file
65
archive/dawntools/prefabtool/PrefabGen.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "PrefabGen.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void PrefabGen::generate(
|
||||
std::vector<std::string> *out,
|
||||
struct Prefab *info,
|
||||
std::string tabs
|
||||
) {
|
||||
struct ClassGenInfo classInfo;
|
||||
classInfo.clazz = info->name;
|
||||
classInfo.extend = "SceneItemPrefab<" + info->name + ">";
|
||||
classInfo.constructorArgs = "Scene *scene, sceneitemid_t id";
|
||||
classInfo.extendArgs = "scene, id";
|
||||
|
||||
struct MethodGenInfo methodAssets;
|
||||
methodAssets.name = "prefabAssets";
|
||||
methodAssets.isStatic = true;
|
||||
methodAssets.type = "std::vector<Asset*>";
|
||||
methodAssets.args = "AssetManager *man";
|
||||
line(&methodAssets.body, "std::vector<Asset*> assets;", "");
|
||||
|
||||
struct MethodGenInfo methodInit;
|
||||
methodInit.name = "prefabInit";
|
||||
methodInit.isOverride = true;
|
||||
methodInit.args = "AssetManager *man";
|
||||
|
||||
classInfo.includes.push_back("prefab/SceneItemPrefab.hpp");
|
||||
|
||||
// Generate
|
||||
int32_t assetNumber = 0;
|
||||
int32_t childNumber = 0;
|
||||
int32_t componentNumber = 0;
|
||||
std::map<std::string, std::string> assetMap;
|
||||
info->root.ref = "this";
|
||||
SceneItemGenerator::generate(
|
||||
assetNumber,
|
||||
componentNumber,
|
||||
childNumber,
|
||||
assetMap,
|
||||
classInfo.includes,
|
||||
&classInfo.publicProperties,
|
||||
&methodInit.body,
|
||||
&methodAssets.body,
|
||||
"",
|
||||
"scene",
|
||||
&info->root,
|
||||
""
|
||||
);
|
||||
|
||||
// Seal methods
|
||||
line(&methodAssets.body, "return assets;", "");
|
||||
|
||||
// Add in methods
|
||||
CodeGen::methodGen(&classInfo.publicCode, methodAssets);
|
||||
line(&classInfo.publicCode, "", "");
|
||||
CodeGen::methodGen(&classInfo.publicCode, methodInit);
|
||||
|
||||
CodeGen::classGen(out, classInfo);
|
||||
}
|
19
archive/dawntools/prefabtool/PrefabGen.hpp
Normal file
19
archive/dawntools/prefabtool/PrefabGen.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "PrefabParser.hpp"
|
||||
#include "util/generator/SceneItemGenerator.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class PrefabGen : public CodeGen {
|
||||
public:
|
||||
static void generate(
|
||||
std::vector<std::string> *out,
|
||||
struct Prefab *prefab,
|
||||
std::string tabs
|
||||
);
|
||||
};
|
||||
}
|
28
archive/dawntools/prefabtool/PrefabParser.cpp
Normal file
28
archive/dawntools/prefabtool/PrefabParser.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "PrefabParser.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> PrefabParser::getRequiredAttributes() {
|
||||
return { "name", "type" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> PrefabParser::getOptionalAttributes() {
|
||||
return { };
|
||||
}
|
||||
|
||||
int32_t PrefabParser::onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct Prefab *out,
|
||||
std::string *error
|
||||
) {
|
||||
out->name = values["name"];
|
||||
out->type = values["type"];
|
||||
out->root.registry = out->registry;
|
||||
return (SceneItemParser()).parse(node, &out->root, error);
|
||||
}
|
29
archive/dawntools/prefabtool/PrefabParser.hpp
Normal file
29
archive/dawntools/prefabtool/PrefabParser.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "util/parser/SceneItemComponentParser.hpp"
|
||||
#include "util/parser/SceneItemParser.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct Prefab {
|
||||
struct SceneItemComponentRegistry *registry;
|
||||
struct SceneItem root;
|
||||
std::string name;
|
||||
std::string type;
|
||||
};
|
||||
|
||||
class PrefabParser : public XmlParser<struct Prefab> {
|
||||
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 Prefab *out,
|
||||
std::string *error
|
||||
) override;
|
||||
};
|
||||
}
|
69
archive/dawntools/prefabtool/PrefabTool.cpp
Normal file
69
archive/dawntools/prefabtool/PrefabTool.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "PrefabTool.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> PrefabTool::getRequiredFlags() {
|
||||
return { "input", "output", "sources" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> PrefabTool::getOptionalFlags() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t PrefabTool::start() {
|
||||
File input = File(flags["input"]);
|
||||
if(!input.exists()) {
|
||||
std::cout << "Input file " + input.filename + " does not exist!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string data;
|
||||
if(!input.readString(&data)) {
|
||||
std::cout << "Failed to read input file!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct SceneItemComponentRegistry registry;
|
||||
registry.sources = File::normalizeSlashes(flags["sources"]);
|
||||
|
||||
auto xml = Xml::load(data);
|
||||
std::string error;
|
||||
struct Prefab prefab;
|
||||
prefab.registry = ®istry;
|
||||
auto result = ((PrefabParser()).parse(&xml, &prefab, &error));
|
||||
if(result != 0) {
|
||||
std::cout << "Failed to parse prefab: " << error << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Generate output
|
||||
std::vector<std::string> outputData;
|
||||
PrefabGen::generate(&outputData, &prefab, "");
|
||||
|
||||
// Load output file from name and type
|
||||
File outputFile = File(flags["output"] + "/prefabs/" + prefab.type + "/" + prefab.name + ".hpp");
|
||||
if(!outputFile.mkdirp()) {
|
||||
std::cout << "Failed to create output directory!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Combine vector into single string
|
||||
std::string outputDataStr = "";
|
||||
auto it = outputData.begin();
|
||||
while(it != outputData.end()) {
|
||||
outputDataStr += *it + "\n";
|
||||
++it;
|
||||
}
|
||||
|
||||
if(!outputFile.writeString(outputDataStr)) {
|
||||
std::cout << "Failed to write output file!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
18
archive/dawntools/prefabtool/PrefabTool.hpp
Normal file
18
archive/dawntools/prefabtool/PrefabTool.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "PrefabGen.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class PrefabTool : public DawnTool {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredFlags() override;
|
||||
std::map<std::string, std::string> getOptionalFlags() override;
|
||||
|
||||
public:
|
||||
int32_t start();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user