From 6d01314a40228977d88d0d7e06fe1d7f92a80c12 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Mon, 6 Feb 2023 19:34:08 +0000 Subject: [PATCH] [General]: Cleanup some code --- src/Components/Modules/Download.cpp | 18 +++---- src/Components/Modules/Network.cpp | 14 ++--- src/Components/Modules/Network.hpp | 10 ++-- src/Components/Modules/Party.cpp | 2 +- src/Components/Modules/Zones.cpp | 83 ++++++++++++++--------------- src/Components/Modules/Zones.hpp | 8 +-- src/Game/Dvars.cpp | 3 ++ src/Game/Dvars.hpp | 3 ++ 8 files changed, 74 insertions(+), 67 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index cdfbbab4..976aad6d 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -384,15 +384,15 @@ namespace Components auto delta = Game::Sys_Milliseconds() - fDownload->download->lastTimeStamp; if (delta > 300) { - bool doFormat = fDownload->download->lastTimeStamp != 0; + const auto doFormat = fDownload->download->lastTimeStamp != 0; fDownload->download->lastTimeStamp = Game::Sys_Milliseconds(); - auto dataLeft = fDownload->download->totalBytes - fDownload->download->downBytes; + const auto dataLeft = fDownload->download->totalBytes - fDownload->download->downBytes; int timeLeft = 0; if (fDownload->download->timeStampBytes) { - double timeLeftD = ((1.0 * dataLeft) / fDownload->download->timeStampBytes) * delta; + const double timeLeftD = ((1.0 * dataLeft) / fDownload->download->timeStampBytes) * delta; timeLeft = static_cast(timeLeftD); } @@ -509,7 +509,7 @@ namespace Components static std::string mapNamePre; static nlohmann::json jsonList; - const auto mapName = (Party::IsInUserMapLobby() ? Dvar::Var("ui_mapname").get() : Maps::GetUserMap()->getName()); + const std::string mapName = Party::IsInUserMapLobby() ? (*Game::ui_mapname)->current.string : Maps::GetUserMap()->getName(); if (!Maps::GetUserMap()->isValid() && !Party::IsInUserMapLobby()) { mapNamePre.clear(); @@ -554,17 +554,16 @@ namespace Components Utils::String::Replace(url, "\\", "/"); - // Strip /file - url = url.substr(6); + url = url.substr(6); // Strip /file Utils::String::Replace(url, "%20", " "); auto isMap = false; if (url.starts_with("map/")) { isMap = true; - url = url.substr(4); + url = url.substr(4); // Strip map/ - auto mapName = (Party::IsInUserMapLobby() ? Dvar::Var("ui_mapname").get() : Maps::GetUserMap()->getName()); + std::string mapName = (Party::IsInUserMapLobby() ? (*Game::ui_mapname)->current.string : Maps::GetUserMap()->getName()); auto isValidFile = false; for (std::size_t i = 0; i < ARRAYSIZE(Maps::UserMapFiles); ++i) { @@ -592,9 +591,10 @@ namespace Components } } - std::string file; const std::string fsGame = (*Game::fs_gameDirVar)->current.string; const auto path = std::format("{}\\{}{}", (*Game::fs_basepath)->current.string, isMap ? ""s : (fsGame + "\\"s), url); + + std::string file; if ((!isMap && fsGame.empty()) || !Utils::IO::ReadFile(path, &file)) { mg_http_reply(c, 404, "Content-Type: text/html\r\n", "404 - Not Found %s", path.data()); diff --git a/src/Components/Modules/Network.cpp b/src/Components/Modules/Network.cpp index edc26200..6b135803 100644 --- a/src/Components/Modules/Network.cpp +++ b/src/Components/Modules/Network.cpp @@ -91,7 +91,7 @@ namespace Components return {this->getCString()}; } - bool Network::Address::isLocal() + bool Network::Address::isLocal() const noexcept { // According to: https://en.wikipedia.org/wiki/Private_network @@ -112,7 +112,7 @@ namespace Components return false; } - bool Network::Address::isSelf() + bool Network::Address::isSelf() const noexcept { if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback if (this->getPort() != GetPort()) return false; // Port not equal @@ -128,7 +128,7 @@ namespace Components return false; } - bool Network::Address::isLoopback() const + bool Network::Address::isLoopback() const noexcept { if (this->getIP().full == 0x100007f) // 127.0.0.1 { @@ -138,7 +138,7 @@ namespace Components return Game::NET_IsLocalAddress(this->address); } - bool Network::Address::isValid() const + bool Network::Address::isValid() const noexcept { return (this->getType() != Game::NA_BAD && this->getType() >= Game::NA_BOT && this->getType() <= Game::NA_IP); } @@ -225,9 +225,11 @@ namespace Components StartupSignal.clear(); } - unsigned short Network::GetPort() + std::uint16_t Network::GetPort() { - return static_cast(Dvar::Var(0x64A3004).get()); + assert((*Game::port)); + assert((*Game::port)->current.unsignedInt <= std::numeric_limits::max()); + return static_cast((*Game::port)->current.unsignedInt); } __declspec(naked) void Network::NetworkStartStub() diff --git a/src/Components/Modules/Network.hpp b/src/Components/Modules/Network.hpp index 388ba28c..9c55a014 100644 --- a/src/Components/Modules/Network.hpp +++ b/src/Components/Modules/Network.hpp @@ -37,10 +37,10 @@ namespace Components [[nodiscard]] const char* getCString() const; [[nodiscard]] std::string getString() const; - [[nodiscard]] bool isLocal(); - [[nodiscard]] bool isSelf(); - [[nodiscard]] bool isValid() const; - [[nodiscard]] bool isLoopback() const; + [[nodiscard]] bool isLocal() const noexcept; + [[nodiscard]] bool isSelf() const noexcept; + [[nodiscard]] bool isValid() const noexcept; + [[nodiscard]] bool isLoopback() const noexcept; private: Game::netadr_t address; @@ -52,7 +52,7 @@ namespace Components Network(); - static unsigned short GetPort(); + static std::uint16_t GetPort(); static void OnStart(const Utils::Slot& callback); diff --git a/src/Components/Modules/Party.cpp b/src/Components/Modules/Party.cpp index e5f3bd7d..a095f530 100644 --- a/src/Components/Modules/Party.cpp +++ b/src/Components/Modules/Party.cpp @@ -166,7 +166,7 @@ namespace Components bool Party::IsInUserMapLobby() { - return (IsInLobby() && Maps::IsUserMap(Dvar::Var("ui_mapname").get())); + return (IsInLobby() && Maps::IsUserMap((*Game::ui_mapname)->current.string)); } bool Party::IsEnabled() diff --git a/src/Components/Modules/Zones.cpp b/src/Components/Modules/Zones.cpp index cfcd276b..947fbdfb 100644 --- a/src/Components/Modules/Zones.cpp +++ b/src/Components/Modules/Zones.cpp @@ -3032,12 +3032,13 @@ namespace Components { __asm { - sub esp, 0x33C; + sub esp, 0x33C - push 0x643276; - retn; + push 0x643276 + ret } } + int Zones::FS_FOpenFileReadForThreadHook(const char* file, int* filePointer, int thread) { const auto retval = FS_FOpenFileReadForThreadOriginal(file, filePointer, thread); @@ -3051,11 +3052,11 @@ namespace Components // check if file should be skipped auto skipFile = false; - if (strlen(file) > 5 && ((strncmp(&file[strlen(file) - 4], ".iwi", 4) != 0))) + if (std::strlen(file) > 5 && ((std::strncmp(&file[strlen(file) - 4], ".iwi", 4) != 0))) { skipFile = true; } - else if (readSize >= 3 && (!memcmp(&fileBuffer[0], "IWi", 3))) + else if (readSize >= 3 && (std::memcmp(&fileBuffer[0], "IWi", 3) == 0)) { skipFile = true; } @@ -3086,8 +3087,8 @@ namespace Components // attempt to decrypt the IWI symmetric_CTR ctr_state; - memset(&ctr_state, 0, sizeof(symmetric_CTR)); - + ZeroMemory(&ctr_state, sizeof(symmetric_CTR)); + // decryption keys std::uint8_t aesKey[24] = { 0x15, 0x9a, 0x03, 0x25, 0xe0, 0x75, 0x2e, 0x80, 0xc6, 0xc0, 0x94, 0x2a, 0x50, 0x5c, 0x1c, 0x68, 0x8c, 0x17, 0xef, 0x53, 0x99, 0xf8, 0x68, 0x3c }; std::uint32_t aesIV[4] = { 0x1010101, 0x1010101, 0x1010101, 0x1010101 }; @@ -3096,9 +3097,9 @@ namespace Components auto nonce = HashCRC32StringInt(strippedFileName, strippedFileName.size()); std::uint8_t iv[16]; - memset(iv, 0, sizeof iv); - memcpy(iv, &nonce, 4); - memcpy(iv + 4, &unpackedSize, 4); + std::memset(iv, 0, sizeof iv); + std::memcpy(iv, &nonce, 4); + std::memcpy(iv + 4, &unpackedSize, 4); ctr_start(aes, reinterpret_cast(&aesIV[0]), &aesKey[0], sizeof aesKey, 0, CTR_COUNTER_BIG_ENDIAN, &ctr_state); @@ -3109,8 +3110,8 @@ namespace Components auto left = (packedSize - readDataSize); auto blockSize = (left > 0x8000) ? 0x8000 : left; - memcpy(iv + 8, &readDataSize, 4); - memcpy(iv + 12, &blockSize, 4); + std::memcpy(iv + 8, &readDataSize, 4); + std::memcpy(iv + 12, &blockSize, 4); ctr_setiv(iv, sizeof iv, &ctr_state); ctr_decrypt(reinterpret_cast(&encryptedData[readDataSize]), reinterpret_cast(&decryptedData[readDataSize]), blockSize, &ctr_state); @@ -3134,7 +3135,7 @@ namespace Components // insert file data if (result == Z_OK) { - std::lock_guard $(fileDataMutex); + std::lock_guard _(fileDataMutex); fileDataMap[*filePointer] = data; return unpackedSize; } @@ -3152,50 +3153,51 @@ namespace Components { __asm { - push ecx; - mov eax, [esp + 0x10]; + push ecx + mov eax, [esp + 0x10] - push 0x4A04C5; - retn; + push 0x4A04C5 + ret } } + int Zones::FS_ReadHook(void* buffer, size_t size, int filePointer) { - std::lock_guard $(fileDataMutex); - - auto itr = fileDataMap.find(filePointer); - if (itr != fileDataMap.end()) + std::lock_guard _(fileDataMutex); + + if (auto itr = fileDataMap.find(filePointer); itr != fileDataMap.end()) { if (!itr->second.fileContents.empty()) { const auto readSize = std::min(size, itr->second.fileContents.size() - itr->second.readPos); - memcpy(buffer, &itr->second.fileContents[itr->second.readPos], readSize); + std::memcpy(buffer, &itr->second.fileContents[itr->second.readPos], readSize); itr->second.readPos += readSize; - return readSize; + return static_cast(readSize); } } return FS_ReadOriginal(buffer, size, filePointer); } + __declspec(naked) void Zones::FS_FCloseFileOriginal(int) { __asm { - mov eax, [esp + 4]; - push esi; + mov eax, [esp + 4] + push esi - push 0x462005; - retn; + push 0x462005 + ret } } + void Zones::FS_FCloseFileHook(int filePointer) { - std::lock_guard $(fileDataMutex); + std::lock_guard _(fileDataMutex); FS_FCloseFileOriginal(filePointer); - - const auto itr = fileDataMap.find(filePointer); - if (itr != fileDataMap.end()) + + if (const auto itr = fileDataMap.find(filePointer); itr != fileDataMap.end()) { fileDataMap.erase(itr); } @@ -3204,19 +3206,18 @@ namespace Components { __asm { - push esi; - mov esi, [esp + 8]; + push esi + mov esi, [esp + 8] - push 0x4A63D5; - retn; + push 0x4A63D5 + ret } } std::uint32_t Zones::FS_SeekHook(int fileHandle, int seekPosition, int seekOrigin) { - std::lock_guard $(fileDataMutex); + std::lock_guard _(fileDataMutex); - const auto itr = fileDataMap.find(fileHandle); - if (itr != fileDataMap.end()) + if (const auto itr = fileDataMap.find(fileHandle); itr != fileDataMap.end()) { if (seekOrigin == Game::FS_SEEK_SET) { @@ -3233,10 +3234,8 @@ namespace Components return itr->second.readPos; } - else - { - return FS_SeekOriginal(fileHandle, seekPosition, seekOrigin); - } + + return FS_SeekOriginal(fileHandle, seekPosition, seekOrigin); } __declspec(naked) void Zones::LoadMapTriggersModelPointer() diff --git a/src/Components/Modules/Zones.hpp b/src/Components/Modules/Zones.hpp index 5c4899b0..97154438 100644 --- a/src/Components/Modules/Zones.hpp +++ b/src/Components/Modules/Zones.hpp @@ -21,15 +21,17 @@ namespace Components static void SetVersion(int version); - static int Version() { return Zones::ZoneVersion; } + static int Version() { return ZoneVersion; } private: - static int ZoneVersion; static int FxEffectIndex; static char* FxEffectStrings[64]; + static std::unordered_map fileDataMap; + static std::mutex fileDataMutex; + static bool CheckGameMapSp(int type); static void GameMapSpPatchStub(); @@ -82,8 +84,6 @@ namespace Components static void Load_ClipInfo(bool atStreamStart); static int LoadClipMap(bool atStreamStart); static uint32_t HashCRC32StringInt(const std::string& Value, uint32_t Initial); - static std::unordered_map fileDataMap; - static std::mutex fileDataMutex; static int FS_FOpenFileReadForThreadOriginal(const char*, int*, int); static int FS_FOpenFileReadForThreadHook(const char* file, int* filePointer, int thread); static int FS_ReadOriginal(void*, size_t, int); diff --git a/src/Game/Dvars.cpp b/src/Game/Dvars.cpp index f1c9474e..f3caaa44 100644 --- a/src/Game/Dvars.cpp +++ b/src/Game/Dvars.cpp @@ -90,6 +90,9 @@ namespace Game const dvar_t** party_minplayers = reinterpret_cast(0x1081BFC); const dvar_t** party_maxplayers = reinterpret_cast(0x1080998); + const dvar_t** ip = reinterpret_cast(0x64A1DF8); + const dvar_t** port = reinterpret_cast(0x64A3004); + __declspec(naked) void Dvar_SetVariant(dvar_t*, DvarValue, DvarSetSource) { static DWORD Dvar_SetVariant_t = 0x647400; diff --git a/src/Game/Dvars.hpp b/src/Game/Dvars.hpp index 4de32a6b..a88f236d 100644 --- a/src/Game/Dvars.hpp +++ b/src/Game/Dvars.hpp @@ -142,6 +142,9 @@ namespace Game extern const dvar_t** party_minplayers; extern const dvar_t** party_maxplayers; + extern const dvar_t** ip; + extern const dvar_t** port; + extern void Dvar_SetVariant(dvar_t* var, DvarValue value, DvarSetSource source); extern void Dvar_SetFromStringFromSource(const dvar_t* dvar, const char* string, DvarSetSource source); }