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 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/AssetManager.cpp') 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; -- cgit v1.2.3 From 404b04570452fe24676b0294fdd77878806a904b Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 17 Feb 2019 13:11:08 +0500 Subject: Minor lua-api improvement --- src/AssetManager.cpp | 53 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 1ae9929..361e2ef 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -103,34 +103,55 @@ void LoadTextures() { void LoadScripts() { lua.open_libraries(sol::lib::base, sol::lib::table); + lua["AC"] = lua.create_table(); + lua["plugins"] = lua.create_table(); + lua["AC"]["RegisterPlugin"].set_function([&](sol::table &self, sol::table &plugin) { + std::string pluginName; + try { + pluginName = plugin["name"]; + lua["plugins"][pluginName] = plugin; + LOG(INFO) << "Loading plugin " << (lua["plugins"][pluginName]["displayName"] ? lua["plugins"][pluginName]["displayName"] : pluginName); + if (lua["plugins"][pluginName]["onLoad"]) + lua["plugins"][pluginName]["onLoad"].call(lua["plugins"][pluginName]); + } catch (sol::error &e) { + if (pluginName.empty()) + return; + + LOG(ERROR) << "Plugin " << pluginName << " loading failed: " << e.what(); + lua["plugins"][pluginName] = sol::lua_nil; + } + }); - LOG(INFO) << "Loading lua-init-scripts"; + LOG(INFO) << "Loading Lua..."; 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; + if (child->name == "scripts") { + for (auto &script : child->childs) + { + AssetScript *asset = dynamic_cast(script->asset.get()); + if (!asset) { + LOG(ERROR) << "Unrecognised script file /" << it->name; + continue; + } + try { + lua.script(asset->code); + } + catch (sol::error &e) { + LOG(ERROR) << "LUA script " << it->name << "/" << child->name << "/" << script->name << " parsing failed: " << e.what(); + failedScripts.push_back(it->name); + continue; + } + loadedScripts.push_back(it->name); } - loadedScripts.push_back(it->name); } } } - LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); + LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); } void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { -- cgit v1.2.3 From a2fd708de4ede7427589125e680f3fb339926f4e Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 17 Feb 2019 21:24:52 +0500 Subject: Refactored lua-api --- src/AssetManager.cpp | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 361e2ef..66e7767 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -11,9 +11,9 @@ #define STB_IMAGE_IMPLEMENTATION #define STBI_ONLY_PNG #include -#include #include "Utility.hpp" +#include "Plugin.hpp" namespace fs = std::experimental::filesystem::v1; @@ -25,7 +25,6 @@ std::map blockIdToBlockName; std::unique_ptr assetTree; std::unique_ptr atlas; std::map blockIdToBlockFaces; -sol::state lua; void LoadIds(); void LoadAssets(); @@ -62,6 +61,8 @@ void AssetManager::InitAssetManager() LoadIds(); ParseBlockModels(); + + PluginSystem::Init(); LoadScripts(); } @@ -102,30 +103,6 @@ void LoadTextures() { } void LoadScripts() { - lua.open_libraries(sol::lib::base, sol::lib::table); - lua["AC"] = lua.create_table(); - lua["plugins"] = lua.create_table(); - lua["AC"]["RegisterPlugin"].set_function([&](sol::table &self, sol::table &plugin) { - std::string pluginName; - try { - pluginName = plugin["name"]; - lua["plugins"][pluginName] = plugin; - LOG(INFO) << "Loading plugin " << (lua["plugins"][pluginName]["displayName"] ? lua["plugins"][pluginName]["displayName"] : pluginName); - if (lua["plugins"][pluginName]["onLoad"]) - lua["plugins"][pluginName]["onLoad"].call(lua["plugins"][pluginName]); - } catch (sol::error &e) { - if (pluginName.empty()) - return; - - LOG(ERROR) << "Plugin " << pluginName << " loading failed: " << e.what(); - lua["plugins"][pluginName] = sol::lua_nil; - } - }); - - LOG(INFO) << "Loading Lua..."; - std::vector loadedScripts; - std::vector failedScripts; - AssetTreeNode *node = AssetManager::GetAssetByAssetName("/"); for (auto &it : node->childs) { for (auto &child : it->childs) { @@ -137,21 +114,12 @@ void LoadScripts() { LOG(ERROR) << "Unrecognised script file /" << it->name; continue; } - try { - lua.script(asset->code); - } - catch (sol::error &e) { - LOG(ERROR) << "LUA script " << it->name << "/" << child->name << "/" << script->name << " parsing failed: " << e.what(); - failedScripts.push_back(it->name); - continue; - } - loadedScripts.push_back(it->name); + PluginSystem::Execute(asset->code); } } } } - - LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); + LOG(INFO) << "Scripts loaded"; } void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { -- cgit v1.2.3 From 868ba6279a20e4d1412c2d576c67400167de6694 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Tue, 30 Apr 2019 16:12:35 +0500 Subject: Integrated Optick profiler --- src/AssetManager.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 66e7767..ba2b4f4 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" #include "Plugin.hpp" @@ -661,6 +662,7 @@ std::string AssetManager::GetAssetNameByBlockId(BlockId block) { } Asset *AssetManager::GetAssetPtr(const std::string & assetName) { + OPTICK_EVENT(); AssetTreeNode *node; if (assetName[0] != '/') node = GetAssetByAssetName('/' + assetName); -- cgit v1.2.3 From 8c5320a94b4c91f2801c05766f6a1747de42a2e5 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 18 May 2019 18:12:56 +0500 Subject: Implemented more scripting APIs --- src/AssetManager.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index ba2b4f4..eb3186a 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -115,7 +115,12 @@ void LoadScripts() { LOG(ERROR) << "Unrecognised script file /" << it->name; continue; } - PluginSystem::Execute(asset->code); + try { + PluginSystem::Execute(asset->code, true); + } + catch (std::exception& e) { + LOG(ERROR) << "Failed loading script '" << script->name << "' in '" << it->name << "'"; + } } } } -- cgit v1.2.3 From 35e786c2b4632f92518c8881db650ba63beecd5c Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 15:25:03 +0500 Subject: Implemented lua's "require" for AM --- src/AssetManager.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index eb3186a..be69dd0 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -110,6 +110,9 @@ void LoadScripts() { if (child->name == "scripts") { for (auto &script : child->childs) { + if (script->name != "init") + continue; + AssetScript *asset = dynamic_cast(script->asset.get()); if (!asset) { LOG(ERROR) << "Unrecognised script file /" << it->name; @@ -118,9 +121,11 @@ void LoadScripts() { try { PluginSystem::Execute(asset->code, true); } - catch (std::exception& e) { + catch (std::exception & e) { LOG(ERROR) << "Failed loading script '" << script->name << "' in '" << it->name << "'"; } + + break; } } } @@ -171,7 +176,7 @@ void ParseAsset(AssetTreeNode &node) { return; } - if (node.name == "init") { + if (node.parent->name == "scripts") { ParseAssetScript(node); return; } -- cgit v1.2.3 From 646f77ec6bc27af231b6ff8974e631b86188beb6 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 23:03:48 +0500 Subject: Implemented block-api --- src/AssetManager.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index be69dd0..19cd452 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -613,16 +613,16 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { return blockIdToBlockFaces.find(block)->second; } - auto blockStateName = TransformBlockIdToBlockStateName(block); - AssetBlockState *asset = GetAsset("/minecraft/blockstates/" + blockStateName.first); + BlockInfo blockInfo = GetBlockInfo(block); + AssetBlockState *asset = GetAsset("/minecraft/blockstates/" + blockInfo.blockstate); if (!asset) return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockState &blockState = asset->blockState; - if (blockState.variants.find(blockStateName.second) == blockState.variants.end()) + if (blockState.variants.find(blockInfo.variant) == blockState.variants.end()) return GetBlockModelByBlockId(BlockId{ 7788,0 }); - BlockStateVariant &variant = blockState.variants[blockStateName.second]; + BlockStateVariant &variant = blockState.variants[blockInfo.variant]; if (variant.models.empty()) return GetBlockModelByBlockId(BlockId{ 7788,0 }); -- cgit v1.2.3