mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-03 00:05:34 +00:00
lua Register
This commit is contained in:
parent
950cee9fd0
commit
b1caf5c29a
107
src/TLuaFile.cpp
107
src/TLuaFile.cpp
@ -6,7 +6,7 @@
|
||||
#include "TLuaEngine.h"
|
||||
#include "TNetwork.h"
|
||||
#include "TServer.h"
|
||||
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
|
||||
@ -625,7 +625,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 <const size_t _UniqueId, typename Res, typename... ArgTypes>
|
||||
struct fun_ptr_helper
|
||||
{
|
||||
public:
|
||||
typedef std::function<Res(ArgTypes...)> 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 <const size_t _UniqueId, typename _Res, typename... _ArgTypes>
|
||||
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<int(lua_State*)>([=](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) {
|
||||
auto Lock = std::unique_lock(mInitMutex);
|
||||
@ -724,6 +808,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);
|
||||
@ -802,24 +887,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()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user