Skip to content

Commit bb56707

Browse files
committed
fix: Small code adjustments to the memory files
1 parent dbfa09f commit bb56707

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/modules/cportal_player.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ DETOUR_T(bool, CPortal_Player::FlashlightTurnOn, bool playSound)
6868
* @param playSound If flashlight sound should play. Coded to always pass as false.
6969
*/
7070
DEFINE_HOOK(CPortal_Player, FlashlightTurnOff);
71-
//Hook* CPortal_Player::h_CPortal_Player_FlashlightTurnOff = nullptr;
7271
DETOUR_T(void, CPortal_Player::FlashlightTurnOff, bool playSound)
7372
{
7473
CBaseEntity* pEntity = static_cast<CBaseEntity*>(thisPtr);

src/utils/memory.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ bool Memory::TryGetModule(const char* moduleName, Memory::ModuleInfo* info)
4545
if (!GetModuleFileName(hMods[i], buffer, sizeof(buffer)))
4646
continue;
4747

48-
auto modinfo = MODULEINFO();
49-
if (!GetModuleInformation(pHandle, hMods[i], &modinfo, sizeof(modinfo)))
48+
auto modInfo = MODULEINFO();
49+
if (!GetModuleInformation(pHandle, hMods[i], &modInfo, sizeof(modInfo)))
5050
continue;
5151

5252
auto module = ModuleInfo();
@@ -56,8 +56,8 @@ bool Memory::TryGetModule(const char* moduleName, Memory::ModuleInfo* info)
5656
temp = temp.substr(index + 1, temp.length() - index);
5757

5858
std::snprintf(module.name, sizeof(module.name), "%s", temp.c_str());
59-
module.base = reinterpret_cast<uintptr_t>(modinfo.lpBaseOfDll);
60-
module.size = static_cast<uintptr_t>(modinfo.SizeOfImage);
59+
module.base = reinterpret_cast<uintptr_t>(modInfo.lpBaseOfDll);
60+
module.size = static_cast<uintptr_t>(modInfo.SizeOfImage);
6161
std::snprintf(module.path, sizeof(module.path), "%s", buffer);
6262

6363
Memory::moduleList.push_back(module);
@@ -75,7 +75,7 @@ bool Memory::TryGetModule(const char* moduleName, Memory::ModuleInfo* info)
7575
{
7676
// FIXME: we really want data segments too! but +x is more important
7777
if (info->dlpi_phdr[i].p_flags & 1) // execute
78-
{
78+
{
7979
Memory::ModuleInfo module;
8080
module.base = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr;
8181
module.size = info->dlpi_phdr[i].p_memsz;
@@ -91,7 +91,7 @@ bool Memory::TryGetModule(const char* moduleName, Memory::ModuleInfo* info)
9191
#endif
9292
}
9393

94-
for (Memory::ModuleInfo &item : Memory::moduleList)
94+
for (const Memory::ModuleInfo& item : Memory::moduleList)
9595
{
9696
if (!std::strcmp(item.name, moduleName))
9797
{
@@ -104,7 +104,7 @@ bool Memory::TryGetModule(const char* moduleName, Memory::ModuleInfo* info)
104104

105105
return false;
106106
}
107-
void* Memory::GetModuleHandleByName(const char *moduleName)
107+
void* Memory::GetModuleHandleByName(const char* moduleName)
108108
{
109109
auto info = Memory::ModuleInfo();
110110
#ifdef _WIN32
@@ -113,7 +113,7 @@ void* Memory::GetModuleHandleByName(const char *moduleName)
113113
return (TryGetModule(moduleName, &info)) ? dlopen(info.path, RTLD_NOLOAD | RTLD_NOW) : nullptr;
114114
#endif
115115
}
116-
void Memory::CloseModuleHandle(void *moduleHandle)
116+
void Memory::CloseModuleHandle(void* moduleHandle)
117117
{
118118
#ifndef _WIN32
119119
dlclose(moduleHandle);
@@ -159,8 +159,8 @@ uintptr_t Memory::Scan(const char *moduleName, const char* pattern, const int of
159159
auto info = Memory::ModuleInfo();
160160
if (Memory::TryGetModule(moduleName, &info))
161161
{
162-
auto start = uintptr_t(info.base);
163-
auto end = start + info.size;
162+
const auto start = info.base;
163+
const auto end = start + info.size;
164164
result = Memory::FindAddress(start, end, pattern);
165165
if (result)
166166
result += offset;
@@ -189,7 +189,7 @@ std::vector<uintptr_t> Memory::MultiScan(const char* moduleName, const char* pat
189189
auto info = Memory::ModuleInfo();
190190
if (Memory::TryGetModule(moduleName, &info))
191191
{
192-
auto start = uintptr_t(info.base);
192+
auto start = info.base;
193193
const auto end = start + info.size;
194194
while (true) {
195195
if (auto addr = Memory::FindAddress(start, end, pattern))
@@ -211,7 +211,7 @@ std::vector<uintptr_t> Memory::Scan(const char* moduleName, const Memory::Patter
211211
auto info = Memory::ModuleInfo();
212212
if (Memory::TryGetModule(moduleName, &info))
213213
{
214-
const auto start = uintptr_t(info.base);
214+
const auto start = info.base;
215215
const auto end = start + info.size;
216216
if (const auto addr = Memory::FindAddress(start, end, pattern->signature))
217217
{
@@ -228,10 +228,10 @@ std::vector<std::vector<uintptr_t>> Memory::MultiScan(const char* moduleName, co
228228
auto info = Memory::ModuleInfo();
229229
if (Memory::TryGetModule(moduleName, &info))
230230
{
231-
const auto moduleStart = uintptr_t(info.base);
231+
const auto moduleStart = info.base;
232232
const auto moduleEnd = moduleStart + info.size;
233233

234-
for (const auto &pattern : *patterns)
234+
for (const auto& pattern : *patterns)
235235
{
236236
const auto length = std::strlen(pattern->signature);
237237
auto start = moduleStart;
@@ -241,7 +241,7 @@ std::vector<std::vector<uintptr_t>> Memory::MultiScan(const char* moduleName, co
241241
if (const auto addr = Memory::FindAddress(start, moduleEnd, pattern->signature))
242242
{
243243
auto result = std::vector<uintptr_t>();
244-
for (const auto &offset : pattern->offsets)
244+
for (const auto& offset : pattern->offsets)
245245
result.push_back(addr + offset);
246246

247247
results.push_back(result);
@@ -270,23 +270,26 @@ Memory::Patch::~Patch()
270270
}
271271
this->isPatched = false;
272272
}
273+
273274
bool Memory::Patch::Execute()
274275
{
275276
if (this->isPatched)
276277
return true; // already executed
277-
unsigned char *tmpPatch = new unsigned char[this->size];
278+
unsigned char* tmpPatch = new unsigned char[this->size];
278279
// We create another patch, because this->patch is gonna be deleted
279280
memcpy(tmpPatch, this->patch, this->size);
280281
auto ret = this->Execute(this->location, tmpPatch, this->size);
281282
delete[] tmpPatch;
282283
return ret;
283284
}
284285

285-
bool Memory::Patch::Execute(uintptr_t location, unsigned char* bytes, const size_t size_)
286+
bool Memory::Patch::Execute(const uintptr_t location_, unsigned char* bytes, const size_t size_)
286287
{
287-
if (this->isPatched) return true; // already executed
288-
this->location = location;
288+
if (this->isPatched)
289+
return true; // already executed
290+
this->location = location_;
289291
this->size = size_;
292+
290293
if (this->original)
291294
{
292295
delete[] this->original;
@@ -329,7 +332,7 @@ bool Memory::Patch::Execute(uintptr_t location, unsigned char* bytes, const size
329332
}
330333
bool Memory::Patch::Restore()
331334
{
332-
if (!this || !this->location || !this->original)
335+
if (!this->location || !this->original)
333336
return false;
334337

335338
if (!this->isPatched)
@@ -340,8 +343,9 @@ bool Memory::Patch::Restore()
340343
#else
341344
// Should be already unprotected, but just in case
342345
Memory::UnProtect(reinterpret_cast<void *>(this->location), this->size);
343-
for (size_t i = 0; i < this->size; ++i) {
344-
*(uint8_t *)(this->location + i) = this->original[i];
346+
for (size_t i = 0; i < this->size; ++i)
347+
{
348+
*(uint8_t*)(this->location + i) = this->original[i];
345349
}
346350
#endif
347351
this->isPatched = false;

src/utils/memory.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace Memory
5555
Patch() : location(0), original(nullptr), patch(nullptr), size(0), isPatched(false) {}
5656
~Patch();
5757
bool Execute();
58-
bool Execute(uintptr_t location, unsigned char* bytes, size_t size_);
58+
bool Execute(uintptr_t location_, unsigned char* bytes, size_t size_);
5959
template <size_t size>
6060
bool Execute(const uintptr_t location_, unsigned char (&bytes)[size])
6161
{

0 commit comments

Comments
 (0)