Skip to content

Commit 4d33d24

Browse files
committed
Free the ID properly
1 parent 31c281d commit 4d33d24

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# API
2+
This is the Lua API that is provided.
3+
4+
### Hooks
5+
All hooks call hook.Run. not gamemode.Call!
6+
7+
#### bool `PlayerQueue:OnSetSignonState`( number slot, number state, number spawncount )
8+
number slot - The Slot which you can use to call `PlayerQueue.SetSignOnState`.
9+
Its a number that counts up with each hook call and will become invalid if we don't return true.
10+
11+
number state - The SignOnState -> https://wiki.facepunch.com/gmod/Enums/SIGNONSTATE
12+
number spawncount - I have no idea :/
13+
14+
### Library functions ( PlayerQueue )
15+
16+
#### bool PlayerQueue.SetSignOnState( number slot, number state )
17+
number slot - The slot that `PlayerQueue:OnSetSignonState` provided.
18+
number state - The SignOnState we want to set.
19+
20+
returns:
21+
bool success - `true` If the SignOnState was successfully set

source/lua.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
IServer* Server;
1010
int iSlot = 0;
1111
std::unordered_map<int, IClient*> pClients;
12+
void FreeID(int playerSlot)
13+
{
14+
auto it = pClients.find(playerSlot);
15+
if ( it != pClients.end() )
16+
{
17+
pClients.erase(playerSlot);
18+
}
19+
}
20+
1221
bool Lua::Hooks::OnSetSignonState(IClient* cl, int state, int spawncount) // Return true to block it. You would need to block SIGNONSTATE_PRESPAWN to block it from spawning the player.
1322
{
1423
if (Lua::PushHook("PlayerQueue:OnSetSignonState"))
@@ -25,6 +34,9 @@ bool Lua::Hooks::OnSetSignonState(IClient* cl, int state, int spawncount) // Ret
2534
if ( g_Lua->GetType(-1) != GarrysMod::Lua::Type::Nil )
2635
ret = g_Lua->GetBool(-1);
2736

37+
if ( !ret )
38+
FreeID( iSlot );
39+
2840
g_Lua->Pop(1);
2941

3042
return ret;
@@ -45,6 +57,7 @@ LUA_FUNCTION_STATIC(SetSignOnState)
4557
LUA->PushBool(false);
4658
} else {
4759
Detours::Function::SetSignOnState(it->second, state, -1);
60+
FreeID(it->first);
4861
LUA->PushBool(true);
4962
}
5063

@@ -79,13 +92,12 @@ bool Lua::PushHook(const char* hook)
7992
return true;
8093
}
8194

82-
8395
void Lua::Init(GarrysMod::Lua::ILuaBase* LUA)
8496
{
8597
g_Lua = (GarrysMod::Lua::ILuaInterface*)LUA;
8698

8799
Start_Table();
88-
Add_Func(SetSignOnState, "SetSignOnState");
100+
Add_Func(SetSignOnState, "SetSignOnState"); // NOTE: After calling this, the ID the hook gave you cannot be used again!
89101
Finish_Table("PlayerQueue");
90102
Msg("Pushed PlayerQueue\n");
91103
}

0 commit comments

Comments
 (0)