[General]: Cleanup some code

This commit is contained in:
FutureRave 2023-02-06 19:34:08 +00:00
parent e10f558271
commit 6d01314a40
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
8 changed files with 74 additions and 67 deletions

View File

@ -384,15 +384,15 @@ namespace Components
auto delta = Game::Sys_Milliseconds() - fDownload->download->lastTimeStamp; auto delta = Game::Sys_Milliseconds() - fDownload->download->lastTimeStamp;
if (delta > 300) if (delta > 300)
{ {
bool doFormat = fDownload->download->lastTimeStamp != 0; const auto doFormat = fDownload->download->lastTimeStamp != 0;
fDownload->download->lastTimeStamp = Game::Sys_Milliseconds(); 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; int timeLeft = 0;
if (fDownload->download->timeStampBytes) 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<int>(timeLeftD); timeLeft = static_cast<int>(timeLeftD);
} }
@ -509,7 +509,7 @@ namespace Components
static std::string mapNamePre; static std::string mapNamePre;
static nlohmann::json jsonList; static nlohmann::json jsonList;
const auto mapName = (Party::IsInUserMapLobby() ? Dvar::Var("ui_mapname").get<std::string>() : Maps::GetUserMap()->getName()); const std::string mapName = Party::IsInUserMapLobby() ? (*Game::ui_mapname)->current.string : Maps::GetUserMap()->getName();
if (!Maps::GetUserMap()->isValid() && !Party::IsInUserMapLobby()) if (!Maps::GetUserMap()->isValid() && !Party::IsInUserMapLobby())
{ {
mapNamePre.clear(); mapNamePre.clear();
@ -554,17 +554,16 @@ namespace Components
Utils::String::Replace(url, "\\", "/"); Utils::String::Replace(url, "\\", "/");
// Strip /file url = url.substr(6); // Strip /file
url = url.substr(6);
Utils::String::Replace(url, "%20", " "); Utils::String::Replace(url, "%20", " ");
auto isMap = false; auto isMap = false;
if (url.starts_with("map/")) if (url.starts_with("map/"))
{ {
isMap = true; isMap = true;
url = url.substr(4); url = url.substr(4); // Strip map/
auto mapName = (Party::IsInUserMapLobby() ? Dvar::Var("ui_mapname").get<std::string>() : Maps::GetUserMap()->getName()); std::string mapName = (Party::IsInUserMapLobby() ? (*Game::ui_mapname)->current.string : Maps::GetUserMap()->getName());
auto isValidFile = false; auto isValidFile = false;
for (std::size_t i = 0; i < ARRAYSIZE(Maps::UserMapFiles); ++i) 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 std::string fsGame = (*Game::fs_gameDirVar)->current.string;
const auto path = std::format("{}\\{}{}", (*Game::fs_basepath)->current.string, isMap ? ""s : (fsGame + "\\"s), url); 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)) 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()); mg_http_reply(c, 404, "Content-Type: text/html\r\n", "404 - Not Found %s", path.data());

View File

@ -91,7 +91,7 @@ namespace Components
return {this->getCString()}; return {this->getCString()};
} }
bool Network::Address::isLocal() bool Network::Address::isLocal() const noexcept
{ {
// According to: https://en.wikipedia.org/wiki/Private_network // According to: https://en.wikipedia.org/wiki/Private_network
@ -112,7 +112,7 @@ namespace Components
return false; return false;
} }
bool Network::Address::isSelf() bool Network::Address::isSelf() const noexcept
{ {
if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback
if (this->getPort() != GetPort()) return false; // Port not equal if (this->getPort() != GetPort()) return false; // Port not equal
@ -128,7 +128,7 @@ namespace Components
return false; return false;
} }
bool Network::Address::isLoopback() const bool Network::Address::isLoopback() const noexcept
{ {
if (this->getIP().full == 0x100007f) // 127.0.0.1 if (this->getIP().full == 0x100007f) // 127.0.0.1
{ {
@ -138,7 +138,7 @@ namespace Components
return Game::NET_IsLocalAddress(this->address); 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); return (this->getType() != Game::NA_BAD && this->getType() >= Game::NA_BOT && this->getType() <= Game::NA_IP);
} }
@ -225,9 +225,11 @@ namespace Components
StartupSignal.clear(); StartupSignal.clear();
} }
unsigned short Network::GetPort() std::uint16_t Network::GetPort()
{ {
return static_cast<unsigned short>(Dvar::Var(0x64A3004).get<unsigned int>()); assert((*Game::port));
assert((*Game::port)->current.unsignedInt <= std::numeric_limits<std::uint16_t>::max());
return static_cast<std::uint16_t>((*Game::port)->current.unsignedInt);
} }
__declspec(naked) void Network::NetworkStartStub() __declspec(naked) void Network::NetworkStartStub()

