Skip to content

Commit 12cde71

Browse files
committed
repo: improve debugging & extent it to gmod
Related to #27
1 parent 010330f commit 12cde71

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

source/lua.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ void Lua::Shutdown()
9393
void Lua::FinalShutdown()
9494
{
9595
g_Lua = NULL;
96+
97+
for (auto& ref : Util::g_pReference)
98+
{
99+
if (Util::holylib_debug_mainutil.GetBool())
100+
Msg("holylib: Discarding of old reference %i\n", ref);
101+
}
102+
103+
Util::g_pReference.clear();
96104
}
97105

98106
void Lua::ManualShutdown()

source/symbols.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ namespace Symbols
106106
NULL_SIGNATURE, // ToDo
107107
};
108108

109+
const std::vector<Symbol> CLuaInterface_ReferenceFreeSym = {
110+
Symbol::FromName("_ZN13CLuaInterface13ReferenceFreeEi"),
111+
NULL_SIGNATURE, // ToDo
112+
};
113+
114+
const std::vector<Symbol> CLuaInterface_ReferenceCreateSym = {
115+
Symbol::FromName("_ZN13CLuaInterface15ReferenceCreateEv"),
116+
NULL_SIGNATURE, // ToDo
117+
};
118+
109119
//---------------------------------------------------------------------------------
110120
// Purpose: gameevent Symbols
111121
//---------------------------------------------------------------------------------

source/symbols.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ namespace Symbols
152152
typedef void (GMCOMMON_CALLING_CONVENTION* CBaseEntity_SetMoveType)(void* ent, int, int);
153153
extern const std::vector<Symbol> CBaseEntity_SetMoveTypeSym;
154154

155+
typedef int (GMCOMMON_CALLING_CONVENTION* CLuaInterface_ReferenceCreate)(void* lua);
156+
extern const std::vector<Symbol> CLuaInterface_ReferenceCreateSym;
157+
158+
typedef void (GMCOMMON_CALLING_CONVENTION* CLuaInterface_ReferenceFree)(void* lua, int ref);
159+
extern const std::vector<Symbol> CLuaInterface_ReferenceFreeSym;
160+
155161
//---------------------------------------------------------------------------------
156162
// Purpose: gameevent Symbols
157163
//---------------------------------------------------------------------------------

source/util.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,46 @@ static void hook_CSteam3Server_NotifyClientDisconnect(void* pServer, CBaseClient
197197
detour_CSteam3Server_NotifyClientDisconnect.GetTrampoline<Symbols::CSteam3Server_NotifyClientDisconnect>()(pServer, pClient);
198198
}
199199

200+
bool Util::bOurReferenceCall = false;
201+
static std::unordered_set<int> g_pGmodReference;
202+
static Detouring::Hook detour_CLuaInterface_ReferenceCreate;
203+
static int hook_CLuaInterface_ReferenceCreate(void* lua)
204+
{
205+
int ref = detour_CLuaInterface_ReferenceCreate.GetTrampoline<Symbols::CLuaInterface_ReferenceCreate>()(lua);
206+
207+
if (Util::holylib_debug_mainutil.GetBool())
208+
Msg("holylib: Created reference %i\n", ref);
209+
210+
if (Util::bOurReferenceCall)
211+
{
212+
auto it = Util::g_pReference.find(ref);
213+
if (it != Util::g_pReference.end())
214+
{
215+
Error("holylib - gmd: Created a reference when we already holded it. How. Crash this shit.\n"); // If this happens maybe gmod does some weird shit?
216+
}
217+
}
218+
219+
return ref;
220+
}
221+
222+
static Detouring::Hook detour_CLuaInterface_ReferenceFree;
223+
static void hook_CLuaInterface_ReferenceFree(void* lua, int ref)
224+
{
225+
if (Util::holylib_debug_mainutil.GetBool())
226+
Msg("holylib - gmod: Freed reference %i\n", ref);
227+
228+
if (Util::bOurReferenceCall)
229+
{
230+
auto it = Util::g_pReference.find(ref);
231+
if (it == Util::g_pReference.end())
232+
{
233+
Error("holylib - gmod: Freed a reference when we didn't holded it. How. Crash this shit.\n"); // If this happens I'm happy.
234+
}
235+
}
236+
237+
detour_CLuaInterface_ReferenceFree.GetTrampoline<Symbols::CLuaInterface_ReferenceFree>()(lua, ref);
238+
}
239+
200240
IGet* Util::get;
201241
CBaseEntityList* g_pEntityList = NULL;
202242
Symbols::lua_rawseti Util::func_lua_rawseti;
@@ -270,6 +310,18 @@ void Util::AddDetour()
270310
func_lua_rawgeti = (Symbols::lua_rawgeti)Detour::GetFunction(lua_shared_loader.GetModule(), Symbols::lua_rawgetiSym);
271311
Detour::CheckFunction((void*)func_lua_rawgeti, "lua_rawgeti");
272312

313+
Detour::Create(
314+
&detour_CLuaInterface_ReferenceCreate, "CLuaInterface::ReferenceCreate",
315+
lua_shared_loader.GetModule(), Symbols::CLuaInterface_ReferenceCreateSym,
316+
(void*)hook_CLuaInterface_ReferenceCreate, 0
317+
);
318+
319+
Detour::Create(
320+
&detour_CLuaInterface_ReferenceFree, "CLuaInterface::ReferenceFree",
321+
lua_shared_loader.GetModule(), Symbols::CLuaInterface_ReferenceFreeSym,
322+
(void*)hook_CLuaInterface_ReferenceFree, 0
323+
);
324+
273325
pEntityList = g_pModuleManager.FindModuleByName("entitylist");
274326

275327
/*

source/util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ namespace Util
7373
/*
7474
* Pure debugging
7575
*/
76+
extern bool bOurReferenceCall;
7677
extern std::unordered_set<int> g_pReference;
7778
extern ConVar holylib_debug_mainutil;
7879
inline int ReferenceCreate(const char* reason)
7980
{
81+
bOurReferenceCall = true;
8082
int iReference = g_Lua->ReferenceCreate();
83+
bOurReferenceCall = false;
8184

8285
if (holylib_debug_mainutil.GetBool())
8386
Msg("holylib: Created reference %i (%s)\n", iReference, reason);
@@ -105,7 +108,9 @@ namespace Util
105108
}
106109

107110
g_pReference.erase(it);
111+
bOurReferenceCall = true;
108112
g_Lua->ReferenceFree(iReference);
113+
bOurReferenceCall = false;
109114
}
110115

111116
inline void StartTable() {

0 commit comments

Comments
 (0)