Skip to content

Commit

Permalink
physenv: testing a fix for another crash
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelIT7 committed Nov 19, 2024
1 parent 5a9cd8e commit 41f84ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
40 changes: 25 additions & 15 deletions source/modules/physenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ struct ILuaPhysicsEnvironment

~ILuaPhysicsEnvironment()
{
Delete_ILuaPhysicsEnvironment(this);

if (pObjectEvent)
delete pObjectEvent;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -374,6 +378,7 @@ LUA_FUNCTION_STATIC(physenv_GetActiveEnvironmentByIndex)
return 1;
}

Symbols::CBaseEntity_VPhysicsDestroyObject func_CBaseEntity_VPhysicsDestroyObject;
LUA_FUNCTION_STATIC(physenv_DestroyEnvironment)
{
if (!physics)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<Symbols::CBaseEntity_GMOD_VPhysicsTest>()(pEnt, obj);
Msg("%p\n", ret);
return ret;
// detour_CBaseEntity_GMOD_VPhysicsTest.GetTrampoline<Symbols::CBaseEntity_GMOD_VPhysicsTest>()(pEnt, obj);
}*/

/*
Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions source/symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ namespace Symbols
Symbol::FromName("_ZN11CBaseEntity17GMOD_VPhysicsTestEP14IPhysicsObject"),
};

const std::vector<Symbol> CBaseEntity_VPhysicsDestroyObjectSym = {
Symbol::FromName("_ZN11CBaseEntity21VPhysicsDestroyObjectEv"),
};

const std::vector<Symbol> CGetSym = { // 64x ToDo
Symbol::FromName("get"),
};
Expand Down
5 changes: 4 additions & 1 deletion source/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ namespace Symbols
typedef void (*CBaseEntity_VPhysicsUpdate)(void* fancy_class, void* obj);
extern const std::vector<Symbol> 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<Symbol> CBaseEntity_GMOD_VPhysicsTestSym;

typedef void (*CBaseEntity_VPhysicsDestroyObject)(void* fancy_class);
extern const std::vector<Symbol> CBaseEntity_VPhysicsDestroyObjectSym;

extern const std::vector<Symbol> CGetSym;

//---------------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion source/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 41f84ae

Please sign in to comment.