View File

@ -37,10 +37,10 @@ namespace Components
[[nodiscard]] const char* getCString() const; [[nodiscard]] const char* getCString() const;
[[nodiscard]] std::string getString() const; [[nodiscard]] std::string getString() const;
[[nodiscard]] bool isLocal(); [[nodiscard]] bool isLocal() const noexcept;
[[nodiscard]] bool isSelf(); [[nodiscard]] bool isSelf() const noexcept;
[[nodiscard]] bool isValid() const; [[nodiscard]] bool isValid() const noexcept;
[[nodiscard]] bool isLoopback() const; [[nodiscard]] bool isLoopback() const noexcept;
private: private:
Game::netadr_t address; Game::netadr_t address;
@ -52,7 +52,7 @@ namespace Components
Network(); Network();
static unsigned short GetPort(); static std::uint16_t GetPort();
static void OnStart(const Utils::Slot<CallbackRaw>& callback); static void OnStart(const Utils::Slot<CallbackRaw>& callback);

View File

@ -166,7 +166,7 @@ namespace Components
bool Party::IsInUserMapLobby() bool Party::IsInUserMapLobby()
{ {
return (IsInLobby() && Maps::IsUserMap(Dvar::Var("ui_mapname").get<const char*>())); return (IsInLobby() && Maps::IsUserMap((*Game::ui_mapname)->current.string));
} }
bool Party::IsEnabled() bool Party::IsEnabled()

View File

