diff --git a/src/dawntools/prefabtool/CMakeLists.txt b/src/dawntools/prefabtool/CMakeLists.txt index cfa6ebc6..08fc0a7e 100644 --- a/src/dawntools/prefabtool/CMakeLists.txt +++ b/src/dawntools/prefabtool/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(prefabtool PRIVATE ${DAWN_SHARED_SOURCES} ${DAWN_TOOL_SOURCES} + PrefabAssetParser.cpp PrefabTool.cpp PrefabGen.cpp PrefabChildParser.cpp diff --git a/src/dawntools/prefabtool/PrefabAssetParser.cpp b/src/dawntools/prefabtool/PrefabAssetParser.cpp new file mode 100644 index 00000000..55d94b35 --- /dev/null +++ b/src/dawntools/prefabtool/PrefabAssetParser.cpp @@ -0,0 +1,34 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "PrefabAssetParser.hpp" + +using namespace Dawn; + +std::vector PrefabAssetParser::getRequiredAttributes() { + return { "type", "name" }; +} + +std::map PrefabAssetParser::getOptionalAttributes() { + return { }; +} + +int32_t PrefabAssetParser::onParse( + Xml *node, + std::map values, + struct PrefabAsset *out, + std::string *error +) { + out->name = values["name"]; + + if(values["type"] == "texture") { + out->type = PREFAB_ASSET_TYPE_TEXTURE; + } else { + *error = "Unknown asset type '" + values["type"] + "'"; + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/src/dawntools/prefabtool/PrefabAssetParser.hpp b/src/dawntools/prefabtool/PrefabAssetParser.hpp new file mode 100644 index 00000000..c2c57e77 --- /dev/null +++ b/src/dawntools/prefabtool/PrefabAssetParser.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "util/XmlParser.hpp" + +namespace Dawn { + enum PrefabAssetType { + PREFAB_ASSET_TYPE_TEXTURE + }; + + struct PrefabAsset { + PrefabAssetType type; + std::string fileName; + std::string usageName; + }; + + class PrefabAssetParser : public XmlParser { + public: + virtual std::vector getRequiredAttributes(); + virtual std::map getOptionalAttributes(); + + virtual int32_t onParse( + Xml *node, + std::map values, + struct PrefabAsset *out, + std::string *error + ); + }; +} \ No newline at end of file diff --git a/src/dawntools/prefabtool/PrefabChildParser.cpp b/src/dawntools/prefabtool/PrefabChildParser.cpp index 0dc5ecb6..7f5c412e 100644 --- a/src/dawntools/prefabtool/PrefabChildParser.cpp +++ b/src/dawntools/prefabtool/PrefabChildParser.cpp @@ -47,6 +47,13 @@ int32_t PrefabChildParser::onParse( auto ret = (PrefabChildParser()).parse(c, &child, error); if(ret != 0) return ret; out->children.push_back(child); + + } else if(c->node == "asset") { + struct PrefabAsset asset; + auto ret = (PrefabAssetParser()).parse(c, &asset, error); + if(ret != 0) return ret; + out->assets.push_back(asset); + } else { struct PrefabComponent component; component.registry = out->registry; diff --git a/src/dawntools/prefabtool/PrefabChildParser.hpp b/src/dawntools/prefabtool/PrefabChildParser.hpp index e9309f78..c6f85790 100644 --- a/src/dawntools/prefabtool/PrefabChildParser.hpp +++ b/src/dawntools/prefabtool/PrefabChildParser.hpp @@ -5,6 +5,7 @@ #pragma once #include "PrefabComponentParser.hpp" +#include "PrefabAssetParser.hpp" namespace Dawn { struct PrefabChild { @@ -14,6 +15,7 @@ namespace Dawn { std::string scale; std::vector components; std::vector children; + std::vector assets; }; class PrefabChildParser : public XmlParser { diff --git a/src/dawntools/prefabtool/PrefabGen.cpp b/src/dawntools/prefabtool/PrefabGen.cpp index 5a79f7d3..39d9909a 100644 --- a/src/dawntools/prefabtool/PrefabGen.cpp +++ b/src/dawntools/prefabtool/PrefabGen.cpp @@ -32,6 +32,30 @@ void PrefabGen::generate( classInfo.includes.push_back("prefab/SceneItemPrefab.hpp"); + // Process assets + int32_t assetNumber = 0; + auto processAsset = [&](struct PrefabAsset a) { + std::string assetType = ""; + switch(a.type) { + case PREFAB_ASSET_TYPE_TEXTURE: + assetType = "TextureAsset"; + break; + + default: + assertUnreachable(); + } + a.usageName = "asset" + std::to_string(assetNumber++); + line(&methodInit.body, "auto " + a.usageName + " = man->get<" + assetType + ">(" + a.fileName + ");", ""); + }; + + // Load self assets + auto itAssets = info->root.assets.begin(); + while(itAssets != info->root.assets.end()) { + processAsset(*itAssets); + ++itAssets; + } + // TODO: Load child assets? + // Process root and all of its children int32_t childNumber = 0; int32_t componentNumber = 0;