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 --- cwd/assets/altcraft/scripts/init.lua | 13 ++++-- src/AssetManager.cpp | 42 +++---------------- src/AssetManager.hpp | 1 - src/Plugin.cpp | 81 ++++++++++++++++++++++++++++++++++++ src/Plugin.hpp | 11 +++++ src/Render.cpp | 17 +++++++- 6 files changed, 122 insertions(+), 43 deletions(-) create mode 100644 src/Plugin.cpp create mode 100644 src/Plugin.hpp diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua index d48ecd0..5fd9e11 100644 --- a/cwd/assets/altcraft/scripts/init.lua +++ b/cwd/assets/altcraft/scripts/init.lua @@ -1,12 +1,17 @@ -plug = { +local plugin = { name = 'altcraft', displayName = "AltCraft Core Plugin", onLoad = nil, onUnload = nil, + onChangeState = nil, } -function plug:onLoad () - print("Loaded "..self.name.."-plugin!") +function plugin.onLoad () + print("Loaded AltCraft plugin!") end -AC:RegisterPlugin(plug) \ No newline at end of file +function plugin.onChangeState (newState) + AC:LogWarning("New state: "..newState) +end + +AC:RegisterPlugin(plugin) \ No newline at end of file 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) { diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 97e1d63..bf948b3 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include "Vector.hpp" #include "Block.hpp" diff --git a/src/Plugin.cpp b/src/Plugin.cpp new file mode 100644 index 0000000..40ff82f --- /dev/null +++ b/src/Plugin.cpp @@ -0,0 +1,81 @@ +#include "Plugin.hpp" + +#include + +#include +#include + +struct Plugin { + const std::string name; + const std::string displayName; + const std::function onLoad; + const std::function onUnload; + const std::function onChangeState; +}; + + +std::vector plugins; +sol::state lua; + + +namespace PluginApi { + + void RegisterPlugin(sol::table &self, sol::table &plugin) { + Plugin nativePlugin { + plugin["name"].get_or("75"), + plugin["displayName"].get_or(""), + plugin["onLoad"].get_or(std::function()), + plugin["onUnload"].get_or(std::function()), + plugin["onChangeState"].get_or(std::function()) + }; + plugins.push_back(nativePlugin); + nativePlugin.onLoad(); + + LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); + } + + void LogWarning(sol::table &self, std::string text) { + LOG(WARNING) << text; + } + +} + +void PluginSystem::Init() +{ + LOG(INFO) << "Initializing plugin system"; + for (Plugin &plugin : plugins) { + if (plugin.onUnload) + plugin.onUnload(); + } + + plugins.clear(); + lua = sol::state(); + lua.open_libraries(); + + sol::table apiTable = lua["AC"].get_or_create(); + + apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; + apiTable["LogWarning"] = PluginApi::LogWarning; +} + +void PluginSystem::Execute(const std::string &luaCode) +{ + try { + lua.safe_script(luaCode); + } catch (sol::error &e) { + LOG(ERROR) << e.what(); + } +} + +void PluginSystem::CallOnChangeState(std::string newState) +{ + for (Plugin &plugin : plugins) { + if (plugin.onChangeState) + try { + plugin.onChangeState(newState); + } + catch (sol::error &e) { + LOG(ERROR) << e.what(); + } + } +} diff --git a/src/Plugin.hpp b/src/Plugin.hpp new file mode 100644 index 0000000..a5f75e1 --- /dev/null +++ b/src/Plugin.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace PluginSystem { + void Init(); + + void Execute(const std::string &luaCode); + + void CallOnChangeState(std::string newState); +} \ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index cf108e4..f86e5ec 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -14,6 +14,7 @@ #include "RendererWorld.hpp" #include "Settings.hpp" #include "Framebuffer.hpp" +#include "Plugin.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) @@ -71,7 +72,9 @@ Render::~Render() { Settings::WriteDouble("brightness", fieldBrightness); Settings::WriteDouble("resolutionScale", fieldResolutionScale); Settings::Save(); - + + PluginSystem::Init(); + framebuffer.reset(); ImGui_ImplSdlGL3_Shutdown(); SDL_GL_DeleteContext(glContext); @@ -717,13 +720,25 @@ void Render::InitEvents() { switch (GlobalState::GetState()) { case State::Playing: SetMouseCapture(true); + PluginSystem::CallOnChangeState("Playing"); break; case State::InitialLoading: + PluginSystem::CallOnChangeState("InitialLoading"); + break; case State::MainMenu: + PluginSystem::CallOnChangeState("MainMenu"); + break; case State::Loading: + PluginSystem::CallOnChangeState("Loading"); + break; case State::Paused: + PluginSystem::CallOnChangeState("Paused"); + break; case State::Inventory: + PluginSystem::CallOnChangeState("Inventory"); + break; case State::Chat: + PluginSystem::CallOnChangeState("Chat"); SetMouseCapture(false); break; } -- cgit v1.2.3