Skip to content

Commit

Permalink
Added LuaThreaded.LockMain
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelIT7 committed May 17, 2024
1 parent 7810e76 commit 9edddba
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
28 changes: 28 additions & 0 deletions source/library_umsg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "lua_threaded.h"

LUA_FUNCTION(umsg_Start)
{
return 0;
}

void InitUmsg(GarrysMod::Lua::ILuaInterface* LUA)
{
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->CreateTable();
Add_Func(LUA, umsg_Start, "Start");
Add_Func(LUA, umsg_Start, "Short");
Add_Func(LUA, umsg_Start, "PoolString");
Add_Func(LUA, umsg_Start, "Long");
Add_Func(LUA, umsg_Start, "Float");
Add_Func(LUA, umsg_Start, "Entity");
Add_Func(LUA, umsg_Start, "End");
Add_Func(LUA, umsg_Start, "Char");
Add_Func(LUA, umsg_Start, "Bool");
Add_Func(LUA, umsg_Start, "Angle");
Add_Func(LUA, umsg_Start, "String");
Add_Func(LUA, umsg_Start, "Vector");
Add_Func(LUA, umsg_Start, "VectorNormal");

LUA->SetField(-2, "umsg");
LUA->Pop();
}
3 changes: 3 additions & 0 deletions source/library_umsg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <GarrysMod/Lua/LuaInterface.h>

extern void InitUmsg(GarrysMod::Lua::ILuaInterface*);
33 changes: 33 additions & 0 deletions source/lua_LuaThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,37 @@ LUA_FUNCTION(LuaThread_ReadyThreads)
return 1;
}

LUA_FUNCTION(LuaThread_LockMain)
{
GMOD->request_lock = true;

while (!GMOD->is_locked) { ThreadSleep(1); };

return 0;
}

LUA_FUNCTION(LuaThread_UnlockMain)
{
GMOD->request_lock = false;

return 0;
}

LUA_FUNCTION(LuaThread_Think)
{
if (GMOD->request_lock)
{
GMOD->is_locked = true;
while (GMOD->request_lock)
{
ThreadSleep(1);
}
GMOD->is_locked = false;
}

return 0;
}

void InitLuaThreaded(GarrysMod::Lua::ILuaInterface* LUA, int id)
{
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
Expand All @@ -228,6 +259,8 @@ void InitLuaThreaded(GarrysMod::Lua::ILuaInterface* LUA, int id)
Add_Func(LUA, LuaThread_GetValue, "GetValue");
Add_Func(LUA, LuaThread_IsMainThread, "IsMainThread");
Add_Func(LUA, LuaThread_ReadyThreads, "ReadyThreads");
Add_Func(LUA, LuaThread_LockMain, "LockMain");
Add_Func(LUA, LuaThread_UnlockMain, "UnlockMain");

LUA->SetField(-2, "LuaThreaded");
LUA->Pop();
Expand Down
1 change: 1 addition & 0 deletions source/lua_LuaThread.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <GarrysMod/Lua/LuaInterface.h>

extern int LuaThread_Think(lua_State*);
extern void InitLuaThreaded(GarrysMod::Lua::ILuaInterface*, int = 0);
15 changes: 15 additions & 0 deletions source/lua_threaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ GMOD_MODULE_OPEN()
}
LUA->Pop(2);

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "hook");
if (LUA->IsType(-1, GarrysMod::Lua::Type::Table)) {
LUA->GetField(-1, "Add");
if (LUA->IsType(-1, GarrysMod::Lua::Type::Function)) {
LUA->PushString("Think");
LUA->PushString("LuaThreaded");
LUA->PushCFunction(LuaThread_Think);
LLUA->CallFunctionProtected(3, 0, true);
} else {
LUA->Pop();
}
}
LUA->Pop(2);

// NOTE: We need to wait until InitPostEntity is called because a bunch of stuff is missing which cause a crash.
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "hook");
Expand Down
7 changes: 7 additions & 0 deletions source/lua_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ struct ILuaThread

// File library
std::vector<IAsyncFile*> async;

// Umsg Library
bf_write* umsg_buffer;
};

struct GMOD_Info
Expand All @@ -225,6 +228,10 @@ struct GMOD_Info
ILuaValue* usercontent;

const char* active_gamemode;

// Locking
bool request_lock;
bool is_locked;
};

inline float Lerp(float delta, float from, float to)
Expand Down

0 comments on commit 9edddba

Please sign in to comment.