From 41f84ae58491987d4bbef835985b087265f6863b Mon Sep 17 00:00:00 2001 From: RaphaelIT7 Date: Tue, 19 Nov 2024 18:54:01 +0100 Subject: [PATCH] physenv: testing a fix for another crash --- source/modules/physenv.cpp | 40 ++++++++++++++++++++++++-------------- source/symbols.cpp | 4 ++++ source/symbols.h | 5 ++++- source/util.h | 7 ++++++- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/source/modules/physenv.cpp b/source/modules/physenv.cpp index 8c1616a..d0984a7 100644 --- a/source/modules/physenv.cpp +++ b/source/modules/physenv.cpp @@ -257,6 +257,8 @@ struct ILuaPhysicsEnvironment ~ILuaPhysicsEnvironment() { + Delete_ILuaPhysicsEnvironment(this); + if (pObjectEvent) delete pObjectEvent; @@ -299,13 +301,15 @@ Get_LuaClass(ICollisionQuery, ICollisionQuery_TypeID, "ICollisionQuery") static Push_LuaClass(Vector, GarrysMod::Lua::Type::Vector) -inline IPhysicsEnvironment* GetPhysicsEnvironment(int iStackPos, bool bError) +static IPhysicsEnvironment* GetPhysicsEnvironment(int iStackPos, bool bError) { - ILuaPhysicsEnvironment* pEnvironment = Get_ILuaPhysicsEnvironment(iStackPos, bError); - if (!pEnvironment->pEnvironment && bError) + ILuaPhysicsEnvironment* pLuaEnv = Get_ILuaPhysicsEnvironment(iStackPos, bError); + if (!pLuaEnv->pEnvironment && bError) g_Lua->ThrowError(triedNull_ILuaPhysicsEnvironment.c_str()); - return pEnvironment->pEnvironment; + Msg("%p (%s, %i)\n", pLuaEnv->pEnvironment, bError ? "true" : "false", iStackPos); + + return pLuaEnv->pEnvironment; } LUA_FUNCTION_STATIC(physenv_CreateEnvironment) @@ -374,6 +378,7 @@ LUA_FUNCTION_STATIC(physenv_GetActiveEnvironmentByIndex) return 1; } +Symbols::CBaseEntity_VPhysicsDestroyObject func_CBaseEntity_VPhysicsDestroyObject; LUA_FUNCTION_STATIC(physenv_DestroyEnvironment) { if (!physics) @@ -383,21 +388,25 @@ LUA_FUNCTION_STATIC(physenv_DestroyEnvironment) CPhysicsEnvironment* pEnvironment = (CPhysicsEnvironment*)pLuaEnv->pEnvironment; if (pLuaEnv->pEnvironment) { - int index = -1; - for (int i = pEnvironment->m_objects.Count(); --i >= 0; ) + if (func_CBaseEntity_VPhysicsDestroyObject) { - IPhysicsObject* pObject = pEnvironment->m_objects[i]; - CBaseEntity* pEntity = (CBaseEntity*)pObject->GetGameData(); - if (pEntity) + for (int i = pEnvironment->m_objects.Count(); --i >= 0; ) { - + IPhysicsObject* pObject = pEnvironment->m_objects[i]; + CBaseEntity* pEntity = (CBaseEntity*)pObject->GetGameData(); + if (pEntity) + { + //pEntity->VPhysicsUpdate(NULL); // Since the vtables are broken since ~4 functions were removed, this should currently call VPhysicsDestroyObject + func_CBaseEntity_VPhysicsDestroyObject(pEntity); // I'm like 100% certain that the workaround above caused some unholy behavior. + } } } physics->DestroyEnvironment(pEnvironment); + pLuaEnv->pEnvironment = NULL; } - Delete_ILuaPhysicsEnvironment(pLuaEnv); + delete pLuaEnv; return 0; } @@ -1674,12 +1683,10 @@ bool hook_GMod_Util_IsPhysicsObjectValid(IPhysicsObject* obj) * NOTE: This only ocurrs when you delete a Environment that still has objects? */ /*Detouring::Hook detour_CBaseEntity_GMOD_VPhysicsTest; -void* hook_CBaseEntity_GMOD_VPhysicsTest(CBaseEntity* pEnt, IPhysicsObject* obj) +void hook_CBaseEntity_GMOD_VPhysicsTest(CBaseEntity* pEnt, IPhysicsObject* obj) { // NUKE THE FUNCTION for now. - void* ret = detour_CBaseEntity_GMOD_VPhysicsTest.GetTrampoline()(pEnt, obj); - Msg("%p\n", ret); - return ret; + // detour_CBaseEntity_GMOD_VPhysicsTest.GetTrampoline()(pEnt, obj); }*/ /* @@ -2003,4 +2010,7 @@ void CPhysEnvModule::InitDetour(bool bPreServer) func_CBaseEntity_VPhysicsUpdate = (Symbols::CBaseEntity_VPhysicsUpdate)Detour::GetFunction(server_loader.GetModule(), Symbols::CBaseEntity_VPhysicsUpdateSym); Detour::CheckValue("get function", "CBaseEntity::VPhysicsUpdate", func_CBaseEntity_VPhysicsUpdate != NULL); + + func_CBaseEntity_VPhysicsDestroyObject = (Symbols::CBaseEntity_VPhysicsDestroyObject)Detour::GetFunction(server_loader.GetModule(), Symbols::CBaseEntity_VPhysicsDestroyObjectSym); + Detour::CheckValue("get function", "CBaseEntity::VPhysicsDestroyObject", func_CBaseEntity_VPhysicsDestroyObject != NULL); } \ No newline at end of file diff --git a/source/symbols.cpp b/source/symbols.cpp index cf79c36..055c72c 100644 --- a/source/symbols.cpp +++ b/source/symbols.cpp @@ -67,6 +67,10 @@ namespace Symbols Symbol::FromName("_ZN11CBaseEntity17GMOD_VPhysicsTestEP14IPhysicsObject"), }; + const std::vector CBaseEntity_VPhysicsDestroyObjectSym = { + Symbol::FromName("_ZN11CBaseEntity21VPhysicsDestroyObjectEv"), + }; + const std::vector CGetSym = { // 64x ToDo Symbol::FromName("get"), }; diff --git a/source/symbols.h b/source/symbols.h index be65b41..bc4d56e 100644 --- a/source/symbols.h +++ b/source/symbols.h @@ -104,9 +104,12 @@ namespace Symbols typedef void (*CBaseEntity_VPhysicsUpdate)(void* fancy_class, void* obj); extern const std::vector CBaseEntity_VPhysicsUpdateSym; - typedef void* (*CBaseEntity_GMOD_VPhysicsTest)(void* fancy_class, void* obj); + typedef void (*CBaseEntity_GMOD_VPhysicsTest)(void* fancy_class, void* obj); extern const std::vector CBaseEntity_GMOD_VPhysicsTestSym; + typedef void (*CBaseEntity_VPhysicsDestroyObject)(void* fancy_class); + extern const std::vector CBaseEntity_VPhysicsDestroyObjectSym; + extern const std::vector CGetSym; //--------------------------------------------------------------------------------- diff --git a/source/util.h b/source/util.h index e725556..f1f008f 100644 --- a/source/util.h +++ b/source/util.h @@ -175,7 +175,12 @@ void Push_##className(className* var) \ } struct LuaUserData { // ToDo: Maybe implement this also for other things? + LuaUserData() { + Msg("LuaUserData got created!\n"); + } + ~LuaUserData() { + Msg("LuaUserData got deleted! (%i, %i)\n", iReference, iTableReference); if (iReference != -1) { g_Lua->ReferencePush(iReference); @@ -196,7 +201,7 @@ struct LuaUserData { // ToDo: Maybe implement this also for other things? int iReference = -1; int iTableReference = -1; - int pAdditionalData = -1; // Used by HLTVClient. + int pAdditionalData = NULL; // Used by HLTVClient. }; // This one is special