#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(); } } }