diff --git a/src/TLuaFile.cpp b/src/TLuaFile.cpp index d6b1f33..ba1d7f1 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -5,7 +5,7 @@ #include "TLuaEngine.h" #include "TNetwork.h" #include "TServer.h" - +#include #include #include @@ -601,7 +601,91 @@ int lua_Print(lua_State* L) { } } -int lua_TempFix(lua_State* L); +int lua_TempFix(lua_State* L) { + if (lua_isnumber(L, 1)) { + int ID = int(lua_tonumber(L, 1)); + auto MaybeClient = GetClient(Engine().Server(), ID); + if (!MaybeClient || MaybeClient.value().expired()) + return 0; + std::string Ret; + auto c = MaybeClient.value().lock(); + if (c->IsGuest()) { + Ret = "Guest-" + c->GetName(); + } else + Ret = c->GetName(); + lua_pushstring(L, Ret.c_str()); + } else + SendError(Engine(), L, "GetDID not enough arguments"); + return 1; +} + +template +struct fun_ptr_helper +{ +public: + typedef std::function function_type; + + static void bind(function_type&& f) + { instance().fn_.swap(f); } + + static void bind(const function_type& f) + { instance().fn_=f; } + + static Res invoke(ArgTypes... args) + { return instance().fn_(args...); } + + typedef decltype(&fun_ptr_helper::invoke) pointer_type; + static pointer_type ptr() + { return &invoke; } + +private: + static fun_ptr_helper& instance() + { + static fun_ptr_helper inst_; + return inst_; + } + + fun_ptr_helper() {} + + function_type fn_; +}; + +template +typename fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::pointer_type +get_fn_ptr(const std::function<_Res(_ArgTypes...)>& f) +{ + fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::bind(f); + return fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::ptr(); +} + + +int lua_Register(lua_State* L) { + if(lua_isstring(L, 1)){ + std::string Name(lua_tolstring(L, 1, nullptr)); + lua_getglobal(L, Name.c_str()); + if (lua_isfunction(L, -1)) { + for (auto& Script : Engine().LuaFiles()) { + if(Script->GetState() != L){ + lua_CFunction Func = get_fn_ptr<0>(std::function([=](lua_State* A) { + lua_getglobal(L, Name.c_str()); + if (lua_isfunction(L, -1)) { + lua_pcall(L, 0, 0, 0); + } + return 0; + })); + lua_register(Script->GetState(), Name.c_str(), Func); + } + } + + } else { + SendError(Engine(), L, Name + " is not a global function!"); + ClearStack(L); + } + } else { + SendError(Engine(), L, "Register wrong arguments expected string"); + } + return 0; +} void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote) { // set global engine for lua_* functions @@ -700,6 +784,7 @@ void TLuaFile::Load() { lua_register(mLuaState, "StopThread", lua_StopThread); lua_register(mLuaState, "DropPlayer", lua_dropPlayer); lua_register(mLuaState, "GetPlayerHWID", lua_HWID); + lua_register(mLuaState, "Register", lua_Register); lua_register(mLuaState, "exit", lua_ServerExit); lua_register(mLuaState, "Sleep", lua_Sleep); lua_register(mLuaState, "print", lua_Print); @@ -778,24 +863,6 @@ void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) { warn(a + (" | Incorrect Call of ") + msg); } -int lua_TempFix(lua_State* L) { - if (lua_isnumber(L, 1)) { - int ID = int(lua_tonumber(L, 1)); - auto MaybeClient = GetClient(Engine().Server(), ID); - if (!MaybeClient || MaybeClient.value().expired()) - return 0; - std::string Ret; - auto c = MaybeClient.value().lock(); - if (c->IsGuest()) { - Ret = "Guest-" + c->GetName(); - } else - Ret = c->GetName(); - lua_pushstring(L, Ret.c_str()); - } else - SendError(Engine(), L, "GetDID not enough arguments"); - return 1; -} - void TLuaArg::PushArgs(lua_State* State) { for (std::any arg : args) { if (!arg.has_value()) {