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