This commit is contained in:
Lion Kortlepel
2021-06-25 00:48:52 +02:00
parent 80432eb718
commit 518cb0664e

View File

@@ -707,52 +707,44 @@ int lua_TempFix(lua_State* L) {
} }
template <const size_t _UniqueId, typename Res, typename... ArgTypes> template <const size_t _UniqueId, typename Res, typename... ArgTypes>
struct fun_ptr_helper struct fun_ptr_helper {
{
public: public:
typedef std::function<Res(ArgTypes...)> function_type; typedef std::function<Res(ArgTypes...)> function_type;
static void bind(function_type&& f) static void bind(function_type&& f) { instance().fn_.swap(f); }
{ instance().fn_.swap(f); }
static void bind(const function_type& f) static void bind(const function_type& f) { instance().fn_ = f; }
{ instance().fn_=f; }
static Res invoke(ArgTypes... args) static Res invoke(ArgTypes... args) { return instance().fn_(args...); }
{ return instance().fn_(args...); }
typedef decltype(&fun_ptr_helper::invoke) pointer_type; typedef decltype(&fun_ptr_helper::invoke) pointer_type;
static pointer_type ptr() static pointer_type ptr() { return &invoke; }
{ return &invoke; }
private: private:
static fun_ptr_helper& instance() static fun_ptr_helper& instance() {
{
static fun_ptr_helper inst_; static fun_ptr_helper inst_;
return inst_; return inst_;
} }
fun_ptr_helper() {} fun_ptr_helper() { }
function_type fn_; function_type fn_;
}; };
template <const size_t _UniqueId, typename _Res, typename... _ArgTypes> template <const size_t _UniqueId, typename _Res, typename... _ArgTypes>
typename fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::pointer_type typename fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::pointer_type
get_fn_ptr(const std::function<_Res(_ArgTypes...)>& f) get_fn_ptr(const std::function<_Res(_ArgTypes...)>& f) {
{
fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::bind(f); fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::bind(f);
return fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::ptr(); return fun_ptr_helper<_UniqueId, _Res, _ArgTypes...>::ptr();
} }
int lua_Register(lua_State* L) { int lua_Register(lua_State* L) {
if(lua_isstring(L, 1)){ if (lua_isstring(L, 1)) {
std::string Name(lua_tolstring(L, 1, nullptr)); std::string Name(lua_tolstring(L, 1, nullptr));
lua_getglobal(L, Name.c_str()); lua_getglobal(L, Name.c_str());
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
for (auto& Script : Engine().LuaFiles()) { for (auto& Script : Engine().LuaFiles()) {
if(Script->GetState() != L){ if (Script->GetState() != L) {
lua_CFunction Func = get_fn_ptr<0>(std::function<int(lua_State*)>([=](lua_State* A) { lua_CFunction Func = get_fn_ptr<0>(std::function<int(lua_State*)>([=](lua_State* A) {
lua_getglobal(L, Name.c_str()); lua_getglobal(L, Name.c_str());
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
@@ -860,6 +852,7 @@ int lua_GetOSName(lua_State* L) {
#else #else
lua_pushstring(L, "Unknown"); lua_pushstring(L, "Unknown");
#endif #endif
return 1;
} }
void TLuaFile::Load() { void TLuaFile::Load() {
@@ -884,7 +877,7 @@ void TLuaFile::Load() {
LuaTable::InsertFunction(mLuaState, "GetPlayerGuest", lua_GetGuest); LuaTable::InsertFunction(mLuaState, "GetPlayerGuest", lua_GetGuest);
LuaTable::InsertFunction(mLuaState, "StopThread", lua_StopThread); LuaTable::InsertFunction(mLuaState, "StopThread", lua_StopThread);
LuaTable::InsertFunction(mLuaState, "DropPlayer", lua_dropPlayer); LuaTable::InsertFunction(mLuaState, "DropPlayer", lua_dropPlayer);
lua_register(mLuaState, "Register", lua_Register); LuaTable::InsertFunction(mLuaState, "Register", lua_Register);
LuaTable::InsertFunction(mLuaState, "GetPlayerHWID", lua_HWID); LuaTable::InsertFunction(mLuaState, "GetPlayerHWID", lua_HWID);
LuaTable::InsertFunction(mLuaState, "Sleep", lua_Sleep); LuaTable::InsertFunction(mLuaState, "Sleep", lua_Sleep);
LuaTable::InsertFunction(mLuaState, "Set", lua_Set); LuaTable::InsertFunction(mLuaState, "Set", lua_Set);
@@ -967,7 +960,6 @@ void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
warn(a + (" | Incorrect Call of ") + msg); warn(a + (" | Incorrect Call of ") + msg);
} }
void TLuaArg::PushArgs(lua_State* State) { void TLuaArg::PushArgs(lua_State* State) {
for (std::any arg : args) { for (std::any arg : args) {
if (!arg.has_value()) { if (!arg.has_value()) {
@@ -992,4 +984,4 @@ void TLuaArg::PushArgs(lua_State* State) {
error("what in the hell is " + std::string(arg.type().name())); error("what in the hell is " + std::string(arg.type().name()));
} }
} }
} }