Prefab Generator Refactor
This commit is contained in:
61
src/dawntools/scenetool/CMakeLists.txt
Normal file
61
src/dawntools/scenetool/CMakeLists.txt
Normal file
@@ -0,0 +1,61 @@
|
||||
# Copyright (c) 2023 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Texture Build Tool
|
||||
project(scenetool VERSION 1.0)
|
||||
add_executable(scenetool)
|
||||
|
||||
# Subdirs
|
||||
|
||||
# Sources
|
||||
target_sources(scenetool
|
||||
PRIVATE
|
||||
${DAWN_SHARED_SOURCES}
|
||||
${DAWN_TOOL_SOURCES}
|
||||
SceneTool.cpp
|
||||
SceneParser.cpp
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(scenetool
|
||||
PUBLIC
|
||||
${DAWN_SHARED_INCLUDES}
|
||||
${DAWN_TOOL_INCLUDES}
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Definitions
|
||||
target_compile_definitions(scenetool
|
||||
PUBLIC
|
||||
${DAWN_SHARED_DEFINITIONS}
|
||||
DAWN_TOOL_INSTANCE=SceneTool
|
||||
DAWN_TOOL_HEADER="SceneTool.hpp"
|
||||
)
|
||||
|
||||
# Libraries
|
||||
target_link_libraries(scenetool
|
||||
PUBLIC
|
||||
${DAWN_BUILD_HOST_LIBS}
|
||||
)
|
||||
|
||||
# Tool Function
|
||||
function(tool_scene in)
|
||||
set(DEPS "")
|
||||
if(DAWN_BUILD_TOOLS)
|
||||
set(DEPS scenetool)
|
||||
endif()
|
||||
|
||||
STRING(REGEX REPLACE "[\.|\\|\/]" "-" scene_name ${in})
|
||||
add_custom_target(scene_${scene_name}
|
||||
COMMAND scenetool --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_GENERATED_DIR}/generatedscenes" --sources="${DAWN_SOURCES_DIR}"
|
||||
COMMENT "Generating prefab from ${in}"
|
||||
DEPENDS ${DEPS}
|
||||
)
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${DAWN_GENERATED_DIR}/generatedscenes
|
||||
)
|
||||
add_dependencies(${DAWN_TARGET_NAME} scene_${scene_name})
|
||||
endfunction()
|
||||
49
src/dawntools/scenetool/SceneParser.cpp
Normal file
49
src/dawntools/scenetool/SceneParser.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SceneParser.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> SceneParser::getRequiredAttributes() {
|
||||
return { "name" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> SceneParser::getOptionalAttributes() {
|
||||
return {};
|
||||
}
|
||||
|
||||
int32_t SceneParser::onParse(
|
||||
Xml *node,
|
||||
std::map<std::string, std::string> values,
|
||||
struct Scene *out,
|
||||
std::string *error
|
||||
) {
|
||||
int32_t ret;
|
||||
|
||||
//Create the scene item
|
||||
out->name = values["name"];
|
||||
|
||||
//Parse the children
|
||||
auto itChildren = node->children.begin();
|
||||
while(itChildren != node->children.end()) {
|
||||
Xml *child = *itChildren;
|
||||
|
||||
if(child->node == "item") {
|
||||
struct SceneItem item;
|
||||
item.registry = out->registry;
|
||||
ret = (SceneItemParser()).parse(child, &item, error);
|
||||
if(ret != 0) return 1;
|
||||
out->items.push_back(item);
|
||||
} else {
|
||||
*error = "Unknown node type " + child->node;
|
||||
return 1;
|
||||
}
|
||||
|
||||
++itChildren;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
27
src/dawntools/scenetool/SceneParser.hpp
Normal file
27
src/dawntools/scenetool/SceneParser.hpp
Normal file
@@ -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 "util/parser/SceneItemParser.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct Scene {
|
||||
std::string name;
|
||||
std::vector<struct SceneItem> items;
|
||||
struct SceneItemComponentRegistry *registry;
|
||||
};
|
||||
|
||||
class SceneParser : public XmlParser<struct Scene> {
|
||||
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 Scene *out,
|
||||
std::string *error
|
||||
) override;
|
||||
};
|
||||
}
|
||||
71
src/dawntools/scenetool/SceneTool.cpp
Normal file
71
src/dawntools/scenetool/SceneTool.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SceneTool.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> SceneTool::getRequiredFlags() {
|
||||
return { "input", "output", "sources" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> SceneTool::getOptionalFlags() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t SceneTool::start() {
|
||||
File input = File(flags["input"]);
|
||||
if(!input.exists()) {
|
||||
std::cout << "Input file 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;
|
||||
}
|
||||
|
||||
auto xml = Xml::load(data);
|
||||
std::string error;
|
||||
|
||||
struct Scene scene;
|
||||
struct SceneItemComponentRegistry registry;
|
||||
registry.sources = File::normalizeSlashes(flags["sources"]);
|
||||
scene.registry = ®istry;
|
||||
|
||||
auto result = ((SceneParser()).parse(&xml, &scene, &error));
|
||||
if(result != 0) {
|
||||
std::cout << "Failed to parse scene " << input.filename << "::" << error << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// Generate output
|
||||
std::vector<std::string> outputData;
|
||||
// VNSceneGen::generate(&outputData, &scene, "");
|
||||
|
||||
// Load output file from name and type
|
||||
File outputFile = File(flags["output"] + "/scenes/TestScene.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! " << input.filename << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
src/dawntools/scenetool/SceneTool.hpp
Normal file
21
src/dawntools/scenetool/SceneTool.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
// 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/Xml.hpp"
|
||||
#include "SceneParser.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class SceneTool : 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