Skip to content

Commit

Permalink
fix: re-implement memory leak fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisAMJ committed Oct 20, 2024
1 parent d7a76bb commit c1a4dda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/Modules/MaterialSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MemTexRegen : public ITextureRegenerator {
};

REDECL(MaterialSystem::UncacheUnusedMaterials);
REDECL(MaterialSystem::CreateMaterial);

DETOUR(MaterialSystem::UncacheUnusedMaterials, bool bRecomputeStateSnapshots) {
auto start = std::chrono::high_resolution_clock::now();
Expand All @@ -53,12 +54,37 @@ DETOUR(MaterialSystem::UncacheUnusedMaterials, bool bRecomputeStateSnapshots) {
return result;
}

DETOUR_T(IMaterial *, MaterialSystem::CreateMaterial, const char *pMaterialName, void *pVMTKeyValues) {
if (sar.game->Is(SourceGame_Portal2 | SourceGame_PortalReloaded | SourceGame_PortalStoriesMel)) {
// Patched by Valve, no need for us to do it again
return MaterialSystem::CreateMaterial(thisptr, pMaterialName, pVMTKeyValues);
}

std::string sMaterialName(pMaterialName);
std::string sMapName(engine->m_szLevelName);
std::replace(sMaterialName.begin(), sMaterialName.end(), '\\', '/');
std::replace(sMapName.begin(), sMapName.end(), '\\', '/');

// Memory leak ultimate fix! -route credits to krzyhau
// apparently the game loads PeTI related materials into the memory every time you
// load the game. This simply prevents that from happening.
// Revived from the dead for mods!
bool isPetiMaterial = sMaterialName.find("props_map_editor") != std::string::npos;
bool isWhiteMaterial = sMaterialName.find("vgui/white") != std::string::npos;
bool isPetiMap = sMapName.find("puzzlemaker/") != std::string::npos;
if ((isPetiMaterial || isWhiteMaterial) && !isPetiMap) {
return 0;
}

return MaterialSystem::CreateMaterial(thisptr, pMaterialName, pVMTKeyValues);
}

bool MaterialSystem::Init() {
this->materials = Interface::Create(this->Name(), "VMaterialSystem080");
if (this->materials) {
this->materials->Hook(MaterialSystem::UncacheUnusedMaterials_Hook, MaterialSystem::UncacheUnusedMaterials, Offsets::UncacheUnusedMaterials);
this->materials->Hook(MaterialSystem::CreateMaterial_Hook, MaterialSystem::CreateMaterial, Offsets::CreateMaterial);

this->CreateMaterial = this->materials->Original<_CreateMaterial>(Offsets::CreateMaterial);
this->RemoveMaterial = this->materials->Original<_RemoveMaterial>(Offsets::RemoveMaterial);

this->KeyValues_SetString = (_KeyValues_SetString)Memory::Scan(this->Name(), Offsets::KeyValues_SetString);
Expand Down
4 changes: 1 addition & 3 deletions src/Modules/MaterialSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class MaterialSystem : public Module {
public:
Interface *materials = nullptr;

using _CreateMaterial = IMaterial*(__rescall*)(void* thisptr, const char *pMaterialName, void *pVMTKeyValues);
_CreateMaterial CreateMaterial = nullptr;

using _RemoveMaterial = void(__rescall*)(void* thisptr, IMaterialInternal* pMaterial);
_RemoveMaterial RemoveMaterial = nullptr;

Expand All @@ -22,6 +19,7 @@ class MaterialSystem : public Module {

public:
DECL_DETOUR(UncacheUnusedMaterials, bool bRecomputeStateSnapshots);
DECL_DETOUR_T(IMaterial *, CreateMaterial, const char *pMaterialName, void *pVMTKeyValues);

bool Init() override;
void Shutdown() override;
Expand Down

0 comments on commit c1a4dda

Please sign in to comment.