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/Plugin.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/Plugin.cpp (limited to 'src/Plugin.cpp') 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(); + } + } +} -- cgit v1.2.3 From 93ab4aa89d8d81e5dd042201b75165f8db4968bd Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 18 Feb 2019 09:03:05 +0500 Subject: Trying to fix build in linux --- src/Plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Plugin.cpp') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 40ff82f..41c690c 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -20,7 +20,7 @@ sol::state lua; namespace PluginApi { - void RegisterPlugin(sol::table &self, sol::table &plugin) { + void RegisterPlugin(sol::table self, sol::table plugin) { Plugin nativePlugin { plugin["name"].get_or("75"), plugin["displayName"].get_or(""), @@ -34,7 +34,7 @@ namespace PluginApi { LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); } - void LogWarning(sol::table &self, std::string text) { + void LogWarning(sol::table self, std::string text) { LOG(WARNING) << text; } -- 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/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 From 1d965bebefe11f1fd5e34c686058fa932bb6b82e Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 13:20:44 +0500 Subject: Implemented GameState lua-api --- src/Plugin.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 6 deletions(-) (limited to 'src/Plugin.cpp') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 9641e0f..4ef1240 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -8,9 +8,11 @@ #include "GameState.hpp" #include "Game.hpp" +#include "Event.hpp" struct Plugin { + int errors; const std::string name; const std::string displayName; const std::function onLoad; @@ -28,6 +30,7 @@ namespace PluginApi { void RegisterPlugin(sol::table plugin) { Plugin nativePlugin { + 0, plugin["name"].get_or(""), plugin["displayName"].get_or(""), plugin["onLoad"].get_or(std::function()), @@ -62,7 +65,7 @@ void PluginSystem::Init() { OPTICK_EVENT(); LOG(INFO) << "Initializing plugin system"; for (Plugin &plugin : plugins) { - if (plugin.onUnload) + if (plugin.onUnload && plugin.errors < 10) plugin.onUnload(); } @@ -75,16 +78,70 @@ void PluginSystem::Init() { lua.new_usertype("GameState", "GetPlayer", &GameState::GetPlayer, - "GetWorld", &GameState::GetWorld); - - lua.new_usertype("World"); + "GetWorld", &GameState::GetWorld, + "GetTimeStatus", &GameState::GetTimeStatus, + "GetGameStatus", &GameState::GetGameStatus, + "GetPlayerStatus", &GameState::GetPlayerStatus, + "GetSelectionStatus", &GameState::GetSelectionStatus, + "GetInventory", &GameState::GetInventory); + + lua.new_usertype("TimeStatus", + "interpolatedTimeOfDay", &TimeStatus::interpolatedTimeOfDay, + "worldAge", &TimeStatus::worldAge, + "timeOfDay", &TimeStatus::timeOfDay, + "doDaylightCycle", &TimeStatus::doDaylightCycle); + + lua.new_usertype("GameStatus", + "levelType", &GameStatus::levelType, + "spawnPosition", &GameStatus::spawnPosition, + "gamemode", &GameStatus::gamemode, + "dimension", &GameStatus::dimension, + "difficulty", &GameStatus::difficulty, + "maxPlayers", &GameStatus::maxPlayers, + "isGameStarted", &GameStatus::isGameStarted, + "reducedDebugInfo", &GameStatus::reducedDebugInfo); + + lua.new_usertype("SelectionStatus", + "raycastHit", &SelectionStatus::raycastHit, + "selectedBlock", &SelectionStatus::selectedBlock, + "distanceToSelectedBlock", &SelectionStatus::distanceToSelectedBlock, + "isBlockSelected", &SelectionStatus::isBlockSelected); + + lua.new_usertype("PlayerStatus", + "uid", &PlayerStatus::uid, + "name", &PlayerStatus::name, + "flyingSpeed", &PlayerStatus::flyingSpeed, + "fovModifier", &PlayerStatus::fovModifier, + "health", &PlayerStatus::health, + "eid", &PlayerStatus::eid, + "invulnerable", &PlayerStatus::invulnerable, + "flying", &PlayerStatus::flying, + "allowFlying", &PlayerStatus::allowFlying, + "creativeMode", &PlayerStatus::creativeMode); + + lua.new_usertype("World", + "GetEntitiesList", &World::GetEntitiesList, + "GetEntity",&World::GetEntityPtr, + "Raycast", &World::Raycast, + "GetBlockId", &World::GetBlockId, + "SetBlockId", &World::SetBlockId); + + lua.new_usertype("BlockId", + "id", sol::property( + [](BlockId & bid) { return bid.id; }, + [](BlockId & bid, unsigned short id) { bid.id = id; }), + "state", sol::property( + [](BlockId & bid) { return bid.state; }, + [](BlockId & bid, unsigned char state) { bid.state = state; })); lua.new_usertype("Vector", + sol::constructors(), "x", &Vector::x, "y", &Vector::y, "z", &Vector::z); lua.new_usertype("VectorF", + sol::constructors(), "x", &VectorF::x, "y", &VectorF::y, "z", &VectorF::z); @@ -112,12 +169,13 @@ void PluginSystem::Execute(const std::string &luaCode, bool except) { void PluginSystem::CallOnChangeState(std::string newState) { OPTICK_EVENT(); for (Plugin &plugin : plugins) { - if (plugin.onChangeState) + if (plugin.onChangeState && plugin.errors < 10) try { plugin.onChangeState(newState); } catch (sol::error &e) { LOG(ERROR) << e.what(); + plugin.errors++; } } } @@ -125,12 +183,13 @@ void PluginSystem::CallOnChangeState(std::string newState) { void PluginSystem::CallOnTick(double deltaTime) { OPTICK_EVENT(); for (Plugin& plugin : plugins) { - if (plugin.onTick) + if (plugin.onTick && plugin.errors < 10) try { plugin.onTick(deltaTime); } catch (sol::error &e) { LOG(ERROR) << e.what(); + plugin.errors++; } } } -- 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/Plugin.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/Plugin.cpp') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 4ef1240..6acfb08 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -9,6 +9,7 @@ #include "GameState.hpp" #include "Game.hpp" #include "Event.hpp" +#include "AssetManager.hpp" struct Plugin { @@ -61,6 +62,24 @@ namespace PluginApi { } } +int LoadFileRequire(lua_State* L) { + std::string path = sol::stack::get(L); + + std::string package = path.substr(0, path.find('/')); + std::string script = path.substr(path.find('/') + 1); + + std::string scriptPath = "/" + package + "/scripts/" + script; + + AssetScript *asset = AssetManager::GetAsset(scriptPath); + if (!asset) { + sol::stack::push(L, "Module '" + scriptPath + "' not found"); + return 1; + } + + luaL_loadbuffer(L, asset->code.data(), asset->code.size(), path.c_str()); + return 1; +} + void PluginSystem::Init() { OPTICK_EVENT(); LOG(INFO) << "Initializing plugin system"; @@ -73,6 +92,10 @@ void PluginSystem::Init() { lua = sol::state(); lua.open_libraries(); + lua["package"]["searchers"] = lua.create_table_with( + 1, LoadFileRequire + ); + lua.new_usertype("Entity", "pos", &Entity::pos); -- 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/Plugin.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src/Plugin.cpp') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 6acfb08..8d2de94 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -20,6 +20,7 @@ struct Plugin { const std::function onUnload; const std::function onChangeState; const std::function onTick; + const std::function onRequestBlockInfo; }; @@ -37,7 +38,8 @@ namespace PluginApi { plugin["onLoad"].get_or(std::function()), plugin["onUnload"].get_or(std::function()), plugin["onChangeState"].get_or(std::function()), - plugin["onTick"].get_or(std::function()) + plugin["onTick"].get_or(std::function()), + plugin["onRequestBlockInfo"].get_or(std::function()), }; plugins.push_back(nativePlugin); nativePlugin.onLoad(); @@ -60,6 +62,14 @@ namespace PluginApi { GameState *GetGameState() { return ::GetGameState(); } + + void RegisterBlock(BlockId blockId, bool collides, std::string blockstate, std::string variant) { + RegisterStaticBlockInfo(blockId, BlockInfo{ + collides, + blockstate, + variant + }); + } } int LoadFileRequire(lua_State* L) { @@ -149,7 +159,16 @@ void PluginSystem::Init() { "GetBlockId", &World::GetBlockId, "SetBlockId", &World::SetBlockId); + auto bidFactory1 = []() { + return BlockId{ 0,0 }; + }; + auto bidFactory2 = [](unsigned short id, unsigned char state) { + return BlockId{ id,state }; + }; + lua.new_usertype("BlockId", + "new", sol::factories([]() {return BlockId{ 0,0 };}, + [](unsigned short id, unsigned char state) {return BlockId{ id, state };}), "id", sol::property( [](BlockId & bid) { return bid.id; }, [](BlockId & bid, unsigned short id) { bid.id = id; }), @@ -169,6 +188,11 @@ void PluginSystem::Init() { "y", &VectorF::y, "z", &VectorF::z); + lua.new_usertype("BlockInfo", + "collides", &BlockInfo::collides, + "blockstate", &BlockInfo::blockstate, + "variant", &BlockInfo::variant); + sol::table apiTable = lua["AC"].get_or_create(); apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; @@ -176,6 +200,7 @@ void PluginSystem::Init() { apiTable["LogInfo"] = PluginApi::LogInfo; apiTable["LogError"] = PluginApi::LogError; apiTable["GetGameState"] = PluginApi::GetGameState; + apiTable["RegisterBlock"] = PluginApi::RegisterBlock; } void PluginSystem::Execute(const std::string &luaCode, bool except) { @@ -216,3 +241,21 @@ void PluginSystem::CallOnTick(double deltaTime) { } } } + +BlockInfo PluginSystem::RequestBlockInfo(Vector blockPos) { + OPTICK_EVENT(); + BlockInfo ret; + for (Plugin& plugin : plugins) { + if (plugin.onRequestBlockInfo && plugin.errors < 10) + try { + ret = plugin.onRequestBlockInfo(blockPos); + if (!ret.blockstate.empty()) + break; + } + catch (sol::error & e) { + LOG(ERROR) << e.what(); + plugin.errors++; + } + } + return ret; +} -- cgit v1.2.3