@@ -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+
200240IGet* Util::get;
201241CBaseEntityList* g_pEntityList = NULL ;
202242Symbols::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 /*
0 commit comments