diff --git a/src/Components/Modules/Colors.cpp b/src/Components/Modules/Colors.cpp index 91ab345f..5d62c25d 100644 --- a/src/Components/Modules/Colors.cpp +++ b/src/Components/Modules/Colors.cpp @@ -6,6 +6,8 @@ namespace Components void Colors::Strip(const char* in, char* out, int max) { + if (!in || !out) return; + max--; int current = 0; while (*in != 0 && current < max) @@ -18,9 +20,9 @@ namespace Components } else { - *in++; + in++; } - *in++; + in++; } *out = '\0'; } diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index ac1ea8dd..d87a889e 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -12,6 +12,8 @@ namespace Components void Maps::LoadMapZones(Game::XZoneInfo *zoneInfo, unsigned int zoneCount, int sync) { + if (!zoneInfo) return; + Maps::CurrentDependencies.clear(); for (auto i = Maps::DependencyList.begin(); i != Maps::DependencyList.end(); i++) { @@ -24,19 +26,25 @@ namespace Components } } - Game::XZoneInfo* data = new Game::XZoneInfo[zoneCount + Maps::CurrentDependencies.size()]; - memcpy(data, zoneInfo, sizeof(Game::XZoneInfo) * zoneCount); + std::vector data; + + for (unsigned int i = 0; i < zoneCount; i++) + { + data.push_back(zoneInfo[i]); + } for (unsigned int i = 0; i < Maps::CurrentDependencies.size(); i++) { - data[zoneCount + i].name = (&Maps::CurrentDependencies[i])->data(); - data[zoneCount + i].allocFlags = data->allocFlags; - data[zoneCount + i].freeFlags = data->freeFlags; + Game::XZoneInfo info; + + info.name = (&Maps::CurrentDependencies[i])->data(); + info.allocFlags = zoneInfo->allocFlags; + info.freeFlags = zoneInfo->freeFlags; + + data.push_back(info); } - Game::DB_LoadXAssets(data, zoneCount + Maps::CurrentDependencies.size(), sync); - - delete[] data; + Game::DB_LoadXAssets(data.data(), data.size(), sync); } bool Maps::LoadAssetRestrict(Game::XAssetType type, Game::XAssetHeader asset, const char* name) diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index feb7efa7..667044e0 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -69,6 +69,11 @@ namespace Components script->next = NULL; source = (Game::source_t *)calloc(1, sizeof(Game::source_t)); + if (!source) + { + Game::FreeMemory(script); + return 0; + } strncpy(source->filename, "string", 64); source->scriptstack = script; @@ -111,7 +116,15 @@ namespace Components Game::menuDef_t* Menus::ParseMenu(int handle) { Game::menuDef_t* menu = (Game::menuDef_t*)calloc(1, sizeof(Game::menuDef_t)); + if (!menu) return nullptr; + menu->items = (Game::itemDef_t**)calloc(512, sizeof(Game::itemDef_t*)); + if (!menu->items) + { + free(menu); + return nullptr; + } + Menus::MenuList.push_back(menu); Game::pc_token_t token; @@ -187,7 +200,8 @@ namespace Components if (!_stricmp(token.string, "menudef")) { - menus.push_back(Menus::ParseMenu(handle)); + Game::menuDef_t* menudef = Menus::ParseMenu(handle); + if (menudef) menus.push_back(menudef); } } @@ -217,8 +231,16 @@ namespace Components // Allocate new menu list Game::MenuList* newList = (Game::MenuList*)calloc(1, sizeof(Game::MenuList)); - newList->name = _strdup(menu); + if (!newList) return nullptr; + newList->menus = (Game::menuDef_t **)calloc(menus.size(), sizeof(Game::menuDef_t *)); + if (!newList->menus) + { + free(newList); + return nullptr; + } + + newList->name = _strdup(menu); newList->menuCount = menus.size(); // Copy new menus @@ -254,8 +276,16 @@ namespace Components // Allocate new menu list Game::MenuList* newList = (Game::MenuList*)calloc(1, sizeof(Game::MenuList)); - newList->name = _strdup(menuList->name); + if (!newList) return menuList; + newList->menus = (Game::menuDef_t **)calloc(menus.size(), sizeof(Game::menuDef_t *)); + if (!newList->menus) + { + free(newList); + return menuList; + } + + newList->name = _strdup(menuList->name); newList->menuCount = menus.size(); // Copy new menus @@ -335,20 +365,22 @@ namespace Components void Menus::FreeMenuList(Game::MenuList* menuList) { - if (menuList) + if (!menuList) return; + + // Keep our compiler happy + Game::MenuList list = { menuList->name, menuList->menuCount, menuList->menus }; + + if (list.name) { - if (menuList->name) - { - free((void*)menuList->name); - } - - if (menuList->menus) - { - free(menuList->menus); - } - - free(menuList); + free(list.name); } + + if (list.menus) + { + free(list.menus); + } + + free(menuList); } void Menus::RemoveMenu(Game::menuDef_t* menudef) @@ -498,7 +530,7 @@ namespace Components Utils::Hook::Nop(0x453406, 5); //make Com_Error and similar go back to main_text instead of menu_xboxlive. - strcpy((char*)0x6FC790, "main_text"); + Utils::Hook::SetString(0x6FC790, "main_text"); Command::Add("openmenu", [] (Command::Params params) { diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index ceccc44b..470ea770 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -102,7 +102,6 @@ namespace Components // increase font sizes for chat on higher resolutions static float float13 = 13.0f; static float float10 = 10.0f; - Utils::Hook::Set(0x5814AE, &float13); Utils::Hook::Set(0x5814C8, &float10); @@ -130,7 +129,7 @@ namespace Components Utils::Hook::Nop(0x4AA8A1, 6); // Rename stat file - TODO: beautify - strcpy((char*)0x71C048, "iw4x.stat"); + Utils::Hook::SetString(0x71C048, "iw4x.stat"); // Patch stats steamid Utils::Hook::Nop(0x682EBF, 20); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 66b015e9..cd8f2378 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -599,7 +599,7 @@ namespace Game struct MenuList { - const char *name; + char *name; int menuCount; menuDef_t **menus; }; diff --git a/src/Utils/Hooking.cpp b/src/Utils/Hooking.cpp index 64cb4b74..345b7d77 100644 --- a/src/Utils/Hooking.cpp +++ b/src/Utils/Hooking.cpp @@ -46,8 +46,8 @@ namespace Utils Hook::Installed = true; - DWORD d; - VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &d); + DWORD d = 1; + VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &this->Protection); memcpy(Hook::Buffer, Hook::Place, sizeof(Hook::Buffer)); char* Code = (char*)Hook::Place; @@ -56,7 +56,7 @@ namespace Utils *(size_t*)&Code[1] = (size_t)Hook::Stub - ((size_t)Hook::Place + 5); - VirtualProtect(Hook::Place, sizeof(Hook::Buffer), d, &d); + VirtualProtect(Hook::Place, sizeof(Hook::Buffer), Hook::Protection, &this->Protection); FlushInstructionCache(GetCurrentProcess(), Hook::Place, sizeof(Hook::Buffer)); @@ -85,12 +85,11 @@ namespace Utils Hook::Installed = false; - DWORD d; - VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &d); + VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &this->Protection); memcpy(Hook::Place, Hook::Buffer, sizeof(Hook::Buffer)); - VirtualProtect(Hook::Place, sizeof(Hook::Buffer), d, &d); + VirtualProtect(Hook::Place, sizeof(Hook::Buffer), Hook::Protection, &this->Protection); FlushInstructionCache(GetCurrentProcess(), Hook::Place, sizeof(Hook::Buffer)); @@ -113,4 +112,24 @@ namespace Utils { Nop((void*)place, length); } + + void Hook::SetString(void* place, const char* string, size_t length) + { + strncpy((char*)place, string, length); + } + + void Hook::SetString(DWORD place, const char* string, size_t length) + { + Hook::SetString((void*)place, string, length); + } + + void Hook::SetString(void* place, const char* string) + { + Hook::SetString(place, string, strlen((char*)place)); + } + + void Hook::SetString(DWORD place, const char* string) + { + Hook::SetString((void*)place, string); + } } diff --git a/src/Utils/Hooking.hpp b/src/Utils/Hooking.hpp index 2dbe33ff..f65730fb 100644 --- a/src/Utils/Hooking.hpp +++ b/src/Utils/Hooking.hpp @@ -8,7 +8,7 @@ namespace Utils class Hook { public: - Hook() : Place(nullptr), Stub(nullptr), Initialized(false), Installed(false), UseJump(false) { ZeroMemory(Hook::Buffer, sizeof(Hook::Buffer)); } + Hook() : Place(nullptr), Stub(nullptr), Initialized(false), Installed(false), UseJump(false), Protection(0) { ZeroMemory(Hook::Buffer, sizeof(Hook::Buffer)); } Hook(void* place, void* stub, bool useJump = true) : Hook() { Hook::Initialize(place, stub, useJump); } Hook(DWORD place, void* stub, bool useJump = true) : Hook((void*)place, stub, useJump) {} @@ -32,6 +32,12 @@ namespace Utils } } + static void SetString(void* place, const char* string, size_t length); + static void SetString(DWORD place, const char* string, size_t length); + + static void SetString(void* place, const char* string); + static void SetString(DWORD place, const char* string); + static void Nop(void* place, size_t length); static void Nop(DWORD place, size_t length); @@ -99,6 +105,8 @@ namespace Utils char Buffer[5]; bool UseJump; + DWORD Protection; + std::mutex StateMutex; }; }