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/Plugin.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 10 deletions(-) (limited to 'src/Plugin.cpp') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 41c690c..9641e0f 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -4,6 +4,11 @@ #include #include +#include + +#include "GameState.hpp" +#include "Game.hpp" + struct Plugin { const std::string name; @@ -11,6 +16,7 @@ struct Plugin { const std::function onLoad; const std::function onUnload; const std::function onChangeState; + const std::function onTick; }; @@ -20,13 +26,14 @@ sol::state lua; namespace PluginApi { - void RegisterPlugin(sol::table self, sol::table plugin) { + void RegisterPlugin(sol::table plugin) { Plugin nativePlugin { - plugin["name"].get_or("75"), + plugin["name"].get_or(""), plugin["displayName"].get_or(""), plugin["onLoad"].get_or(std::function()), plugin["onUnload"].get_or(std::function()), - plugin["onChangeState"].get_or(std::function()) + plugin["onChangeState"].get_or(std::function()), + plugin["onTick"].get_or(std::function()) }; plugins.push_back(nativePlugin); nativePlugin.onLoad(); @@ -34,14 +41,25 @@ namespace PluginApi { LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); } - void LogWarning(sol::table self, std::string text) { + void LogWarning(std::string text) { LOG(WARNING) << text; } + void LogInfo(std::string text) { + LOG(INFO) << text; + } + + void LogError(std::string text) { + LOG(ERROR) << text; + } + + GameState *GetGameState() { + return ::GetGameState(); + } } -void PluginSystem::Init() -{ +void PluginSystem::Init() { + OPTICK_EVENT(); LOG(INFO) << "Initializing plugin system"; for (Plugin &plugin : plugins) { if (plugin.onUnload) @@ -52,23 +70,47 @@ void PluginSystem::Init() lua = sol::state(); lua.open_libraries(); + lua.new_usertype("Entity", + "pos", &Entity::pos); + + lua.new_usertype("GameState", + "GetPlayer", &GameState::GetPlayer, + "GetWorld", &GameState::GetWorld); + + lua.new_usertype("World"); + + lua.new_usertype("Vector", + "x", &Vector::x, + "y", &Vector::y, + "z", &Vector::z); + + lua.new_usertype("VectorF", + "x", &VectorF::x, + "y", &VectorF::y, + "z", &VectorF::z); + sol::table apiTable = lua["AC"].get_or_create(); apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; apiTable["LogWarning"] = PluginApi::LogWarning; + apiTable["LogInfo"] = PluginApi::LogInfo; + apiTable["LogError"] = PluginApi::LogError; + apiTable["GetGameState"] = PluginApi::GetGameState; } -void PluginSystem::Execute(const std::string &luaCode) -{ +void PluginSystem::Execute(const std::string &luaCode, bool except) { + OPTICK_EVENT(); try { lua.safe_script(luaCode); } catch (sol::error &e) { LOG(ERROR) << e.what(); + if (except) + throw; } } -void PluginSystem::CallOnChangeState(std::string newState) -{ +void PluginSystem::CallOnChangeState(std::string newState) { + OPTICK_EVENT(); for (Plugin &plugin : plugins) { if (plugin.onChangeState) try { @@ -79,3 +121,16 @@ void PluginSystem::CallOnChangeState(std::string newState) } } } + +void PluginSystem::CallOnTick(double deltaTime) { + OPTICK_EVENT(); + for (Plugin& plugin : plugins) { + if (plugin.onTick) + try { + plugin.onTick(deltaTime); + } + catch (sol::error &e) { + LOG(ERROR) << e.what(); + } + } +} -- cgit v1.2.3