|
| 1 | +#include "VPhysics.hpp" |
| 2 | + |
| 3 | +#include "Event.hpp" |
| 4 | +#include "Interface.hpp" |
| 5 | +#include "Offsets.hpp" |
| 6 | +#include "SAR.hpp" |
| 7 | + |
| 8 | + |
| 9 | +// Kinda a silly way to keep track of this, but it works |
| 10 | +// and I'm lazy |
| 11 | +static std::vector<int *> vphysEnvironments; |
| 12 | +static std::vector<int *> vphysEnvironmentsLast; |
| 13 | + |
| 14 | +// INFRA memory leak |
| 15 | +// The game has a "fast load" feature that skips a bunch of stuff, including |
| 16 | +// destroying the old physics environment. This leaks ~4MB (one env) per load. |
| 17 | +// Slow loads destroy the two environments that are in use, and create a new |
| 18 | +// two. We detect this and destroy the old environments that are no longer in |
| 19 | +// use. There are other leaks, but this is the most significant. |
| 20 | +ON_EVENT(SESSION_START) { |
| 21 | + if (!sar.game->Is(SourceGame_INFRA)) return; |
| 22 | + vphysEnvironmentsLast = vphysEnvironments; |
| 23 | + vphysEnvironments = {}; |
| 24 | + for (int i = 0; ; ++i) { |
| 25 | + int *env = vphysics->GetActivePhysicsEnvironmentByIndex(i); |
| 26 | + if (!env) break; |
| 27 | + vphysEnvironments.push_back(env); |
| 28 | + } |
| 29 | + |
| 30 | + // console->Print("%d active physics environment(s)\n", (int)vphysEnvironments.size()); |
| 31 | + // int i = 0; |
| 32 | + // for (const auto &env : vphysEnvironmentsLast) { |
| 33 | + // if (std::find(vphysEnvironments.begin(), vphysEnvironments.end(), env) == vphysEnvironments.end()) { |
| 34 | + // console->ColorMsg(Color(255,96,64), "- %d %p\n", i, env); |
| 35 | + // } |
| 36 | + // i++; |
| 37 | + // } |
| 38 | + // i = 0; |
| 39 | + // for (const auto &env : vphysEnvironments) { |
| 40 | + // if (std::find(vphysEnvironmentsLast.begin(), vphysEnvironmentsLast.end(), env) == vphysEnvironmentsLast.end()) { |
| 41 | + // console->ColorMsg(Color(96,255,64), "+ %d %p\n", i, env); |
| 42 | + // } else { |
| 43 | + // console->Print(" %d %p\n", i, env); |
| 44 | + // } |
| 45 | + // i++; |
| 46 | + // } |
| 47 | + |
| 48 | + int newEnvs = 0; |
| 49 | + for (const auto &env : vphysEnvironments) { |
| 50 | + if (std::find(vphysEnvironmentsLast.begin(), vphysEnvironmentsLast.end(), env) == vphysEnvironmentsLast.end()) { |
| 51 | + newEnvs++; |
| 52 | + } |
| 53 | + } |
| 54 | + if (newEnvs == 2) { |
| 55 | + for (const auto &env : vphysEnvironments) { |
| 56 | + if (std::find(vphysEnvironmentsLast.begin(), vphysEnvironmentsLast.end(), env) != vphysEnvironmentsLast.end()) { |
| 57 | + vphysics->DestroyPhysicsEnvironment(env); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +int *VPhysics::GetActivePhysicsEnvironmentByIndex(int index) { |
| 64 | + if (!this->GetActiveEnvironmentByIndex) return nullptr; |
| 65 | + return this->GetActiveEnvironmentByIndex(this->g_pVphysics->ThisPtr(), index); |
| 66 | +} |
| 67 | + |
| 68 | +void VPhysics::DestroyPhysicsEnvironment(int *env) { |
| 69 | + if (!this->DestroyEnvironment) return; |
| 70 | + this->DestroyEnvironment(this->g_pVphysics->ThisPtr(), env); |
| 71 | +} |
| 72 | + |
| 73 | +bool VPhysics::Init() { |
| 74 | + this->g_pVphysics = Interface::Create(this->Name(), "VPhysics031", false); |
| 75 | + |
| 76 | + if (Offsets::GetActiveEnvironmentByIndex) { |
| 77 | + GetActiveEnvironmentByIndex = g_pVphysics->Original<_GetActiveEnvironmentByIndex>(Offsets::GetActiveEnvironmentByIndex); |
| 78 | + } |
| 79 | + if (Offsets::DestroyEnvironment) { |
| 80 | + DestroyEnvironment = g_pVphysics->Original<_DestroyEnvironment>(Offsets::DestroyEnvironment); |
| 81 | + } |
| 82 | + |
| 83 | + return this->hasLoaded = this->g_pVphysics; |
| 84 | +} |
| 85 | + |
| 86 | +void VPhysics::Shutdown() { |
| 87 | + Interface::Delete(this->g_pVphysics); |
| 88 | +} |
| 89 | + |
| 90 | +VPhysics *vphysics; |
0 commit comments