@ -3032,12 +3032,13 @@ namespace Components
{ {
__asm __asm
{ {
sub esp, 0x33C; sub esp, 0x33C
push 0x643276; push 0x643276
retn; ret
} }
} }
int Zones::FS_FOpenFileReadForThreadHook(const char* file, int* filePointer, int thread) int Zones::FS_FOpenFileReadForThreadHook(const char* file, int* filePointer, int thread)
{ {
const auto retval = FS_FOpenFileReadForThreadOriginal(file, filePointer, thread); const auto retval = FS_FOpenFileReadForThreadOriginal(file, filePointer, thread);
@ -3051,11 +3052,11 @@ namespace Components
// check if file should be skipped // check if file should be skipped
auto skipFile = false; 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; skipFile = true;
} }
else if (readSize >= 3 && (!memcmp(&fileBuffer[0], "IWi", 3))) else if (readSize >= 3 && (std::memcmp(&fileBuffer[0], "IWi", 3) == 0))
{ {
skipFile = true; skipFile = true;
} }
@ -3086,7 +3087,7 @@ namespace Components
// attempt to decrypt the IWI // attempt to decrypt the IWI
symmetric_CTR ctr_state; symmetric_CTR ctr_state;
memset(&ctr_state, 0, sizeof(symmetric_CTR)); ZeroMemory(&ctr_state, sizeof(symmetric_CTR));
// decryption keys // 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::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 };
@ -3096,9 +3097,9 @@ namespace Components
auto nonce = HashCRC32StringInt(strippedFileName, strippedFileName.size()); auto nonce = HashCRC32StringInt(strippedFileName, strippedFileName.size());
std::uint8_t iv[16]; std::uint8_t iv[16];
memset(iv, 0, sizeof iv); std::memset(iv, 0, sizeof iv);
memcpy(iv, &nonce, 4); std::memcpy(iv, &nonce, 4);
memcpy(iv + 4, &unpackedSize, 4); std::memcpy(iv + 4, &unpackedSize, 4);
ctr_start(aes, reinterpret_cast<unsigned char*>(&aesIV[0]), &aesKey[0], sizeof aesKey, 0, CTR_COUNTER_BIG_ENDIAN, &ctr_state); ctr_start(aes, reinterpret_cast<unsigned char*>(&aesIV[0]), &aesKey[0], sizeof aesKey, 0, CTR_COUNTER_BIG_ENDIAN, &ctr_state);
@ -3109,8 +3110,8 @@ namespace Components
auto left = (packedSize - readDataSize); auto left = (packedSize - readDataSize);
auto blockSize = (left > 0x8000) ? 0x8000 : left; auto blockSize = (left > 0x8000) ? 0x8000 : left;
memcpy(iv + 8, &readDataSize, 4); std::memcpy(iv + 8, &readDataSize, 4);
memcpy(iv + 12, &blockSize, 4); std::memcpy(iv + 12, &blockSize, 4);
ctr_setiv(iv, sizeof iv, &ctr_state); ctr_setiv(iv, sizeof iv, &ctr_state);
ctr_decrypt(reinterpret_cast<uint8_t*>(&encryptedData[readDataSize]), reinterpret_cast<uint8_t*>(&decryptedData[readDataSize]), blockSize, &ctr_state); ctr_decrypt(reinterpret_cast<uint8_t*>(&encryptedData[readDataSize]), reinterpret_cast<uint8_t*>(&decryptedData[readDataSize]), blockSize, &ctr_state);
@ -3134,7 +3135,7 @@ namespace Components
// insert file data // insert file data
if (result == Z_OK) if (result == Z_OK)
{ {
std::lock_guard<std::mutex> $(fileDataMutex); std::lock_guard _(fileDataMutex);
fileDataMap[*filePointer] = data; fileDataMap[*filePointer] = data;
return unpackedSize; return unpackedSize;
} }
@ -3152,50 +3153,51 @@ namespace Components
{ {
__asm __asm
{ {
push ecx; push ecx
mov eax, [esp + 0x10]; mov eax, [esp + 0x10]
push 0x4A04C5; push 0x4A04C5
retn; ret
} }
} }
int Zones::FS_ReadHook(void* buffer, size_t size, int filePointer) int Zones::FS_ReadHook(void* buffer, size_t size, int filePointer)
{ {
std::lock_guard<std::mutex> $(fileDataMutex); std::lock_guard _(fileDataMutex);
auto itr = fileDataMap.find(filePointer); if (auto itr = fileDataMap.find(filePointer); itr != fileDataMap.end())
if (itr != fileDataMap.end())
{ {
if (!itr->second.fileContents.empty()) if (!itr->second.fileContents.empty())
{ {
const auto readSize = std::min(size, itr->second.fileContents.size() - itr->second.readPos); 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; itr->second.readPos += readSize;
return readSize; return static_cast<int>(readSize);
} }
} }
return FS_ReadOriginal(buffer, size, filePointer); return FS_ReadOriginal(buffer, size, filePointer);
} }
__declspec(naked) void Zones::FS_FCloseFileOriginal(int) __declspec(naked) void Zones::FS_FCloseFileOriginal(int)
{ {
__asm __asm
{ {
mov eax, [esp + 4]; mov eax, [esp + 4]
push esi; push esi
push 0x462005; push 0x462005
retn; ret
} }
} }
void Zones::FS_FCloseFileHook(int filePointer) void Zones::FS_FCloseFileHook(int filePointer)
{ {
std::lock_guard<std::mutex> $(fileDataMutex); std::lock_guard _(fileDataMutex);
FS_FCloseFileOriginal(filePointer); FS_FCloseFileOriginal(filePointer);
const auto itr = fileDataMap.find(filePointer); if (const auto itr = fileDataMap.find(filePointer); itr != fileDataMap.end())
if (itr != fileDataMap.end())
{ {
fileDataMap.erase(itr); fileDataMap.erase(itr);
} }
@ -3204,19 +3206,18 @@ namespace Components
{ {
__asm __asm
{ {
push esi; push esi
mov esi, [esp + 8]; mov esi, [esp + 8]
push 0x4A63D5; push 0x4A63D5
retn; ret
} }
} }
std::uint32_t Zones::FS_SeekHook(int fileHandle, int seekPosition, int seekOrigin) std::uint32_t Zones::FS_SeekHook(int fileHandle, int seekPosition, int seekOrigin)
{ {
std::lock_guard<std::mutex> $(fileDataMutex); std::lock_guard _(fileDataMutex);
const auto itr = fileDataMap.find(fileHandle); if (const auto itr = fileDataMap.find(fileHandle); itr != fileDataMap.end())
if (itr != fileDataMap.end())
{ {
if (seekOrigin == Game::FS_SEEK_SET) if (seekOrigin == Game::FS_SEEK_SET)
{ {
@ -3233,11 +3234,9 @@ namespace Components
return itr->second.readPos; return itr->second.readPos;
} }
else
{
return FS_SeekOriginal(fileHandle, seekPosition, seekOrigin); return FS_SeekOriginal(fileHandle, seekPosition, seekOrigin);
} }
}
__declspec(naked) void Zones::LoadMapTriggersModelPointer() __declspec(naked) void Zones::LoadMapTriggersModelPointer()
{ {

View File

@ -21,15 +21,17 @@ namespace Components
static void SetVersion(int version); static void SetVersion(int version);
static int Version() { return Zones::ZoneVersion; } static int Version() { return ZoneVersion; }
private: private:
static int ZoneVersion; static int ZoneVersion;
static int FxEffectIndex; static int FxEffectIndex;
static char* FxEffectStrings[64]; static char* FxEffectStrings[64];
static std::unordered_map<int, FileData> fileDataMap;
static std::mutex fileDataMutex;
static bool CheckGameMapSp(int type); static bool CheckGameMapSp(int type);
static void GameMapSpPatchStub(); static void GameMapSpPatchStub();
@ -82,8 +84,6 @@ namespace Components
static void Load_ClipInfo(bool atStreamStart); static void Load_ClipInfo(bool atStreamStart);
static int LoadClipMap(bool atStreamStart); static int LoadClipMap(bool atStreamStart);
static uint32_t HashCRC32StringInt(const std::string& Value, uint32_t Initial); static uint32_t HashCRC32StringInt(const std::string& Value, uint32_t Initial);
static std::unordered_map<int, Zones::FileData> fileDataMap;
static std::mutex fileDataMutex;
static int FS_FOpenFileReadForThreadOriginal(const char*, int*, int); static int FS_FOpenFileReadForThreadOriginal(const char*, int*, int);
static int FS_FOpenFileReadForThreadHook(const char* file, int* filePointer, int thread); static int FS_FOpenFileReadForThreadHook(const char* file, int* filePointer, int thread);
static int FS_ReadOriginal(void*, size_t, int); static int FS_ReadOriginal(void*, size_t, int);

View File

@ -90,6 +90,9 @@ namespace Game
const dvar_t** party_minplayers = reinterpret_cast<const dvar_t**>(0x1081BFC); const dvar_t** party_minplayers = reinterpret_cast<const dvar_t**>(0x1081BFC);
const dvar_t** party_maxplayers = reinterpret_cast<const dvar_t**>(0x1080998); const dvar_t** party_maxplayers = reinterpret_cast<const dvar_t**>(0x1080998);
const dvar_t** ip = reinterpret_cast<const dvar_t**>(0x64A1DF8);
const dvar_t** port = reinterpret_cast<const dvar_t**>(0x64A3004);
__declspec(naked) void Dvar_SetVariant(dvar_t*, DvarValue, DvarSetSource) __declspec(naked) void Dvar_SetVariant(dvar_t*, DvarValue, DvarSetSource)
{ {
static DWORD Dvar_SetVariant_t = 0x647400; static DWORD Dvar_SetVariant_t = 0x647400;

View File

@ -142,6 +142,9 @@ namespace Game
extern const dvar_t** party_minplayers; extern const dvar_t** party_minplayers;
extern const dvar_t** party_maxplayers; 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_SetVariant(dvar_t* var, DvarValue value, DvarSetSource source);
extern void Dvar_SetFromStringFromSource(const dvar_t* dvar, const char* string, DvarSetSource source); extern void Dvar_SetFromStringFromSource(const dvar_t* dvar, const char* string, DvarSetSource source);
} }