From 8a3d208ba81a7413607b0feb3e1414b03a3dde11 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 15 Feb 2019 22:24:04 +0500 Subject: Added lua-support --- src/AssetManager.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/AssetManager.hpp | 5 +++++ 2 files changed, 55 insertions(+) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 71800d7..1ae9929 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -11,6 +11,7 @@ #define STB_IMAGE_IMPLEMENTATION #define STBI_ONLY_PNG #include +#include #include "Utility.hpp" @@ -24,10 +25,12 @@ std::map blockIdToBlockName; std::unique_ptr assetTree; std::unique_ptr atlas; std::map blockIdToBlockFaces; +sol::state lua; void LoadIds(); void LoadAssets(); void LoadTextures(); +void LoadScripts(); void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node); void ParseAsset(AssetTreeNode &node); @@ -35,6 +38,7 @@ void ParseAssetTexture(AssetTreeNode &node); void ParseAssetBlockModel(AssetTreeNode &node); void ParseAssetBlockState(AssetTreeNode &node); void ParseAssetShader(AssetTreeNode &node); +void ParseAssetScript(AssetTreeNode &node); void ParseBlockModels(); @@ -58,6 +62,7 @@ void AssetManager::InitAssetManager() LoadIds(); ParseBlockModels(); + LoadScripts(); } void LoadIds() { @@ -96,6 +101,38 @@ void LoadTextures() { atlas = std::make_unique(textureData); } +void LoadScripts() { + lua.open_libraries(sol::lib::base, sol::lib::table); + + LOG(INFO) << "Loading lua-init-scripts"; + std::vector loadedScripts; + std::vector failedScripts; + + AssetTreeNode *node = AssetManager::GetAssetByAssetName("/"); + for (auto &it : node->childs) { + for (auto &child : it->childs) { + if (child->name == "init") { + AssetScript *asset = dynamic_cast(child->asset.get()); + if (!asset) { + LOG(ERROR) << "Unrecognised script file /" << it->name; + continue; + } + try { + lua.script(asset->code); + } + catch (sol::error &e) { + LOG(ERROR) << "LUA init-script " << child->name << " failed: " << e.what(); + failedScripts.push_back(it->name); + continue; + } + loadedScripts.push_back(it->name); + } + } + } + + LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); +} + void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { for (auto &file : fs::directory_iterator(dirEntry)) { node->childs.push_back(std::make_unique()); @@ -138,6 +175,11 @@ void ParseAsset(AssetTreeNode &node) { ParseAssetShader(node); return; } + + if (node.name == "init") { + ParseAssetScript(node); + return; + } } void ParseAssetTexture(AssetTreeNode &node) { @@ -380,6 +422,14 @@ void ParseAssetShader(AssetTreeNode &node) { } } +void ParseAssetScript(AssetTreeNode &node) { + node.asset = std::make_unique(); + AssetScript *asset = dynamic_cast(node.asset.get()); + asset->code = std::string((char*)node.data.data(), (char*)node.data.data() + node.data.size()); + node.data.clear(); + node.data.shrink_to_fit(); +} + void ParseBlockModels() { std::string textureName; diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index de3881e..97e1d63 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "Vector.hpp" #include "Block.hpp" @@ -167,6 +168,10 @@ struct AssetShader : Asset { std::unique_ptr shader; }; +struct AssetScript : Asset { + std::string code; +}; + namespace AssetManager { void InitAssetManager(); -- cgit v1.2.3