Skip to content

Commit f1b26d0

Browse files
committed
feat: patch material memory allocation
1 parent 619d884 commit f1b26d0

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/Modules/MaterialSystem.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "Utils.hpp"
1111
#include "Features/OverlayRender.hpp"
1212

13+
#define RENDERCONTEXT_ALLOC_SIZE 0x672000 // 6.45 MB
14+
1315
class MemTexRegen : public ITextureRegenerator {
1416
uint8_t *bgra;
1517
int w, h;
@@ -92,10 +94,29 @@ bool MaterialSystem::Init() {
9294
OverlayRender::initMaterials();
9395
}
9496

97+
this->renderContextSize = Memory::Scan<uint32_t *>(this->Name(), Offsets::RenderContextSize, Offsets::RenderContextSizeOff);
98+
this->RenderContextShutdown = Memory::Scan<_RenderContextShutdown>(this->Name(), Offsets::RenderContextShutdown);
99+
this->RenderContextInit = Memory::Scan<_RenderContextInit>(this->Name(), Offsets::RenderContextInit);
100+
if (this->renderContextSize && this->RenderContextShutdown && this->RenderContextInit) {
101+
if (*this->renderContextSize != RENDERCONTEXT_ALLOC_SIZE) {
102+
Memory::UnProtect((void *)this->renderContextSize, sizeof(uint32_t));
103+
this->origRenderContextSize = *this->renderContextSize;
104+
*this->renderContextSize = RENDERCONTEXT_ALLOC_SIZE;
105+
this->RenderContextShutdown();
106+
this->RenderContextInit();
107+
}
108+
}
109+
95110
return this->hasLoaded = this->materials;
96111
}
97112
void MaterialSystem::Shutdown() {
98113
Interface::Delete(this->materials);
114+
115+
if (origRenderContextSize) {
116+
*renderContextSize = origRenderContextSize;
117+
RenderContextShutdown();
118+
RenderContextInit();
119+
}
99120
}
100121
IMaterial *MaterialSystem::FindMaterial(const char *materialName, const char *textureGroupName) {
101122
auto func = (IMaterial *(__rescall *)(void *, const char *, const char *, bool, const char *))this->materials->Current(Offsets::FindMaterial);

src/Modules/MaterialSystem.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class MaterialSystem : public Module {
1111
public:
1212
Interface *materials = nullptr;
1313

14+
uint32_t *renderContextSize = nullptr;
15+
uint32_t origRenderContextSize = 0;
16+
17+
using _RenderContextShutdown = void(__cdecl *)(void);
18+
_RenderContextShutdown RenderContextShutdown = nullptr;
19+
20+
using _RenderContextInit = void(__cdecl *)(void);
21+
_RenderContextInit RenderContextInit = nullptr;
22+
1423
using _RemoveMaterial = void(__rescall*)(void* thisptr, IMaterialInternal* pMaterial);
1524
_RemoveMaterial RemoveMaterial = nullptr;
1625

src/Offsets/Portal 2 9568.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,13 @@ SIGSCAN_DEFAULT(UpdateLeaderboardData, "55 8B EC 83 EC 08 53 8B D9 8B 03 8B 50 0
467467
// MaterialSystem
468468
SIGSCAN_DEFAULT(KeyValues_SetString, "55 8B EC 8B 45 08 6A 01 50 E8 ? ? ? ? 85 C0 74 0B",
469469
"53 83 EC ? 8B 5C 24 ? 6A ? FF 74 24 ? FF 74 24 ? E8 ? ? ? ? 83 C4 ? 85 C0 74 ? 89 5C 24")
470-
470+
SIGSCAN_DEFAULT(RenderContextSize, "56 BE ? ? ? ? E8",
471+
"81 C3 ? ? ? ? E9") // "portal2" AND "infested" AND "swarm" -> CMatRenderContextBase::Init -> nSize initial value (linux is weird)
472+
OFFSET_DEFAULT(RenderContextSizeOff, 2, 2)
473+
SIGSCAN_DEFAULT(RenderContextShutdown, "FF 0D ? ? ? ? 75 ? B9",
474+
"83 3D ? ? ? ? 00 0F 85 ? ? ? ? 56") // CMatRenderContextBase::Init -> sm_nInitializeCount usage -> decrement, check for 0 -> CMatRenderContextBase::Shutdown
475+
SIGSCAN_DEFAULT(RenderContextInit, "83 3D ? ? ? ? 00 0F 85 ? ? ? ? 56",
476+
"A1 ? ? ? ? 85 C0 74 ? 83 C0 01") // CMatRenderContextBase::Init
471477

472478
// Server
473479
SIGSCAN_DEFAULT(GlobalEntity_GetIndex, "55 8B EC 51 8B 45 08 50 8D 4D FC 51 B9 ? ? ? ? E8 ? ? ? ? 66 8B 55 FC B8 FF FF 00 00",

0 commit comments

Comments
 (0)