From 8a27c06d62d9757f414c2eaa9b9e47a7e53f6df6 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 30 Dec 2023 12:58:51 +0100 Subject: [PATCH 01/71] feature: auto updater --- src/Components/Loader.cpp | 2 + src/Components/Modules/QuickPatch.cpp | 5 +- src/Components/Modules/Singleton.cpp | 13 +++- src/Components/Modules/Singleton.hpp | 3 + src/Components/Modules/Updater.cpp | 106 ++++++++++++++++++++++++++ src/Components/Modules/Updater.hpp | 10 +++ src/Game/System.cpp | 1 + src/Game/System.hpp | 3 + src/Utils/Library.cpp | 20 ++--- 9 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 src/Components/Modules/Updater.cpp create mode 100644 src/Components/Modules/Updater.hpp diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index fc09bb18..f50b2689 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -59,6 +59,7 @@ #include "Modules/Threading.hpp" #include "Modules/Toast.hpp" #include "Modules/UIFeeder.hpp" +#include "Modules/Updater.hpp" #include "Modules/VisionFile.hpp" #include "Modules/Voice.hpp" #include "Modules/Vote.hpp" @@ -172,6 +173,7 @@ namespace Components Register(new Threading()); Register(new Toast()); Register(new UIFeeder()); + Register(new Updater()); Register(new VisionFile()); Register(new Voice()); Register(new Vote()); diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index fb3a1561..e8a7ded9 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -246,9 +246,10 @@ namespace Components auto workingDir = std::filesystem::current_path().string(); const std::string binary = *Game::sys_exitCmdLine; + const std::string command = binary == "iw4x-sp.exe" ? "iw4x-sp" : "iw4x"; SetEnvironmentVariableA("MW2_INSTALL", workingDir.data()); - Utils::Library::LaunchProcess(binary, "-singleplayer", workingDir); + Utils::Library::LaunchProcess(binary, std::format("{} --pass \"{}\"", command, GetCommandLineA()), workingDir); } __declspec(naked) void QuickPatch::SND_GetAliasOffset_Stub() @@ -320,7 +321,7 @@ namespace Components Utils::Hook::Set(0x51FCDD, QuickPatch::R_AddImageToList_Hk); - Utils::Hook::Set(0x41DB8C, "iw4-sp.exe"); + Utils::Hook::Set(0x41DB8C, "iw4x-sp.exe"); Utils::Hook(0x4D6989, QuickPatch::Sys_SpawnQuitProcess_Hk, HOOK_CALL).install()->quick(); // Fix crash as nullptr goes unchecked diff --git a/src/Components/Modules/Singleton.cpp b/src/Components/Modules/Singleton.cpp index 9ae0b43b..60c7e520 100644 --- a/src/Components/Modules/Singleton.cpp +++ b/src/Components/Modules/Singleton.cpp @@ -6,6 +6,8 @@ namespace Components { + HANDLE Singleton::Mutex; + bool Singleton::FirstInstance = true; bool Singleton::IsFirstInstance() @@ -13,6 +15,14 @@ namespace Components return FirstInstance; } + void Singleton::preDestroy() + { + if (INVALID_HANDLE_VALUE != Mutex) + { + CloseHandle(Mutex); + } + } + Singleton::Singleton() { if (Flags::HasFlag("version")) @@ -31,7 +41,8 @@ namespace Components if (Loader::IsPerformingUnitTests() || Dedicated::IsEnabled() || ZoneBuilder::IsEnabled()) return; - FirstInstance = (CreateMutexA(nullptr, FALSE, "iw4x_mutex") && GetLastError() != ERROR_ALREADY_EXISTS); + Mutex = CreateMutexA(nullptr, FALSE, "iw4x_mutex"); + FirstInstance = ((INVALID_HANDLE_VALUE != Mutex) && GetLastError() != ERROR_ALREADY_EXISTS); if (!FirstInstance && !ConnectProtocol::Used() && MessageBoxA(nullptr, "Do you want to start another instance?\nNot all features will be available!", "Game already running", MB_ICONEXCLAMATION | MB_YESNO) == IDNO) { diff --git a/src/Components/Modules/Singleton.hpp b/src/Components/Modules/Singleton.hpp index db2a11c8..1f5de4e3 100644 --- a/src/Components/Modules/Singleton.hpp +++ b/src/Components/Modules/Singleton.hpp @@ -7,9 +7,12 @@ namespace Components public: Singleton(); + void preDestroy() override; + static bool IsFirstInstance(); private: + static HANDLE Mutex; static bool FirstInstance; }; } diff --git a/src/Components/Modules/Updater.cpp b/src/Components/Modules/Updater.cpp new file mode 100644 index 00000000..9a5964aa --- /dev/null +++ b/src/Components/Modules/Updater.cpp @@ -0,0 +1,106 @@ +#include + +#include "Updater.hpp" +#include "Scheduler.hpp" + +#include + +#include +#include +#include + +namespace Components +{ + namespace + { + const Game::dvar_t* cl_updateAvailable; + + // If they use the alterware-launcher once to install they will have this file + // If they don't, what are they waiting for? + constexpr auto* revisionFile = ".iw4xrevision"; + + void CheckForUpdate() + { + std::string revision; + if (!Utils::IO::ReadFile(revisionFile, &revision) || revision.empty()) + { + Logger::Print(".iw4xrevision does not exist. Notifying the user an update is available\n"); + Game::Dvar_SetBool(cl_updateAvailable, true); + } + + const auto result = Utils::WebIO("IW4x", "https://api.github.com/repos/iw4x/iw4x-client/releases/latest").setTimeout(5000)->get(); + if (result.empty()) + { + // Nothing to do in this situation. We won't know if we need to update or not + Logger::Print("Could not fetch latest tag from GitHub\n"); + return; + } + + rapidjson::Document doc{}; + const rapidjson::ParseResult parseResult = doc.Parse(result); + if (!parseResult || !doc.IsObject()) + { + // Nothing to do in this situation. We won't know if we need to update or not + Logger::Print("GitHub sent an invalid reply\n"); + return; + } + + if (!doc.HasMember("tag_name") || !doc["tag_name"].IsString()) + { + // Nothing to do in this situation. We won't know if we need to update or not + Logger::Print("GitHub sent an invalid reply\n"); + return; + } + + const auto* tag = doc["tag_name"].GetString(); + if (revision != tag) + { + // A new version came out! + Game::Dvar_SetBool(cl_updateAvailable, true); + } + } + + std::optional GetLauncher() + { + if (Utils::IO::FileExists("alterware-launcher.exe")) + { + return "alterware-launcher.exe"; + } + + if (Utils::IO::FileExists("alterware-launcher-x86.exe")) + { + return "alterware-launcher-x86.exe"; + } + + if (Utils::IsWineEnvironment() && Utils::IO::FileExists("alterware-launcher")) + { + return "alterware-launcher"; + } + + return {}; + } + } + + Updater::Updater() + { + cl_updateAvailable = Game::Dvar_RegisterBool("cl_updateAvailable", false, Game::DVAR_NONE, "Whether an update is available or not"); + Scheduler::Once(CheckForUpdate, Scheduler::Pipeline::ASYNC); + + UIScript::Add("checkForUpdate", [](const UIScript::Token& /*token*/, const Game::uiInfo_s* /*info*/) + { + CheckForUpdate(); + }); + + UIScript::Add("getAutoUpdate", [](const UIScript::Token& /*token*/, const Game::uiInfo_s* /*info*/) + { + if (const auto exe = GetLauncher(); exe.has_value()) + { + Game::Sys_QuitAndStartProcess(exe.value().data()); + return; + } + + // No launcher was found on the system, time to tell them to download it from GitHub + Utils::OpenUrl("https://forum.alterware.dev/t/how-to-install-the-alterware-launcher/56"); + }); + } +} diff --git a/src/Components/Modules/Updater.hpp b/src/Components/Modules/Updater.hpp new file mode 100644 index 00000000..46e65980 --- /dev/null +++ b/src/Components/Modules/Updater.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace Components +{ + class Updater : public Component + { + public: + Updater(); + }; +} diff --git a/src/Game/System.cpp b/src/Game/System.cpp index 50f9d3ab..0571204a 100644 --- a/src/Game/System.cpp +++ b/src/Game/System.cpp @@ -25,6 +25,7 @@ namespace Game Sys_SetValue_t Sys_SetValue = Sys_SetValue_t(0x4B2F50); Sys_CreateFile_t Sys_CreateFile = Sys_CreateFile_t(0x4B2EF0); Sys_OutOfMemErrorInternal_t Sys_OutOfMemErrorInternal = Sys_OutOfMemErrorInternal_t(0x4B2E60); + Sys_QuitAndStartProcess_t Sys_QuitAndStartProcess = Sys_QuitAndStartProcess_t(0x45FCF0); char(*sys_exitCmdLine)[1024] = reinterpret_cast(0x649FB68); diff --git a/src/Game/System.hpp b/src/Game/System.hpp index 70b37c1c..84aff611 100644 --- a/src/Game/System.hpp +++ b/src/Game/System.hpp @@ -71,6 +71,9 @@ namespace Game typedef void(*Sys_OutOfMemErrorInternal_t)(const char* filename, int line); extern Sys_OutOfMemErrorInternal_t Sys_OutOfMemErrorInternal; + typedef void(*Sys_QuitAndStartProcess_t)(const char*); + extern Sys_QuitAndStartProcess_t Sys_QuitAndStartProcess; + extern char(*sys_exitCmdLine)[1024]; extern RTL_CRITICAL_SECTION* s_criticalSection; diff --git a/src/Utils/Library.cpp b/src/Utils/Library.cpp index f7740ff5..70d661de 100644 --- a/src/Utils/Library.cpp +++ b/src/Utils/Library.cpp @@ -104,20 +104,20 @@ namespace Utils void Library::LaunchProcess(const std::string& process, const std::string& commandLine, const std::string& currentDir) { - STARTUPINFOA startup_info; - PROCESS_INFORMATION process_info; + STARTUPINFOA startupInfo; + PROCESS_INFORMATION processInfo; - ZeroMemory(&startup_info, sizeof(startup_info)); - ZeroMemory(&process_info, sizeof(process_info)); - startup_info.cb = sizeof(startup_info); + ZeroMemory(&startupInfo, sizeof(startupInfo)); + ZeroMemory(&processInfo, sizeof(processInfo)); + startupInfo.cb = sizeof(startupInfo); CreateProcessA(process.data(), const_cast(commandLine.data()), nullptr, nullptr, false, NULL, nullptr, currentDir.data(), - &startup_info, &process_info); + &startupInfo, &processInfo); - if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) - CloseHandle(process_info.hThread); - if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) - CloseHandle(process_info.hProcess); + if (processInfo.hThread && processInfo.hThread != INVALID_HANDLE_VALUE) + CloseHandle(processInfo.hThread); + if (processInfo.hProcess && processInfo.hProcess != INVALID_HANDLE_VALUE) + CloseHandle(processInfo.hProcess); } } From 0b349f2bd896a27429a66b88075c62e8484bbc7c Mon Sep 17 00:00:00 2001 From: Diavolo Date: Fri, 12 Jan 2024 09:48:22 +0100 Subject: [PATCH 02/71] address review n1 --- src/Components/Modules/Updater.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Components/Modules/Updater.cpp b/src/Components/Modules/Updater.cpp index 9a5964aa..0ea98f22 100644 --- a/src/Components/Modules/Updater.cpp +++ b/src/Components/Modules/Updater.cpp @@ -6,8 +6,6 @@ #include #include -#include -#include namespace Components { @@ -17,18 +15,20 @@ namespace Components // If they use the alterware-launcher once to install they will have this file // If they don't, what are they waiting for? - constexpr auto* revisionFile = ".iw4xrevision"; + constexpr auto* REVISION_FILE = ".iw4xrevision"; + constexpr auto* GITHUB_REMOTE_URL = "https://api.github.com/repos/iw4x/iw4x-client/releases/latest"; + constexpr auto* INSTALL_GUIDE_REMOTE_URL = "https://forum.alterware.dev/t/how-to-install-the-alterware-launcher/56"; void CheckForUpdate() { std::string revision; - if (!Utils::IO::ReadFile(revisionFile, &revision) || revision.empty()) + if (!Utils::IO::ReadFile(REVISION_FILE, &revision) || revision.empty()) { - Logger::Print(".iw4xrevision does not exist. Notifying the user an update is available\n"); + Logger::Print("{} does not exist. Notifying the user an update is available\n", REVISION_FILE); Game::Dvar_SetBool(cl_updateAvailable, true); } - const auto result = Utils::WebIO("IW4x", "https://api.github.com/repos/iw4x/iw4x-client/releases/latest").setTimeout(5000)->get(); + const auto result = Utils::WebIO("IW4x", GITHUB_REMOTE_URL).setTimeout(5000)->get(); if (result.empty()) { // Nothing to do in this situation. We won't know if we need to update or not @@ -60,6 +60,7 @@ namespace Components } } + // Depending on Linux/Windows 32/64 there are a few things we must check std::optional GetLauncher() { if (Utils::IO::FileExists("alterware-launcher.exe")) @@ -100,7 +101,7 @@ namespace Components } // No launcher was found on the system, time to tell them to download it from GitHub - Utils::OpenUrl("https://forum.alterware.dev/t/how-to-install-the-alterware-launcher/56"); + Utils::OpenUrl(INSTALL_GUIDE_REMOTE_URL); }); } } From 4059bf4f822fb74ea7155293088f70ddd5fcace8 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Fri, 12 Jan 2024 09:53:58 +0100 Subject: [PATCH 03/71] feature: fail2ban native integration --- src/Components/Modules/Auth.cpp | 5 +++ src/Components/Modules/Logger.cpp | 47 ++++++++++++++++++++++++++--- src/Components/Modules/Logger.hpp | 16 +++++++++- src/Components/Modules/Network.cpp | 30 ++++++++++++++++++ src/Components/Modules/Network.hpp | 3 ++ src/Components/Modules/RCon.cpp | 16 +++++++--- src/Components/Modules/Security.cpp | 1 + src/Components/Modules/Voice.cpp | 1 + 8 files changed, 108 insertions(+), 11 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 42269ba9..d46e58fb 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -144,6 +144,7 @@ namespace Components Proto::Auth::Connect connectData; if (msg->cursize <= 12 || !connectData.ParseFromString(std::string(reinterpret_cast(&msg->data[12]), msg->cursize - 12))) { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nInvalid connect packet!"); return; } @@ -161,6 +162,7 @@ namespace Components } else { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nInvalid infostring data!"); } } @@ -170,6 +172,7 @@ namespace Components // Validate proto data if (connectData.signature().empty() || connectData.publickey().empty() || connectData.token().empty() || connectData.infostring().empty()) { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nInvalid connect data!"); return; } @@ -184,6 +187,7 @@ namespace Components // Ensure there are enough params if (params.size() < 3) { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nInvalid connect string!"); return; } @@ -197,6 +201,7 @@ namespace Components if (steamId.empty() || challenge.empty()) { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nInvalid connect data!"); return; } diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index d575efe3..1d90e816 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -13,7 +13,8 @@ namespace Components std::recursive_mutex Logger::LoggingMutex; std::vector Logger::LoggingAddresses[2]; - Dvar::Var Logger::IW4x_oneLog; + Dvar::Var Logger::IW4x_one_log; + Dvar::Var Logger::IW4x_fail2ban_location; void(*Logger::PipeCallback)(const std::string&) = nullptr;; @@ -43,8 +44,8 @@ namespace Components if (shouldPrint) { - std::printf("%s", msg.data()); - std::fflush(stdout); + (void)std::fputs(msg.data(), stdout); + (void)std::fflush(stdout); return; } @@ -116,6 +117,38 @@ namespace Components MessagePrint(channel, msg); } + void Logger::PrintFail2BanInternal(const std::string_view& fmt, std::format_args&& args) + { + static const auto shouldPrint = []() -> bool + { + return Flags::HasFlag("fail2ban"); + }(); + + if (!shouldPrint) + { + return; + } + + auto msg = std::vformat(fmt, args); + + static auto log_next_time_stamp = true; + if (log_next_time_stamp) + { + auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + // Convert to local time + std::tm timeInfo = *std::localtime(&now); + + std::ostringstream ss; + ss << std::put_time(&timeInfo, "%Y-%m-%d %H:%M:%S "); + + msg.insert(0, ss.str()); + } + + log_next_time_stamp = (msg.find('\n') != std::string::npos); + + Utils::IO::WriteFile(IW4x_fail2ban_location.get(), msg, true); + } + void Logger::Frame() { std::unique_lock _(MessageMutex); @@ -233,7 +266,7 @@ namespace Components { if (std::strcmp(folder, "userraw") != 0) { - if (IW4x_oneLog.get()) + if (IW4x_one_log.get()) { strncpy_s(folder, 256, "userraw", _TRUNCATE); } @@ -388,7 +421,6 @@ namespace Components Logger::Logger() { - IW4x_oneLog = Dvar::Register("iw4x_onelog", false, Game::DVAR_LATCH, "Only write the game log to the 'userraw' OS folder"); Utils::Hook(0x642139, BuildOSPath_Stub, HOOK_JUMP).install()->quick(); Scheduler::Loop(Frame, Scheduler::Pipeline::SERVER); @@ -405,6 +437,11 @@ namespace Components } Events::OnSVInit(AddServerCommands); + Events::OnDvarInit([] + { + IW4x_one_log = Dvar::Register("iw4x_onelog", false, Game::DVAR_LATCH, "Only write the game log to the 'userraw' OS folder"); + IW4x_fail2ban_location = Dvar::Register("iw4x_fail2ban_location", "/var/log/iw4x.log", Game::DVAR_NONE, "Fail2Ban logfile location"); + }); } Logger::~Logger() diff --git a/src/Components/Modules/Logger.hpp b/src/Components/Modules/Logger.hpp index 2ada2a40..8b43704d 100644 --- a/src/Components/Modules/Logger.hpp +++ b/src/Components/Modules/Logger.hpp @@ -18,6 +18,7 @@ namespace Components static void ErrorInternal(Game::errorParm_t error, const std::string_view& fmt, std::format_args&& args); static void PrintErrorInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args); static void WarningInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args); + static void PrintFail2BanInternal(const std::string_view& fmt, std::format_args&& args); static void DebugInternal(const std::string_view& fmt, std::format_args&& args, const std::source_location& loc); static void Print(const std::string_view& fmt) @@ -80,6 +81,18 @@ namespace Components PrintErrorInternal(channel, fmt, std::make_format_args(args...)); } + static void PrintFail2Ban(const std::string_view& fmt) + { + PrintFail2BanInternal(fmt, std::make_format_args(0)); + } + + template + static void PrintFail2Ban(const std::string_view& fmt, Args&&... args) + { + (Utils::String::SanitizeFormatArgs(args), ...); + PrintFail2BanInternal(fmt, std::make_format_args(args...)); + } + struct FormatWithLocation { std::string_view format; @@ -114,7 +127,8 @@ namespace Components static std::recursive_mutex LoggingMutex; static std::vector LoggingAddresses[2]; - static Dvar::Var IW4x_oneLog; + static Dvar::Var IW4x_one_log; + static Dvar::Var IW4x_fail2ban_location; static void(*PipeCallback)(const std::string&); diff --git a/src/Components/Modules/Network.cpp b/src/Components/Modules/Network.cpp index fe987d0d..7ca0d506 100644 --- a/src/Components/Modules/Network.cpp +++ b/src/Components/Modules/Network.cpp @@ -39,6 +39,11 @@ namespace Components return ntohs(this->address.port); } + unsigned short Network::Address::getPortRaw() const + { + return this->address.port; + } + void Network::Address::setIP(DWORD ip) { this->address.ip.full = ip; @@ -151,6 +156,31 @@ namespace Components return (this->getType() != Game::NA_BAD && this->getType() >= Game::NA_BOT && this->getType() <= Game::NA_IP); } + const char* Network::AdrToString(const Address& a, const bool port) + { + if (a.getType() == Game::netadrtype_t::NA_LOOPBACK) + { + return "loopback"; + } + + if (a.getType() == Game::netadrtype_t::NA_BOT) + { + return "bot"; + } + + if (a.getType() == Game::netadrtype_t::NA_IP || a.getType() == Game::netadrtype_t::NA_BROADCAST) + { + if (a.getPort() && port) + { + return Utils::String::VA("%u.%u.%u.%u:%u", a.getIP().bytes[0], a.getIP().bytes[1], a.getIP().bytes[2], a.getIP().bytes[3], htons(a.getPortRaw())); + } + + return Utils::String::VA("%u.%u.%u.%u", a.getIP().bytes[0], a.getIP().bytes[1], a.getIP().bytes[2], a.getIP().bytes[3]); + } + + return "bad"; + } + void Network::Send(Game::netsrc_t type, const Address& target, const std::string& data) { // Do not use NET_OutOfBandPrint. It only supports non-binary data! diff --git a/src/Components/Modules/Network.hpp b/src/Components/Modules/Network.hpp index d4382942..5c108c09 100644 --- a/src/Components/Modules/Network.hpp +++ b/src/Components/Modules/Network.hpp @@ -22,6 +22,7 @@ namespace Components void setPort(unsigned short port); [[nodiscard]] unsigned short getPort() const; + [[nodiscard]] unsigned short getPortRaw() const; void setIP(DWORD ip); void setIP(Game::netIP_t ip); @@ -51,6 +52,8 @@ namespace Components Network(); + static const char* AdrToString(const Address& a, bool port = false); + static std::uint16_t GetPort(); // Send quake-styled binary data diff --git a/src/Components/Modules/RCon.cpp b/src/Components/Modules/RCon.cpp index 369b903d..87a20462 100644 --- a/src/Components/Modules/RCon.cpp +++ b/src/Components/Modules/RCon.cpp @@ -180,7 +180,8 @@ namespace Components const auto pos = data.find_first_of(' '); if (pos == std::string::npos) { - Logger::Print(Game::CON_CHANNEL_NETWORK, "Invalid RCon request from {}\n", address.getString()); + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(address)); + Logger::Print(Game::CON_CHANNEL_NETWORK, "Invalid RCon request from {}\n", Network::AdrToString(address)); return; } @@ -203,7 +204,8 @@ namespace Components if (svPassword != password) { - Logger::Print(Game::CON_CHANNEL_NETWORK, "Invalid RCon password sent from {}\n", address.getString()); + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(address)); + Logger::Print(Game::CON_CHANNEL_NETWORK, "Invalid RCon password sent from {}\n", Network::AdrToString(address)); return; } @@ -213,7 +215,7 @@ namespace Components if (RConLogRequests.get()) #endif { - Logger::Print(Game::CON_CHANNEL_NETWORK, "Executing RCon request from {}: {}\n", address.getString(), command); + Logger::Print(Game::CON_CHANNEL_NETWORK, "Executing RCon request from {}: {}\n", Network::AdrToString(address), command); } Logger::PipeOutput([](const std::string& output) @@ -318,6 +320,7 @@ namespace Components const auto time = Game::Sys_Milliseconds(); if (!IsRateLimitCheckDisabled() && !RateLimitCheck(address, time)) { + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(address)); return; } @@ -341,6 +344,7 @@ namespace Components const auto time = Game::Sys_Milliseconds(); if (!IsRateLimitCheckDisabled() && !RateLimitCheck(address, time)) { + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(address)); return; } @@ -360,13 +364,15 @@ namespace Components Proto::RCon::Command directive; if (!directive.ParseFromString(data)) { - Logger::PrintError(Game::CON_CHANNEL_NETWORK, "Unable to parse secure command from {}\n", address.getString()); + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(address)); + Logger::PrintError(Game::CON_CHANNEL_NETWORK, "Unable to parse secure command from {}\n", Network::AdrToString(address)); return; } if (!Utils::Cryptography::RSA::VerifyMessage(key, directive.command(), directive.signature())) { - Logger::PrintError(Game::CON_CHANNEL_NETWORK, "RSA signature verification failed for message from {}\n", address.getString()); + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(address)); + Logger::PrintError(Game::CON_CHANNEL_NETWORK, "RSA signature verification failed for message from {}\n", Network::AdrToString(address)); return; } diff --git a/src/Components/Modules/Security.cpp b/src/Components/Modules/Security.cpp index 240407db..64b01e61 100644 --- a/src/Components/Modules/Security.cpp +++ b/src/Components/Modules/Security.cpp @@ -119,6 +119,7 @@ namespace Components { if ((client->reliableSequence - client->reliableAcknowledge) < 0) { + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(client->header.netchan.remoteAddress)); Logger::Print(Game::CON_CHANNEL_NETWORK, "Negative reliableAcknowledge from {} - cl->reliableSequence is {}, reliableAcknowledge is {}\n", client->name, client->reliableSequence, client->reliableAcknowledge); client->reliableAcknowledge = client->reliableSequence; diff --git a/src/Components/Modules/Voice.cpp b/src/Components/Modules/Voice.cpp index 7d7fd1cf..c8c66e69 100644 --- a/src/Components/Modules/Voice.cpp +++ b/src/Components/Modules/Voice.cpp @@ -168,6 +168,7 @@ namespace Components voicePacket.dataSize = Game::MSG_ReadByte(msg); if (voicePacket.dataSize <= 0 || voicePacket.dataSize > MAX_VOICE_PACKET_DATA) { + Logger::PrintFail2Ban("Invalid packet from IP address: {}\n", Network::AdrToString(cl->header.netchan.remoteAddress)); Logger::Print(Game::CON_CHANNEL_SERVER, "Received invalid voice packet of size {} from {}\n", voicePacket.dataSize, cl->name); return; } From d1dd4af6af1f81fb7ffde7cd0938b68ebd6fae23 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 13 Jan 2024 13:15:16 +0100 Subject: [PATCH 04/71] fix(download): restore password verification --- src/Components/Modules/Download.cpp | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 43433425..927f4946 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -462,6 +462,40 @@ namespace Components mg_http_reply(connection, 200, formatted.c_str(), "%s", data.c_str()); } + bool VerifyPassword([[maybe_unused]] mg_connection* c, [[maybe_unused]] const mg_http_message* hm) + { + const std::string g_password = *Game::g_password ? (*Game::g_password)->current.string : ""; + if (g_password.empty()) return true; + + // SHA256 hashes are 64 characters long but we're gonna be safe here + char buffer[128]{}; + const auto len = mg_http_get_var(&hm->query, "password", buffer, sizeof(buffer)); + + const auto reply = [&c](const char* s) -> void + { + mg_printf(c, "%s", "HTTP/1.1 403 Forbidden\r\n"); + mg_printf(c, "%s", "Content-Type: text/plain\r\n"); + mg_printf(c, "Connection: close\r\n"); + mg_printf(c, "%s", "\r\n"); + mg_printf(c, "%s", s); + }; + + if (len <= 0) + { + reply("Password Required"); + return false; + } + + const auto password = std::string(buffer, len); + if (password != Utils::String::DumpHex(Utils::Cryptography::SHA256::Compute(g_password), "")) + { + reply("Invalid Password"); + return false; + } + + return true; + } + std::optional Download::InfoHandler([[maybe_unused]] mg_connection* c, [[maybe_unused]] const mg_http_message* hm) { if (!(*Game::com_sv_running)->current.enabled) @@ -524,6 +558,12 @@ namespace Components static nlohmann::json jsonList; static std::filesystem::path fsGamePre; + if (!VerifyPassword(c, hm)) + { + // Custom reply done in VerifyPassword + return {}; + } + const std::filesystem::path fsGame = (*Game::fs_gameDirVar)->current.string; if (!fsGame.empty() && (fsGamePre != fsGame)) @@ -572,6 +612,12 @@ namespace Components static std::string mapNamePre; static nlohmann::json jsonList; + if (!VerifyPassword(c, hm)) + { + // Custom reply done in VerifyPassword + return {}; + } + const std::string mapName = Party::IsInUserMapLobby() ? (*Game::ui_mapname)->current.string : Maps::GetUserMap()->getName(); if (!Maps::GetUserMap()->isValid() && !Party::IsInUserMapLobby()) { From afb9e5da47b5b3882a9ec97f92f57e8c73a4aafd Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 13 Jan 2024 18:08:06 +0100 Subject: [PATCH 05/71] Fixes for weapon writing --- src/Components/Modules/AssetInterfaces/IWeapon.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/AssetInterfaces/IWeapon.cpp b/src/Components/Modules/AssetInterfaces/IWeapon.cpp index e256ebcd..dc27bef7 100644 --- a/src/Components/Modules/AssetInterfaces/IWeapon.cpp +++ b/src/Components/Modules/AssetInterfaces/IWeapon.cpp @@ -396,6 +396,7 @@ namespace Assets continue; } + buffer->align(Utils::Stream::ALIGN_4); buffer->saveMax(sizeof(Game::snd_alias_list_t*)); buffer->saveString(def->bounceSound[i]->aliasName); } @@ -525,7 +526,7 @@ namespace Assets if (def->physCollmap) { - dest->physCollmap = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_PHYSCOLLMAP, def->overlayMaterialEMPLowRes).physCollmap; + dest->physCollmap = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_PHYSCOLLMAP, def->physCollmap).physCollmap; } if (def->projectileModel) From fea8a51abba674f3c61877b8c32c46c97b211fb8 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 13 Jan 2024 19:09:43 +0100 Subject: [PATCH 06/71] Fix sqr distance overflowing over 32 --- src/Components/Modules/Renderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 03cd72ca..191b82f5 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -382,7 +382,7 @@ namespace Components auto world = gfxAsset->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); - auto sqrDist = drawDistance * drawDistance; + auto sqrDist = drawDistance * static_cast(drawDistance); switch (val) { From a39435f800096ad5ef4b72e9b0899baf94c9d1ec Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 13 Jan 2024 19:12:11 +0100 Subject: [PATCH 07/71] Bump IW4OF --- deps/iw4-open-formats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/iw4-open-formats b/deps/iw4-open-formats index 700a2ae8..3468c066 160000 --- a/deps/iw4-open-formats +++ b/deps/iw4-open-formats @@ -1 +1 @@ -Subproject commit 700a2ae858c27568d95e21ce8c5f941d36c4a6c4 +Subproject commit 3468c0665c4d27e43b92945bcb295103394cdfd0 From 6be4b617b82415a9a9a2178e479b0cc9aa00b8c6 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 13 Jan 2024 20:10:26 +0100 Subject: [PATCH 08/71] Restore some code that was hastily removed, and add a warning for images of missing category --- src/Components/Modules/AssetHandler.cpp | 12 ++ src/Components/Modules/StructuredData.cpp | 159 +++++++++++++++++++++- 2 files changed, 167 insertions(+), 4 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index ae538b8b..b76def14 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -224,6 +224,18 @@ namespace Components void AssetHandler::ModifyAsset(Game::XAssetType type, Game::XAssetHeader asset, const std::string& name) { +#ifdef DEBUG + if (type == Game::XAssetType::ASSET_TYPE_IMAGE && name[0] != ',') + { + const auto image = asset.image; + const auto cat = static_cast(image->category); + if (cat == Game::ImageCategory::IMG_CATEGORY_UNKNOWN) + { + Logger::Warning(Game::CON_CHANNEL_GFX, "Image {} has wrong category IMG_CATEGORY_UNKNOWN, this is an IMPORTANT ISSUE that should be fixed!\n", name); + } + } +#endif + if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) { if (Zones::Version() >= VERSION_ALPHA2) diff --git a/src/Components/Modules/StructuredData.cpp b/src/Components/Modules/StructuredData.cpp index 7036559e..80a1fb8c 100644 --- a/src/Components/Modules/StructuredData.cpp +++ b/src/Components/Modules/StructuredData.cpp @@ -149,10 +149,161 @@ namespace Components { if (Dedicated::IsEnabled()) return; - // Correctly upgrade stats - Utils::Hook(0x42F088, StructuredData::UpdateVersionOffsets, HOOK_CALL).install()->quick(); + // Do not execute this when building zones + if (!ZoneBuilder::IsEnabled()) + { + // Correctly upgrade stats + Utils::Hook(0x42F088, StructuredData::UpdateVersionOffsets, HOOK_CALL).install()->quick(); - // 15 or more custom classes - Utils::Hook::Set(0x60A2FE, NUM_CUSTOM_CLASSES); + // 15 or more custom classes + Utils::Hook::Set(0x60A2FE, NUM_CUSTOM_CLASSES); + + return; + } + + + // TODO: Since all of the following is zonebuilder-only code, move it to IW4OF or IStructuredDataDefSet.cpp + AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, const std::string& filename, bool* /*restrict*/) + { + // Only intercept playerdatadef loading + if (type != Game::ASSET_TYPE_STRUCTURED_DATA_DEF || filename != "mp/playerdata.def") return; + + // Store asset + Game::StructuredDataDefSet* data = asset.structuredDataDefSet; + if (!data) return; + + if (data->defCount != 1) + { + Logger::Error(Game::ERR_FATAL, "PlayerDataDefSet contains more than 1 definition!"); + return; + } + + if (data->defs[0].version != 155) + { + Logger::Error(Game::ERR_FATAL, "Initial PlayerDataDef is not version 155, patching not possible!"); + return; + } + + std::unordered_map>> patchDefinitions; + std::unordered_map> otherPatchDefinitions; + + // First check if all versions are present + for (int i = 156;; ++i) + { + // We're on DB thread (OnLoad) so use DB thread for FS + FileSystem::File definition(std::format("{}/{}.json", filename, i), Game::FsThread::FS_THREAD_DATABASE); + if (!definition.exists()) break; + + std::vector> enumContainer; + std::unordered_map otherPatches; + + nlohmann::json defData; + try + { + defData = nlohmann::json::parse(definition.getBuffer()); + } + catch (const nlohmann::json::parse_error& ex) + { + Logger::PrintError(Game::CON_CHANNEL_ERROR, "JSON Parse Error: {}\n", ex.what()); + return; + } + + if (!defData.is_object()) + { + Logger::Error(Game::ERR_FATAL, "PlayerDataDef patch for version {} is invalid!", i); + return; + } + + for (auto pType = 0; pType < StructuredData::PlayerDataType::COUNT; ++pType) + { + auto enumData = defData[StructuredData::EnumTranslation[pType]]; + + std::vector entryData; + + if (enumData.is_array()) + { + for (const auto& rawEntry : enumData) + { + if (rawEntry.is_string()) + { + entryData.push_back(rawEntry.get()); + } + } + } + + enumContainer.push_back(entryData); + } + + auto other = defData["other"]; + + if (other.is_object()) + { + for (auto& item : other.items()) + { + if (item.value().is_string()) + { + otherPatches[item.key()] = item.value().get(); + } + } + } + + patchDefinitions[i] = enumContainer; + otherPatchDefinitions[i] = otherPatches; + } + + // Nothing to patch + if (patchDefinitions.empty()) return; + + // Reallocate the definition + auto* newData = StructuredData::MemAllocator.allocateArray(data->defCount + patchDefinitions.size()); + std::memcpy(&newData[patchDefinitions.size()], data->defs, sizeof Game::StructuredDataDef * data->defCount); + + // Prepare the buffers + for (unsigned int i = 0; i < patchDefinitions.size(); ++i) + { + std::memcpy(&newData[i], data->defs, sizeof Game::StructuredDataDef); + newData[i].version = (patchDefinitions.size() - i) + 155; + + // Reallocate the enum array + auto* newEnums = StructuredData::MemAllocator.allocateArray(data->defs->enumCount); + std::memcpy(newEnums, data->defs->enums, sizeof Game::StructuredDataEnum * data->defs->enumCount); + newData[i].enums = newEnums; + } + + // Apply new data + data->defs = newData; + data->defCount += patchDefinitions.size(); + + // Patch the definition + for (unsigned int i = 0; i < data->defCount; ++i) + { + // No need to patch version 155 + if (newData[i].version == 155) continue; + + if (patchDefinitions.contains(newData[i].version)) + { + auto patchData = patchDefinitions[newData[i].version]; + auto otherData = otherPatchDefinitions[newData[i].version]; + + // Invalid patch data + if (patchData.size() != StructuredData::PlayerDataType::COUNT) + { + Logger::Error(Game::ERR_FATAL, "PlayerDataDef patch for version {} wasn't parsed correctly!", newData[i].version); + continue; + } + + // Apply the patch data + for (auto pType = 0; pType < StructuredData::PlayerDataType::COUNT; ++pType) + { + if (!patchData[pType].empty()) + { + StructuredData::PatchPlayerDataEnum(&newData[i], static_cast(pType), patchData[pType]); + } + } + + StructuredData::PatchAdditionalData(&newData[i], otherData); + } + } + }); } } From 4badcdd0086f976d88c0b94e1abeaa8afd1fb29b Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 13 Jan 2024 20:10:47 +0100 Subject: [PATCH 09/71] Bump IW4OF --- deps/iw4-open-formats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/iw4-open-formats b/deps/iw4-open-formats index 3468c066..f174f1c4 160000 --- a/deps/iw4-open-formats +++ b/deps/iw4-open-formats @@ -1 +1 @@ -Subproject commit 3468c0665c4d27e43b92945bcb295103394cdfd0 +Subproject commit f174f1c4c4c465c337166b92e03b355695c5bc93 From 70f34a93192dda647a9a5937b48109c7bf2d47e1 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 13 Jan 2024 21:10:15 +0100 Subject: [PATCH 10/71] No structured data shenanigans while dumping --- src/Components/Modules/StructuredData.cpp | 232 +++++++++++----------- src/Components/Modules/ZoneBuilder.hpp | 3 +- 2 files changed, 120 insertions(+), 115 deletions(-) diff --git a/src/Components/Modules/StructuredData.cpp b/src/Components/Modules/StructuredData.cpp index 80a1fb8c..34f9e6c9 100644 --- a/src/Components/Modules/StructuredData.cpp +++ b/src/Components/Modules/StructuredData.cpp @@ -73,12 +73,12 @@ namespace Components // Sort alphabetically qsort(indices, dataVector.size(), sizeof(Game::StructuredDataEnumEntry), [](void const* first, void const* second) - { - const Game::StructuredDataEnumEntry* entry1 = reinterpret_cast(first); - const Game::StructuredDataEnumEntry* entry2 = reinterpret_cast(second); + { + const Game::StructuredDataEnumEntry* entry1 = reinterpret_cast(first); + const Game::StructuredDataEnumEntry* entry2 = reinterpret_cast(second); - return std::string(entry1->string).compare(entry2->string); - }); + return std::string(entry1->string).compare(entry2->string); + }); // Apply our patches dataEnum->entryCount = dataVector.size(); @@ -117,7 +117,7 @@ namespace Components } } - bool StructuredData::UpdateVersionOffsets(Game::StructuredDataDefSet *set, Game::StructuredDataBuffer *buffer, Game::StructuredDataDef *whatever) + bool StructuredData::UpdateVersionOffsets(Game::StructuredDataDefSet* set, Game::StructuredDataBuffer* buffer, Game::StructuredDataDef* whatever) { Game::StructuredDataDef* newDef = &set->defs[0]; Game::StructuredDataDef* oldDef = &set->defs[0]; @@ -164,146 +164,150 @@ namespace Components // TODO: Since all of the following is zonebuilder-only code, move it to IW4OF or IStructuredDataDefSet.cpp AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, const std::string& filename, bool* /*restrict*/) - { - // Only intercept playerdatadef loading - if (type != Game::ASSET_TYPE_STRUCTURED_DATA_DEF || filename != "mp/playerdata.def") return; - - // Store asset - Game::StructuredDataDefSet* data = asset.structuredDataDefSet; - if (!data) return; - - if (data->defCount != 1) { - Logger::Error(Game::ERR_FATAL, "PlayerDataDefSet contains more than 1 definition!"); - return; - } - - if (data->defs[0].version != 155) - { - Logger::Error(Game::ERR_FATAL, "Initial PlayerDataDef is not version 155, patching not possible!"); - return; - } - - std::unordered_map>> patchDefinitions; - std::unordered_map> otherPatchDefinitions; - - // First check if all versions are present - for (int i = 156;; ++i) - { - // We're on DB thread (OnLoad) so use DB thread for FS - FileSystem::File definition(std::format("{}/{}.json", filename, i), Game::FsThread::FS_THREAD_DATABASE); - if (!definition.exists()) break; - - std::vector> enumContainer; - std::unordered_map otherPatches; - - nlohmann::json defData; - try - { - defData = nlohmann::json::parse(definition.getBuffer()); - } - catch (const nlohmann::json::parse_error& ex) - { - Logger::PrintError(Game::CON_CHANNEL_ERROR, "JSON Parse Error: {}\n", ex.what()); + if (ZoneBuilder::IsDumpingZone()) { return; } - if (!defData.is_object()) + // Only intercept playerdatadef loading + if (type != Game::ASSET_TYPE_STRUCTURED_DATA_DEF || filename != "mp/playerdata.def") return; + + // Store asset + Game::StructuredDataDefSet* data = asset.structuredDataDefSet; + if (!data) return; + + if (data->defCount != 1) { - Logger::Error(Game::ERR_FATAL, "PlayerDataDef patch for version {} is invalid!", i); + Logger::Error(Game::ERR_FATAL, "PlayerDataDefSet contains more than 1 definition!"); return; } - for (auto pType = 0; pType < StructuredData::PlayerDataType::COUNT; ++pType) + if (data->defs[0].version != 155) { - auto enumData = defData[StructuredData::EnumTranslation[pType]]; + Logger::Error(Game::ERR_FATAL, "Initial PlayerDataDef is not version 155, patching not possible!"); + return; + } - std::vector entryData; + std::unordered_map>> patchDefinitions; + std::unordered_map> otherPatchDefinitions; - if (enumData.is_array()) + // First check if all versions are present + for (int i = 156;; ++i) + { + // We're on DB thread (OnLoad) so use DB thread for FS + FileSystem::File definition(std::format("{}/{}.json", filename, i), Game::FsThread::FS_THREAD_DATABASE); + if (!definition.exists()) break; + + std::vector> enumContainer; + std::unordered_map otherPatches; + + nlohmann::json defData; + try { - for (const auto& rawEntry : enumData) + defData = nlohmann::json::parse(definition.getBuffer()); + } + catch (const nlohmann::json::parse_error& ex) + { + Logger::PrintError(Game::CON_CHANNEL_ERROR, "JSON Parse Error: {}\n", ex.what()); + return; + } + + if (!defData.is_object()) + { + Logger::Error(Game::ERR_FATAL, "PlayerDataDef patch for version {} is invalid!", i); + return; + } + + for (auto pType = 0; pType < StructuredData::PlayerDataType::COUNT; ++pType) + { + auto enumData = defData[StructuredData::EnumTranslation[pType]]; + + std::vector entryData; + + if (enumData.is_array()) { - if (rawEntry.is_string()) + for (const auto& rawEntry : enumData) { - entryData.push_back(rawEntry.get()); + if (rawEntry.is_string()) + { + entryData.push_back(rawEntry.get()); + } + } + } + + enumContainer.push_back(entryData); + } + + auto other = defData["other"]; + + if (other.is_object()) + { + for (auto& item : other.items()) + { + if (item.value().is_string()) + { + otherPatches[item.key()] = item.value().get(); } } } - enumContainer.push_back(entryData); + patchDefinitions[i] = enumContainer; + otherPatchDefinitions[i] = otherPatches; } - auto other = defData["other"]; + // Nothing to patch + if (patchDefinitions.empty()) return; - if (other.is_object()) + // Reallocate the definition + auto* newData = StructuredData::MemAllocator.allocateArray(data->defCount + patchDefinitions.size()); + std::memcpy(&newData[patchDefinitions.size()], data->defs, sizeof Game::StructuredDataDef * data->defCount); + + // Prepare the buffers + for (unsigned int i = 0; i < patchDefinitions.size(); ++i) { - for (auto& item : other.items()) - { - if (item.value().is_string()) - { - otherPatches[item.key()] = item.value().get(); - } - } + std::memcpy(&newData[i], data->defs, sizeof Game::StructuredDataDef); + newData[i].version = (patchDefinitions.size() - i) + 155; + + // Reallocate the enum array + auto* newEnums = StructuredData::MemAllocator.allocateArray(data->defs->enumCount); + std::memcpy(newEnums, data->defs->enums, sizeof Game::StructuredDataEnum * data->defs->enumCount); + newData[i].enums = newEnums; } - patchDefinitions[i] = enumContainer; - otherPatchDefinitions[i] = otherPatches; - } + // Apply new data + data->defs = newData; + data->defCount += patchDefinitions.size(); - // Nothing to patch - if (patchDefinitions.empty()) return; - - // Reallocate the definition - auto* newData = StructuredData::MemAllocator.allocateArray(data->defCount + patchDefinitions.size()); - std::memcpy(&newData[patchDefinitions.size()], data->defs, sizeof Game::StructuredDataDef * data->defCount); - - // Prepare the buffers - for (unsigned int i = 0; i < patchDefinitions.size(); ++i) - { - std::memcpy(&newData[i], data->defs, sizeof Game::StructuredDataDef); - newData[i].version = (patchDefinitions.size() - i) + 155; - - // Reallocate the enum array - auto* newEnums = StructuredData::MemAllocator.allocateArray(data->defs->enumCount); - std::memcpy(newEnums, data->defs->enums, sizeof Game::StructuredDataEnum * data->defs->enumCount); - newData[i].enums = newEnums; - } - - // Apply new data - data->defs = newData; - data->defCount += patchDefinitions.size(); - - // Patch the definition - for (unsigned int i = 0; i < data->defCount; ++i) - { - // No need to patch version 155 - if (newData[i].version == 155) continue; - - if (patchDefinitions.contains(newData[i].version)) + // Patch the definition + for (unsigned int i = 0; i < data->defCount; ++i) { - auto patchData = patchDefinitions[newData[i].version]; - auto otherData = otherPatchDefinitions[newData[i].version]; + // No need to patch version 155 + if (newData[i].version == 155) continue; - // Invalid patch data - if (patchData.size() != StructuredData::PlayerDataType::COUNT) + if (patchDefinitions.contains(newData[i].version)) { - Logger::Error(Game::ERR_FATAL, "PlayerDataDef patch for version {} wasn't parsed correctly!", newData[i].version); - continue; - } + auto patchData = patchDefinitions[newData[i].version]; + auto otherData = otherPatchDefinitions[newData[i].version]; - // Apply the patch data - for (auto pType = 0; pType < StructuredData::PlayerDataType::COUNT; ++pType) - { - if (!patchData[pType].empty()) + // Invalid patch data + if (patchData.size() != StructuredData::PlayerDataType::COUNT) { - StructuredData::PatchPlayerDataEnum(&newData[i], static_cast(pType), patchData[pType]); + Logger::Error(Game::ERR_FATAL, "PlayerDataDef patch for version {} wasn't parsed correctly!", newData[i].version); + continue; } - } - StructuredData::PatchAdditionalData(&newData[i], otherData); + // Apply the patch data + for (auto pType = 0; pType < StructuredData::PlayerDataType::COUNT; ++pType) + { + if (!patchData[pType].empty()) + { + StructuredData::PatchPlayerDataEnum(&newData[i], static_cast(pType), patchData[pType]); + } + } + + StructuredData::PatchAdditionalData(&newData[i], otherData); + } } - } - }); + }); } } diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index c6fb46b6..1450faef 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -129,6 +129,7 @@ namespace Components #endif static bool IsEnabled(); + static bool IsDumpingZone() { return DumpingZone.length() > 0; }; static std::string TraceZone; static std::vector> TraceAssets; @@ -142,7 +143,7 @@ namespace Components static iw4of::api* GetExporter(); private: - static int StoreTexture(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image); + static int StoreTexture(Game::GfxImageLoadDef** loadDef, Game::GfxImage* image); static void ReleaseTexture(Game::XAssetHeader header); static std::string FindMaterialByTechnique(const std::string& name); From a0b1ef68bf700ee4188c667ae6caffd7f32983b5 Mon Sep 17 00:00:00 2001 From: m Date: Fri, 19 Jan 2024 13:33:57 -0600 Subject: [PATCH 11/71] fix COD:OL zm maps --- src/Components/Modules/Maps.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 8b398751..c8fd5a54 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -317,7 +317,7 @@ namespace Components // TODO : Remove hook entirely? void Maps::GetBSPName(char* buffer, size_t size, const char* format, const char* mapname) { - if (!Utils::String::StartsWith(mapname, "mp_")) + if (!Utils::String::StartsWith(mapname, "mp_") && !Utils::String::StartsWith(mapname, "zm_")) { format = "maps/%s.d3dbsp"; } From 90dacd70b2eec75c3fadd4ea26578cdea194341d Mon Sep 17 00:00:00 2001 From: Louve <33836535+Rackover@users.noreply.github.com> Date: Sat, 20 Jan 2024 12:51:33 +0100 Subject: [PATCH 12/71] Update build.yml --- .github/workflows/build.yml | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f3692290..f102479a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -51,30 +51,3 @@ jobs: build/bin/Win32/${{matrix.configuration}}/iw4x.dll build/bin/Win32/${{matrix.configuration}}/iw4x.pdb - deploy: - name: Deploy artifacts - needs: build - runs-on: ubuntu-latest - if: github.event_name == 'push' && (github.ref == 'refs/heads/develop') - steps: - - name: Setup develop environment - if: github.ref == 'refs/heads/develop' - run: echo "DIAMANTE_MASTER_PATH=${{ secrets.DIAMANTE_MASTER_SSH_PATH }}" >> $GITHUB_ENV - - - name: Download Release binaries - uses: actions/download-artifact@v3.0.2 - with: - name: Release binaries - - # Set up committer info and GPG key - - name: Install SSH key - uses: shimataro/ssh-key-action@v2.5.1 - with: - key: ${{ secrets.DIAMANTE_MASTER_SSH_PRIVATE_KEY }} - known_hosts: 'just-a-placeholder-so-we-dont-get-errors' - - - name: Add known hosts - run: ssh-keyscan -H ${{ secrets.DIAMANTE_MASTER_SSH_ADDRESS }} >> ~/.ssh/known_hosts - - - name: Upload iw4x-client binary - run: rsync -avz iw4x.dll ${{ secrets.DIAMANTE_MASTER_SSH_USER }}@${{ secrets.DIAMANTE_MASTER_SSH_ADDRESS }}:${{ env.DIAMANTE_MASTER_PATH }}/legacy/ From cbdc616a9f827a475491b51ec3909556c3cc5dc6 Mon Sep 17 00:00:00 2001 From: Edo Date: Sat, 20 Jan 2024 13:47:53 +0100 Subject: [PATCH 13/71] address review --- src/Components/Modules/Updater.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Updater.cpp b/src/Components/Modules/Updater.cpp index 0ea98f22..134a74b7 100644 --- a/src/Components/Modules/Updater.cpp +++ b/src/Components/Modules/Updater.cpp @@ -41,14 +41,14 @@ namespace Components if (!parseResult || !doc.IsObject()) { // Nothing to do in this situation. We won't know if we need to update or not - Logger::Print("GitHub sent an invalid reply\n"); + Logger::Print("GitHub sent an invalid reply (malformed JSON)\n"); return; } if (!doc.HasMember("tag_name") || !doc["tag_name"].IsString()) { // Nothing to do in this situation. We won't know if we need to update or not - Logger::Print("GitHub sent an invalid reply\n"); + Logger::Print("GitHub sent an invalid reply (missing 'tag_name' JSON member)\n"); return; } From c6762826dcf3a6205fc2742046515db0a853bd6c Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Jan 2024 13:57:59 +0100 Subject: [PATCH 14/71] address review --- src/Components/Modules/Download.cpp | 20 ++++++++------------ src/Components/Modules/Download.hpp | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 927f4946..f70f53fa 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -435,7 +435,7 @@ namespace Components MongooseLogBuffer.push_back(c); } - void Download::ReplyError(mg_connection* connection, int code) + void Download::ReplyError(mg_connection* connection, int code, std::string messageOverride) { std::string msg{}; switch(code) @@ -453,6 +453,11 @@ namespace Components break; } + if (!messageOverride.empty()) + { + msg = message; + } + mg_http_reply(connection, code, "Content-Type: text/plain\r\n", "%s", msg.c_str()); } @@ -471,25 +476,16 @@ namespace Components char buffer[128]{}; const auto len = mg_http_get_var(&hm->query, "password", buffer, sizeof(buffer)); - const auto reply = [&c](const char* s) -> void - { - mg_printf(c, "%s", "HTTP/1.1 403 Forbidden\r\n"); - mg_printf(c, "%s", "Content-Type: text/plain\r\n"); - mg_printf(c, "Connection: close\r\n"); - mg_printf(c, "%s", "\r\n"); - mg_printf(c, "%s", s); - }; - if (len <= 0) { - reply("Password Required"); + ReplyError(connection, 403, "Password Required"); return false; } const auto password = std::string(buffer, len); if (password != Utils::String::DumpHex(Utils::Cryptography::SHA256::Compute(g_password), "")) { - reply("Invalid Password"); + ReplyError(connection, 403, "Invalid Password"); return false; } diff --git a/src/Components/Modules/Download.hpp b/src/Components/Modules/Download.hpp index b4e4dde1..d93ac94f 100644 --- a/src/Components/Modules/Download.hpp +++ b/src/Components/Modules/Download.hpp @@ -105,7 +105,7 @@ namespace Components static bool DownloadFile(ClientDownload* download, unsigned int index); static void LogFn(char c, void* param); - static void ReplyError(mg_connection* connection, int code); + static void ReplyError(mg_connection* connection, int code, std::string messageOverride = {}); static void Reply(mg_connection* connection, const std::string& contentType, const std::string& data); static std::optional FileHandler(mg_connection* c, const mg_http_message* hm); From 8ed849bf53925329a9b50ff2fd7bb89b0cc3d991 Mon Sep 17 00:00:00 2001 From: Edo Date: Sat, 20 Jan 2024 14:10:58 +0100 Subject: [PATCH 15/71] Update Download.cpp --- src/Components/Modules/Download.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index f70f53fa..6910e4c5 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -455,7 +455,7 @@ namespace Components if (!messageOverride.empty()) { - msg = message; + msg = messageOverride; } mg_http_reply(connection, code, "Content-Type: text/plain\r\n", "%s", msg.c_str()); @@ -478,14 +478,14 @@ namespace Components if (len <= 0) { - ReplyError(connection, 403, "Password Required"); + ReplyError(c, 403, "Password Required"); return false; } const auto password = std::string(buffer, len); if (password != Utils::String::DumpHex(Utils::Cryptography::SHA256::Compute(g_password), "")) { - ReplyError(connection, 403, "Invalid Password"); + ReplyError(c, 403, "Invalid Password"); return false; } From 45cfea133bb160df672efd5fbfc236e909b479de Mon Sep 17 00:00:00 2001 From: Edo Date: Sat, 20 Jan 2024 14:15:12 +0100 Subject: [PATCH 16/71] Update Download.cpp --- src/Components/Modules/Download.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 6910e4c5..237c3ef9 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -478,14 +478,14 @@ namespace Components if (len <= 0) { - ReplyError(c, 403, "Password Required"); + Download::ReplyError(c, 403, "Password Required"); return false; } const auto password = std::string(buffer, len); if (password != Utils::String::DumpHex(Utils::Cryptography::SHA256::Compute(g_password), "")) { - ReplyError(c, 403, "Invalid Password"); + Download::ReplyError(c, 403, "Invalid Password"); return false; } From b81c1b4e0685d52636d0f1225507c653abef9758 Mon Sep 17 00:00:00 2001 From: Edo Date: Sat, 20 Jan 2024 14:20:44 +0100 Subject: [PATCH 17/71] Update Download.hpp --- src/Components/Modules/Download.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/Download.hpp b/src/Components/Modules/Download.hpp index d93ac94f..09b5702e 100644 --- a/src/Components/Modules/Download.hpp +++ b/src/Components/Modules/Download.hpp @@ -17,6 +17,8 @@ namespace Components static void InitiateClientDownload(const std::string& mod, bool needPassword, bool map = false); static void InitiateMapDownload(const std::string& map, bool needPassword); + static void ReplyError(mg_connection* connection, int code, std::string messageOverride = {}); + static Dvar::Var SV_wwwDownload; static Dvar::Var SV_wwwBaseUrl; @@ -105,7 +107,6 @@ namespace Components static bool DownloadFile(ClientDownload* download, unsigned int index); static void LogFn(char c, void* param); - static void ReplyError(mg_connection* connection, int code, std::string messageOverride = {}); static void Reply(mg_connection* connection, const std::string& contentType, const std::string& data); static std::optional FileHandler(mg_connection* c, const mg_http_message* hm); From 25ce596df9ed935341886643c7dd17503f5eab09 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Jan 2024 14:23:25 +0100 Subject: [PATCH 18/71] feat: containers! --- src/Components/Modules/FastFiles.cpp | 45 +++++++++++++++++++++-- src/Components/Modules/FastFiles.hpp | 4 +++ src/Components/Modules/FileSystem.cpp | 50 +++++++++++++++++++++++++- src/Components/Modules/FileSystem.hpp | 12 ++++++- src/Components/Modules/ZoneBuilder.cpp | 14 -------- src/Game/FileSystem.cpp | 1 + src/Game/FileSystem.hpp | 3 ++ src/Game/Structs.hpp | 7 ++++ src/Game/System.cpp | 6 ++++ src/Game/System.hpp | 2 ++ src/Utils/Utils.cpp | 45 ++++++++++++++++++----- src/Utils/Utils.hpp | 1 + 12 files changed, 164 insertions(+), 26 deletions(-) diff --git a/src/Components/Modules/FastFiles.cpp b/src/Components/Modules/FastFiles.cpp index d2a35cbd..a25f5d22 100644 --- a/src/Components/Modules/FastFiles.cpp +++ b/src/Components/Modules/FastFiles.cpp @@ -245,8 +245,6 @@ namespace Components const char* FastFiles::GetZoneLocation(const char* file) { - const auto* dir = (*Game::fs_basepath)->current.string; - std::vector paths; const std::string fsGame = (*Game::fs_gameDirVar)->current.string; @@ -270,6 +268,7 @@ namespace Components Utils::String::Replace(zone, "_load", ""); } + // Only check for usermaps on our working directory if (Utils::IO::FileExists(std::format("usermaps\\{}\\{}.ff", zone, filename))) { return Utils::String::Format("usermaps\\{}\\", zone); @@ -506,11 +505,53 @@ namespace Components return Utils::Hook::Call(0x004925B0)(atStreamStart, surface->numsurfs); } + void FastFiles::DB_BuildOSPath_FromSource_Default(const char* zoneName, Game::FF_DIR source, unsigned int size, char* filename) + { + // TODO: this is where user map and mod.ff check should happen + if (source == Game::FFD_DEFAULT) + { + (void)sprintf_s(filename, size, "%s\\%s%s.ff", FileSystem::Sys_DefaultInstallPath_Hk(), GetZoneLocation(zoneName), zoneName); + } + } + + void FastFiles::DB_BuildOSPath_FromSource_Custom(const char* zoneName, Game::FF_DIR source, unsigned int size, char* filename) + { + // TODO: this is where user map and mod.ff check should happen + if (source == Game::FFD_DEFAULT) + { + (void)sprintf_s(filename, size, "%s\\%s%s.ff", FileSystem::Sys_HomePath_Hk(), GetZoneLocation(zoneName), zoneName); + } + } + + Game::Sys_File FastFiles::Sys_CreateFile_Stub(const char* dir, const char* filename) + { + static_assert(sizeof(Game::Sys_File) == 4); + + auto file = Game::Sys_CreateFile(dir, filename); + + static const std::filesystem::path home = FileSystem::Sys_HomePath_Hk(); + if (file.handle == INVALID_HANDLE_VALUE && !home.empty()) + { + file.handle = Game::Sys_OpenFileReliable(Utils::String::VA("%s\\%s%s", FileSystem::Sys_HomePath_Hk(), dir, filename)); + } + + if (file.handle == INVALID_HANDLE_VALUE && ZoneBuilder::IsEnabled()) + { + file = Game::Sys_CreateFile("zone\\zonebuilder\\", filename); + } + + return file; + } + FastFiles::FastFiles() { Dvar::Register("ui_zoneDebug", false, Game::DVAR_ARCHIVE, "Display current loaded zone."); g_loadingInitialZones = Dvar::Register("g_loadingInitialZones", true, Game::DVAR_NONE, "Is loading initial zones"); + Utils::Hook(0x5BC832, Sys_CreateFile_Stub, HOOK_CALL).install()->quick(); + + Utils::Hook(0x4CCDF0, DB_FileExists_Hk, HOOK_JUMP).install()->quick(); + // Fix XSurface assets Utils::Hook(0x0048E8A5, FastFiles::Load_XSurfaceArray, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/FastFiles.hpp b/src/Components/Modules/FastFiles.hpp index 24f0a03a..9677c040 100644 --- a/src/Components/Modules/FastFiles.hpp +++ b/src/Components/Modules/FastFiles.hpp @@ -70,5 +70,9 @@ namespace Components static void Load_XSurfaceArray(int atStreamStart, int count); + static void DB_BuildOSPath_FromSource_Default(const char* zoneName, Game::FF_DIR source, unsigned int size, char* filename); + static void DB_BuildOSPath_FromSource_Custom(const char* zoneName, Game::FF_DIR source, unsigned int size, char* filename); + static Game::Sys_File Sys_CreateFile_Stub(const char* dir, const char* filename); + static bool DB_FileExists_Hk(const char* zoneName, Game::FF_DIR source); }; } diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index 02b27006..eae77d1a 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -281,7 +281,7 @@ namespace Components int FileSystem::Cmd_Exec_f_Stub(const char* s0, [[maybe_unused]] const char* s1) { int f; - auto len = Game::FS_FOpenFileByMode(s0, &f, Game::FS_READ); + const auto len = Game::FS_FOpenFileByMode(s0, &f, Game::FS_READ); if (len < 0) { return 1; // Not found @@ -336,6 +336,39 @@ namespace Components return current_path.data(); } + FILE* FileSystem::FS_FileOpenReadText_Hk(const char* file) + { + const auto path = Utils::GetBaseFilesLocation(); + if (!path.empty() && Utils::IO::FileExists((path / file).string())) + { + return Game::FS_FileOpenReadText((path / file).string().data()); + } + + return Game::FS_FileOpenReadText(file); + } + + const char* FileSystem::Sys_DefaultCDPath_Hk() + { + return Sys_DefaultInstallPath_Hk(); + } + + const char* FileSystem::Sys_HomePath_Hk() + { + const auto path = Utils::GetBaseFilesLocation(); + if (!path.empty()) + { + static auto current_path = path.string(); + return current_path.data(); + } + + return ""; + } + + const char* FileSystem::Sys_Cwd_Hk() + { + return Sys_DefaultInstallPath_Hk(); + } + FileSystem::FileSystem() { // Thread safe file system interaction @@ -383,6 +416,21 @@ namespace Components // Set the working dir based on info from the Xlabs launcher Utils::Hook(0x4326E0, Sys_DefaultInstallPath_Hk, HOOK_JUMP).install()->quick(); + + // Make the exe run from a folder other than the game folder + Utils::Hook(0x406D26, FS_FileOpenReadText_Hk, HOOK_CALL).install()->quick(); + + // Make the exe run from a folder other than the game folder + Utils::Hook::Nop(0x4290D8, 5); // FS_IsBasePathValid + Utils::Hook::Set(0x4290DF, 0xEB); + // ^^ This check by the game above is super redundant, IW4x has other checks in place to make sure we + // are running from a properly installed directory. This only breaks the containerized patch and we don't need it + + // Patch FS dvar values + Utils::Hook(0x643194, Sys_DefaultCDPath_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x643232, Sys_HomePath_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x6431B6, Sys_Cwd_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x51C29A, Sys_Cwd_Hk, HOOK_CALL).install()->quick(); } FileSystem::~FileSystem() diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index cb721369..9d4e94cf 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -99,6 +99,16 @@ namespace Components static std::vector GetSysFileList(const std::string& path, const std::string& extension, bool folders = false); static bool _DeleteFile(const std::string& folder, const std::string& file); + /** + * The game will check in FS_Startup if homepath != default(base) path + * If they differ which will happen when IW4x is containerized it will register two brand new search paths: + * one for the container and one where the game files are. Pretty cool! + */ + static const char* Sys_DefaultInstallPath_Hk(); + static const char* Sys_DefaultCDPath_Hk(); + static const char* Sys_HomePath_Hk(); + static const char* Sys_Cwd_Hk(); + private: static std::mutex Mutex; static std::recursive_mutex FSMutex; @@ -122,6 +132,6 @@ namespace Components static void IwdFreeStub(Game::iwd_t* iwd); - static const char* Sys_DefaultInstallPath_Hk(); + static FILE* FS_FileOpenReadText_Hk(const char* file); }; } diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index d045a34a..b2fcce55 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -1094,18 +1094,6 @@ namespace Components sound->info.data_ptr = allocatedSpace; } - Game::Sys_File ZoneBuilder::Sys_CreateFile_Stub(const char* dir, const char* filename) - { - auto file = Game::Sys_CreateFile(dir, filename); - - if (file.handle == INVALID_HANDLE_VALUE) - { - file = Game::Sys_CreateFile("zone\\zonebuilder\\", filename); - } - - return file; - } - iw4of::params_t ZoneBuilder::GetExporterAPIParams() { iw4of::params_t params{}; @@ -1162,8 +1150,6 @@ namespace Components // Prevent loading textures (preserves loaddef) //Utils::Hook::Set(Game::Load_Texture, 0xC3); - Utils::Hook(0x5BC832, Sys_CreateFile_Stub, HOOK_CALL).install()->quick(); - // Store the loaddef Utils::Hook(Game::Load_Texture, StoreTexture, HOOK_JUMP).install()->quick(); diff --git a/src/Game/FileSystem.cpp b/src/Game/FileSystem.cpp index 4486f405..d700607f 100644 --- a/src/Game/FileSystem.cpp +++ b/src/Game/FileSystem.cpp @@ -10,6 +10,7 @@ namespace Game FS_FOpenFileAppend_t FS_FOpenFileAppend = FS_FOpenFileAppend_t(0x410BB0); FS_FOpenFileWrite_t FS_FOpenFileWrite = FS_FOpenFileWrite_t(0x4BA530); FS_FOpenTextFileWrite_t FS_FOpenTextFileWrite = FS_FOpenTextFileWrite_t(0x43FD90); + FS_FileOpenReadText_t FS_FileOpenReadText = FS_FileOpenReadText_t(0x446B60); FS_FOpenFileRead_t FS_FOpenFileRead = FS_FOpenFileRead_t(0x46CBF0); FS_FOpenFileReadDatabase_t FS_FOpenFileReadDatabase = FS_FOpenFileReadDatabase_t(0x42ECA0); FS_FOpenFileReadForThread_t FS_FOpenFileReadForThread = FS_FOpenFileReadForThread_t(0x643270); diff --git a/src/Game/FileSystem.hpp b/src/Game/FileSystem.hpp index 41a0029e..74c2f793 100644 --- a/src/Game/FileSystem.hpp +++ b/src/Game/FileSystem.hpp @@ -26,6 +26,9 @@ namespace Game typedef int(*FS_FOpenFileRead_t)(const char* filename, int* file); extern FS_FOpenFileRead_t FS_FOpenFileRead; + typedef FILE*(*FS_FileOpenReadText_t)(const char* filename); + extern FS_FileOpenReadText_t FS_FileOpenReadText; + typedef int(*FS_FOpenFileReadDatabase_t)(const char* filename, int* file); extern FS_FOpenFileReadDatabase_t FS_FOpenFileReadDatabase; diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 21842c63..047504d7 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -11059,6 +11059,13 @@ namespace Game huff_t compressDecompress; }; + enum FF_DIR + { + FFD_DEFAULT = 0x0, + FFD_MOD_DIR = 0x1, + FFD_USER_MAP = 0x2, + }; + #pragma endregion #ifndef IDA diff --git a/src/Game/System.cpp b/src/Game/System.cpp index 50f9d3ab..9b34bda2 100644 --- a/src/Game/System.cpp +++ b/src/Game/System.cpp @@ -55,4 +55,10 @@ namespace Game return TryEnterCriticalSection(&s_criticalSection[critSect]) != FALSE; } + + HANDLE Sys_OpenFileReliable(const char* filename) + { + return CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, nullptr); + } } diff --git a/src/Game/System.hpp b/src/Game/System.hpp index 70b37c1c..d88b476c 100644 --- a/src/Game/System.hpp +++ b/src/Game/System.hpp @@ -81,6 +81,8 @@ namespace Game extern bool Sys_TryEnterCriticalSection(CriticalSection critSect); + extern HANDLE Sys_OpenFileReliable(const char* filename); + class Sys { public: diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index a1066804..daf68be9 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -25,8 +25,10 @@ namespace Utils void OutputDebugLastError() { +#ifdef DEBUG DWORD errorMessageID = ::GetLastError(); - OutputDebugStringA(Utils::String::VA("Last error code: 0x%08X (%s)\n", errorMessageID, GetLastWindowsError().data())); + OutputDebugStringA(String::VA("Last error code: 0x%08X (%s)\n", errorMessageID, GetLastWindowsError().data())); +#endif } std::string GetLastWindowsError() @@ -85,8 +87,7 @@ namespace Utils if (!ntdll) return nullptr; - static uint8_t ntQueryInformationThread[] = { 0xB1, 0x8B, 0xAE, 0x8A, 0x9A, 0x8D, 0x86, 0xB6, 0x91, 0x99, 0x90, 0x8D, 0x92, 0x9E, 0x8B, 0x96, 0x90, 0x91, 0xAB, 0x97, 0x8D, 0x9A, 0x9E, 0x9B }; // NtQueryInformationThread - NtQueryInformationThread_t NtQueryInformationThread = NtQueryInformationThread_t(GetProcAddress(ntdll, String::XOR(std::string(reinterpret_cast(ntQueryInformationThread), sizeof ntQueryInformationThread), -1).data())); + NtQueryInformationThread_t NtQueryInformationThread = NtQueryInformationThread_t(GetProcAddress(ntdll, "NtQueryInformationThread")); if (!NtQueryInformationThread) return nullptr; HANDLE dupHandle, currentProcess = GetCurrentProcess(); @@ -116,26 +117,54 @@ namespace Utils SetCurrentDirectoryW(binaryPath); } + /** + * IW4x_INSTALL should point to where the IW4x rawfiles/client files are + * or the current working dir + */ void SetEnvironment() { char* buffer{}; std::size_t size{}; - if (_dupenv_s(&buffer, &size, "MW2_INSTALL") != 0 || buffer == nullptr) + if (_dupenv_s(&buffer, &size, "IW4x_INSTALL") != 0 || buffer == nullptr) { SetLegacyEnvironment(); return; } - const auto _0 = gsl::finally([&] { std::free(buffer); }); - SetCurrentDirectoryA(buffer); SetDllDirectoryA(buffer); + std::free(buffer); + } + + /** + * Points to where the Modern Warfare 2 folder is + */ + std::filesystem::path GetBaseFilesLocation() + { + char* buffer{}; + std::size_t size{}; + if (_dupenv_s(&buffer, &size, "BASE_INSTALL") != 0 || buffer == nullptr) + { + return {}; + } + + try + { + std::filesystem::path result = buffer; + std::free(buffer); + return result; + } + catch (const std::exception& ex) + { + printf("Failed to convert '%s' to native file system path. Got error '%s'\n", buffer, ex.what()); + std::free(buffer); + return {}; + } } HMODULE GetNTDLL() { - static uint8_t ntdll[] = { 0x91, 0x8B, 0x9B, 0x93, 0x93, 0xD1, 0x9B, 0x93, 0x93 }; // ntdll.dll - return GetModuleHandleA(Utils::String::XOR(std::string(reinterpret_cast(ntdll), sizeof ntdll), -1).data()); + return GetModuleHandleA("ntdll.dll"); } void SafeShellExecute(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd) diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index eba34b59..f864a670 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -20,6 +20,7 @@ namespace Utils HMODULE GetNTDLL(); void SetEnvironment(); + std::filesystem::path GetBaseFilesLocation(); void OpenUrl(const std::string& url); From e4c2ddc9f270d7780818b0c45551f9884bfd2bcc Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Jan 2024 14:25:04 +0100 Subject: [PATCH 19/71] forgot about adding this back --- src/Components/Modules/FastFiles.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Components/Modules/FastFiles.cpp b/src/Components/Modules/FastFiles.cpp index a25f5d22..a99c8b13 100644 --- a/src/Components/Modules/FastFiles.cpp +++ b/src/Components/Modules/FastFiles.cpp @@ -279,6 +279,7 @@ namespace Components for (auto& path : paths) { + const auto* dir = (*Game::fs_basepath)->current.string; auto absoluteFile = std::format("{}\\{}{}", dir, path, file); // No ".ff" appended, append it manually From 1dd3416653b20be3aaaaf82d699222ed9ff2f946 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Jan 2024 14:30:24 +0100 Subject: [PATCH 20/71] and i also forgot one hook --- src/Components/Modules/FastFiles.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Components/Modules/FastFiles.cpp b/src/Components/Modules/FastFiles.cpp index a99c8b13..0e7e6dc8 100644 --- a/src/Components/Modules/FastFiles.cpp +++ b/src/Components/Modules/FastFiles.cpp @@ -524,6 +524,27 @@ namespace Components } } + bool FastFiles::DB_FileExists_Hk(const char* zoneName, Game::FF_DIR source) + { + char filename[256]{}; + + DB_BuildOSPath_FromSource_Default(zoneName, source, sizeof(filename), filename); + if (auto zoneFile = Game::Sys_OpenFileReliable(filename); zoneFile != INVALID_HANDLE_VALUE) + { + CloseHandle(zoneFile); + return true; + } + + DB_BuildOSPath_FromSource_Custom(zoneName, source, sizeof(filename), filename); + if (auto zoneFile = Game::Sys_OpenFileReliable(filename); zoneFile != INVALID_HANDLE_VALUE) + { + CloseHandle(zoneFile); + return true; + } + + return false; + } + Game::Sys_File FastFiles::Sys_CreateFile_Stub(const char* dir, const char* filename) { static_assert(sizeof(Game::Sys_File) == 4); From 960fd6ee89bcb3035ccc884e6f6f20e0b2986f71 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sun, 21 Jan 2024 11:30:40 +0100 Subject: [PATCH 21/71] feat: log failed g_password login/connect attempts --- src/Components/Modules/Auth.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index d46e58fb..2cae7191 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -214,6 +214,7 @@ namespace Components if (Bans::IsBanned({guid, address.getIP()})) { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nEXE_ERR_BANNED_PERM"); return; } @@ -315,6 +316,12 @@ namespace Components } } + void ClientConnectFailedStub(Game::netsrc_t sock, Game::netadr_t adr, const char* data) + { + Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(adr)); + Game::NET_OutOfBandPrint(sock, adr, data); + } + unsigned __int64 Auth::GetKeyHash(const std::string& key) { std::string hash = Utils::Cryptography::SHA1::Compute(key); @@ -502,6 +509,9 @@ namespace Components Utils::Hook(0x41D3E3, SendConnectDataStub, HOOK_CALL).install()->quick(); + // Hook for Fail2Ban (Hook near client connect to detect password brute forcing) + Utils::Hook(0x4611CA, ClientConnectFailedStub, HOOK_CALL).install()->quick(); // NET_OutOfBandPrint (Grab IP super easy) + // SteamIDs can only contain 31 bits of actual 'id' data. // The other 33 bits are steam internal data like universe and so on. // Using only 31 bits for fingerprints is pretty insecure. From e2b78b44a419807522e7e14d1bee21bd21e17905 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Wed, 24 Jan 2024 18:32:10 +0100 Subject: [PATCH 22/71] Stuff --- src/Components/Loader.cpp | 2 +- src/Components/Modules/AssetHandler.cpp | 480 +++++++++++++++++++++++- src/Game/Structs.hpp | 6 + src/Steam/Proxy.cpp | 2 + 4 files changed, 488 insertions(+), 2 deletions(-) diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index f50b2689..aa3ce5d6 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -173,7 +173,7 @@ namespace Components Register(new Threading()); Register(new Toast()); Register(new UIFeeder()); - Register(new Updater()); + //Register(new Updater()); Register(new VisionFile()); Register(new Voice()); Register(new Vote()); diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index b76def14..7ebc7ad4 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -221,7 +221,7 @@ namespace Components retn } } - +#pragma optimize( "", off ) void AssetHandler::ModifyAsset(Game::XAssetType type, Game::XAssetHeader asset, const std::string& name) { #ifdef DEBUG @@ -236,6 +236,483 @@ namespace Components } #endif + if (name == "body_urban_civ_female_a"s) + //if (name == "mp_body_tf141_lmg"s) + { + class QuatInt16 + { + public: + static uint16_t ToInt16(const float quat) + { + return static_cast(quat * INT16_MAX); + } + + static float ToFloat(const uint16_t quat) + { + return static_cast(quat) / static_cast(INT16_MAX); + } + }; + + struct BoneEnsemble + { + uint16_t name; + Game::DObjAnimMat mat; + Game::XBoneInfo info; + int16_t quat[4]; + float trans[3]; + + BoneEnsemble() {}; + + BoneEnsemble(Game::XModel* model, int index) + { + name = model->boneNames[index]; + mat = model->baseMat[index]; + info = model->boneInfo[index]; + + std::memcpy(quat, &model->quats[(index - 1) * 4], 4 * sizeof(uint16_t)); + std::memcpy(trans, &model->trans[(index - 1) * 3], 3 * sizeof(float)); + } + }; + + const auto equals = [](const BoneEnsemble& a, const BoneEnsemble& b) + { + if (a.name == b.name) + { + if (b.mat.transWeight != a.mat.transWeight) + { + return false; + } + + for (size_t i = 0; i < 4; i++) + { + if (b.mat.quat[i] != a.mat.quat[i]) + { + return false; + } + + if (b.quat[i] != a.quat[i]) + { + return false; + } + } + + for (size_t i = 0; i < 3; i++) + { + if (b.mat.trans[i] != a.mat.trans[i]) + { + return false; + } + + if (b.trans[i] != a.trans[i]) + { + return false; + } + + if (b.info.bounds.halfSize[i] != a.info.bounds.halfSize[i]) + { + return false; + } + + if (b.info.bounds.midPoint[i] != a.info.bounds.midPoint[i]) + { + return false; + } + } + + if (b.info.radiusSquared != a.info.radiusSquared) + { + return false; + } + + return true; + } + + return false; + }; + + const auto model = asset.model; + + static std::vector names{}; + + for (auto i = 0; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto name = Game::SL_ConvertToString(bone); + names.push_back(name); + } + + const auto getIndexOfBone = [&](std::string name) + { + for (uint8_t i = 0; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto boneName = Game::SL_ConvertToString(bone); + if (name == boneName) + { + return i; + } + } + + return static_cast(UCHAR_MAX); + }; + + const auto getParentIndexOfBone = [&](uint8_t index) + { + const auto parentIndex = index - model->parentList[index - model->numRootBones]; + return parentIndex; + }; + + const auto setParentIndexOfBone = [&](uint8_t boneIndex, uint8_t parentIndex) + { + if (boneIndex == SCHAR_MAX) + { + return; + } + + model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex; + }; + + const auto getParentOfBone = [&](int index) + { + const auto parentIndex = getParentIndexOfBone(index); + const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); + return boneName; + }; + + const auto insertBone = [&](const std::string& boneName, uint8_t parentIndex) + { + uint8_t newBoneCount = model->numBones + 1; + uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; + + int atPosition = model->numBones; + + // Find best position to insert it + for (int index = model->numRootBones; index < model->numBones; index++) + { + int parent = getParentIndexOfBone(index); + if (parent >= parentIndex) + { + atPosition = index; + break; + } + } + + const auto newBoneIndex = atPosition; + const auto newBoneIndexMinusRoot = atPosition - model->numRootBones; + + // Reallocate + const auto newBoneNames = (uint16_t*)Game::Z_Malloc(sizeof(uint16_t) * newBoneCount); + const auto newMats = (Game::DObjAnimMat*)Game::Z_Malloc(sizeof(Game::DObjAnimMat) * newBoneCount); + const auto newBoneInfo = (Game::XBoneInfo*)Game::Z_Malloc(sizeof(Game::XBoneInfo) * newBoneCount); + const auto newQuats = (int16_t*)Game::Z_Malloc(sizeof(uint16_t) * 4 * newBoneCountMinusRoot); + const auto newTrans = (float*)Game::Z_Malloc(sizeof(float) * 3 * newBoneCountMinusRoot); + const auto newParentList = reinterpret_cast(Game::Z_Malloc(sizeof(uint8_t) * newBoneCountMinusRoot)); + + int lengthOfFirstPart = atPosition; + int lengthOfSecondPart = model->numBones - atPosition; + + int lengthOfFirstPartM1 = atPosition - model->numRootBones; + int lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones); + + int atPositionM1 = atPosition - model->numRootBones; + + // should be equal to model->numBones + int total = lengthOfFirstPart + lengthOfSecondPart; + assert(total = model->numBones); + + // should be equal to model->numBones - model->numRootBones + int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; + assert(totalM1 == model->numBones - model->numRootBones); + + // Copy before + if (lengthOfFirstPart > 0) + { + std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart); + std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart); + std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart); + std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1); + std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1); + } + + // Insert new bone + { + unsigned int name = Game::SL_GetString(boneName.data(), 0); + Game::XBoneInfo boneInfo{}; + + Game::DObjAnimMat mat{}; + + mat = model->baseMat[parentIndex]; + boneInfo = model->boneInfo[parentIndex]; + + uint16_t quat[4]{}; + std::memcpy(quat, &model->quats[(parentIndex - model->numRootBones) * sizeof(uint16_t)], ARRAYSIZE(quat) * sizeof(uint16_t)); + + float trans[3]{}; + std::memcpy(trans, &model->trans[(parentIndex - model->numRootBones) * sizeof(float)], ARRAYSIZE(trans) * sizeof(float)); + + //mat.quat[3] = 1.0f; + + //mat.trans[0] = -3.4; + //mat.trans[1] = -6; + //mat.trans[2] = 37; + + //mat.transWeight = 2.0f; + + uint8_t part = 5; // Unused ? + + newMats[newBoneIndex] = mat; + newBoneInfo[newBoneIndex] = boneInfo; + newBoneNames[newBoneIndex] = name; + + std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t)); + std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float)); + } + + // Copy after + if (lengthOfSecondPart > 0) + { + std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); + std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); + std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); + std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); + std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); + } + + // Assign reallocated + model->baseMat = newMats; + model->boneInfo = newBoneInfo; + model->boneNames = newBoneNames; + model->quats = newQuats; + model->trans = newTrans; + model->parentList = newParentList; + + model->numBones++; + + // Update vertex weights + for (int i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + size_t weightOffset = 0u; + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + + { + const auto fixVertexBlendIndex = [&](unsigned int offset) { + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + if (index >= atPosition) + { + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + index++; + + surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + if (index >= atPosition) + { + index++; + vertList->boneOffset = index * sizeof(Game::DObjSkelMat); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + fixVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + + // TODO free memory of original lists + printf(""); + + return atPosition; // Bone index of added bone + }; + + auto indexOfSpine = getIndexOfBone("j_spinelower"); + if (indexOfSpine < UCHAR_MAX) + { + const auto nameOfParent = getParentOfBone(indexOfSpine); + const auto stabilizer = getIndexOfBone("torso_stabilizer"); + const auto otherIndex = stabilizer - model->numRootBones; + + { + const auto root = getIndexOfBone("j_mainroot"); + if (root < UCHAR_MAX) { + + // + std::map parentsBefore{}; + std::map bonesBefore{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsBefore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); + bonesBefore[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); + } + // + +#if 0 + // Add pelvis + auto indexOfPelvis = getIndexOfBone("pelvis"); + if (indexOfPelvis == UCHAR_MAX) + { + indexOfPelvis = insertBone("pelvis", root); + + parentsBefore["pelvis"] = "j_mainroot"; + setParentIndexOfBone(indexOfPelvis, getIndexOfBone("j_mainroot")); + + assert(root == getIndexOfBone("j_mainroot")); + + parentsBefore["j_hip_le"] = "pelvis"; + setParentIndexOfBone(getIndexOfBone("j_hip_le"), getIndexOfBone("pelvis")); + + parentsBefore["j_hip_ri"] = "pelvis"; + setParentIndexOfBone(getIndexOfBone("j_hip_ri"), getIndexOfBone("pelvis")); + + parentsBefore["tag_stowed_hip_rear"] = "pelvis"; + setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), getIndexOfBone("pelvis")); + + } + + uint8_t torsoStabilizer = insertBone("torso_stabilizer", indexOfPelvis); + assert(indexOfPelvis == getIndexOfBone("pelvis")); + +#else + const auto parentIndex = 30; + uint8_t torsoStabilizer = insertBone("torso_stabilizer", parentIndex); + //setParentIndexOfBone(torsoStabilizer, parentIndex); +#endif + +#if DEBUG + const auto newRoot = getIndexOfBone("j_mainroot"); + assert(root == newRoot); +#endif + //parentsBefore["j_spinelower"] = "torso_stabilizer"; + //setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); + + //parentsBefore["torso_stabilizer"] = "pelvis"; + //setParentIndexOfBone(torsoStabilizer, indexOfPelvis); + + // + std::map parentsAfter{}; + std::map bonesAfter{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsAfter[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); + bonesAfter[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); + } + // + + for (const auto& kv : parentsBefore) + { + // Fix parents + const auto key = kv.first; + const auto beforeVal = kv.second; + const auto afterVal = parentsAfter[kv.first]; + if (beforeVal != afterVal) + { + const auto parentIndex = getIndexOfBone(beforeVal); + const auto index = getIndexOfBone(key); + setParentIndexOfBone(index, parentIndex); + parentsAfter[Game::SL_ConvertToString(model->boneNames[index])] = getParentOfBone(index); + } + } + + + // check + for (const auto& kv : parentsBefore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto afterVal = parentsAfter[key]; + + if (beforeVal != afterVal) + { + printf(""); + } + } + // + + // + for (const auto& kv : bonesBefore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + const auto afterVal = bonesAfter[kv.first]; + if (equals(beforeVal, afterVal)) + { + // good + } + else + { + printf(""); + } + } + // + printf(""); + } + } + printf(""); + } + + printf(""); + } + if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) { if (Zones::Version() >= VERSION_ALPHA2) @@ -291,6 +768,7 @@ namespace Components asset.gfxWorld->sortKeyDistortion = 43; } } +#pragma optimize( "", on ) bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader *asset) { diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 21842c63..8e2ae66f 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1133,6 +1133,12 @@ namespace Game unsigned __int16 triCount; XSurfaceCollisionTree* collisionTree; }; + + struct DObjSkelMat + { + float axis[3][4]; + float origin[4]; + }; struct XSurface { diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index c4710b4a..248723ff 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -276,6 +276,8 @@ namespace Steam void Proxy::RunFrame() { + return; + std::lock_guard _(Proxy::CallMutex); if (Proxy::SteamUtils) From 1bd6f0c4adc45976bbd6ebe4fb58a14bb3045f27 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Thu, 25 Jan 2024 01:52:56 +0100 Subject: [PATCH 23/71] uuuh --- src/Components/Modules/AssetHandler.cpp | 638 ++++++++++++++---------- src/Components/Modules/QuickPatch.cpp | 19 + src/Components/Modules/Renderer.cpp | 27 + src/Game/Structs.hpp | 16 +- 4 files changed, 449 insertions(+), 251 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 7ebc7ad4..37bf8ea5 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -118,7 +118,7 @@ namespace Components } return header; - } + } int AssetHandler::HasThreadBypass() { @@ -160,7 +160,7 @@ namespace Components // Check if custom handler should be bypassed call AssetHandler::HasThreadBypass - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax @@ -180,45 +180,45 @@ namespace Components add esp, 8h - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax test eax, eax jnz finishFound - checkTempAssets: + checkTempAssets : mov al, AssetHandler::ShouldSearchTempAssets // check to see if enabled - test eax, eax - jz finishOriginal + test eax, eax + jz finishOriginal - mov ecx, [esp + 18h] // Asset type - mov ebx, [esp + 1Ch] // Filename + mov ecx, [esp + 18h] // Asset type + mov ebx, [esp + 1Ch] // Filename - push ebx - push ecx + push ebx + push ecx - call AssetHandler::FindTemporaryAsset + call AssetHandler::FindTemporaryAsset - add esp, 8h + add esp, 8h - test eax, eax - jnz finishFound + test eax, eax + jnz finishFound - finishOriginal: + finishOriginal : // Asset not found using custom handlers or in temp assets or bypasses were enabled // redirect to DB_FindXAssetHeader - mov ebx, ds:6D7190h // InterlockedDecrement - mov eax, 40793Bh - jmp eax + mov ebx, ds : 6D7190h // InterlockedDecrement + mov eax, 40793Bh + jmp eax - finishFound: + finishFound : pop edi - pop esi - pop ebp - pop ebx - pop ecx - retn + pop esi + pop ebp + pop ebx + pop ecx + retn } } #pragma optimize( "", off ) @@ -236,9 +236,38 @@ namespace Components } #endif - if (name == "body_urban_civ_female_a"s) - //if (name == "mp_body_tf141_lmg"s) + if (type == Game::XAssetType::ASSET_TYPE_XMODEL) + //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) { + const auto model = asset.model; + if (!model->quats || !model->trans) + { + return; + } + + // Update vertex weights + //for (int i = 0; i < model->numLods; i++) + //{ + // const auto lod = &model->lodInfo[i]; + + // for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + // { + // auto vertsBlendOffset = 0u; + + // const auto surface = &lod->modelSurfs->surfs[surfIndex]; + // surface->partBits[0] = 0x00000000; + // surface->partBits[1] = 0x00000000; + // surface->partBits[2] = 0x00000000; + // surface->partBits[3] = 0x00000000; + // surface->partBits[4] = 0x00000000; + // surface->partBits[5] = 0x00000000; + // } + //} + + //return; + + + class QuatInt16 { public: @@ -253,94 +282,31 @@ namespace Components } }; - struct BoneEnsemble - { - uint16_t name; - Game::DObjAnimMat mat; - Game::XBoneInfo info; - int16_t quat[4]; - float trans[3]; - - BoneEnsemble() {}; - - BoneEnsemble(Game::XModel* model, int index) - { - name = model->boneNames[index]; - mat = model->baseMat[index]; - info = model->boneInfo[index]; - - std::memcpy(quat, &model->quats[(index - 1) * 4], 4 * sizeof(uint16_t)); - std::memcpy(trans, &model->trans[(index - 1) * 3], 3 * sizeof(float)); - } - }; - - const auto equals = [](const BoneEnsemble& a, const BoneEnsemble& b) - { - if (a.name == b.name) - { - if (b.mat.transWeight != a.mat.transWeight) - { - return false; - } - - for (size_t i = 0; i < 4; i++) - { - if (b.mat.quat[i] != a.mat.quat[i]) - { - return false; - } - - if (b.quat[i] != a.quat[i]) - { - return false; - } - } - - for (size_t i = 0; i < 3; i++) - { - if (b.mat.trans[i] != a.mat.trans[i]) - { - return false; - } - - if (b.trans[i] != a.trans[i]) - { - return false; - } - - if (b.info.bounds.halfSize[i] != a.info.bounds.halfSize[i]) - { - return false; - } - - if (b.info.bounds.midPoint[i] != a.info.bounds.midPoint[i]) - { - return false; - } - } - - if (b.info.radiusSquared != a.info.radiusSquared) - { - return false; - } - - return true; - } - - return false; - }; - - const auto model = asset.model; - + // static std::vector names{}; + names.clear(); for (auto i = 0; i < model->numBones; i++) { const auto bone = model->boneNames[i]; const auto name = Game::SL_ConvertToString(bone); - names.push_back(name); + names.push_back( + std::format("{} => q({} {} {} {}) t({} {} {})", + name, + model->baseMat[i-1].quat[0], + model->baseMat[i-1].quat[1], + model->baseMat[i-1].quat[2], + model->baseMat[i-1].quat[3], + model->baseMat[i-1].trans[0], + model->baseMat[i-1].trans[1], + model->baseMat[i-1].trans[2] + ) + ); } + printf(""); + // + const auto getIndexOfBone = [&](std::string name) { for (uint8_t i = 0; i < model->numBones; i++) @@ -379,23 +345,73 @@ namespace Components return boneName; }; - const auto insertBone = [&](const std::string& boneName, uint8_t parentIndex) + const auto insertBoneInPartbits = [&](int8_t atPosition, int* partBits, bool hasAnyWeight = false) { + constexpr auto LENGTH = 6; + // Bit masks and bit shifting are a pain honestly + std::vector flags{}; + + int check[LENGTH]{}; + std::memcpy(check, partBits, LENGTH * sizeof(int)); + + for (auto i = 0; i < LENGTH; i++) + { + for (signed int bitPosition = 32 - 1; bitPosition >= 0; bitPosition--) + { + flags.emplace_back((partBits[i] >> bitPosition) & 0x1); + } + } + + // copy parent flag + const auto partBitIndex = atPosition / 32; + const auto bitPosition = atPosition % 32; // The vector is already reversed so this should be correct + + flags.insert(flags.begin() + (partBitIndex * 32 + bitPosition), hasAnyWeight); + + flags.pop_back(); + + // clear it + for (auto i = 0; i < LENGTH; i++) + { + partBits[i] = 0; + } + + // Write it + for (auto i = 0; i < LENGTH; i++) + { + partBits[i] = 0; + for (int bitPosition = 0; bitPosition < 32; bitPosition++) + { + const auto value = flags[i * 32 + 31 - bitPosition]; + partBits[i] |= (value << bitPosition); + } + } + + flags.clear(); + }; + + + const auto insertBone = [&](const std::string& boneName, const std::string& parentName) + { + assert(getIndexOfBone(boneName) == UCHAR_MAX); + + // Start with backing up parent links that we will have to restore + // We'll restore them at the end + std::map parentsToRestore{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); + } + + uint8_t newBoneCount = model->numBones + 1; uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; - int atPosition = model->numBones; + const auto parentIndex = getIndexOfBone(parentName); - // Find best position to insert it - for (int index = model->numRootBones; index < model->numBones; index++) - { - int parent = getParentIndexOfBone(index); - if (parent >= parentIndex) - { - atPosition = index; - break; - } - } + assert(parentIndex != UCHAR_MAX); + + int atPosition = parentIndex + 1; const auto newBoneIndex = atPosition; const auto newBoneIndexMinusRoot = atPosition - model->numRootBones; @@ -441,24 +457,18 @@ namespace Components Game::DObjAnimMat mat{}; + // It's ABSOLUTE! mat = model->baseMat[parentIndex]; + boneInfo = model->boneInfo[parentIndex]; + // It's RELATIVE ! uint16_t quat[4]{}; - std::memcpy(quat, &model->quats[(parentIndex - model->numRootBones) * sizeof(uint16_t)], ARRAYSIZE(quat) * sizeof(uint16_t)); + quat[3] = 32767; // 0 0 0 32767 float trans[3]{}; - std::memcpy(trans, &model->trans[(parentIndex - model->numRootBones) * sizeof(float)], ARRAYSIZE(trans) * sizeof(float)); - //mat.quat[3] = 1.0f; - - //mat.trans[0] = -3.4; - //mat.trans[1] = -6; - //mat.trans[2] = 37; - - //mat.transWeight = 2.0f; - - uint8_t part = 5; // Unused ? + mat.transWeight = 1.9999f; // Should be 1.9999 like everybody? newMats[newBoneIndex] = mat; newBoneInfo[newBoneIndex] = boneInfo; @@ -492,7 +502,8 @@ namespace Components for (int i = 0; i < model->numLods; i++) { const auto lod = &model->lodInfo[i]; - size_t weightOffset = 0u; + insertBoneInPartbits(atPosition, lod->partBits, false); + insertBoneInPartbits(atPosition, lod->modelSurfs->partBits, false); for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) { @@ -500,6 +511,15 @@ namespace Components const auto surface = &lod->modelSurfs->surfs[surfIndex]; + //surface->partBits[0] = 0b00000000000000000000000000000000; + //surface->partBits[1] = 0b01010101010101010101010101010101; + //surface->partBits[2] = 0b00110011001100110011001100110011; + //surface->partBits[3] = 0b00011100011100011100011100011100; + //surface->partBits[4] = 0b00001111000011110000111100001111; + //surface->partBits[5] = 0b00000111110000011111000001111100; + + insertBoneInPartbits(atPosition, surface->partBits, false); + { const auto fixVertexBlendIndex = [&](unsigned int offset) { int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); @@ -507,14 +527,14 @@ namespace Components { if (index < 0 || index >= model->numBones - 1) { - assert(false); + //assert(false); } index++; surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); } - }; + }; // Fix bone offsets if (surface->vertList) @@ -525,7 +545,7 @@ namespace Components auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); if (index < 0 || index >= model->numBones - 1) { - assert(false); + //assert(false); } if (index >= atPosition) @@ -560,7 +580,6 @@ namespace Components fixVertexBlendIndex(vertsBlendOffset + 1); fixVertexBlendIndex(vertsBlendOffset + 3); - vertsBlendOffset += 5; } @@ -581,136 +600,255 @@ namespace Components // TODO free memory of original lists printf(""); + setParentIndexOfBone(atPosition, parentIndex); + + // Restore parents + for (const auto& kv : parentsToRestore) + { + // Fix parents + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto parentIndex = getIndexOfBone(beforeVal); + const auto index = getIndexOfBone(key); + setParentIndexOfBone(index, parentIndex); + } + + + // check + for (const auto& kv : parentsToRestore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto index = getIndexOfBone(key); + const auto afterVal = getParentOfBone(index); + + if (beforeVal != afterVal) + { + printf(""); + } + } + // + return atPosition; // Bone index of added bone }; + + const auto transferWeights = [&](const uint8_t origin, const uint8_t destination) + { + return; + + const auto originalWeights = model->baseMat[origin].transWeight; + model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; + model->baseMat[destination].transWeight = originalWeights; + + for (int i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + { + const auto transferVertexBlendIndex = [&](unsigned int offset) { + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + if (index == origin) + { + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + index = destination; + + surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + if (index == origin) + { + index = destination; + vertList->boneOffset = index * sizeof(Game::DObjSkelMat); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + transferVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + }; + auto indexOfSpine = getIndexOfBone("j_spinelower"); if (indexOfSpine < UCHAR_MAX) { const auto nameOfParent = getParentOfBone(indexOfSpine); - const auto stabilizer = getIndexOfBone("torso_stabilizer"); - const auto otherIndex = stabilizer - model->numRootBones; + if (getIndexOfBone("torso_stabilizer") == UCHAR_MAX) { + // No stabilizer - let's do surgery + // We're trying to get there: + // tag_origin + // j_main_root + // pelvis + // j_hip_le + // j_hip_ri + // tag_stowed_hip_rear + // torso_stabilizer + // j_spinelower + // back_low + // j_spineupper + // back_mid + // j_spine4 + + const auto root = getIndexOfBone("j_mainroot"); if (root < UCHAR_MAX) { - // - std::map parentsBefore{}; - std::map bonesBefore{}; - for (int i = model->numRootBones; i < model->numBones; i++) - { - parentsBefore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); - bonesBefore[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); - } - // - -#if 0 +#if true // Add pelvis - auto indexOfPelvis = getIndexOfBone("pelvis"); - if (indexOfPelvis == UCHAR_MAX) - { - indexOfPelvis = insertBone("pelvis", root); + const uint8_t indexOfPelvis = insertBone("pelvis", "j_mainroot"); + transferWeights(root, indexOfPelvis); - parentsBefore["pelvis"] = "j_mainroot"; - setParentIndexOfBone(indexOfPelvis, getIndexOfBone("j_mainroot")); + setParentIndexOfBone(getIndexOfBone("j_hip_le"), indexOfPelvis); + setParentIndexOfBone(getIndexOfBone("j_hip_ri"), indexOfPelvis); + setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), indexOfPelvis); - assert(root == getIndexOfBone("j_mainroot")); + const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "pelvis"); + setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); - parentsBefore["j_hip_le"] = "pelvis"; - setParentIndexOfBone(getIndexOfBone("j_hip_le"), getIndexOfBone("pelvis")); + const uint8_t backLow = insertBone("back_low", "j_spinelower"); + transferWeights(getIndexOfBone("j_spinelower"), backLow); + setParentIndexOfBone(getIndexOfBone("j_spineupper"), backLow); - parentsBefore["j_hip_ri"] = "pelvis"; - setParentIndexOfBone(getIndexOfBone("j_hip_ri"), getIndexOfBone("pelvis")); + const uint8_t backMid = insertBone("back_mid", "j_spineupper"); + transferWeights(getIndexOfBone("j_spineupper"), backMid); + setParentIndexOfBone(getIndexOfBone("j_spine4"), backMid); - parentsBefore["tag_stowed_hip_rear"] = "pelvis"; - setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), getIndexOfBone("pelvis")); - } - - uint8_t torsoStabilizer = insertBone("torso_stabilizer", indexOfPelvis); + assert(root == getIndexOfBone("j_mainroot")); assert(indexOfPelvis == getIndexOfBone("pelvis")); + assert(backLow == getIndexOfBone("back_low")); + assert(backMid == getIndexOfBone("back_mid")); + + // Fix up torso stabilizer + model->baseMat[torsoStabilizer].quat[0] = 0.F; + model->baseMat[torsoStabilizer].quat[1] = 0.F; + model->baseMat[torsoStabilizer].quat[2] = 0.F; + model->baseMat[torsoStabilizer].quat[3] = 1.F; + + const auto spineLowerM1 = getIndexOfBone("j_spinelower")-1; + + //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 0] = 2.0f; + //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 1] = -5.0f; + //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 2] = 2.0F; + + model->trans[spineLowerM1 * 3 + 0] = 0.069828756; + model->trans[spineLowerM1 * 3 + 1] = -0.0f; + model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; + + //// Euler -180.000 88.572 -90.000 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 0] = 16179; // 0.4952 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 1] = 16586; // 0.5077 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 2] = 16586; // 0.5077 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 3] = 16178; // 0.4952 #else - const auto parentIndex = 30; - uint8_t torsoStabilizer = insertBone("torso_stabilizer", parentIndex); - //setParentIndexOfBone(torsoStabilizer, parentIndex); + const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); + transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); + setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); #endif #if DEBUG const auto newRoot = getIndexOfBone("j_mainroot"); assert(root == newRoot); #endif - //parentsBefore["j_spinelower"] = "torso_stabilizer"; - //setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); - //parentsBefore["torso_stabilizer"] = "pelvis"; - //setParentIndexOfBone(torsoStabilizer, indexOfPelvis); - - // - std::map parentsAfter{}; - std::map bonesAfter{}; - for (int i = model->numRootBones; i < model->numBones; i++) - { - parentsAfter[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); - bonesAfter[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); - } - // - - for (const auto& kv : parentsBefore) - { - // Fix parents - const auto key = kv.first; - const auto beforeVal = kv.second; - const auto afterVal = parentsAfter[kv.first]; - if (beforeVal != afterVal) - { - const auto parentIndex = getIndexOfBone(beforeVal); - const auto index = getIndexOfBone(key); - setParentIndexOfBone(index, parentIndex); - parentsAfter[Game::SL_ConvertToString(model->boneNames[index])] = getParentOfBone(index); - } - } - - - // check - for (const auto& kv : parentsBefore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto afterVal = parentsAfter[key]; - - if (beforeVal != afterVal) - { - printf(""); - } - } - // - - // - for (const auto& kv : bonesBefore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - const auto afterVal = bonesAfter[kv.first]; - if (equals(beforeVal, afterVal)) - { - // good - } - else - { - printf(""); - } - } - // - printf(""); + printf(""); } } printf(""); } printf(""); + + // + names.clear(); + for (auto i = 1; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto name = Game::SL_ConvertToString(bone); + const auto m1 = i-1; + const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", + name, + model->quats[m1 * 4 + 0], + model->quats[m1* 4 + 1], + model->quats[m1 * 4 + 2], + model->quats[m1 * 4 + 3], + model->trans[m1 * 3 + 0], + model->trans[m1* 3 +1], + model->trans[m1 * 3 +2] + ); + names.push_back( + fmt + ); + printf(""); + } + + printf(""); + // } if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) @@ -770,7 +908,7 @@ namespace Components } #pragma optimize( "", on ) - bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader *asset) + bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) { const char* name = Game::DB_GetXAssetNameHandlers[type](asset); if (!name) return false; @@ -811,12 +949,12 @@ namespace Components push eax pushad - push [esp + 2Ch] - push [esp + 2Ch] + push[esp + 2Ch] + push[esp + 2Ch] call AssetHandler::IsAssetEligible add esp, 8h - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax @@ -828,9 +966,9 @@ namespace Components mov ecx, 5BB657h jmp ecx - doNotLoad: + doNotLoad : mov eax, [esp + 8h] - retn + retn } } @@ -843,9 +981,9 @@ namespace Components { AssetHandler::RestrictSignal.connect(callback); - return [callback](){ + return [callback]() { AssetHandler::RestrictSignal.disconnect(callback); - }; + }; } void AssetHandler::ClearRelocations() @@ -1051,25 +1189,25 @@ namespace Components // Log missing empty assets Scheduler::Loop([] - { - if (FastFiles::Ready() && !AssetHandler::EmptyAssets.empty()) { - for (auto& asset : AssetHandler::EmptyAssets) + if (FastFiles::Ready() && !AssetHandler::EmptyAssets.empty()) { - Logger::Warning(Game::CON_CHANNEL_FILES, "Could not load {} \"{}\".\n", Game::DB_GetXAssetTypeName(asset.first), asset.second); - } + for (auto& asset : AssetHandler::EmptyAssets) + { + Logger::Warning(Game::CON_CHANNEL_FILES, "Could not load {} \"{}\".\n", Game::DB_GetXAssetTypeName(asset.first), asset.second); + } - AssetHandler::EmptyAssets.clear(); - } - }, Scheduler::Pipeline::MAIN); + AssetHandler::EmptyAssets.clear(); + } + }, Scheduler::Pipeline::MAIN); AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool*) - { - if (Dvar::Var("r_noVoid").get() && type == Game::ASSET_TYPE_XMODEL && name == "void") { - asset.model->numLods = 0; - } - }); + if (Dvar::Var("r_noVoid").get() && type == Game::ASSET_TYPE_XMODEL && name == "void") + { + asset.model->numLods = 0; + } + }); Game::ReallocateAssetPool(Game::ASSET_TYPE_GAMEWORLD_SP, 1); Game::ReallocateAssetPool(Game::ASSET_TYPE_IMAGE, ZoneBuilder::IsEnabled() ? 14336 * 2 : 7168); diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index e8a7ded9..8eeb44e7 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -298,8 +298,27 @@ namespace Components return Game::Dvar_RegisterBool(dvarName, value_, flags, description); } + void DObjCalcAnim(Game::DObj *a1, int *partBits, Game::XAnimCalcAnimInfo *a3) + { + printf(""); + + if (a1->models[0]->name == "body_urban_civ_female_a"s) + { + printf(""); + } + + Utils::Hook::Call(0x49E230)(a1, partBits, a3); + + printf(""); + } + QuickPatch::QuickPatch() { + // Do not call DObjCalcAnim + Utils::Hook(0x6508C4, DObjCalcAnim, HOOK_CALL).install()->quick(); + Utils::Hook(0x06508F6, DObjCalcAnim, HOOK_CALL).install()->quick(); + + // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning Utils::Hook(0x5FBD6E, QuickPatch::IsDynClassname_Stub, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 191b82f5..8df1b33f 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -713,6 +713,32 @@ namespace Components return result; } + void DebugTest() + { + //auto clientNum = Game::CG_GetClientNum(); + //auto* clientEntity = &Game::g_entities[clientNum]; + + //// Ingame only & player only + //if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr) + //{ + // return; + //} + + + //static std::string str = ""; + + //str += std::format("\n{} => {} {} {} {} {} {}", "s.partBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); + // + + //const auto clientNumber = clientEntity->r.ownerNum.number; + //Game::scene->sceneDObj[clientNumber].obj->hidePartBits; + + //str += std::format("\n{} => {} {} {} {} {} {}", "DOBJ hidePartBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); + + //Game::R_AddCmdDrawText(); + + } + Renderer::Renderer() { if (Dedicated::IsEnabled()) return; @@ -731,6 +757,7 @@ namespace Components ListSamplers(); DrawPrimaryLights(); DebugDrawClipmap(); + DebugTest(); } }, Scheduler::Pipeline::RENDERER); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 8e2ae66f..3fd346d3 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -8368,8 +8368,22 @@ namespace Game { DSkelPartBits partBits; int timeStamp; - /*DObjAnimMat*/void* mat; + DObjAnimMat* mat; }; + + struct bitarray + { + int array[6]; + }; + + /* 1923 */ + struct XAnimCalcAnimInfo + { + DObjAnimMat rotTransArray[1152]; + bitarray animPartBits; + bitarray ignorePartBits; + }; + struct DObj { From 1edf6ce398e80376f8173b901e115fadf1b45db5 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Fri, 26 Jan 2024 14:10:13 +0100 Subject: [PATCH 24/71] clean it up! --- .../Modules/AssetInterfaces/IXModel.cpp | 685 +++++++++++++++++- .../Modules/AssetInterfaces/IXModel.hpp | 12 + src/Components/Modules/ZoneBuilder.cpp | 472 ++---------- src/Components/Modules/ZoneBuilder.hpp | 6 +- src/Game/Structs.hpp | 2 +- 5 files changed, 756 insertions(+), 421 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index d4444712..ad4f76b0 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -1,13 +1,17 @@ #include + +#include + #include "IXModel.hpp" namespace Assets { + void IXModel::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) { header->model = builder->getIW4OfApi()->read(Game::XAssetType::ASSET_TYPE_XMODEL, name); - if (header->model) + if (header->model) { // ??? if (header->model->physCollmap) @@ -257,4 +261,683 @@ namespace Assets buffer->popBlock(); } + + + uint8_t IXModel::GetIndexOfBone(const Game::XModel* model, std::string name) + { + for (uint8_t i = 0; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto boneName = Game::SL_ConvertToString(bone); + if (name == boneName) + { + return i; + } + } + + return static_cast(UCHAR_MAX); + }; + + uint8_t IXModel::GetParentIndexOfBone(const Game::XModel* model, uint8_t index) + { + const auto parentIndex = index - model->parentList[index - model->numRootBones]; + return static_cast(parentIndex); + }; + + void IXModel::SetParentIndexOfBone(Game::XModel* model, uint8_t boneIndex, uint8_t parentIndex) + { + if (boneIndex == SCHAR_MAX) + { + return; + } + + model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex; + }; + + std::string IXModel::GetParentOfBone(Game::XModel* model, uint8_t index) + { + const auto parentIndex = GetParentIndexOfBone(model, index); + const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); + return boneName; + }; + + uint8_t IXModel::GetHighestAffectingBoneIndex(const Game::XModelLodInfo* lod) + { + uint8_t highestBoneIndex = 0; + constexpr auto LENGTH = 6; + + { + for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + { + const auto surface = &lod->surfs[surfIndex]; + + auto vertsBlendOffset = 0; + + int rebuiltPartBits[6]{}; + std::unordered_set affectingBones{}; + + const auto registerBoneAffectingSurface = [&](unsigned int offset) { + uint8_t index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + highestBoneIndex = std::max(highestBoneIndex, index); + }; + + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + registerBoneAffectingSurface(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + + for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + { + highestBoneIndex = std::max(highestBoneIndex, static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); + } + } + } + + return highestBoneIndex; + }; + + void IXModel::RebuildPartBits(Game::XModel* model) + { + constexpr auto LENGTH = 6; + + for (auto i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + int lodPartBits[6]{}; + + for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + { + const auto surface = &lod->surfs[surfIndex]; + + auto vertsBlendOffset = 0; + + int rebuiltPartBits[6]{}; + std::unordered_set affectingBones{}; + + const auto registerBoneAffectingSurface = [&](unsigned int offset) { + uint8_t index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + + assert(index >= 0); + assert(index < model->numBones); + + affectingBones.emplace(index); + }; + + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + registerBoneAffectingSurface(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + + for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + { + affectingBones.emplace(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat)); + } + + // Actually rebuilding + for (const auto& boneIndex : affectingBones) + { + const auto bitPosition = 31 - boneIndex % 32; + const auto groupIndex = boneIndex / 32; + + assert(groupIndex < 6); + assert(groupIndex >= 0); + + rebuiltPartBits[groupIndex] |= 1 << bitPosition; + lodPartBits[groupIndex] |= 1 << bitPosition; + } + + std::memcpy(surface->partBits, rebuiltPartBits, 6 * sizeof(int32_t)); + } + + std::memcpy(lod->partBits, lodPartBits, 6 * sizeof(int32_t)); + std::memcpy(lod->modelSurfs->partBits, lodPartBits, 6 * sizeof(int32_t)); + + // here's a little lesson in trickery: + // We set the 192nd part bit to TRUE because it has no consequences + // but allows us to find out whether that surf was already converted in the past or not + lod->partBits[LENGTH - 1] |= 0x1; + lod->modelSurfs->partBits[LENGTH - 1] |= 0x1; + } + }; + + + uint8_t IXModel::InsertBone(Game::XModel* model, const std::string& boneName, const std::string& parentName, Utils::Memory::Allocator& allocator) + { + assert(GetIndexOfBone(model, boneName) == UCHAR_MAX); + + constexpr auto MAX_BONES = 192; + + assert(model->numBones < MAX_BONES); + + // Start with backing up parent links that we will have to restore + // We'll restore them at the end + std::map parentsToRestore{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = GetParentOfBone(model, i); + } + + const uint8_t newBoneCount = model->numBones + 1; + const uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; + + const auto parentIndex = GetIndexOfBone(model, parentName); + + assert(parentIndex != UCHAR_MAX); + + const uint8_t atPosition = parentIndex + 1; + + const uint8_t newBoneIndex = atPosition; + const uint8_t newBoneIndexMinusRoot = atPosition - model->numRootBones; + + Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition-1]), Game::SL_ConvertToString(model->boneNames[atPosition+1])); + + // Reallocate + const auto newBoneNames = allocator.allocateArray(newBoneCount); + const auto newMats = allocator.allocateArray(newBoneCount); + const auto newBoneInfo = allocator.allocateArray(newBoneCount); + const auto newPartsClassification = allocator.allocateArray(newBoneCount); + const auto newQuats = allocator.allocateArray(4 * newBoneCountMinusRoot); + const auto newTrans = allocator.allocateArray(3 * newBoneCountMinusRoot); + const auto newParentList = allocator.allocateArray(newBoneCountMinusRoot); + + const uint8_t lengthOfFirstPart = atPosition; + const uint8_t lengthOfSecondPart = model->numBones - atPosition; + + const uint8_t lengthOfFirstPartM1 = atPosition - model->numRootBones; + const uint8_t lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones); + + const uint8_t atPositionM1 = atPosition - model->numRootBones; + + // should be equal to model->numBones + int total = lengthOfFirstPart + lengthOfSecondPart; + assert(total = model->numBones); + + // should be equal to model->numBones - model->numRootBones + int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; + assert(totalM1 == model->numBones - model->numRootBones); + + // Copy before + if (lengthOfFirstPart > 0) + { + std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart); + std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart); + std::memcpy(newPartsClassification, model->partClassification, lengthOfFirstPart); + std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart); + std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1); + std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1); + } + + // Insert new bone + { + unsigned int name = Game::SL_GetString(boneName.data(), 0); + Game::XBoneInfo boneInfo{}; + + Game::DObjAnimMat mat{}; + + // It's ABSOLUTE! + mat = model->baseMat[parentIndex]; + + boneInfo = model->boneInfo[parentIndex]; + + // It's RELATIVE ! + uint16_t quat[4]{}; + quat[3] = 32767; // 0 0 0 32767 + + float trans[3]{}; + + mat.transWeight = 1.9999f; // Should be 1.9999 like everybody? + + newMats[newBoneIndex] = mat; + newBoneInfo[newBoneIndex] = boneInfo; + newBoneNames[newBoneIndex] = static_cast(name); + + // TODO parts Classification + + std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t)); + std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float)); + } + + // Copy after + if (lengthOfSecondPart > 0) + { + std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); + std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); + std::memcpy(&newPartsClassification[atPosition+1], &model->partClassification[atPosition], lengthOfSecondPart); + std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); + std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); + std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); + } + + //Game::Z_VirtualFree(model->baseMat); + //Game::Z_VirtualFree(model->boneInfo); + //Game::Z_VirtualFree(model->boneNames); + //Game::Z_VirtualFree(model->quats); + //Game::Z_VirtualFree(model->trans); + //Game::Z_VirtualFree(model->parentList); + + // Assign reallocated + model->baseMat = newMats; + model->boneInfo = newBoneInfo; + model->boneNames = newBoneNames; + model->quats = newQuats; + model->trans = newTrans; + model->parentList = newParentList; + + model->numBones = newBoneCount; + + // Update vertex weight + for (uint8_t lodIndex = 0; lodIndex < model->numLods; lodIndex++) + { + const auto lod = &model->lodInfo[lodIndex]; + + if ((lod->partBits[5] & 0x1) == 0x1) + { + // surface lod already converted (more efficient) + continue; + } + + if (GetHighestAffectingBoneIndex(lod) >= model->numBones) + { + // surface lod already converted (more accurate) + continue; + } + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + + static_assert(sizeof(Game::DObjSkelMat) == 64); + + { + const auto fixVertexBlendIndex = [&](unsigned int offset) { + + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + + if (index >= atPosition) + { + index++; + + if (index < 0 || index >= model->numBones) + { + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + assert(false); + } + + surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index >= atPosition) + { + index++; + + if (index < 0 || index >= model->numBones) + { + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working list blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + assert(false); + } + + vertList->boneOffset = static_cast(index * sizeof(Game::DObjSkelMat)); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + fixVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + + SetParentIndexOfBone(model, atPosition, parentIndex); + + // Restore parents + for (const auto& kv : parentsToRestore) + { + // Fix parents + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto parentIndex = GetIndexOfBone(model, beforeVal); + const auto index = GetIndexOfBone(model, key); + SetParentIndexOfBone(model, index, parentIndex); + } + +#if DEBUG + // check + for (const auto& kv : parentsToRestore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto index = GetIndexOfBone(model, key); + const auto afterVal = GetParentOfBone(model, index); + + if (beforeVal != afterVal) + { + printf(""); + } + } + // +#endif + + return atPosition; // Bone index of added bone + }; + + + void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) + { + // Does not work + return; + + const auto originalWeights = model->baseMat[origin].transWeight; + model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; + model->baseMat[destination].transWeight = originalWeights; + + for (int i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + { + const auto transferVertexBlendIndex = [&](unsigned int offset) { + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + if (index == origin) + { + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + index = destination; + + surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + if (index == origin) + { + index = destination; + vertList->boneOffset = static_cast(index * sizeof(Game::DObjSkelMat)); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + transferVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + }; + + + void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) + { + auto indexOfSpine = GetIndexOfBone(model, "j_spinelower"); + if (indexOfSpine < UCHAR_MAX) // It has a spine so it must be some sort of humanoid + { + const auto nameOfParent = GetParentOfBone(model, indexOfSpine); + + if (GetIndexOfBone(model, "torso_stabilizer") == UCHAR_MAX) // Singleplayer model is likely + { + + Components::Logger::Print("Converting {}\n", model->name); + + // No stabilizer - let's do surgery + // We're trying to get there: + // tag_origin + // j_main_root + // pelvis + // j_hip_le + // j_hip_ri + // tag_stowed_hip_rear + // torso_stabilizer + // j_spinelower + // back_low + // j_spineupper + // back_mid + // j_spine4 + + + const auto root = GetIndexOfBone(model, "j_mainroot"); + if (root < UCHAR_MAX) { + +#if true + //// Works + //InsertBone(model, "offsetron_the_great_offsetter_of_bones", "j_mainroot", allocator); + + //// Breaks the model + //InsertBone(model, "offsetron2_the_greater_offsetter_of_bones", "j_mainroot", allocator); + + //for (auto lodIndex = 0; lodIndex < model->numLods; lodIndex++) + //{ + // convertedSurfs.emplace(model->lodInfo[lodIndex].modelSurfs); + //} + + //RebuildPartBits(model); + + //return; + + // Add pelvis + const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); + + TransferWeights(model, root, indexOfPelvis); + + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_le"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); + + const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spinelower"), torsoStabilizer); + + const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); + + const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spineupper"), backMid); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spine4"), backMid); + + + assert(root == GetIndexOfBone(model, "j_mainroot")); + assert(indexOfPelvis == GetIndexOfBone(model, "pelvis")); + assert(backLow == GetIndexOfBone(model, "back_low")); + assert(backMid == GetIndexOfBone(model, "back_mid")); + + // Fix up torso stabilizer + model->baseMat[torsoStabilizer].quat[0] = 0.F; + model->baseMat[torsoStabilizer].quat[1] = 0.F; + model->baseMat[torsoStabilizer].quat[2] = 0.F; + model->baseMat[torsoStabilizer].quat[3] = 1.F; + + const auto spineLowerM1 = GetIndexOfBone(model, "j_spinelower") - model->numRootBones; + + // This doesn't feel like it should be necessary + model->trans[spineLowerM1 * 3 + 0] = 0.069828756; + model->trans[spineLowerM1 * 3 + 1] = -0.0f; + model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; + + //// Euler -180.000 88.572 -90.000 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 0] = 16179; // 0.4952 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 1] = 16586; // 0.5077 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 2] = 16586; // 0.5077 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 3] = 16178; // 0.4952 + +#else + const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); + transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); + setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); +#endif + + RebuildPartBits(model); + } + } + printf(""); + } + } + } diff --git a/src/Components/Modules/AssetInterfaces/IXModel.hpp b/src/Components/Modules/AssetInterfaces/IXModel.hpp index 2c68489a..13a58b32 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.hpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.hpp @@ -10,5 +10,17 @@ namespace Assets void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; void mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; void load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) override; + + static void ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator); + + private: + static uint8_t GetIndexOfBone(const Game::XModel* model, std::string name); + static uint8_t GetParentIndexOfBone(const Game::XModel* model, uint8_t index); + static void SetParentIndexOfBone(Game::XModel* model, uint8_t boneIndex, uint8_t parentIndex); + static std::string GetParentOfBone(Game::XModel* model, uint8_t index); + static uint8_t GetHighestAffectingBoneIndex(const Game::XModelLodInfo* lod); + static void RebuildPartBits(Game::XModel* model); + static uint8_t InsertBone(Game::XModel* model, const std::string& boneName, const std::string& parentName, Utils::Memory::Allocator& allocator); + static void TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination); }; } diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index d045a34a..c2e39cab 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -789,7 +789,7 @@ namespace Components params.request_mark_asset = [this](int type, void* data) -> void { - Game::XAsset asset {static_cast(type), {data}}; + Game::XAsset asset{ static_cast(type), {data} }; AssetHandler::ZoneMark(asset, this); this->addRawAsset(static_cast(type), data); @@ -1150,6 +1150,35 @@ namespace Components return params; } + std::function ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector>& assets) + { + ZoneBuilder::BeginAssetTrace(zone); + + Game::XZoneInfo info{}; + info.name = zone.data(); + info.allocFlags = Game::DB_ZONE_MOD; + info.freeFlags = 0; + + Logger::Print("Loading zone '{}'...\n", zone); + + Game::DB_LoadXAssets(&info, 1, true); + AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded + + assets = ZoneBuilder::EndAssetTrace(); + + return [zone]() { + Logger::Print("Unloading zone '{}'...\n", zone); + + Game::XZoneInfo info{}; + info.freeFlags = Game::DB_ZONE_MOD; + info.allocFlags = 0; + info.name = nullptr; + + Game::DB_LoadXAssets(&info, 1, true); + AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded + }; + } + ZoneBuilder::ZoneBuilder() { // ReSharper disable CppStaticAssertFailure @@ -1259,83 +1288,47 @@ namespace Components Utils::Hook::Nop(0x5BC791, 5); AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/) + { + if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) { - if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) - { - ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); -#ifdef _DEBUG - OutputDebugStringA(Utils::String::Format("%s\n", name)); -#endif - } - }); + ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); + } + }); Command::Add("dumpzone", [](const Command::Params* params) { if (params->size() < 2) return; - + std::string zone = params->get(1); + ZoneBuilder::DumpingZone = zone; ZoneBuilder::RefreshExporterWorkDirectory(); - Game::XZoneInfo info; - info.name = zone.data(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; + std::vector> assets{}; + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); - Logger::Print("Loading zone '{}'...\n", zone); + Logger::Print("Dumping zone '{}'...\n", zone); + for (const auto& asset : assets) { - struct asset_t + const auto type = asset.first; + const auto name = asset.second; + if (ExporterAPI.is_type_supported(type) && name[0] != ',') { - Game::XAssetType type; - char name[128]; - }; - - std::vector assets{}; - const auto handle = AssetHandler::OnLoad([&](Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* /*restrict*/) - { - if (ExporterAPI.is_type_supported(type) && name[0] != ',') - { - Game::XAsset xasset = { type, asset }; - asset_t assetIdentifier{}; - - // Fetch name - const auto assetName = Game::DB_GetXAssetName(&xasset); - std::memcpy(assetIdentifier.name, assetName, strnlen(assetName, ARRAYSIZE(assetIdentifier.name) - 1)); - assetIdentifier.name[ARRAYSIZE(assetIdentifier.name) - 1] = '\x00'; - - assetIdentifier.type = type; - - assets.push_back(assetIdentifier); - } - }); - - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded - - Logger::Print("Dumping zone '{}'...\n", zone); - handle(); // Release - for (const auto& asset : assets) - { - const auto assetHeader = Game::DB_FindXAssetHeader(asset.type, asset.name); + const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); if (assetHeader.data) { - ExporterAPI.write(asset.type, assetHeader.data); + ExporterAPI.write(type, assetHeader.data); } else { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!", asset.name); + Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); } } } - Logger::Print("Unloading zone '{}'...\n", zone); - info.freeFlags = Game::DB_ZONE_MOD; - info.allocFlags = 0; - info.name = nullptr; + unload(); - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); ZoneBuilder::DumpingZone = std::string(); }); @@ -1345,30 +1338,8 @@ namespace Components if (params->size() < 2) return; std::string zone = params->get(1); - - ZoneBuilder::BeginAssetTrace(zone); - - Game::XZoneInfo info; - info.name = zone.data(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; - - Logger::Print("Loading zone '{}'...\n", zone); - - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded - - auto assets = ZoneBuilder::EndAssetTrace(); - - Logger::Print("Unloading zone '{}'...\n", zone); - info.freeFlags = Game::DB_ZONE_MOD; - info.allocFlags = 0; - info.name = nullptr; - - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - - Logger::Print("Zone '{}' loaded with {} assets:\n", zone, assets.size()); + std::vector> assets{}; + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); int count = 0; for (auto i = assets.begin(); i != assets.end(); ++i, ++count) @@ -1377,6 +1348,7 @@ namespace Components } Logger::Print("\n"); + unload(); }); Command::Add("buildzone", [](const Command::Params* params) @@ -1405,23 +1377,6 @@ namespace Components } }); - static std::set curTechsets_list; - static std::set techsets_list; - - AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader, const std::string& name, bool*) - { - if (type == Game::ASSET_TYPE_TECHNIQUE_SET) - { - if (name[0] == ',') return; // skip techsets from common_mp - if (techsets_list.find(name) == techsets_list.end()) - { - curTechsets_list.emplace(name); - techsets_list.emplace(name); - } - } - }); - - AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, [[maybe_unused]] const std::string& name, [[maybe_unused]] bool* restrict) { if (type != Game::ASSET_TYPE_SOUND) @@ -1441,6 +1396,11 @@ namespace Components { // ouch // This should never happen and will cause a memory leak +#if DEBUG + // TODO: Crash the game proper when this happen, and do it in ModifyAsset maybe? + __debugbreak(); + assert(false); +#else // Let's change it to a streamed sound instead thisSound.soundFile->type = Game::SAT_STREAMED; @@ -1451,264 +1411,12 @@ namespace Components auto dir = virtualPath.remove_filename().string(); dir = dir.substr(0, dir.size() - 1); // remove / thisSound.soundFile->u.streamSnd.filename.info.raw.dir = Utils::Memory::DuplicateString(dir); +#endif } } } }); - Command::Add("buildtechsets", [](const Command::Params*) - { - Utils::IO::CreateDir("zone_source/techsets"); - Utils::IO::CreateDir("zone/techsets"); - - std::string csvStr; - - const auto dir = std::format("zone/{}", Game::Win_GetLanguage()); - auto fileList = Utils::IO::ListFiles(dir, false); - for (const auto& entry : fileList) - { - auto zone = entry.path().string(); - Utils::String::Replace(zone, Utils::String::VA("zone/%s/", Game::Win_GetLanguage()), ""); - Utils::String::Replace(zone, ".ff", ""); - - if (Utils::IO::FileExists("zone/techsets/" + zone + "_techsets.ff")) - { - Logger::Print("Skipping previously generated zone {}\n", zone); - continue; - } - - if (zone.find("_load") != std::string::npos) - { - Logger::Print("Skipping loadscreen zone {}\n", zone); - continue; - } - - if (Game::DB_IsZoneLoaded(zone.c_str()) || !FastFiles::Exists(zone)) - { - continue; - } - - if (zone[0] == '.') continue; // fucking mac dotfiles - - curTechsets_list.clear(); // clear from last run - - // load the zone - Game::XZoneInfo info; - info.name = zone.c_str(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0x0; - Game::DB_LoadXAssets(&info, 1, 0); - - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(100ms); // wait till its fully loaded - - if (curTechsets_list.empty()) - { - Logger::Print("Skipping empty zone {}\n", zone); - // unload zone - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - continue; - } - - // ok so we're just gonna use the materials because they will use the techsets - csvStr.clear(); - for (auto tech : curTechsets_list) - { - std::string mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - // save csv - Utils::IO::WriteFile("zone_source/techsets/" + zone + "_techsets.csv", csvStr); - - // build the techset zone - std::string zoneName = "techsets/" + zone + "_techsets"; - Logger::Print("Building zone '{}'...\n", zoneName); - Zone(zoneName).build(); - - // unload original zone - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); // wait till its fully loaded - } - - curTechsets_list.clear(); - techsets_list.clear(); - - Game::DB_EnumXAssets(Game::ASSET_TYPE_TECHNIQUE_SET, [](Game::XAssetHeader header, void*) - { - curTechsets_list.emplace(header.techniqueSet->name); - techsets_list.emplace(header.techniqueSet->name); - }, nullptr, false); - - // HACK: set language to 'techsets' to load from that dir - const char* language = Utils::Hook::Get(0x649E740); - Utils::Hook::Set(0x649E740, "techsets"); - - // load generated techset fastfiles - auto list = Utils::IO::ListFiles("zone/techsets", false); - int i = 0; - int subCount = 0; - for (const auto& entry : list) - { - auto it = entry.path().string(); - - Utils::String::Replace(it, "zone/techsets/", ""); - Utils::String::Replace(it, ".ff", ""); - - if (it.find("_techsets") == std::string::npos) continue; // skip files we didn't generate for this - - if (!Game::DB_IsZoneLoaded(it.data())) - { - Game::XZoneInfo info; - info.name = it.data(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; - - Game::DB_LoadXAssets(&info, 1, 0); - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); // wait till its fully loaded - } - else - { - Logger::Print("Zone '{}' already loaded\n", it); - } - - if (i == 20) // cap at 20 just to be safe - { - // create csv with the techsets in it - csvStr.clear(); - for (auto tech : curTechsets_list) - { - std::string mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - std::string tempZoneFile = Utils::String::VA("zone_source/techsets/techsets%d.csv", subCount); - std::string tempZone = Utils::String::VA("techsets/techsets%d", subCount); - - Utils::IO::WriteFile(tempZoneFile, csvStr); - - Logger::Print("Building zone '{}'...\n", tempZone); - Zone(tempZone).build(); - - // unload all zones - Game::XZoneInfo info; - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - - Utils::Hook::Set(0x649E740, "techsets"); - - i = 0; - subCount++; - curTechsets_list.clear(); - techsets_list.clear(); - } - - i++; - } - - // last iteration - if (i != 0) - { - // create csv with the techsets in it - csvStr.clear(); - for (auto tech : curTechsets_list) - { - std::string mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - Logger::Print("Couldn't find a material for techset {}. Sort Keys will be incorrect.\n", tech); - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - std::string tempZoneFile = Utils::String::VA("zone_source/techsets/techsets%d.csv", subCount); - std::string tempZone = Utils::String::VA("techsets/techsets%d", subCount); - - Utils::IO::WriteFile(tempZoneFile, csvStr); - - Logger::Print("Building zone '{}'...\n", tempZone); - Zone(tempZone).build(); - - // unload all zones - Game::XZoneInfo info; - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - - subCount++; - } - - // build final techsets fastfile - if (subCount > 24) - { - Logger::Error(Game::ERR_DROP, "How did you have 576 fastfiles?\n"); - } - - curTechsets_list.clear(); - techsets_list.clear(); - - for (int j = 0; j < subCount; ++j) - { - Game::XZoneInfo info; - info.name = Utils::String::VA("techsets%d", j); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; - - Game::DB_LoadXAssets(&info, 1, 0); - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); // wait till its fully loaded - } - - // create csv with the techsets in it - csvStr.clear(); - for (const auto& tech : curTechsets_list) - { - auto mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - Utils::IO::WriteFile("zone_source/techsets/techsets.csv", csvStr); - - // set language back - Utils::Hook::Set(0x649E740, language); - - Logger::Print("Building zone 'techsets/techsets'...\n"); - Zone("techsets/techsets").build(); - }); - Command::Add("listassets", [](const Command::Params* params) { if (params->size() < 2) return; @@ -1723,39 +1431,6 @@ namespace Components }, &type, false); } }); - - Command::Add("loadtempzone", [](const Command::Params* params) - { - if (params->size() < 2) return; - - if (FastFiles::Exists(params->get(1))) - { - Game::XZoneInfo info; - info.name = params->get(1); - info.allocFlags = 0x80; - info.freeFlags = 0x0; - Game::DB_LoadXAssets(&info, 1, 0); - } - }); - - Command::Add("unloadtempzones", [](const Command::Params*) - { - Game::XZoneInfo info; - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = 0x80; - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }); - - Command::Add("materialInfoDump", [](const Command::Params*) - { - Game::DB_EnumXAssets(Game::ASSET_TYPE_MATERIAL, [](Game::XAssetHeader header, void*) - { - Logger::Print("{}: {:#X} {:#X} {:#X}\n", - header.material->info.name, header.material->info.sortKey & 0xFF, header.material->info.gameFlags & 0xFF, header.material->stateFlags & 0xFF); - }, nullptr, false); - }); } } @@ -1767,37 +1442,4 @@ namespace Components ZoneBuilder::CommandThread.join(); } } - -#if defined(DEBUG) || defined(FORCE_UNIT_TESTS) - bool ZoneBuilder::unitTest() - { - printf("Testing circular bit shifting (left)..."); - - unsigned int integer = 0x80000000; - Utils::RotLeft(integer, 1); - - if (integer != 1) - { - printf("Error\n"); - printf("Bit shifting failed: %X\n", integer); - return false; - } - - printf("Success\n"); - printf("Testing circular bit shifting (right)..."); - - unsigned char byte = 0b00000011; - Utils::RotRight(byte, 2); - - if (byte != 0b11000000) - { - printf("Error\n"); - printf("Bit shifting failed %X\n", byte & 0xFF); - return false; - } - - printf("Success\n"); - return true; - } -#endif } diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 1450faef..4f4a4427 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -124,10 +124,6 @@ namespace Components ZoneBuilder(); ~ZoneBuilder(); -#if defined(DEBUG) || defined(FORCE_UNIT_TESTS) - bool unitTest() override; -#endif - static bool IsEnabled(); static bool IsDumpingZone() { return DumpingZone.length() > 0; }; @@ -161,6 +157,8 @@ namespace Components static iw4of::params_t GetExporterAPIParams(); + static std::function LoadZoneWithTrace(const std::string& zone, OUT std::vector> &assets); + static void Com_Quitf_t(); static void CommandThreadCallback(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 3fd346d3..4b2b0c2a 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -7118,7 +7118,7 @@ namespace Game IMG_FORMAT_COUNT = 0x17, }; - enum $25EF9448C800B18F0C83DB367159AFD6 + enum XAnimPartType { PART_TYPE_NO_QUAT = 0x0, PART_TYPE_HALF_QUAT = 0x1, From f1ec16983a3a9636a8c7b729d423b471b8b43789 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Fri, 26 Jan 2024 14:10:33 +0100 Subject: [PATCH 25/71] more cleanup & some fucking around with xanimparts --- src/Components/Modules/AssetHandler.cpp | 682 ++++-------------------- 1 file changed, 108 insertions(+), 574 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 37bf8ea5..5c17fc1a 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -235,615 +235,148 @@ namespace Components } } #endif + if (type == Game::XAssetType::ASSET_TYPE_XANIMPARTS) + { + auto anim = asset.parts; + if (anim->name == "unarmed_cowerstand_idle"s || anim->name == "civilian_walk_cool"s || anim->name == "civilian_run_upright"s) + { + auto str = Game::SL_FindString("j_spinelower"); + int interestingPart = 0; + for (size_t i = 0; i < anim->boneCount[Game::XAnimPartType::PART_TYPE_ALL]; i++) + { + if (anim->names[i] == str) + { + interestingPart = i; + //anim->names[i] = Game::SL_FindString("torso_stabilizer"); + } + } + + // Something interesting to do there + //for (size_t frameIndex = 0; frameIndex < anim->numframes; frameIndex++) + //{ + // const auto delta = &anim->deltaPart[frameIndex]; + + // const auto frameCount = anim->numframes; + + // if (delta->trans->size) + // { + // const auto test = delta->trans->u.frames; + + // if (frameCount > 0xFF) + // { + // delta->trans->u.frames.indices._1 + // buffer->saveArray(delta->trans->u.frames.indices._2, delta->trans->size + 1); + // } + // else + // { + // buffer->saveArray(delta->trans->u.frames.indices._1, delta->trans->size + 1); + // } + + // if (delta->trans->u.frames.frames._1) + // { + // if (delta->trans->smallTrans) + // { + // buffer->save(delta->trans->u.frames.frames._1, 3, delta->trans->size + 1); + // } + // else + // { + // buffer->align(Utils::Stream::ALIGN_4); + // buffer->save(delta->trans->u.frames.frames._1, 6, delta->trans->size + 1); + // } + // } + // } + // else + // { + // buffer->save(delta->trans->u.frame0, 12); + // } + //} + } + } + if (type == Game::XAssetType::ASSET_TYPE_XMODEL) - //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) + //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) { - const auto model = asset.model; - if (!model->quats || !model->trans) + + if (name == "body_urban_civ_male_aa"s) { - return; + printf(""); } - // Update vertex weights - //for (int i = 0; i < model->numLods; i++) - //{ - // const auto lod = &model->lodInfo[i]; + const auto model = asset.model; - // for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) - // { - // auto vertsBlendOffset = 0u; + int total = + model->noScalePartBits[0] | + model->noScalePartBits[1] | + model->noScalePartBits[2] | + model->noScalePartBits[3] | + model->noScalePartBits[4] | + model->noScalePartBits[5]; - // const auto surface = &lod->modelSurfs->surfs[surfIndex]; - // surface->partBits[0] = 0x00000000; - // surface->partBits[1] = 0x00000000; - // surface->partBits[2] = 0x00000000; - // surface->partBits[3] = 0x00000000; - // surface->partBits[4] = 0x00000000; - // surface->partBits[5] = 0x00000000; - // } - //} - - //return; - - - - class QuatInt16 + if (total != 0) { - public: - static uint16_t ToInt16(const float quat) - { - return static_cast(quat * INT16_MAX); - } + printf(""); + } - static float ToFloat(const uint16_t quat) - { - return static_cast(quat) / static_cast(INT16_MAX); - } - }; + if (!model->quats || !model->trans || model->numBones < 10) + { + // Unlikely candidate + return; + } // static std::vector names{}; names.clear(); + static Utils::Memory::Allocator allocator{}; for (auto i = 0; i < model->numBones; i++) { const auto bone = model->boneNames[i]; - const auto name = Game::SL_ConvertToString(bone); + const auto boneName = Game::SL_ConvertToString(bone); + const auto str = std::format("{} => q({} {} {} {}) t({} {} {})", + boneName, + model->baseMat[i - 1].quat[0], + model->baseMat[i - 1].quat[1], + model->baseMat[i - 1].quat[2], + model->baseMat[i - 1].quat[3], + model->baseMat[i - 1].trans[0], + model->baseMat[i - 1].trans[1], + model->baseMat[i - 1].trans[2] + ); + + const auto duplicated = allocator.duplicateString(str); names.push_back( - std::format("{} => q({} {} {} {}) t({} {} {})", - name, - model->baseMat[i-1].quat[0], - model->baseMat[i-1].quat[1], - model->baseMat[i-1].quat[2], - model->baseMat[i-1].quat[3], - model->baseMat[i-1].trans[0], - model->baseMat[i-1].trans[1], - model->baseMat[i-1].trans[2] - ) + duplicated ); } - printf(""); // - const auto getIndexOfBone = [&](std::string name) - { - for (uint8_t i = 0; i < model->numBones; i++) - { - const auto bone = model->boneNames[i]; - const auto boneName = Game::SL_ConvertToString(bone); - if (name == boneName) - { - return i; - } - } - - return static_cast(UCHAR_MAX); - }; - - const auto getParentIndexOfBone = [&](uint8_t index) - { - const auto parentIndex = index - model->parentList[index - model->numRootBones]; - return parentIndex; - }; - - const auto setParentIndexOfBone = [&](uint8_t boneIndex, uint8_t parentIndex) - { - if (boneIndex == SCHAR_MAX) - { - return; - } - - model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex; - }; - - const auto getParentOfBone = [&](int index) - { - const auto parentIndex = getParentIndexOfBone(index); - const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); - return boneName; - }; - - const auto insertBoneInPartbits = [&](int8_t atPosition, int* partBits, bool hasAnyWeight = false) - { - constexpr auto LENGTH = 6; - // Bit masks and bit shifting are a pain honestly - std::vector flags{}; - - int check[LENGTH]{}; - std::memcpy(check, partBits, LENGTH * sizeof(int)); - - for (auto i = 0; i < LENGTH; i++) - { - for (signed int bitPosition = 32 - 1; bitPosition >= 0; bitPosition--) - { - flags.emplace_back((partBits[i] >> bitPosition) & 0x1); - } - } - - // copy parent flag - const auto partBitIndex = atPosition / 32; - const auto bitPosition = atPosition % 32; // The vector is already reversed so this should be correct - - flags.insert(flags.begin() + (partBitIndex * 32 + bitPosition), hasAnyWeight); - - flags.pop_back(); - - // clear it - for (auto i = 0; i < LENGTH; i++) - { - partBits[i] = 0; - } - - // Write it - for (auto i = 0; i < LENGTH; i++) - { - partBits[i] = 0; - for (int bitPosition = 0; bitPosition < 32; bitPosition++) - { - const auto value = flags[i * 32 + 31 - bitPosition]; - partBits[i] |= (value << bitPosition); - } - } - - flags.clear(); - }; - - - const auto insertBone = [&](const std::string& boneName, const std::string& parentName) - { - assert(getIndexOfBone(boneName) == UCHAR_MAX); - - // Start with backing up parent links that we will have to restore - // We'll restore them at the end - std::map parentsToRestore{}; - for (int i = model->numRootBones; i < model->numBones; i++) - { - parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); - } - - - uint8_t newBoneCount = model->numBones + 1; - uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; - - const auto parentIndex = getIndexOfBone(parentName); - - assert(parentIndex != UCHAR_MAX); - - int atPosition = parentIndex + 1; - - const auto newBoneIndex = atPosition; - const auto newBoneIndexMinusRoot = atPosition - model->numRootBones; - - // Reallocate - const auto newBoneNames = (uint16_t*)Game::Z_Malloc(sizeof(uint16_t) * newBoneCount); - const auto newMats = (Game::DObjAnimMat*)Game::Z_Malloc(sizeof(Game::DObjAnimMat) * newBoneCount); - const auto newBoneInfo = (Game::XBoneInfo*)Game::Z_Malloc(sizeof(Game::XBoneInfo) * newBoneCount); - const auto newQuats = (int16_t*)Game::Z_Malloc(sizeof(uint16_t) * 4 * newBoneCountMinusRoot); - const auto newTrans = (float*)Game::Z_Malloc(sizeof(float) * 3 * newBoneCountMinusRoot); - const auto newParentList = reinterpret_cast(Game::Z_Malloc(sizeof(uint8_t) * newBoneCountMinusRoot)); - - int lengthOfFirstPart = atPosition; - int lengthOfSecondPart = model->numBones - atPosition; - - int lengthOfFirstPartM1 = atPosition - model->numRootBones; - int lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones); - - int atPositionM1 = atPosition - model->numRootBones; - - // should be equal to model->numBones - int total = lengthOfFirstPart + lengthOfSecondPart; - assert(total = model->numBones); - - // should be equal to model->numBones - model->numRootBones - int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; - assert(totalM1 == model->numBones - model->numRootBones); - - // Copy before - if (lengthOfFirstPart > 0) - { - std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart); - std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart); - std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart); - std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1); - std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1); - } - - // Insert new bone - { - unsigned int name = Game::SL_GetString(boneName.data(), 0); - Game::XBoneInfo boneInfo{}; - - Game::DObjAnimMat mat{}; - - // It's ABSOLUTE! - mat = model->baseMat[parentIndex]; - - boneInfo = model->boneInfo[parentIndex]; - - // It's RELATIVE ! - uint16_t quat[4]{}; - quat[3] = 32767; // 0 0 0 32767 - - float trans[3]{}; - - mat.transWeight = 1.9999f; // Should be 1.9999 like everybody? - - newMats[newBoneIndex] = mat; - newBoneInfo[newBoneIndex] = boneInfo; - newBoneNames[newBoneIndex] = name; - - std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t)); - std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float)); - } - - // Copy after - if (lengthOfSecondPart > 0) - { - std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); - std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); - std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); - std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); - std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); - } - - // Assign reallocated - model->baseMat = newMats; - model->boneInfo = newBoneInfo; - model->boneNames = newBoneNames; - model->quats = newQuats; - model->trans = newTrans; - model->parentList = newParentList; - - model->numBones++; - - // Update vertex weights - for (int i = 0; i < model->numLods; i++) - { - const auto lod = &model->lodInfo[i]; - insertBoneInPartbits(atPosition, lod->partBits, false); - insertBoneInPartbits(atPosition, lod->modelSurfs->partBits, false); - - for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) - { - auto vertsBlendOffset = 0u; - - const auto surface = &lod->modelSurfs->surfs[surfIndex]; - - //surface->partBits[0] = 0b00000000000000000000000000000000; - //surface->partBits[1] = 0b01010101010101010101010101010101; - //surface->partBits[2] = 0b00110011001100110011001100110011; - //surface->partBits[3] = 0b00011100011100011100011100011100; - //surface->partBits[4] = 0b00001111000011110000111100001111; - //surface->partBits[5] = 0b00000111110000011111000001111100; - - insertBoneInPartbits(atPosition, surface->partBits, false); - - { - const auto fixVertexBlendIndex = [&](unsigned int offset) { - int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); - if (index >= atPosition) - { - if (index < 0 || index >= model->numBones - 1) - { - //assert(false); - } - - index++; - - surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); - } - }; - - // Fix bone offsets - if (surface->vertList) - { - for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) - { - const auto vertList = &surface->vertList[vertListIndex]; - auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); - if (index < 0 || index >= model->numBones - 1) - { - //assert(false); - } - - if (index >= atPosition) - { - index++; - vertList->boneOffset = index * sizeof(Game::DObjSkelMat); - } - } - } - - // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - - vertsBlendOffset += 1; - } - - // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - fixVertexBlendIndex(vertsBlendOffset + 1); - - vertsBlendOffset += 3; - } - - // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - fixVertexBlendIndex(vertsBlendOffset + 1); - fixVertexBlendIndex(vertsBlendOffset + 3); - - vertsBlendOffset += 5; - } - - // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - fixVertexBlendIndex(vertsBlendOffset + 1); - fixVertexBlendIndex(vertsBlendOffset + 3); - fixVertexBlendIndex(vertsBlendOffset + 5); - - vertsBlendOffset += 7; - } - } - } - } - - // TODO free memory of original lists - printf(""); - - setParentIndexOfBone(atPosition, parentIndex); - - // Restore parents - for (const auto& kv : parentsToRestore) - { - // Fix parents - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto parentIndex = getIndexOfBone(beforeVal); - const auto index = getIndexOfBone(key); - setParentIndexOfBone(index, parentIndex); - } - - - // check - for (const auto& kv : parentsToRestore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto index = getIndexOfBone(key); - const auto afterVal = getParentOfBone(index); - - if (beforeVal != afterVal) - { - printf(""); - } - } - // - - return atPosition; // Bone index of added bone - }; - - - const auto transferWeights = [&](const uint8_t origin, const uint8_t destination) - { - return; - - const auto originalWeights = model->baseMat[origin].transWeight; - model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; - model->baseMat[destination].transWeight = originalWeights; - - for (int i = 0; i < model->numLods; i++) - { - const auto lod = &model->lodInfo[i]; - - for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) - { - auto vertsBlendOffset = 0u; - - const auto surface = &lod->modelSurfs->surfs[surfIndex]; - { - const auto transferVertexBlendIndex = [&](unsigned int offset) { - int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); - if (index == origin) - { - if (index < 0 || index >= model->numBones - 1) - { - assert(false); - } - - index = destination; - - surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); - } - }; - - // Fix bone offsets - if (surface->vertList) - { - for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) - { - const auto vertList = &surface->vertList[vertListIndex]; - auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); - if (index < 0 || index >= model->numBones - 1) - { - assert(false); - } - - if (index == origin) - { - index = destination; - vertList->boneOffset = index * sizeof(Game::DObjSkelMat); - } - } - } - - // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - - vertsBlendOffset += 1; - } - - // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - transferVertexBlendIndex(vertsBlendOffset + 1); - - vertsBlendOffset += 3; - } - - // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - transferVertexBlendIndex(vertsBlendOffset + 1); - transferVertexBlendIndex(vertsBlendOffset + 3); - - - vertsBlendOffset += 5; - } - - // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - transferVertexBlendIndex(vertsBlendOffset + 1); - transferVertexBlendIndex(vertsBlendOffset + 3); - transferVertexBlendIndex(vertsBlendOffset + 5); - - vertsBlendOffset += 7; - } - } - } - } - }; - - auto indexOfSpine = getIndexOfBone("j_spinelower"); - if (indexOfSpine < UCHAR_MAX) - { - const auto nameOfParent = getParentOfBone(indexOfSpine); - - if (getIndexOfBone("torso_stabilizer") == UCHAR_MAX) - { - // No stabilizer - let's do surgery - // We're trying to get there: - // tag_origin - // j_main_root - // pelvis - // j_hip_le - // j_hip_ri - // tag_stowed_hip_rear - // torso_stabilizer - // j_spinelower - // back_low - // j_spineupper - // back_mid - // j_spine4 - - - const auto root = getIndexOfBone("j_mainroot"); - if (root < UCHAR_MAX) { - -#if true - // Add pelvis - const uint8_t indexOfPelvis = insertBone("pelvis", "j_mainroot"); - transferWeights(root, indexOfPelvis); - - setParentIndexOfBone(getIndexOfBone("j_hip_le"), indexOfPelvis); - setParentIndexOfBone(getIndexOfBone("j_hip_ri"), indexOfPelvis); - setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), indexOfPelvis); - - const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "pelvis"); - setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); - - const uint8_t backLow = insertBone("back_low", "j_spinelower"); - transferWeights(getIndexOfBone("j_spinelower"), backLow); - setParentIndexOfBone(getIndexOfBone("j_spineupper"), backLow); - - const uint8_t backMid = insertBone("back_mid", "j_spineupper"); - transferWeights(getIndexOfBone("j_spineupper"), backMid); - setParentIndexOfBone(getIndexOfBone("j_spine4"), backMid); - - - assert(root == getIndexOfBone("j_mainroot")); - assert(indexOfPelvis == getIndexOfBone("pelvis")); - assert(backLow == getIndexOfBone("back_low")); - assert(backMid == getIndexOfBone("back_mid")); - - // Fix up torso stabilizer - model->baseMat[torsoStabilizer].quat[0] = 0.F; - model->baseMat[torsoStabilizer].quat[1] = 0.F; - model->baseMat[torsoStabilizer].quat[2] = 0.F; - model->baseMat[torsoStabilizer].quat[3] = 1.F; - - const auto spineLowerM1 = getIndexOfBone("j_spinelower")-1; - - //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 0] = 2.0f; - //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 1] = -5.0f; - //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 2] = 2.0F; - - model->trans[spineLowerM1 * 3 + 0] = 0.069828756; - model->trans[spineLowerM1 * 3 + 1] = -0.0f; - model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; - - //// Euler -180.000 88.572 -90.000 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 0] = 16179; // 0.4952 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 1] = 16586; // 0.5077 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 2] = 16586; // 0.5077 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 3] = 16178; // 0.4952 - -#else - const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); - transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); - setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); -#endif - -#if DEBUG - const auto newRoot = getIndexOfBone("j_mainroot"); - assert(root == newRoot); -#endif - - printf(""); - } - } - printf(""); - } - + Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); printf(""); + // names.clear(); for (auto i = 1; i < model->numBones; i++) { const auto bone = model->boneNames[i]; - const auto name = Game::SL_ConvertToString(bone); - const auto m1 = i-1; + const auto boneName = Game::SL_ConvertToString(bone); + const auto m1 = i - 1; const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", - name, - model->quats[m1 * 4 + 0], - model->quats[m1* 4 + 1], - model->quats[m1 * 4 + 2], - model->quats[m1 * 4 + 3], - model->trans[m1 * 3 + 0], - model->trans[m1* 3 +1], - model->trans[m1 * 3 +2] - ); + boneName, + model->quats[m1 * 4 + 0], + model->quats[m1 * 4 + 1], + model->quats[m1 * 4 + 2], + model->quats[m1 * 4 + 3], + model->trans[m1 * 3 + 0], + model->trans[m1 * 3 + 1], + model->trans[m1 * 3 + 2] + ); names.push_back( fmt ); + printf(""); } @@ -911,6 +444,7 @@ namespace Components bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) { const char* name = Game::DB_GetXAssetNameHandlers[type](asset); + if (!name) return false; for (auto i = AssetHandler::EmptyAssets.begin(); i != AssetHandler::EmptyAssets.end();) From 8fdde66bdc5ab5fad996a62fddf28b5513f0feed Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 02:23:52 +0100 Subject: [PATCH 26/71] Zonebuilder improvements --- src/Components/Modules/AssetHandler.cpp | 95 +-------- .../Modules/AssetInterfaces/IXModel.cpp | 171 ++++++++++------ .../Modules/AssetInterfaces/IXModel.hpp | 3 + src/Components/Modules/QuickPatch.cpp | 3 - src/Components/Modules/ZoneBuilder.cpp | 193 +++++++++++++++--- src/Components/Modules/ZoneBuilder.hpp | 5 +- src/Game/Structs.hpp | 2 +- 7 files changed, 284 insertions(+), 188 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 5c17fc1a..3424c04a 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -242,54 +242,14 @@ namespace Components { auto str = Game::SL_FindString("j_spinelower"); int interestingPart = 0; - for (size_t i = 0; i < anim->boneCount[Game::XAnimPartType::PART_TYPE_ALL]; i++) { if (anim->names[i] == str) { interestingPart = i; - //anim->names[i] = Game::SL_FindString("torso_stabilizer"); } } // Something interesting to do there - //for (size_t frameIndex = 0; frameIndex < anim->numframes; frameIndex++) - //{ - // const auto delta = &anim->deltaPart[frameIndex]; - - // const auto frameCount = anim->numframes; - - // if (delta->trans->size) - // { - // const auto test = delta->trans->u.frames; - - // if (frameCount > 0xFF) - // { - // delta->trans->u.frames.indices._1 - // buffer->saveArray(delta->trans->u.frames.indices._2, delta->trans->size + 1); - // } - // else - // { - // buffer->saveArray(delta->trans->u.frames.indices._1, delta->trans->size + 1); - // } - - // if (delta->trans->u.frames.frames._1) - // { - // if (delta->trans->smallTrans) - // { - // buffer->save(delta->trans->u.frames.frames._1, 3, delta->trans->size + 1); - // } - // else - // { - // buffer->align(Utils::Stream::ALIGN_4); - // buffer->save(delta->trans->u.frames.frames._1, 6, delta->trans->size + 1); - // } - // } - // } - // else - // { - // buffer->save(delta->trans->u.frame0, 12); - // } - //} } } @@ -323,65 +283,14 @@ namespace Components // Unlikely candidate return; } - - // - static std::vector names{}; - names.clear(); - static Utils::Memory::Allocator allocator{}; - for (auto i = 0; i < model->numBones; i++) - { - const auto bone = model->boneNames[i]; - const auto boneName = Game::SL_ConvertToString(bone); - const auto str = std::format("{} => q({} {} {} {}) t({} {} {})", - boneName, - model->baseMat[i - 1].quat[0], - model->baseMat[i - 1].quat[1], - model->baseMat[i - 1].quat[2], - model->baseMat[i - 1].quat[3], - model->baseMat[i - 1].trans[0], - model->baseMat[i - 1].trans[1], - model->baseMat[i - 1].trans[2] - ); - - const auto duplicated = allocator.duplicateString(str); - names.push_back( - duplicated - ); - } // - - Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); - printf(""); - - - // - names.clear(); - for (auto i = 1; i < model->numBones; i++) + //if (name == "body_urban_civ_female_a"s) { - const auto bone = model->boneNames[i]; - const auto boneName = Game::SL_ConvertToString(bone); - const auto m1 = i - 1; - const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", - boneName, - model->quats[m1 * 4 + 0], - model->quats[m1 * 4 + 1], - model->quats[m1 * 4 + 2], - model->quats[m1 * 4 + 3], - model->trans[m1 * 3 + 0], - model->trans[m1 * 3 + 1], - model->trans[m1 * 3 + 2] - ); - names.push_back( - fmt - ); - + Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); printf(""); } - - printf(""); - // } if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index ad4f76b0..74561206 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -296,6 +296,7 @@ namespace Assets std::string IXModel::GetParentOfBone(Game::XModel* model, uint8_t index) { + assert(index > 0); const auto parentIndex = GetParentIndexOfBone(model, index); const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); return boneName; @@ -313,17 +314,17 @@ namespace Assets auto vertsBlendOffset = 0; - int rebuiltPartBits[6]{}; + int rebuiltPartBits[LENGTH]{}; std::unordered_set affectingBones{}; const auto registerBoneAffectingSurface = [&](unsigned int offset) { uint8_t index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); highestBoneIndex = std::max(highestBoneIndex, index); - }; + }; // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); @@ -331,7 +332,7 @@ namespace Assets } // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -340,7 +341,7 @@ namespace Assets } // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -350,7 +351,7 @@ namespace Assets } // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -360,7 +361,7 @@ namespace Assets vertsBlendOffset += 7; } - for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + for (unsigned int vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) { highestBoneIndex = std::max(highestBoneIndex, static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); } @@ -395,7 +396,7 @@ namespace Assets assert(index < model->numBones); affectingBones.emplace(index); - }; + }; // 1 bone weight @@ -438,7 +439,7 @@ namespace Assets for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) { - affectingBones.emplace(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat)); + affectingBones.emplace(static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); } // Actually rebuilding @@ -497,7 +498,7 @@ namespace Assets const uint8_t newBoneIndex = atPosition; const uint8_t newBoneIndexMinusRoot = atPosition - model->numRootBones; - Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition-1]), Game::SL_ConvertToString(model->boneNames[atPosition+1])); + Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition - 1]), Game::SL_ConvertToString(model->boneNames[atPosition + 1])); // Reallocate const auto newBoneNames = allocator.allocateArray(newBoneCount); @@ -549,7 +550,7 @@ namespace Assets // It's RELATIVE ! uint16_t quat[4]{}; - quat[3] = 32767; // 0 0 0 32767 + quat[3] = SHRT_MAX; // 0 0 0 1 float trans[3]{}; @@ -570,7 +571,7 @@ namespace Assets { std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); - std::memcpy(&newPartsClassification[atPosition+1], &model->partClassification[atPosition], lengthOfSecondPart); + std::memcpy(&newPartsClassification[atPosition + 1], &model->partClassification[atPosition], lengthOfSecondPart); std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); @@ -635,7 +636,7 @@ namespace Assets surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); } - }; + }; // Fix bone offsets if (surface->vertList) @@ -739,8 +740,9 @@ namespace Assets void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) { - // Does not work - return; + const auto from = Game::SL_ConvertToString(model->boneNames[origin]); + const auto to = Game::SL_ConvertToString(model->boneNames[destination]); + Components::Logger::Print("Transferring bone weights from {} to {}\n", from, to); const auto originalWeights = model->baseMat[origin].transWeight; model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; @@ -750,6 +752,12 @@ namespace Assets { const auto lod = &model->lodInfo[i]; + if ((lod->partBits[5] & 0x1) == 0x1) + { + // surface lod already converted (more efficient) + continue; + } + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) { auto vertsBlendOffset = 0u; @@ -760,12 +768,13 @@ namespace Assets int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); if (index == origin) { - if (index < 0 || index >= model->numBones - 1) + index = destination; + + if (index < 0 || index >= model->numBones) { assert(false); } - index = destination; surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); } @@ -778,13 +787,14 @@ namespace Assets { const auto vertList = &surface->vertList[vertListIndex]; auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); - if (index < 0 || index >= model->numBones - 1) - { - assert(false); - } if (index == origin) { + if (index < 0 || index >= model->numBones) + { + assert(false); + } + index = destination; vertList->boneOffset = static_cast(index * sizeof(Game::DObjSkelMat)); } @@ -834,6 +844,46 @@ namespace Assets } }; + void IXModel::SetBoneTrans(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z) + { + if (baseMat) + { + model->baseMat[boneIndex].trans[0] = x; + model->baseMat[boneIndex].trans[1] = y; + model->baseMat[boneIndex].trans[2] = z; + } + else + { + const auto index = boneIndex - model->numRootBones; + assert(index >= 0); + + model->trans[index * 3 + 0] = x; + model->trans[index * 3 + 1] = y; + model->trans[index * 3 + 2] = z; + } + } + + void IXModel::SetBoneQuaternion(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z, float w) + { + if (baseMat) + { + model->baseMat[boneIndex].quat[0] = x; + model->baseMat[boneIndex].quat[1] = y; + model->baseMat[boneIndex].quat[2] = z; + model->baseMat[boneIndex].quat[3] = w; + } + else + { + const auto index = boneIndex - model->numRootBones; + assert(index >= 0); + + model->quats[index * 4 + 0] = static_cast(x * SHRT_MAX); + model->quats[index * 4 + 1] = static_cast(y * SHRT_MAX); + model->quats[index * 4 + 2] = static_cast(z * SHRT_MAX); + model->quats[index * 4 + 3] = static_cast(w * SHRT_MAX); + } + } + void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) { @@ -866,24 +916,14 @@ namespace Assets const auto root = GetIndexOfBone(model, "j_mainroot"); if (root < UCHAR_MAX) { -#if true - //// Works - //InsertBone(model, "offsetron_the_great_offsetter_of_bones", "j_mainroot", allocator); - - //// Breaks the model - //InsertBone(model, "offsetron2_the_greater_offsetter_of_bones", "j_mainroot", allocator); - - //for (auto lodIndex = 0; lodIndex < model->numLods; lodIndex++) - //{ - // convertedSurfs.emplace(model->lodInfo[lodIndex].modelSurfs); - //} - - //RebuildPartBits(model); - - //return; - // Add pelvis +#if false + const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); +#else const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); + SetBoneQuaternion(model, indexOfPelvis, true, -0.494f, -0.506f, -0.506f, 0.494); TransferWeights(model, root, indexOfPelvis); @@ -891,11 +931,23 @@ namespace Assets SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); + // These two are optional + if (GetIndexOfBone(model, "j_coatfront_le") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_le", "pelvis", allocator); + } + + if (GetIndexOfBone(model, "j_coatfront_ri") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_ri", "pelvis", allocator); + } + const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spinelower"), torsoStabilizer); + const uint8_t lowerSpine = GetIndexOfBone(model, "j_spinelower"); + SetParentIndexOfBone(model, lowerSpine, torsoStabilizer); const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); - TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); + TransferWeights(model, lowerSpine, backLow); SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); @@ -908,29 +960,34 @@ namespace Assets assert(backLow == GetIndexOfBone(model, "back_low")); assert(backMid == GetIndexOfBone(model, "back_mid")); - // Fix up torso stabilizer - model->baseMat[torsoStabilizer].quat[0] = 0.F; - model->baseMat[torsoStabilizer].quat[1] = 0.F; - model->baseMat[torsoStabilizer].quat[2] = 0.F; - model->baseMat[torsoStabilizer].quat[3] = 1.F; + // Twister bone + SetBoneQuaternion(model, lowerSpine, false, -0.492f, -0.507f, -0.507f, 0.492f); + SetBoneQuaternion(model, torsoStabilizer, false, 0.494f, 0.506f, 0.506f, 0.494f); - const auto spineLowerM1 = GetIndexOfBone(model, "j_spinelower") - model->numRootBones; // This doesn't feel like it should be necessary - model->trans[spineLowerM1 * 3 + 0] = 0.069828756; - model->trans[spineLowerM1 * 3 + 1] = -0.0f; - model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; + // It is, on singleplayer models unfortunately. Could we add an extra bone to compensate this? + // Or compensate it another way? + SetBoneTrans(model, GetIndexOfBone(model, "j_spinelower"), false, 0.07, 0.0f, 5.2f); - //// Euler -180.000 88.572 -90.000 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 0] = 16179; // 0.4952 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 1] = 16586; // 0.5077 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 2] = 16586; // 0.5077 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 3] = 16178; // 0.4952 + // These are often messed up on civilian models, but there is no obvious way to tell from code + const auto stowedBack = GetIndexOfBone(model, "tag_stowed_back"); + if (stowedBack != UCHAR_MAX) + { + SetBoneTrans(model, stowedBack, false, -0.32f, -6.27f, -2.65F); + SetBoneQuaternion(model, stowedBack, false, -0.044, 0.088, -0.995, 0.025); + SetBoneTrans(model, stowedBack, true, -9.571f, -2.654f, 51.738f); + SetBoneQuaternion(model, stowedBack, true, -0.071f, 0.0f, -0.997f, 0.0f); + } -#else - const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); - transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); - setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); + if (stowedBack != UCHAR_MAX) + { + const auto stowedRear = GetIndexOfBone(model, "tag_stowed_hip_rear"); + SetBoneTrans(model, stowedRear, false, -0.75f, -6.45f, -4.99f); + SetBoneQuaternion(model, stowedRear, false, -0.553f, -0.062f, -0.049f, 0.830f); + SetBoneTrans(model, stowedBack, true, -9.866f, -4.989f, 36.315f); + SetBoneQuaternion(model, stowedRear, true, -0.054, -0.025f, -0.975f, 0.214f); + } #endif RebuildPartBits(model); diff --git a/src/Components/Modules/AssetInterfaces/IXModel.hpp b/src/Components/Modules/AssetInterfaces/IXModel.hpp index 13a58b32..df4c54e4 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.hpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.hpp @@ -22,5 +22,8 @@ namespace Assets static void RebuildPartBits(Game::XModel* model); static uint8_t InsertBone(Game::XModel* model, const std::string& boneName, const std::string& parentName, Utils::Memory::Allocator& allocator); static void TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination); + + static void SetBoneTrans(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z); + static void SetBoneQuaternion(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z, float w); }; } diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index 8eeb44e7..eafc0b47 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -314,9 +314,6 @@ namespace Components QuickPatch::QuickPatch() { - // Do not call DObjCalcAnim - Utils::Hook(0x6508C4, DObjCalcAnim, HOOK_CALL).install()->quick(); - Utils::Hook(0x06508F6, DObjCalcAnim, HOOK_CALL).install()->quick(); // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index c2e39cab..56314b4e 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -7,6 +7,7 @@ #include #include "AssetInterfaces/ILocalizeEntry.hpp" +#include "Branding.hpp" namespace Components { @@ -21,7 +22,8 @@ namespace Components iw4of::api ZoneBuilder::ExporterAPI(GetExporterAPIParams()); std::string ZoneBuilder::DumpingZone{}; - ZoneBuilder::Zone::Zone(const std::string& name) : indexStart(0), externalSize(0), + ZoneBuilder::Zone::Zone(const std::string& name, const std::string& sourceName, const std::string& destination) : + indexStart(0), externalSize(0), // Reserve 100MB by default. // That's totally fine, as the dedi doesn't load images and therefore doesn't need much memory. // That way we can be sure it won't need to reallocate memory. @@ -29,11 +31,17 @@ namespace Components // Well, decompressed maps can get way larger than 100MB, so let's increase that. buffer(0xC800000), zoneName(name), - dataMap("zone_source/" + name + ".csv"), + destination(destination), + dataMap("zone_source/" + sourceName + ".csv"), branding{ nullptr }, assetDepth(0), iw4ofApi(getIW4OfApiParams()) { + + } + + ZoneBuilder::Zone::Zone(const std::string& name) : ZoneBuilder::Zone::Zone(name, name, std::format("zone/english/{}.ff", name)) + { } ZoneBuilder::Zone::~Zone() @@ -148,7 +156,7 @@ namespace Components { Game::XZoneInfo info; info.name = fastfile.data(); - info.allocFlags = 0x20; + info.allocFlags = Game::DB_ZONE_LOAD; info.freeFlags = 0; Game::DB_LoadXAssets(&info, 1, true); @@ -449,11 +457,11 @@ namespace Components zoneBuffer = Utils::Compression::ZLib::Compress(zoneBuffer); outBuffer.append(zoneBuffer); - std::string outFile = "zone/" + this->zoneName + ".ff"; - Utils::IO::WriteFile(outFile, outBuffer); - Logger::Print("done.\n"); - Logger::Print("Zone '{}' written with {} assets and {} script strings\n", outFile, (this->aliasList.size() + this->loadedAssets.size()), this->scriptStrings.size()); + Utils::IO::WriteFile(destination, outBuffer); + + Logger::Print("done writing {}\n", destination); + Logger::Print("Zone '{}' written with {} assets and {} script strings\n", destination, (this->aliasList.size() + this->loadedAssets.size()), this->scriptStrings.size()); } void ZoneBuilder::Zone::saveData() @@ -759,18 +767,23 @@ namespace Components return header; } - void ZoneBuilder::RefreshExporterWorkDirectory() + std::string ZoneBuilder::GetDumpingZonePath() { if (ZoneBuilder::DumpingZone.empty()) { - ExporterAPI.set_work_path(std::format("userraw/dump/stray")); + return std::format("userraw/dump/stray"); } else { - ExporterAPI.set_work_path(std::format("userraw/dump/{}", ZoneBuilder::DumpingZone)); + return std::format("userraw/dump/{}", ZoneBuilder::DumpingZone); } } + void ZoneBuilder::RefreshExporterWorkDirectory() + { + ExporterAPI.set_work_path(GetDumpingZonePath()); + } + iw4of::api* ZoneBuilder::GetExporter() { return &ExporterAPI; @@ -988,10 +1001,10 @@ namespace Components Logger::Print(" --------------------------------------------------------------------------------\n"); Logger::Print(" IW4x ZoneBuilder - {}\n", REVISION_STR); Logger::Print(" Commands:\n"); - Logger::Print("\t-buildzone [zone]: builds a zone from a csv located in zone_source\n"); - Logger::Print("\t-buildall: builds all zones in zone_source\n"); + Logger::Print("\t-buildmod [mod name]: Build a mod.ff from the source located in zone_source/mod_name.csv\n"); + Logger::Print("\t-buildzone [zone]: Builds a zone from a csv located in zone_source\n"); + Logger::Print("\t-dumpzone [zone]: Loads and dump the specified zone in userraw/dump\n"); Logger::Print("\t-verifyzone [zone]: loads and verifies the specified zone\n"); - Logger::Print("\t-dumpzone [zone]: loads and dump the specified zone\n"); Logger::Print("\t-listassets [assettype]: lists all loaded assets of the specified type\n"); Logger::Print("\t-quit: quits the program\n"); Logger::Print(" --------------------------------------------------------------------------------\n"); @@ -1176,7 +1189,7 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }; + }; } ZoneBuilder::ZoneBuilder() @@ -1288,43 +1301,139 @@ namespace Components Utils::Hook::Nop(0x5BC791, 5); AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/) - { - if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) { - ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); - } - }); + if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) + { + ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); + } + }); Command::Add("dumpzone", [](const Command::Params* params) { if (params->size() < 2) return; - + std::string zone = params->get(1); ZoneBuilder::DumpingZone = zone; ZoneBuilder::RefreshExporterWorkDirectory(); std::vector> assets{}; - const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); Logger::Print("Dumping zone '{}'...\n", zone); - for (const auto& asset : assets) { - const auto type = asset.first; - const auto name = asset.second; - if (ExporterAPI.is_type_supported(type) && name[0] != ',') + Utils::IO::CreateDir(GetDumpingZonePath()); + std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); + csv + << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) + << "\n\n"; + + constexpr Game::XAssetType typeOrder[] = { + Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, + Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, + Game::XAssetType::ASSET_TYPE_GFXWORLD, + Game::XAssetType::ASSET_TYPE_COMWORLD, + Game::XAssetType::ASSET_TYPE_FXWORLD, + Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, + Game::XAssetType::ASSET_TYPE_CLIPMAP_SP, + Game::XAssetType::ASSET_TYPE_RAWFILE, + Game::XAssetType::ASSET_TYPE_VEHICLE, + Game::XAssetType::ASSET_TYPE_WEAPON, + Game::XAssetType::ASSET_TYPE_FX, + Game::XAssetType::ASSET_TYPE_TRACER, + Game::XAssetType::ASSET_TYPE_XMODEL, + Game::XAssetType::ASSET_TYPE_MATERIAL, + Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, + Game::XAssetType::ASSET_TYPE_PIXELSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXDECL, + Game::XAssetType::ASSET_TYPE_IMAGE, + Game::XAssetType::ASSET_TYPE_SOUND, + Game::XAssetType::ASSET_TYPE_LOADED_SOUND, + Game::XAssetType::ASSET_TYPE_SOUND_CURVE, + Game::XAssetType::ASSET_TYPE_PHYSPRESET, + }; + + std::unordered_map typePriority{}; + for (auto i = 0; i < ARRAYSIZE(typeOrder); i++) { - const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); - if (assetHeader.data) + const auto type = typeOrder[i]; + typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); + } + + std::map> invalidAssets{}; + + std::sort(assets.begin(), assets.end(), [&]( + const std::pair& a, + const std::pair& b + ) { + if (a.first == b.first) + { + + return a.second.compare(b.second) < 0; + } + else + { + const auto priorityA = typePriority[a.first]; + const auto priorityB = typePriority[b.first]; + + if (priorityA == priorityB) + { + return a.second.compare(b.second) < 0; + } + else + { + return priorityB < priorityA; + } + } + }); + + // Used to format the CSV + Game::XAssetType lastTypeEncountered{}; + + for (const auto& asset : assets) + { + const auto type = asset.first; + const auto name = asset.second; + if (ExporterAPI.is_type_supported(type) && name[0] != ',') { - ExporterAPI.write(type, assetHeader.data); + const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); + if (assetHeader.data) + { + ExporterAPI.write(type, assetHeader.data); + const auto typeName = Game::DB_GetXAssetTypeName(type) ; + + if (type != lastTypeEncountered) + { + csv << "\n### " << typeName << "\n"; + lastTypeEncountered = type; + } + + csv << typeName << "," << name << "\n"; + } + else + { + Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); + invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + } } else { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); + invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); } } + + for (const auto& kv : invalidAssets) + { + csv << "\n### " << kv.first << "\n"; + for (const auto& line : kv.second) + { + csv << "#" << line << "\n"; + } + } + + csv << std::format("\n### {} assets", assets.size()) << "\n"; } unload(); @@ -1339,7 +1448,7 @@ namespace Components std::string zone = params->get(1); std::vector> assets{}; - const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); int count = 0; for (auto i = assets.begin(); i != assets.end(); ++i, ++count) @@ -1361,6 +1470,24 @@ namespace Components Zone(zoneName).build(); }); + Command::Add("buildmod", [](const Command::Params* params) + { + if (params->size() < 2) return; + + std::string modName = params->get(1); + Logger::Print("Building zone for mod '{}'...\n", modName); + + const std::string previousFsGame = Dvar::Var("fs_game").get(); + const std::string dir = "mods/" + modName; + Utils::IO::CreateDir(dir); + + Dvar::Var("fs_game").set(dir); + + Zone("mod", modName, dir + "/mod.ff").build(); + + Dvar::Var("fs_game").set(previousFsGame); + }); + Command::Add("buildall", []() { auto path = std::format("{}\\zone_source", (*Game::fs_basepath)->current.string); @@ -1414,8 +1541,8 @@ namespace Components #endif } } - } - }); + } + }); Command::Add("listassets", [](const Command::Params* params) { @@ -1431,7 +1558,7 @@ namespace Components }, &type, false); } }); - } +} } ZoneBuilder::~ZoneBuilder() diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 4f4a4427..bc3affb5 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -32,7 +32,8 @@ namespace Components private: Zone* builder; }; - + + Zone(const std::string& zoneName, const std::string& sourceName, const std::string& destination); Zone(const std::string& zoneName); ~Zone(); @@ -100,6 +101,7 @@ namespace Components iw4of::api iw4ofApi; std::string zoneName; + std::string destination; Utils::CSV dataMap; Utils::Memory::Allocator memAllocator; @@ -134,6 +136,7 @@ namespace Components static std::vector> EndAssetTrace(); static Game::XAssetHeader GetEmptyAssetIfCommon(Game::XAssetType type, const std::string& name, Zone* builder); + static std::string GetDumpingZonePath(); static void RefreshExporterWorkDirectory(); static iw4of::api* GetExporter(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 4b2b0c2a..67aea351 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -960,7 +960,7 @@ namespace Game union XAnimDynamicFrames { - char(*_1)[3]; + uint8_t(*_1)[3]; unsigned __int16(*_2)[3]; }; From adddbd5ecc2522c6aebad35b608ebc2654a80323 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 14:05:06 +0100 Subject: [PATCH 27/71] Bump IW4OF --- deps/iw4-open-formats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/iw4-open-formats b/deps/iw4-open-formats index f174f1c4..4f7041b9 160000 --- a/deps/iw4-open-formats +++ b/deps/iw4-open-formats @@ -1 +1 @@ -Subproject commit f174f1c4c4c465c337166b92e03b355695c5bc93 +Subproject commit 4f7041b9f259d09bc6ca06c3bc182a66c961f2e4 From 4a61617e88b0148c321f6ba5b262eee380a8b759 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 16:26:24 +0100 Subject: [PATCH 28/71] Cleanup & store extra part bit in msurf instead of lodinfo --- src/Components/Loader.cpp | 2 +- src/Components/Modules/AssetHandler.cpp | 60 +--- .../Modules/AssetInterfaces/IXModel.cpp | 293 +++++++++--------- src/Components/Modules/QuickPatch.cpp | 2 - src/Components/Modules/ZoneBuilder.cpp | 18 +- src/Components/Modules/ZoneBuilder.hpp | 2 + src/Game/Structs.hpp | 2 +- 7 files changed, 164 insertions(+), 215 deletions(-) diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index aa3ce5d6..f50b2689 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -173,7 +173,7 @@ namespace Components Register(new Threading()); Register(new Toast()); Register(new UIFeeder()); - //Register(new Updater()); + Register(new Updater()); Register(new VisionFile()); Register(new Voice()); Register(new Vote()); diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 3424c04a..6d9e0a6c 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -221,7 +221,7 @@ namespace Components retn } } -#pragma optimize( "", off ) + void AssetHandler::ModifyAsset(Game::XAssetType type, Game::XAssetHeader asset, const std::string& name) { #ifdef DEBUG @@ -235,63 +235,6 @@ namespace Components } } #endif - if (type == Game::XAssetType::ASSET_TYPE_XANIMPARTS) - { - auto anim = asset.parts; - if (anim->name == "unarmed_cowerstand_idle"s || anim->name == "civilian_walk_cool"s || anim->name == "civilian_run_upright"s) - { - auto str = Game::SL_FindString("j_spinelower"); - int interestingPart = 0; - { - if (anim->names[i] == str) - { - interestingPart = i; - } - } - - // Something interesting to do there - } - } - - - if (type == Game::XAssetType::ASSET_TYPE_XMODEL) - //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) - { - - if (name == "body_urban_civ_male_aa"s) - { - printf(""); - } - - const auto model = asset.model; - - int total = - model->noScalePartBits[0] | - model->noScalePartBits[1] | - model->noScalePartBits[2] | - model->noScalePartBits[3] | - model->noScalePartBits[4] | - model->noScalePartBits[5]; - - if (total != 0) - { - printf(""); - } - - if (!model->quats || !model->trans || model->numBones < 10) - { - // Unlikely candidate - return; - } - static Utils::Memory::Allocator allocator{}; - - // - //if (name == "body_urban_civ_female_a"s) - { - Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); - printf(""); - } - } if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) { @@ -348,7 +291,6 @@ namespace Components asset.gfxWorld->sortKeyDistortion = 43; } } -#pragma optimize( "", on ) bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) { diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index 74561206..d5667578 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -11,8 +11,19 @@ namespace Assets { header->model = builder->getIW4OfApi()->read(Game::XAssetType::ASSET_TYPE_XMODEL, name); + if (!header->model) + { + // In that case if we want to convert it later potentially we have to grab it now: + header->model = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_XMODEL, name.data()).model; + } + if (header->model) { + if (Components::ZoneBuilder::zb_sp_to_mp.get()) + { + Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(header->model, *builder->getAllocator()); + } + // ??? if (header->model->physCollmap) { @@ -305,16 +316,13 @@ namespace Assets uint8_t IXModel::GetHighestAffectingBoneIndex(const Game::XModelLodInfo* lod) { uint8_t highestBoneIndex = 0; - constexpr auto LENGTH = 6; { for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) { const auto surface = &lod->surfs[surfIndex]; - auto vertsBlendOffset = 0; - int rebuiltPartBits[LENGTH]{}; std::unordered_set affectingBones{}; const auto registerBoneAffectingSurface = [&](unsigned int offset) { @@ -380,7 +388,7 @@ namespace Assets const auto lod = &model->lodInfo[i]; int lodPartBits[6]{}; - for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + for (unsigned short surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) { const auto surface = &lod->surfs[surfIndex]; @@ -400,7 +408,7 @@ namespace Assets // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); @@ -408,7 +416,7 @@ namespace Assets } // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -417,7 +425,7 @@ namespace Assets } // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -427,7 +435,7 @@ namespace Assets } // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -437,7 +445,7 @@ namespace Assets vertsBlendOffset += 7; } - for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + for (unsigned int vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) { affectingBones.emplace(static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); } @@ -481,7 +489,7 @@ namespace Assets // Start with backing up parent links that we will have to restore // We'll restore them at the end std::map parentsToRestore{}; - for (int i = model->numRootBones; i < model->numBones; i++) + for (uint8_t i = model->numRootBones; i < model->numBones; i++) { parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = GetParentOfBone(model, i); } @@ -498,8 +506,6 @@ namespace Assets const uint8_t newBoneIndex = atPosition; const uint8_t newBoneIndexMinusRoot = atPosition - model->numRootBones; - Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition - 1]), Game::SL_ConvertToString(model->boneNames[atPosition + 1])); - // Reallocate const auto newBoneNames = allocator.allocateArray(newBoneCount); const auto newMats = allocator.allocateArray(newBoneCount); @@ -518,8 +524,8 @@ namespace Assets const uint8_t atPositionM1 = atPosition - model->numRootBones; // should be equal to model->numBones - int total = lengthOfFirstPart + lengthOfSecondPart; - assert(total = model->numBones); + unsigned int total = lengthOfFirstPart + lengthOfSecondPart; + assert(total == model->numBones); // should be equal to model->numBones - model->numRootBones int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; @@ -599,9 +605,10 @@ namespace Assets { const auto lod = &model->lodInfo[lodIndex]; - if ((lod->partBits[5] & 0x1) == 0x1) + if ((lod->modelSurfs->partBits[5] & 0x1) == 0x1) { // surface lod already converted (more efficient) + std::memcpy(lod->partBits, lod->modelSurfs->partBits, 6 * sizeof(uint32_t)); continue; } @@ -630,7 +637,7 @@ namespace Assets if (index < 0 || index >= model->numBones) { - Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex blend of: xmodel {} lod {} xmodelsurf {} surf #{}", index, model->numBones, model->name, lodIndex, lod->modelSurfs->name, lodIndex, surfIndex); assert(false); } @@ -652,7 +659,7 @@ namespace Assets if (index < 0 || index >= model->numBones) { - Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working list blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex list of: xmodel {} lod {} xmodelsurf {} surf #{}\n", index, model->numBones, model->name, lodIndex, lod->modelSurfs->name, surfIndex); assert(false); } @@ -711,39 +718,17 @@ namespace Assets const auto key = kv.first; const auto beforeVal = kv.second; - const auto parentIndex = GetIndexOfBone(model, beforeVal); + const auto p = GetIndexOfBone(model, beforeVal); const auto index = GetIndexOfBone(model, key); - SetParentIndexOfBone(model, index, parentIndex); + SetParentIndexOfBone(model, index, p); } -#if DEBUG - // check - for (const auto& kv : parentsToRestore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto index = GetIndexOfBone(model, key); - const auto afterVal = GetParentOfBone(model, index); - - if (beforeVal != afterVal) - { - printf(""); - } - } - // -#endif - return atPosition; // Bone index of added bone }; void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) { - const auto from = Game::SL_ConvertToString(model->boneNames[origin]); - const auto to = Game::SL_ConvertToString(model->boneNames[destination]); - Components::Logger::Print("Transferring bone weights from {} to {}\n", from, to); - const auto originalWeights = model->baseMat[origin].transWeight; model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; model->baseMat[destination].transWeight = originalWeights; @@ -887,114 +872,130 @@ namespace Assets void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) { - auto indexOfSpine = GetIndexOfBone(model, "j_spinelower"); - if (indexOfSpine < UCHAR_MAX) // It has a spine so it must be some sort of humanoid + if (model->name == "body_airport_com_a"s) { - const auto nameOfParent = GetParentOfBone(model, indexOfSpine); - - if (GetIndexOfBone(model, "torso_stabilizer") == UCHAR_MAX) // Singleplayer model is likely - { - - Components::Logger::Print("Converting {}\n", model->name); - - // No stabilizer - let's do surgery - // We're trying to get there: - // tag_origin - // j_main_root - // pelvis - // j_hip_le - // j_hip_ri - // tag_stowed_hip_rear - // torso_stabilizer - // j_spinelower - // back_low - // j_spineupper - // back_mid - // j_spine4 - - - const auto root = GetIndexOfBone(model, "j_mainroot"); - if (root < UCHAR_MAX) { - - // Add pelvis -#if false - const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); - TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); -#else - const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); - SetBoneQuaternion(model, indexOfPelvis, true, -0.494f, -0.506f, -0.506f, 0.494); - - TransferWeights(model, root, indexOfPelvis); - - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_le"), indexOfPelvis); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); - SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); - - // These two are optional - if (GetIndexOfBone(model, "j_coatfront_le") == UCHAR_MAX) - { - InsertBone(model, "j_coatfront_le", "pelvis", allocator); - } - - if (GetIndexOfBone(model, "j_coatfront_ri") == UCHAR_MAX) - { - InsertBone(model, "j_coatfront_ri", "pelvis", allocator); - } - - const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); - const uint8_t lowerSpine = GetIndexOfBone(model, "j_spinelower"); - SetParentIndexOfBone(model, lowerSpine, torsoStabilizer); - - const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); - TransferWeights(model, lowerSpine, backLow); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); - - const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); - TransferWeights(model, GetIndexOfBone(model, "j_spineupper"), backMid); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spine4"), backMid); - - - assert(root == GetIndexOfBone(model, "j_mainroot")); - assert(indexOfPelvis == GetIndexOfBone(model, "pelvis")); - assert(backLow == GetIndexOfBone(model, "back_low")); - assert(backMid == GetIndexOfBone(model, "back_mid")); - - // Twister bone - SetBoneQuaternion(model, lowerSpine, false, -0.492f, -0.507f, -0.507f, 0.492f); - SetBoneQuaternion(model, torsoStabilizer, false, 0.494f, 0.506f, 0.506f, 0.494f); - - - // This doesn't feel like it should be necessary - // It is, on singleplayer models unfortunately. Could we add an extra bone to compensate this? - // Or compensate it another way? - SetBoneTrans(model, GetIndexOfBone(model, "j_spinelower"), false, 0.07, 0.0f, 5.2f); - - // These are often messed up on civilian models, but there is no obvious way to tell from code - const auto stowedBack = GetIndexOfBone(model, "tag_stowed_back"); - if (stowedBack != UCHAR_MAX) - { - SetBoneTrans(model, stowedBack, false, -0.32f, -6.27f, -2.65F); - SetBoneQuaternion(model, stowedBack, false, -0.044, 0.088, -0.995, 0.025); - SetBoneTrans(model, stowedBack, true, -9.571f, -2.654f, 51.738f); - SetBoneQuaternion(model, stowedBack, true, -0.071f, 0.0f, -0.997f, 0.0f); - } - - if (stowedBack != UCHAR_MAX) - { - const auto stowedRear = GetIndexOfBone(model, "tag_stowed_hip_rear"); - SetBoneTrans(model, stowedRear, false, -0.75f, -6.45f, -4.99f); - SetBoneQuaternion(model, stowedRear, false, -0.553f, -0.062f, -0.049f, 0.830f); - SetBoneTrans(model, stowedBack, true, -9.866f, -4.989f, 36.315f); - SetBoneQuaternion(model, stowedRear, true, -0.054, -0.025f, -0.975f, 0.214f); - } -#endif - - RebuildPartBits(model); - } - } printf(""); } + + std::string requiredBonesForHumanoid[] = { + "j_spinelower", + "j_spineupper", + "j_spine4", + "j_mainroot" + }; + + for (const auto& required : requiredBonesForHumanoid) + { + if (GetIndexOfBone(model, required) == UCHAR_MAX) + { + // Not humanoid - nothing to do + return; + } + } + + auto indexOfSpine = GetIndexOfBone(model, "j_spinelower"); + const auto nameOfParent = GetParentOfBone(model, indexOfSpine); + + if (GetIndexOfBone(model, "torso_stabilizer") == UCHAR_MAX) // Singleplayer model is likely + { + + Components::Logger::Print("Converting {} skeleton from SP to MP...\n", model->name); + + // No stabilizer - let's do surgery + // We're trying to get there: + // tag_origin + // j_main_root + // pelvis + // j_hip_le + // j_hip_ri + // tag_stowed_hip_rear + // torso_stabilizer + // j_spinelower + // back_low + // j_spineupper + // back_mid + // j_spine4 + + + const auto root = GetIndexOfBone(model, "j_mainroot"); + if (root < UCHAR_MAX) { + + // Add pelvis + const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); + SetBoneQuaternion(model, indexOfPelvis, true, -0.494f, -0.506f, -0.506f, 0.494f); + + TransferWeights(model, root, indexOfPelvis); + + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_le"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); + + // These two are optional + if (GetIndexOfBone(model, "j_coatfront_le") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_le", "pelvis", allocator); + } + + if (GetIndexOfBone(model, "j_coatfront_ri") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_ri", "pelvis", allocator); + } + + const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); + const uint8_t lowerSpine = GetIndexOfBone(model, "j_spinelower"); + SetParentIndexOfBone(model, lowerSpine, torsoStabilizer); + + const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); + TransferWeights(model, lowerSpine, backLow); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); + + const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spineupper"), backMid); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spine4"), backMid); + + + assert(root == GetIndexOfBone(model, "j_mainroot")); + assert(indexOfPelvis == GetIndexOfBone(model, "pelvis")); + assert(backLow == GetIndexOfBone(model, "back_low")); + assert(backMid == GetIndexOfBone(model, "back_mid")); + + // Twister bone + SetBoneQuaternion(model, lowerSpine, false, -0.492f, -0.507f, -0.507f, 0.492f); + SetBoneQuaternion(model, torsoStabilizer, false, 0.494f, 0.506f, 0.506f, 0.494f); + + + // This doesn't feel like it should be necessary + // It is, on singleplayer models unfortunately. Could we add an extra bone to compensate this? + // Or compensate it another way? + SetBoneTrans(model, GetIndexOfBone(model, "j_spinelower"), false, 0.07f, 0.0f, 5.2f); + + // These are often messed up on civilian models, but there is no obvious way to tell from code + auto stowedBack = GetIndexOfBone(model, "tag_stowed_back"); + if (stowedBack == UCHAR_MAX) + { + stowedBack = InsertBone(model, "tag_stowed_back", "j_spine4", allocator); + } + + SetBoneTrans(model, stowedBack, false, -0.32f, -6.27f, -2.65F); + SetBoneQuaternion(model, stowedBack, false, -0.044f, 0.088f, -0.995f, 0.025f); + SetBoneTrans(model, stowedBack, true, -9.571f, -2.654f, 51.738f); + SetBoneQuaternion(model, stowedBack, true, -0.071f, 0.0f, -0.997f, 0.0f); + + auto stowedRear = GetIndexOfBone(model, "tag_stowed_hip_rear"); + if (stowedRear == UCHAR_MAX) + { + stowedBack = InsertBone(model, "tag_stowed_hip_rear", "pelvis", allocator); + } + + SetBoneTrans(model, stowedRear, false, -0.75f, -6.45f, -4.99f); + SetBoneQuaternion(model, stowedRear, false, -0.553f, -0.062f, -0.049f, 0.830f); + SetBoneTrans(model, stowedBack, true, -9.866f, -4.989f, 36.315f); + SetBoneQuaternion(model, stowedRear, true, -0.054f, -0.025f, -0.975f, 0.214f); + + + RebuildPartBits(model); + } + } } } diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index eafc0b47..cde7379a 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -314,8 +314,6 @@ namespace Components QuickPatch::QuickPatch() { - - // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning Utils::Hook(0x5FBD6E, QuickPatch::IsDynClassname_Stub, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 56314b4e..b0606ef4 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -7,6 +7,8 @@ #include #include "AssetInterfaces/ILocalizeEntry.hpp" + +#include "Events.hpp" #include "Branding.hpp" namespace Components @@ -22,6 +24,8 @@ namespace Components iw4of::api ZoneBuilder::ExporterAPI(GetExporterAPIParams()); std::string ZoneBuilder::DumpingZone{}; + Dvar::Var ZoneBuilder::zb_sp_to_mp{}; + ZoneBuilder::Zone::Zone(const std::string& name, const std::string& sourceName, const std::string& destination) : indexStart(0), externalSize(0), // Reserve 100MB by default. @@ -115,7 +119,7 @@ namespace Components return &iw4ofApi; } - void ZoneBuilder::Zone::Zone::build() + void ZoneBuilder::Zone::build() { if (!this->dataMap.isValid()) { @@ -1189,7 +1193,7 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }; + }; } ZoneBuilder::ZoneBuilder() @@ -1300,6 +1304,8 @@ namespace Components // don't remap techsets Utils::Hook::Nop(0x5BC791, 5); + ZoneBuilder::zb_sp_to_mp = Game::Dvar_RegisterBool("zb_sp_to_mp", false, Game::DVAR_ARCHIVE, "Attempt to convert singleplayer assets to multiplayer format whenever possible"); + AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/) { if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) @@ -1402,7 +1408,7 @@ namespace Components if (assetHeader.data) { ExporterAPI.write(type, assetHeader.data); - const auto typeName = Game::DB_GetXAssetTypeName(type) ; + const auto typeName = Game::DB_GetXAssetTypeName(type); if (type != lastTypeEncountered) { @@ -1541,8 +1547,8 @@ namespace Components #endif } } - } - }); + } + }); Command::Add("listassets", [](const Command::Params* params) { @@ -1558,7 +1564,7 @@ namespace Components }, &type, false); } }); -} + } } ZoneBuilder::~ZoneBuilder() diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index bc3affb5..7fb5a05d 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -135,6 +135,8 @@ namespace Components static void BeginAssetTrace(const std::string& zone); static std::vector> EndAssetTrace(); + static Dvar::Var zb_sp_to_mp; + static Game::XAssetHeader GetEmptyAssetIfCommon(Game::XAssetType type, const std::string& name, Zone* builder); static std::string GetDumpingZonePath(); static void RefreshExporterWorkDirectory(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 67aea351..d1233fb5 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1066,7 +1066,7 @@ namespace Game struct XSurfaceVertexInfo { - __int16 vertCount[4]; + unsigned __int16 vertCount[4]; unsigned __int16* vertsBlend; }; From a603dbea523f6b8179970e32b35491dec5e3fa58 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 17:54:19 +0100 Subject: [PATCH 29/71] Some more cleanup! --- src/Components/Modules/ClientCommand.cpp | 2 +- src/Components/Modules/QuickPatch.cpp | 14 ---- src/Components/Modules/Renderer.cpp | 27 -------- src/Components/Modules/Weapon.cpp | 83 ++++++++++++++---------- src/Components/Modules/Weapon.hpp | 10 +-- src/Steam/Proxy.cpp | 2 - 6 files changed, 54 insertions(+), 84 deletions(-) diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index f7c8f5ca..bd284e8c 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -386,7 +386,7 @@ namespace Components { if (Components::Weapon::GModelIndexHasBeenReallocated) { - model = Components::Weapon::G_ModelIndexReallocated[ent->model]; + model = Components::Weapon::cached_models_reallocated[ent->model]; } else { diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index cde7379a..e8a7ded9 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -298,20 +298,6 @@ namespace Components return Game::Dvar_RegisterBool(dvarName, value_, flags, description); } - void DObjCalcAnim(Game::DObj *a1, int *partBits, Game::XAnimCalcAnimInfo *a3) - { - printf(""); - - if (a1->models[0]->name == "body_urban_civ_female_a"s) - { - printf(""); - } - - Utils::Hook::Call(0x49E230)(a1, partBits, a3); - - printf(""); - } - QuickPatch::QuickPatch() { // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 8df1b33f..191b82f5 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -713,32 +713,6 @@ namespace Components return result; } - void DebugTest() - { - //auto clientNum = Game::CG_GetClientNum(); - //auto* clientEntity = &Game::g_entities[clientNum]; - - //// Ingame only & player only - //if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr) - //{ - // return; - //} - - - //static std::string str = ""; - - //str += std::format("\n{} => {} {} {} {} {} {}", "s.partBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); - // - - //const auto clientNumber = clientEntity->r.ownerNum.number; - //Game::scene->sceneDObj[clientNumber].obj->hidePartBits; - - //str += std::format("\n{} => {} {} {} {} {} {}", "DOBJ hidePartBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); - - //Game::R_AddCmdDrawText(); - - } - Renderer::Renderer() { if (Dedicated::IsEnabled()) return; @@ -757,7 +731,6 @@ namespace Components ListSamplers(); DrawPrimaryLights(); DebugDrawClipmap(); - DebugTest(); } }, Scheduler::Pipeline::RENDERER); diff --git a/src/Components/Modules/Weapon.cpp b/src/Components/Modules/Weapon.cpp index 5f3fd9f0..0413fd9d 100644 --- a/src/Components/Modules/Weapon.cpp +++ b/src/Components/Modules/Weapon.cpp @@ -6,9 +6,18 @@ namespace Components { const Game::dvar_t* Weapon::BGWeaponOffHandFix; - Game::XModel* Weapon::G_ModelIndexReallocated[G_MODELINDEX_LIMIT]; + Game::XModel* Weapon::cached_models_reallocated[G_MODELINDEX_LIMIT]; bool Weapon::GModelIndexHasBeenReallocated; + // Config strings mapping + // 0-1067 unknown + // 1125-1580 cached models (512 long range) + // 1581-1637 also reserved for models? + // 1637-4082 unknown + // 4082-above = bad index? + // 4137 : timescale + // 4138-above = reserved for weapons? + Game::WeaponCompleteDef* Weapon::LoadWeaponCompleteDef(const char* name) { if (auto* rawWeaponFile = Game::BG_LoadWeaponCompleteDefInternal("mp", name)) @@ -22,7 +31,7 @@ namespace Components const char* Weapon::GetWeaponConfigString(int index) { - if (index >= (1200 + 2804)) index += (2939 - 2804); + if (index >= (BASEGAME_WEAPON_LIMIT + 2804)) index += (2939 - 2804); return Game::CL_GetConfigString(index); } @@ -34,7 +43,7 @@ namespace Components { for (unsigned int i = 1; i < Game::BG_GetNumWeapons(); ++i) { - Game::SV_SetConfigstring(i + (i >= 1200 ? 2939 : 2804), Game::BG_GetWeaponName(i)); + Game::SV_SetConfigstring(i + (i >= BASEGAME_WEAPON_LIMIT ? 2939 : 2804), Game::BG_GetWeaponName(i)); } } } @@ -57,12 +66,14 @@ namespace Components return 0; } - if (index >= 4139) + if (index >= BASEGAME_MAX_CONFIGSTRINGS) { + // if above 4139, remap to 1200<>? index -= 2939; } - else if (index > 2804 && index <= 2804 + 1200) + else if (index > 2804 && index <= 2804 + BASEGAME_WEAPON_LIMIT) { + // from 2804 to 4004, remap to 0<>1200 index -= 2804; } else @@ -283,8 +294,8 @@ namespace Components // And http://reverseengineering.stackexchange.com/questions/1397/how-can-i-reverse-optimized-integer-division-modulo-by-constant-operations // The game's magic number is computed using this formula: (1 / 1200) * (2 ^ (32 + 7) // I'm too lazy to generate the new magic number, so we can make use of the fact that using powers of 2 as scales allows to change the compensating shift - static_assert(((WEAPON_LIMIT / 1200) * 1200) == WEAPON_LIMIT && (WEAPON_LIMIT / 1200) != 0 && !((WEAPON_LIMIT / 1200) & ((WEAPON_LIMIT / 1200) - 1)), "WEAPON_LIMIT / 1200 is not a power of 2!"); - const unsigned char compensation = 7 + static_cast(log2(WEAPON_LIMIT / 1200)); // 7 is the compensation the game uses + static_assert(((WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) * BASEGAME_WEAPON_LIMIT) == WEAPON_LIMIT && (WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) != 0 && !((WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) & ((WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) - 1)), "WEAPON_LIMIT / 1200 is not a power of 2!"); + const unsigned char compensation = 7 + static_cast(log2(WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT)); // 7 is the compensation the game uses Utils::Hook::Set(0x49263D, compensation); Utils::Hook::Set(0x5E250C, compensation); Utils::Hook::Set(0x5E2B43, compensation); @@ -407,46 +418,46 @@ namespace Components // Patch bg_weaponDefs on the stack Utils::Hook::Set(0x40C31D, sizeof(bg_weaponDefs)); Utils::Hook::Set(0x40C32F, sizeof(bg_weaponDefs)); - Utils::Hook::Set(0x40C311, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C45F, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C478, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); + Utils::Hook::Set(0x40C311, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C45F, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C478, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); // Move second buffer pointers - Utils::Hook::Set(0x40C336, 0x12E4 + ((sizeof(bg_weaponDefs)) - (1200 * 4))); - Utils::Hook::Set(0x40C3C6, 0x12DC + ((sizeof(bg_weaponDefs)) - (1200 * 4))); - Utils::Hook::Set(0x40C3CE, 0x12DC + ((sizeof(bg_weaponDefs)) - (1200 * 4))); + Utils::Hook::Set(0x40C336, 0x12E4 + ((sizeof(bg_weaponDefs)) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x40C3C6, 0x12DC + ((sizeof(bg_weaponDefs)) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x40C3CE, 0x12DC + ((sizeof(bg_weaponDefs)) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg0 pointers - Utils::Hook::Set(0x40C365, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C44E, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C467, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); + Utils::Hook::Set(0x40C365, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C44E, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C467, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); // Move arg4 pointers - Utils::Hook::Set(0x40C344, 0x25B4 + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); + Utils::Hook::Set(0x40C344, 0x25B4 + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); // Patch bg_sharedAmmoCaps on the stack Utils::Hook::Set(0x4F76E6, sizeof(bg_sharedAmmoCaps)); - Utils::Hook::Set(0x4F7621, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76AF, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76DA, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F77C5, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); + Utils::Hook::Set(0x4F7621, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76AF, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76DA, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F77C5, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg0 pointers - Utils::Hook::Set(0x4F766D, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76B7, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76FB, 0x12EC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); + Utils::Hook::Set(0x4F766D, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76B7, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76FB, 0x12EC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg4 pointers - Utils::Hook::Set(0x4F7630, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); + Utils::Hook::Set(0x4F7630, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Reallocate G_ModelIndex - Utils::Hook::Set(0x420654 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x43BCE4 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x44F27B + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x479087 + 1, G_ModelIndexReallocated); - Utils::Hook::Set(0x48069D + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x48F088 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x4F457C + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x5FC762 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x5FC7BE + 3, G_ModelIndexReallocated); + Utils::Hook::Set(0x420654 + 3, cached_models_reallocated); + Utils::Hook::Set(0x43BCE4 + 3, cached_models_reallocated); + Utils::Hook::Set(0x44F27B + 3, cached_models_reallocated); + Utils::Hook::Set(0x479087 + 1, cached_models_reallocated); + Utils::Hook::Set(0x48069D + 3, cached_models_reallocated); + Utils::Hook::Set(0x48F088 + 3, cached_models_reallocated); + Utils::Hook::Set(0x4F457C + 3, cached_models_reallocated); + Utils::Hook::Set(0x5FC762 + 3, cached_models_reallocated); + Utils::Hook::Set(0x5FC7BE + 3, cached_models_reallocated); Utils::Hook::Set(0x44F256 + 2, G_MODELINDEX_LIMIT); GModelIndexHasBeenReallocated = true; diff --git a/src/Components/Modules/Weapon.hpp b/src/Components/Modules/Weapon.hpp index 60c17c60..3065a1e0 100644 --- a/src/Components/Modules/Weapon.hpp +++ b/src/Components/Modules/Weapon.hpp @@ -1,14 +1,16 @@ #pragma once +#define BASEGAME_WEAPON_LIMIT 1200 +#define BASEGAME_MAX_CONFIGSTRINGS 4139 + // Increase the weapon limit -// Was 1200 before #define WEAPON_LIMIT 2400 -#define MAX_CONFIGSTRINGS (4139 - 1200 + WEAPON_LIMIT) +#define MAX_CONFIGSTRINGS (BASEGAME_MAX_CONFIGSTRINGS - BASEGAME_WEAPON_LIMIT + WEAPON_LIMIT) // Double the limit to allow loading of some heavy-duty MW3 maps #define ADDITIONAL_GMODELS 512 -#define G_MODELINDEX_LIMIT (512 + WEAPON_LIMIT - 1200 + ADDITIONAL_GMODELS) +#define G_MODELINDEX_LIMIT (512 + WEAPON_LIMIT - BASEGAME_WEAPON_LIMIT + ADDITIONAL_GMODELS) namespace Components { @@ -16,7 +18,7 @@ namespace Components { public: Weapon(); - static Game::XModel* G_ModelIndexReallocated[G_MODELINDEX_LIMIT]; + static Game::XModel* cached_models_reallocated[G_MODELINDEX_LIMIT]; static bool GModelIndexHasBeenReallocated; diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index 248723ff..c4710b4a 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -276,8 +276,6 @@ namespace Steam void Proxy::RunFrame() { - return; - std::lock_guard _(Proxy::CallMutex); if (Proxy::SteamUtils) From a8606cb53316348f72d93f0ff1cb4172493d7f58 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 18:05:16 +0100 Subject: [PATCH 30/71] #if DEBUG --- src/Components/Modules/AssetInterfaces/IXModel.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index d5667578..e508f570 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -482,9 +482,10 @@ namespace Assets { assert(GetIndexOfBone(model, boneName) == UCHAR_MAX); +#if DEBUG constexpr auto MAX_BONES = 192; - assert(model->numBones < MAX_BONES); +#endif // Start with backing up parent links that we will have to restore // We'll restore them at the end @@ -523,6 +524,7 @@ namespace Assets const uint8_t atPositionM1 = atPosition - model->numRootBones; +#if DEBUG // should be equal to model->numBones unsigned int total = lengthOfFirstPart + lengthOfSecondPart; assert(total == model->numBones); @@ -530,6 +532,7 @@ namespace Assets // should be equal to model->numBones - model->numRootBones int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; assert(totalM1 == model->numBones - model->numRootBones); +#endif // Copy before if (lengthOfFirstPart > 0) From c11dfd8422f9ba30f52c7b7bdd3cd4e0e93db807 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 18:38:46 +0100 Subject: [PATCH 31/71] Address review --- .../Modules/AssetInterfaces/IXModel.cpp | 2 - src/Components/Modules/ZoneBuilder.cpp | 270 +++++++++--------- src/Components/Modules/ZoneBuilder.hpp | 2 + 3 files changed, 142 insertions(+), 132 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index e508f570..ac22bc5e 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -1,7 +1,5 @@ #include -#include - #include "IXModel.hpp" namespace Assets diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index b0606ef4..455b0136 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -1167,6 +1167,139 @@ namespace Components return params; } + void ZoneBuilder::DumpZone(const std::string& zone) + { + ZoneBuilder::DumpingZone = zone; + ZoneBuilder::RefreshExporterWorkDirectory(); + + std::vector> assets{}; + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); + + Logger::Print("Dumping zone '{}'...\n", zone); + + { + Utils::IO::CreateDir(GetDumpingZonePath()); + std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); + csv + << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) + << "\n\n"; + + // Order the CSV around + // TODO: Trim asset list using IW4OF dependencies + constexpr Game::XAssetType typeOrder[] = { + Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, + Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, + Game::XAssetType::ASSET_TYPE_GFXWORLD, + Game::XAssetType::ASSET_TYPE_COMWORLD, + Game::XAssetType::ASSET_TYPE_FXWORLD, + Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, + Game::XAssetType::ASSET_TYPE_CLIPMAP_SP, + Game::XAssetType::ASSET_TYPE_RAWFILE, + Game::XAssetType::ASSET_TYPE_VEHICLE, + Game::XAssetType::ASSET_TYPE_WEAPON, + Game::XAssetType::ASSET_TYPE_FX, + Game::XAssetType::ASSET_TYPE_TRACER, + Game::XAssetType::ASSET_TYPE_XMODEL, + Game::XAssetType::ASSET_TYPE_MATERIAL, + Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, + Game::XAssetType::ASSET_TYPE_PIXELSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXDECL, + Game::XAssetType::ASSET_TYPE_IMAGE, + Game::XAssetType::ASSET_TYPE_SOUND, + Game::XAssetType::ASSET_TYPE_LOADED_SOUND, + Game::XAssetType::ASSET_TYPE_SOUND_CURVE, + Game::XAssetType::ASSET_TYPE_PHYSPRESET, + }; + + std::unordered_map typePriority{}; + for (auto i = 0; i < ARRAYSIZE(typeOrder); i++) + { + const auto type = typeOrder[i]; + typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); + } + + std::map> invalidAssets{}; + + std::sort(assets.begin(), assets.end(), [&]( + const std::pair& a, + const std::pair& b + ) { + if (a.first == b.first) + { + + return a.second.compare(b.second) < 0; + } + else + { + const auto priorityA = typePriority[a.first]; + const auto priorityB = typePriority[b.first]; + + if (priorityA == priorityB) + { + return a.second.compare(b.second) < 0; + } + else + { + return priorityB < priorityA; + } + } + }); + + // Used to format the CSV + Game::XAssetType lastTypeEncountered{}; + + for (const auto& asset : assets) + { + const auto type = asset.first; + const auto name = asset.second; + if (ExporterAPI.is_type_supported(type) && name[0] != ',') + { + const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); + if (assetHeader.data) + { + ExporterAPI.write(type, assetHeader.data); + const auto typeName = Game::DB_GetXAssetTypeName(type); + + if (type != lastTypeEncountered) + { + csv << "\n### " << typeName << "\n"; + lastTypeEncountered = type; + } + + csv << typeName << "," << name << "\n"; + } + else + { + Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); + invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + } + } + else + { + invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + } + } + + for (const auto& kv : invalidAssets) + { + csv << "\n### " << kv.first << "\n"; + for (const auto& line : kv.second) + { + csv << "#" << line << "\n"; + } + } + + csv << std::format("\n### {} assets", assets.size()) << "\n"; + } + + unload(); + + Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); + ZoneBuilder::DumpingZone = std::string(); + } + + std::function ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector>& assets) { ZoneBuilder::BeginAssetTrace(zone); @@ -1193,7 +1326,7 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }; + }; } ZoneBuilder::ZoneBuilder() @@ -1320,132 +1453,7 @@ namespace Components std::string zone = params->get(1); - ZoneBuilder::DumpingZone = zone; - ZoneBuilder::RefreshExporterWorkDirectory(); - - std::vector> assets{}; - const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); - - Logger::Print("Dumping zone '{}'...\n", zone); - - { - Utils::IO::CreateDir(GetDumpingZonePath()); - std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); - csv - << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) - << "\n\n"; - - constexpr Game::XAssetType typeOrder[] = { - Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, - Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, - Game::XAssetType::ASSET_TYPE_GFXWORLD, - Game::XAssetType::ASSET_TYPE_COMWORLD, - Game::XAssetType::ASSET_TYPE_FXWORLD, - Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, - Game::XAssetType::ASSET_TYPE_CLIPMAP_SP, - Game::XAssetType::ASSET_TYPE_RAWFILE, - Game::XAssetType::ASSET_TYPE_VEHICLE, - Game::XAssetType::ASSET_TYPE_WEAPON, - Game::XAssetType::ASSET_TYPE_FX, - Game::XAssetType::ASSET_TYPE_TRACER, - Game::XAssetType::ASSET_TYPE_XMODEL, - Game::XAssetType::ASSET_TYPE_MATERIAL, - Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, - Game::XAssetType::ASSET_TYPE_PIXELSHADER, - Game::XAssetType::ASSET_TYPE_VERTEXSHADER, - Game::XAssetType::ASSET_TYPE_VERTEXDECL, - Game::XAssetType::ASSET_TYPE_IMAGE, - Game::XAssetType::ASSET_TYPE_SOUND, - Game::XAssetType::ASSET_TYPE_LOADED_SOUND, - Game::XAssetType::ASSET_TYPE_SOUND_CURVE, - Game::XAssetType::ASSET_TYPE_PHYSPRESET, - }; - - std::unordered_map typePriority{}; - for (auto i = 0; i < ARRAYSIZE(typeOrder); i++) - { - const auto type = typeOrder[i]; - typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); - } - - std::map> invalidAssets{}; - - std::sort(assets.begin(), assets.end(), [&]( - const std::pair& a, - const std::pair& b - ) { - if (a.first == b.first) - { - - return a.second.compare(b.second) < 0; - } - else - { - const auto priorityA = typePriority[a.first]; - const auto priorityB = typePriority[b.first]; - - if (priorityA == priorityB) - { - return a.second.compare(b.second) < 0; - } - else - { - return priorityB < priorityA; - } - } - }); - - // Used to format the CSV - Game::XAssetType lastTypeEncountered{}; - - for (const auto& asset : assets) - { - const auto type = asset.first; - const auto name = asset.second; - if (ExporterAPI.is_type_supported(type) && name[0] != ',') - { - const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); - if (assetHeader.data) - { - ExporterAPI.write(type, assetHeader.data); - const auto typeName = Game::DB_GetXAssetTypeName(type); - - if (type != lastTypeEncountered) - { - csv << "\n### " << typeName << "\n"; - lastTypeEncountered = type; - } - - csv << typeName << "," << name << "\n"; - } - else - { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); - invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); - } - } - else - { - invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); - } - } - - for (const auto& kv : invalidAssets) - { - csv << "\n### " << kv.first << "\n"; - for (const auto& line : kv.second) - { - csv << "#" << line << "\n"; - } - } - - csv << std::format("\n### {} assets", assets.size()) << "\n"; - } - - unload(); - - Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); - ZoneBuilder::DumpingZone = std::string(); + ZoneBuilder::DumpZone(zone); }); Command::Add("verifyzone", [](const Command::Params* params) @@ -1480,18 +1488,20 @@ namespace Components { if (params->size() < 2) return; + Dvar::Var fs_game("fs_game"); + std::string modName = params->get(1); Logger::Print("Building zone for mod '{}'...\n", modName); - const std::string previousFsGame = Dvar::Var("fs_game").get(); + const std::string previousFsGame = fs_game.get(); const std::string dir = "mods/" + modName; Utils::IO::CreateDir(dir); - Dvar::Var("fs_game").set(dir); + fs_game.set(dir); Zone("mod", modName, dir + "/mod.ff").build(); - Dvar::Var("fs_game").set(previousFsGame); + fs_game.set(previousFsGame); }); Command::Add("buildall", []() diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 7fb5a05d..ad4a049b 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -162,6 +162,8 @@ namespace Components static iw4of::params_t GetExporterAPIParams(); + static void DumpZone(const std::string& zone); + static std::function LoadZoneWithTrace(const std::string& zone, OUT std::vector> &assets); static void Com_Quitf_t(); From 4ac496d5b79f20f4185b8638e3faff1e01e7dea3 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 29 Jan 2024 00:52:01 +0100 Subject: [PATCH 32/71] zonebuilder_out --- src/Components/Modules/ZoneBuilder.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 455b0136..60c133f9 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -44,7 +44,7 @@ namespace Components } - ZoneBuilder::Zone::Zone(const std::string& name) : ZoneBuilder::Zone::Zone(name, name, std::format("zone/english/{}.ff", name)) + ZoneBuilder::Zone::Zone(const std::string& name) : ZoneBuilder::Zone::Zone(name, name, std::format("zonebuilder_out/{}.ff", name)) { } @@ -462,6 +462,10 @@ namespace Components outBuffer.append(zoneBuffer); + // Make sure directory exists + const auto directoryName = std::filesystem::path(destination).parent_path(); + Utils::IO::CreateDir(directoryName.string()); + Utils::IO::WriteFile(destination, outBuffer); Logger::Print("done writing {}\n", destination); From ce96aa969d8555ad55cec9ccd973b901be132540 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Wed, 31 Jan 2024 01:08:34 +0100 Subject: [PATCH 33/71] Refactor config string handling to allocate multiple areas, allocate models properly & even add rumble support --- src/Components/Loader.cpp | 5 + src/Components/Modules/AssetHandler.cpp | 2 +- src/Components/Modules/ClientCommand.cpp | 6 +- src/Components/Modules/ConfigStrings.cpp | 308 +++++++++++++++++++++++ src/Components/Modules/ConfigStrings.hpp | 43 ++++ src/Components/Modules/Gamepad.hpp | 2 + src/Components/Modules/ModelCache.cpp | 105 ++++++++ src/Components/Modules/ModelCache.hpp | 30 +++ src/Components/Modules/Weapon.cpp | 242 ------------------ src/Components/Modules/Weapon.hpp | 23 +- src/Game/Functions.cpp | 2 +- src/Game/Functions.hpp | 4 +- src/Game/Game.hpp | 4 + src/Game/Structs.hpp | 40 ++- 14 files changed, 546 insertions(+), 270 deletions(-) create mode 100644 src/Components/Modules/ConfigStrings.cpp create mode 100644 src/Components/Modules/ConfigStrings.hpp create mode 100644 src/Components/Modules/ModelCache.cpp create mode 100644 src/Components/Modules/ModelCache.hpp diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index f50b2689..c581c3ec 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -15,6 +15,7 @@ #include "Modules/ClientCommand.hpp" #include "Modules/ConnectProtocol.hpp" #include "Modules/Console.hpp" +#include "Modules/ConfigStrings.hpp" #include "Modules/D3D9Ex.hpp" #include "Modules/Debug.hpp" #include "Modules/Discord.hpp" @@ -32,6 +33,7 @@ #include "Modules/MapRotation.hpp" #include "Modules/Materials.hpp" #include "Modules/ModList.hpp" +#include "Modules/ModelCache.hpp" #include "Modules/ModelSurfs.hpp" #include "Modules/NetworkDebug.hpp" #include "Modules/News.hpp" @@ -110,6 +112,8 @@ namespace Components Register(new UIScript()); Register(new ZoneBuilder()); + Register(new ConfigStrings()); // Needs to be there early !! Before modelcache & weapons + Register(new ArenaLength()); Register(new AssetHandler()); Register(new Bans()); @@ -144,6 +148,7 @@ namespace Components Register(new Materials()); Register(new Menus()); Register(new ModList()); + Register(new ModelCache()); Register(new ModelSurfs()); Register(new NetworkDebug()); Register(new News()); diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 6d9e0a6c..79bfaefb 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -606,7 +606,7 @@ namespace Components Game::ReallocateAssetPool(Game::ASSET_TYPE_VERTEXSHADER, ZoneBuilder::IsEnabled() ? 0x2000 : 3072); Game::ReallocateAssetPool(Game::ASSET_TYPE_MATERIAL, 8192 * 2); Game::ReallocateAssetPool(Game::ASSET_TYPE_VERTEXDECL, ZoneBuilder::IsEnabled() ? 0x400 : 196); - Game::ReallocateAssetPool(Game::ASSET_TYPE_WEAPON, WEAPON_LIMIT); + Game::ReallocateAssetPool(Game::ASSET_TYPE_WEAPON, Weapon::WEAPON_LIMIT); Game::ReallocateAssetPool(Game::ASSET_TYPE_STRINGTABLE, 800); Game::ReallocateAssetPool(Game::ASSET_TYPE_IMPACT_FX, 8); diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index bd284e8c..f1f4012e 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -1,7 +1,7 @@ #include #include "ClientCommand.hpp" -#include "Weapon.hpp" +#include "ModelCache.hpp" #include "GSC/Script.hpp" @@ -384,9 +384,9 @@ namespace Components Game::XModel* model = nullptr; if (ent->model) { - if (Components::Weapon::GModelIndexHasBeenReallocated) + if (Components::ModelCache::modelsHaveBeenReallocated) { - model = Components::Weapon::cached_models_reallocated[ent->model]; + model = Components::ModelCache::cached_models_reallocated[ent->model]; } else { diff --git a/src/Components/Modules/ConfigStrings.cpp b/src/Components/Modules/ConfigStrings.cpp new file mode 100644 index 00000000..c4fd2e0e --- /dev/null +++ b/src/Components/Modules/ConfigStrings.cpp @@ -0,0 +1,308 @@ +#include +#include "ConfigStrings.hpp" + +namespace Components +{ + // Patch game state + // The structure below is our own implementation of the gameState_t structure + static struct ReallocatedGameState_t + { + int stringOffsets[ConfigStrings::MAX_CONFIGSTRINGS]; + char stringData[131072]; // MAX_GAMESTATE_CHARS + int dataCount; + } cl_gameState{}; + + static short sv_configStrings[ConfigStrings::MAX_CONFIGSTRINGS]{}; + + // New mapping (extra data) + constexpr auto EXTRA_WEAPONS_FIRST = ConfigStrings::BASEGAME_MAX_CONFIGSTRINGS; + constexpr auto EXTRA_WEAPONS_LAST = EXTRA_WEAPONS_FIRST + Weapon::ADDED_WEAPONS - 1; + + constexpr auto EXTRA_MODELCACHE_FIRST = EXTRA_WEAPONS_LAST + 1; + constexpr auto EXTRA_MODELCACHE_LAST = EXTRA_MODELCACHE_FIRST + ModelCache::ADDITIONAL_GMODELS; + + constexpr auto RUMBLE_FIRST = EXTRA_MODELCACHE_LAST + 1; + constexpr auto RUMBLE_LAST = RUMBLE_FIRST + 31; // TODO + + void ConfigStrings::PatchConfigStrings() + { + // bump clientstate fields + Utils::Hook::Set(0x4347A7, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x4982F4, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x4F88B6, MAX_CONFIGSTRINGS); // Save file + Utils::Hook::Set(0x5A1FA7, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x5A210D, MAX_CONFIGSTRINGS); // Game state + Utils::Hook::Set(0x5A840E, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x5A853C, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x5AC392, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x5AC3F5, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x5AC542, MAX_CONFIGSTRINGS); // Game state + Utils::Hook::Set(0x5EADF0, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x625388, MAX_CONFIGSTRINGS); + Utils::Hook::Set(0x625516, MAX_CONFIGSTRINGS); + + Utils::Hook::Set(0x405B72, sv_configStrings); + Utils::Hook::Set(0x468508, sv_configStrings); + Utils::Hook::Set(0x47FD7C, sv_configStrings); + Utils::Hook::Set(0x49830E, sv_configStrings); + Utils::Hook::Set(0x498371, sv_configStrings); + Utils::Hook::Set(0x4983D5, sv_configStrings); + Utils::Hook::Set(0x4A74AD, sv_configStrings); + Utils::Hook::Set(0x4BAE7C, sv_configStrings); + Utils::Hook::Set(0x4BAEC3, sv_configStrings); + Utils::Hook::Set(0x6252F5, sv_configStrings); + Utils::Hook::Set(0x625372, sv_configStrings); + Utils::Hook::Set(0x6253D3, sv_configStrings); + Utils::Hook::Set(0x625480, sv_configStrings); + Utils::Hook::Set(0x6254CB, sv_configStrings); + + // TODO: Check if all of these actually mark the end of the array + // Only 2 actually mark the end, the rest is header data or so + Utils::Hook::Set(0x405B8F, &sv_configStrings[ARRAYSIZE(sv_configStrings)]); + Utils::Hook::Set(0x4A74C9, &sv_configStrings[ARRAYSIZE(sv_configStrings)]); + + Utils::Hook(0x405BBE, ConfigStrings::SV_ClearConfigStrings, HOOK_CALL).install()->quick(); + Utils::Hook(0x593CA4, ConfigStrings::CG_ParseConfigStrings, HOOK_CALL).install()->quick(); + + // Weapon + Utils::Hook(0x4BD52D, ConfigStrings::CL_GetWeaponConfigString, HOOK_CALL).install()->quick(); + Utils::Hook(0x45D19E , ConfigStrings::SV_SetWeaponConfigString, HOOK_CALL).install()->quick(); + + // Cached Models + Utils::Hook(0x589908, ConfigStrings::CL_GetCachedModelConfigString, HOOK_CALL).install()->quick(); + Utils::Hook(0x450A30, ConfigStrings::CL_GetCachedModelConfigString, HOOK_CALL).install()->quick(); + Utils::Hook(0x4503F6, ConfigStrings::CL_GetCachedModelConfigString, HOOK_CALL).install()->quick(); + Utils::Hook(0x4504A0, ConfigStrings::CL_GetCachedModelConfigString, HOOK_CALL).install()->quick(); + Utils::Hook(0x450A30, ConfigStrings::CL_GetCachedModelConfigString, HOOK_CALL).install()->quick(); + + Utils::Hook(0x44F217, ConfigStrings::SV_GetCachedModelConfigStringConst, HOOK_CALL).install()->quick(); + Utils::Hook(0X418F93, ConfigStrings::SV_GetCachedModelConfigStringConst, HOOK_CALL).install()->quick(); + Utils::Hook(0x497B0A, ConfigStrings::SV_GetCachedModelConfigStringConst, HOOK_CALL).install()->quick(); + Utils::Hook(0x4F4493, ConfigStrings::SV_GetCachedModelConfigStringConst, HOOK_CALL).install()->quick(); + Utils::Hook(0x5FC46D, ConfigStrings::SV_GetCachedModelConfigStringConst, HOOK_JUMP).install()->quick(); + + Utils::Hook(0x44F282, ConfigStrings::SV_SetCachedModelConfigString, HOOK_CALL).install()->quick(); + + Utils::Hook::Set(0x44A333, sizeof(cl_gameState)); + Utils::Hook::Set(0x5A1F56, sizeof(cl_gameState)); + Utils::Hook::Set(0x5A2043, sizeof(cl_gameState)); + + Utils::Hook::Set(0x5A2053, sizeof(cl_gameState.stringOffsets)); + Utils::Hook::Set(0x5A2098, sizeof(cl_gameState.stringOffsets)); + Utils::Hook::Set(0x5AC32C, sizeof(cl_gameState.stringOffsets)); + + Utils::Hook::Set(0x4235AC, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x434783, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x44A339, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x44ADB7, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A1FE6, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A2048, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A205A, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A2077, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A2091, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A20D7, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A83FF, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A84B0, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5A84E5, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5AC333, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5AC44A, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5AC4F3, &cl_gameState.stringOffsets); + Utils::Hook::Set(0x5AC57A, &cl_gameState.stringOffsets); + + Utils::Hook::Set(0x4235B7, &cl_gameState.stringData); + Utils::Hook::Set(0x43478D, &cl_gameState.stringData); + Utils::Hook::Set(0x44ADBC, &cl_gameState.stringData); + Utils::Hook::Set(0x5A1FEF, &cl_gameState.stringData); + Utils::Hook::Set(0x5A20E6, &cl_gameState.stringData); + Utils::Hook::Set(0x5AC457, &cl_gameState.stringData); + Utils::Hook::Set(0x5AC502, &cl_gameState.stringData); + Utils::Hook::Set(0x5AC586, &cl_gameState.stringData); + + Utils::Hook::Set(0x5A2071, &cl_gameState.dataCount); + Utils::Hook::Set(0x5A20CD, &cl_gameState.dataCount); + Utils::Hook::Set(0x5A20DC, &cl_gameState.dataCount); + Utils::Hook::Set(0x5A20F3, &cl_gameState.dataCount); + Utils::Hook::Set(0x5A2104, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC33F, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC43B, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC450, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC463, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC471, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC4C3, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC4E8, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC4F8, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC50F, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC528, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC56F, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC580, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC592, &cl_gameState.dataCount); + Utils::Hook::Set(0x5AC59F, &cl_gameState.dataCount); + } + + void ConfigStrings::SV_SetConfigString(int index, const char* data, Game::ConfigString basegameLastPosition, int extendedFirstPosition) + { + if (index > basegameLastPosition) + { + // we jump straight to the reallocated part of the array + const auto relativeIndex = index - (basegameLastPosition + 1); + index = extendedFirstPosition + relativeIndex; + } + + // This will go back to our reallocated game state anyway + return Game::SV_SetConfigstring(index, data); + } + + void ConfigStrings::SV_SetWeaponConfigString(int index, const char* data) + { + SV_SetConfigString(index, data, Game::CS_WEAPONFILES_LAST, EXTRA_WEAPONS_FIRST); + } + + void ConfigStrings::SV_SetCachedModelConfigString(int index, const char* data) + { + SV_SetConfigString(index, data, Game::CS_MODELS_LAST, EXTRA_MODELCACHE_FIRST); + } + + unsigned int ConfigStrings::SV_GetConfigString(int index, Game::ConfigString basegameLastPosition, int extendedFirstPosition) + { + if (index > basegameLastPosition) + { + // It's out of range, because we're loading more weapons than the basegame has + // So we jump straight to the reallocated part of the array + const auto relativeIndex = index - (basegameLastPosition + 1); + + index = extendedFirstPosition + relativeIndex; + } + + // This will go back to our reallocated game state anyway + return Game::SV_GetConfigstringConst(index); + } + + const char* ConfigStrings::CL_GetConfigString(int index, Game::ConfigString basegameLastPosition, int extendedFirstPosition) + { + if (index > basegameLastPosition) + { + // It's out of range, because we're loading more weapons than the basegame has + // So we jump straight to the reallocated part of the array + const auto relativeIndex = index - (basegameLastPosition + 1); + + index = extendedFirstPosition + relativeIndex; + } + + // This will go back to our reallocated game state anyway + return Game::CL_GetConfigString(index); + } + + const char* ConfigStrings::CL_GetCachedModelConfigString(int index) + { + return CL_GetConfigString(index, Game::CS_MODELS_LAST, EXTRA_MODELCACHE_FIRST); + } + + int ConfigStrings::SV_GetCachedModelConfigStringConst(int index) + { + return SV_GetConfigString(index, Game::CS_MODELS_LAST, EXTRA_MODELCACHE_FIRST); + } + + const char* ConfigStrings::CL_GetWeaponConfigString(int index) + { + return CL_GetConfigString(index, Game::CS_WEAPONFILES_LAST, EXTRA_WEAPONS_FIRST); + } + + int ConfigStrings::SV_GetWeaponConfigStringConst(int index) + { + return SV_GetConfigString(index, Game::CS_WEAPONFILES_LAST, EXTRA_WEAPONS_FIRST); + } + + int ConfigStrings::CG_ParseExtraConfigStrings() + { + Command::ClientParams params; + + if (params.size() <= 1) + return 0; + + char* end; + const auto* input = params.get(1); + auto index = std::strtol(input, &end, 10); + + if (input == end) + { + Logger::Warning(Game::CON_CHANNEL_DONT_FILTER, "{} is not a valid input\nUsage: {} \n", + input, params.get(0)); + return 0; + } + + // If it's one of our extra data + // bypass parsing and handle it ourselves! + if (index >= BASEGAME_MAX_CONFIGSTRINGS) + { + // Handle extra weapons + if (index >= EXTRA_WEAPONS_FIRST && index <= EXTRA_WEAPONS_LAST) + { + // Recompute weapon index + index = index - EXTRA_WEAPONS_FIRST + Weapon::BASEGAME_WEAPON_LIMIT; + Game::CG_SetupWeaponConfigString(0, index); + } + // Handle extra models + else if (index >= EXTRA_MODELCACHE_FIRST && index <= EXTRA_MODELCACHE_LAST) + { + const auto name = Game::CL_GetConfigString(index); + + const auto R_RegisterModel = 0x50FA00; + + index = index - EXTRA_MODELCACHE_FIRST + ModelCache::BASE_GMODEL_COUNT; + + const auto test = ModelCache::gameModels_reallocated[index]; + + ModelCache::gameModels_reallocated[index] = Utils::Hook::Call(R_RegisterModel)(name); + } + else + { + // Unknown for now? + // Pass it to the game + return 0; + } + + // We handled it + return 1; + } + + // Pass it to the game + return 0; + } + + __declspec(naked) void ConfigStrings::CG_ParseConfigStrings() + { + __asm + { + push eax + pushad + + call ConfigStrings::CG_ParseExtraConfigStrings + + mov[esp + 20h], eax + popad + + pop eax + + test eax, eax + jz continueParsing + + retn + + continueParsing : + push 592960h + retn + } + } + + int ConfigStrings::SV_ClearConfigStrings(void* dest, int value, int size) + { + memset(Utils::Hook::Get(0x405B72), value, MAX_CONFIGSTRINGS * sizeof(uint16_t)); + return Utils::Hook::Call(0x4C98D0)(dest, value, size); // Com_Memset + } + + + ConfigStrings::ConfigStrings() + { + PatchConfigStrings(); + } +} diff --git a/src/Components/Modules/ConfigStrings.hpp b/src/Components/Modules/ConfigStrings.hpp new file mode 100644 index 00000000..26ff78a3 --- /dev/null +++ b/src/Components/Modules/ConfigStrings.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "Weapon.hpp" +#include "ModelCache.hpp" +#include "Gamepad.hpp" + +namespace Components +{ + class ConfigStrings : public Component + { + public: + static const int BASEGAME_MAX_CONFIGSTRINGS = Game::MAX_CONFIGSTRINGS; + static const int MAX_CONFIGSTRINGS = + (BASEGAME_MAX_CONFIGSTRINGS + + Weapon::ADDED_WEAPONS + + ModelCache::ADDITIONAL_GMODELS + + Gamepad::RUMBLE_CONFIGSTRINGS_COUNT + ); + + static_assert(MAX_CONFIGSTRINGS < USHRT_MAX); + + ConfigStrings(); + + private: + static void PatchConfigStrings(); + + static void SV_SetConfigString(int index, const char* data, Game::ConfigString basegameLastPosition, int extendedFirstPosition); + static unsigned int SV_GetConfigString(int index, Game::ConfigString basegameLastPosition, int extendedFirstPosition); + static const char* CL_GetConfigString(int index, Game::ConfigString basegameLastPosition, int extendedFirstPosition); + + static const char* CL_GetCachedModelConfigString(int index); + static int SV_GetCachedModelConfigStringConst(int index); + static void SV_SetCachedModelConfigString(int index, const char* data); + + static const char* CL_GetWeaponConfigString(int index); + static int SV_GetWeaponConfigStringConst(int index); + static void SV_SetWeaponConfigString(int index, const char* data); + + static int CG_ParseExtraConfigStrings(); + static void CG_ParseConfigStrings(); + static int SV_ClearConfigStrings(void* dest, int value, int size); + }; +} diff --git a/src/Components/Modules/Gamepad.hpp b/src/Components/Modules/Gamepad.hpp index 3409d80c..6a9d57a0 100644 --- a/src/Components/Modules/Gamepad.hpp +++ b/src/Components/Modules/Gamepad.hpp @@ -39,6 +39,8 @@ namespace Components }; public: + static const int RUMBLE_CONFIGSTRINGS_COUNT = 31; + Gamepad(); static void OnMouseMove(int x, int y, int dx, int dy); diff --git a/src/Components/Modules/ModelCache.cpp b/src/Components/Modules/ModelCache.cpp new file mode 100644 index 00000000..98c5a136 --- /dev/null +++ b/src/Components/Modules/ModelCache.cpp @@ -0,0 +1,105 @@ +#include +#include "ModelCache.hpp" + +namespace Components +{ + Game::XModel* ModelCache::cached_models_reallocated[G_MODELINDEX_LIMIT]; + Game::XModel* ModelCache::gameModels_reallocated[G_MODELINDEX_LIMIT]; // Partt of cgs_t + bool ModelCache::modelsHaveBeenReallocated; + + void ModelCache::R_RegisterModel_InitGraphics(const char* name, void* atAddress) + { + const unsigned int gameModelsOriginalAddress = 0x7ED658; // Don't put in Game:: + unsigned int index = (reinterpret_cast(atAddress) - gameModelsOriginalAddress) / sizeof(Game::XModel*); + index++; // Models start at 1 (index 0 is unused) + + // R_REgisterModel + gameModels_reallocated[index] = Utils::Hook::Call(0x50FA00)(name); + } + + __declspec(naked) void ModelCache::R_RegisterModel_Hook() + { + _asm + { + pushad; + push esi; + push eax; + call R_RegisterModel_InitGraphics; + pop esi; + pop eax; + popad; + + push 0x58991B; + retn; + } + } + + ModelCache::ModelCache() + { + // To push the model limit we need to update the network protocol because it uses custom integer size + // (currently 9 bits per model, not enough) + const auto oldBitLength = static_cast(std::floor(std::log2(BASE_GMODEL_COUNT - 1)) + 1); + const auto newBitLength = static_cast(std::floor(std::log2(G_MODELINDEX_LIMIT - 1)) + 1); + + assert(oldBitLength == 9); + + if (oldBitLength != newBitLength) + { + static const std::unordered_set fieldsToUpdate = { + "modelindex", + "attachModelIndex[0]", + "attachModelIndex[1]", + "attachModelIndex[2]", + "attachModelIndex[3]", + "attachModelIndex[4]", + "attachModelIndex[5]", + }; + + size_t currentBitOffset = 0; + + for (size_t i = 0; i < Game::clientStateFieldsCount; i++) + { + auto field = & Game::clientStateFields[i]; + + if (fieldsToUpdate.contains(field->name)) + { + assert(field->bits == oldBitLength); + + Utils::Hook::Set(&field->bits, newBitLength); + currentBitOffset++; + } + } + } + + // Reallocate G_ModelIndex + Utils::Hook::Set(0x420654 + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x43BCE4 + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x44F27B + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x479087 + 1, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x48069D + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x48F088 + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x4F457C + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x5FC762 + 3, ModelCache::cached_models_reallocated); + Utils::Hook::Set(0x5FC7BE + 3, ModelCache::cached_models_reallocated); + + // Reallocate cg models + Utils::Hook::Set(0x44506D + 3, ModelCache::gameModels_reallocated); + Utils::Hook::Set(0x46D49C + 3, ModelCache::gameModels_reallocated); + Utils::Hook::Set(0x586015 + 3, ModelCache::gameModels_reallocated); + Utils::Hook::Set(0x586613 + 3, ModelCache::gameModels_reallocated); + Utils::Hook::Set(0x594E1D + 3, ModelCache::gameModels_reallocated); + + // This one is offset by a compiler optimization + Utils::Hook::Set(0x592B3D + 3, reinterpret_cast(ModelCache::gameModels_reallocated) - Game::CS_MODELS * sizeof(Game::XModel*)); + + // This one is annoying to catch + Utils::Hook(0x589916, R_RegisterModel_Hook, HOOK_JUMP).install()->quick(); + + // Patch limit + Utils::Hook::Set(0x479080 + 1, ModelCache::G_MODELINDEX_LIMIT * sizeof(void*)); + Utils::Hook::Set(0x44F256 + 2, ModelCache::G_MODELINDEX_LIMIT); + Utils::Hook::Set(0x44F231 + 2, ModelCache::G_MODELINDEX_LIMIT); + + modelsHaveBeenReallocated = true; + } +} diff --git a/src/Components/Modules/ModelCache.hpp b/src/Components/Modules/ModelCache.hpp new file mode 100644 index 00000000..cbbef0db --- /dev/null +++ b/src/Components/Modules/ModelCache.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "Weapon.hpp" + +namespace Components +{ + class ModelCache : public Component + { + public: + static const int BASE_GMODEL_COUNT = 512; + + // Double the limit to allow loading of some heavy-duty MW3 maps + static const int ADDITIONAL_GMODELS = 512; + + static const int G_MODELINDEX_LIMIT = (BASE_GMODEL_COUNT + Weapon::WEAPON_LIMIT - Weapon::BASEGAME_WEAPON_LIMIT + ADDITIONAL_GMODELS); + + // Server + static Game::XModel* cached_models_reallocated[G_MODELINDEX_LIMIT]; + + // Client game + static Game::XModel* gameModels_reallocated[G_MODELINDEX_LIMIT]; + + static bool modelsHaveBeenReallocated; + + static void R_RegisterModel_InitGraphics(const char* name, void* atAddress); + static void R_RegisterModel_Hook(); + + ModelCache(); + }; +} diff --git a/src/Components/Modules/Weapon.cpp b/src/Components/Modules/Weapon.cpp index 0413fd9d..9d1e036d 100644 --- a/src/Components/Modules/Weapon.cpp +++ b/src/Components/Modules/Weapon.cpp @@ -6,17 +6,6 @@ namespace Components { const Game::dvar_t* Weapon::BGWeaponOffHandFix; - Game::XModel* Weapon::cached_models_reallocated[G_MODELINDEX_LIMIT]; - bool Weapon::GModelIndexHasBeenReallocated; - - // Config strings mapping - // 0-1067 unknown - // 1125-1580 cached models (512 long range) - // 1581-1637 also reserved for models? - // 1637-4082 unknown - // 4082-above = bad index? - // 4137 : timescale - // 4138-above = reserved for weapons? Game::WeaponCompleteDef* Weapon::LoadWeaponCompleteDef(const char* name) { @@ -29,12 +18,6 @@ namespace Components return Game::DB_IsXAssetDefault(Game::ASSET_TYPE_WEAPON, name) ? nullptr : zoneWeaponFile; } - const char* Weapon::GetWeaponConfigString(int index) - { - if (index >= (BASEGAME_WEAPON_LIMIT + 2804)) index += (2939 - 2804); - return Game::CL_GetConfigString(index); - } - void Weapon::SaveRegisteredWeapons() { *reinterpret_cast(0x1A86098) = 0; @@ -47,215 +30,6 @@ namespace Components } } } - - int Weapon::ParseWeaponConfigStrings() - { - Command::ClientParams params; - - if (params.size() <= 1) - return 0; - - char* end; - const auto* input = params.get(1); - auto index = std::strtol(input, &end, 10); - - if (input == end) - { - Logger::Warning(Game::CON_CHANNEL_DONT_FILTER, "{} is not a valid input\nUsage: {} \n", - input, params.get(0)); - return 0; - } - - if (index >= BASEGAME_MAX_CONFIGSTRINGS) - { - // if above 4139, remap to 1200<>? - index -= 2939; - } - else if (index > 2804 && index <= 2804 + BASEGAME_WEAPON_LIMIT) - { - // from 2804 to 4004, remap to 0<>1200 - index -= 2804; - } - else - { - return 0; - } - - Game::CG_SetupWeaponDef(0, index); - return 1; - } - - __declspec(naked) void Weapon::ParseConfigStrings() - { - __asm - { - push eax - pushad - - push edi - call Weapon::ParseWeaponConfigStrings - pop edi - - mov [esp + 20h], eax - popad - pop eax - - test eax, eax - jz continueParsing - - retn - - continueParsing: - push 592960h - retn - } - } - - int Weapon::ClearConfigStrings(void* dest, int value, int size) - { - memset(Utils::Hook::Get(0x405B72), value, MAX_CONFIGSTRINGS * 2); - return Utils::Hook::Call(0x4C98D0)(dest, value, size); // Com_Memset - } - - void Weapon::PatchConfigStrings() - { - Utils::Hook::Set(0x4347A7, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x4982F4, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x4F88B6, MAX_CONFIGSTRINGS); // Save file - Utils::Hook::Set(0x5A1FA7, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x5A210D, MAX_CONFIGSTRINGS); // Game state - Utils::Hook::Set(0x5A840E, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x5A853C, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x5AC392, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x5AC3F5, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x5AC542, MAX_CONFIGSTRINGS); // Game state - Utils::Hook::Set(0x5EADF0, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x625388, MAX_CONFIGSTRINGS); - Utils::Hook::Set(0x625516, MAX_CONFIGSTRINGS); - - // Adjust weapon count index - // Actually this has to stay the way it is! - //Utils::Hook::Set(0x4EB7B3, MAX_CONFIGSTRINGS - 1); - //Utils::Hook::Set(0x5929BA, MAX_CONFIGSTRINGS - 1); - //Utils::Hook::Set(0x5E2FAA, MAX_CONFIGSTRINGS - 1); - - static short configStrings[MAX_CONFIGSTRINGS]; - ZeroMemory(&configStrings, sizeof(configStrings)); - - Utils::Hook::Set(0x405B72, configStrings); - Utils::Hook::Set(0x468508, configStrings); - Utils::Hook::Set(0x47FD7C, configStrings); - Utils::Hook::Set(0x49830E, configStrings); - Utils::Hook::Set(0x498371, configStrings); - Utils::Hook::Set(0x4983D5, configStrings); - Utils::Hook::Set(0x4A74AD, configStrings); - Utils::Hook::Set(0x4BAE7C, configStrings); - Utils::Hook::Set(0x4BAEC3, configStrings); - Utils::Hook::Set(0x6252F5, configStrings); - Utils::Hook::Set(0x625372, configStrings); - Utils::Hook::Set(0x6253D3, configStrings); - Utils::Hook::Set(0x625480, configStrings); - Utils::Hook::Set(0x6254CB, configStrings); - - // This has nothing to do with configstrings - //Utils::Hook::Set(0x608095, configStrings[4139 - 0x16]); - //Utils::Hook::Set(0x6080BC, configStrings[4139 - 0x16]); - //Utils::Hook::Set(0x6082AC, configStrings[4139 - 0x16]); - //Utils::Hook::Set(0x6082B3, configStrings[4139 - 0x16]); - - //Utils::Hook::Set(0x608856, configStrings[4139 - 0x14]); - - // TODO: Check if all of these actually mark the end of the array - // Only 2 actually mark the end, the rest is header data or so - Utils::Hook::Set(0x405B8F, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x459121, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x45A476, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x49FD56, &configStrings[ARRAYSIZE(configStrings)]); - Utils::Hook::Set(0x4A74C9, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x4C8196, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x4EBCE6, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x4F36D6, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x60807C, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x6080A9, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x6080D0, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x6081C4, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x608211, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x608274, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x6083D6, &configStrings[ARRAYSIZE(configStrings)]); - //Utils::Hook::Set(0x60848E, &configStrings[ARRAYSIZE(configStrings)]); - - Utils::Hook(0x405BBE, Weapon::ClearConfigStrings, HOOK_CALL).install()->quick(); - Utils::Hook(0x593CA4, Weapon::ParseConfigStrings, HOOK_CALL).install()->quick(); - Utils::Hook(0x4BD52D, Weapon::GetWeaponConfigString, HOOK_CALL).install()->quick(); - Utils::Hook(0x45D170, Weapon::SaveRegisteredWeapons, HOOK_JUMP).install()->quick(); - - // Patch game state - // The structure below is our own implementation of the gameState_t structure - static struct newGameState_t - { - int stringOffsets[MAX_CONFIGSTRINGS]; - char stringData[131072]; // MAX_GAMESTATE_CHARS - int dataCount; - } gameState; - - ZeroMemory(&gameState, sizeof(gameState)); - - Utils::Hook::Set(0x44A333, sizeof(gameState)); - Utils::Hook::Set(0x5A1F56, sizeof(gameState)); - Utils::Hook::Set(0x5A2043, sizeof(gameState)); - - Utils::Hook::Set(0x5A2053, sizeof(gameState.stringOffsets)); - Utils::Hook::Set(0x5A2098, sizeof(gameState.stringOffsets)); - Utils::Hook::Set(0x5AC32C, sizeof(gameState.stringOffsets)); - - Utils::Hook::Set(0x4235AC, &gameState.stringOffsets); - Utils::Hook::Set(0x434783, &gameState.stringOffsets); - Utils::Hook::Set(0x44A339, &gameState.stringOffsets); - Utils::Hook::Set(0x44ADB7, &gameState.stringOffsets); - Utils::Hook::Set(0x5A1FE6, &gameState.stringOffsets); - Utils::Hook::Set(0x5A2048, &gameState.stringOffsets); - Utils::Hook::Set(0x5A205A, &gameState.stringOffsets); - Utils::Hook::Set(0x5A2077, &gameState.stringOffsets); - Utils::Hook::Set(0x5A2091, &gameState.stringOffsets); - Utils::Hook::Set(0x5A20D7, &gameState.stringOffsets); - Utils::Hook::Set(0x5A83FF, &gameState.stringOffsets); - Utils::Hook::Set(0x5A84B0, &gameState.stringOffsets); - Utils::Hook::Set(0x5A84E5, &gameState.stringOffsets); - Utils::Hook::Set(0x5AC333, &gameState.stringOffsets); - Utils::Hook::Set(0x5AC44A, &gameState.stringOffsets); - Utils::Hook::Set(0x5AC4F3, &gameState.stringOffsets); - Utils::Hook::Set(0x5AC57A, &gameState.stringOffsets); - - Utils::Hook::Set(0x4235B7, &gameState.stringData); - Utils::Hook::Set(0x43478D, &gameState.stringData); - Utils::Hook::Set(0x44ADBC, &gameState.stringData); - Utils::Hook::Set(0x5A1FEF, &gameState.stringData); - Utils::Hook::Set(0x5A20E6, &gameState.stringData); - Utils::Hook::Set(0x5AC457, &gameState.stringData); - Utils::Hook::Set(0x5AC502, &gameState.stringData); - Utils::Hook::Set(0x5AC586, &gameState.stringData); - - Utils::Hook::Set(0x5A2071, &gameState.dataCount); - Utils::Hook::Set(0x5A20CD, &gameState.dataCount); - Utils::Hook::Set(0x5A20DC, &gameState.dataCount); - Utils::Hook::Set(0x5A20F3, &gameState.dataCount); - Utils::Hook::Set(0x5A2104, &gameState.dataCount); - Utils::Hook::Set(0x5AC33F, &gameState.dataCount); - Utils::Hook::Set(0x5AC43B, &gameState.dataCount); - Utils::Hook::Set(0x5AC450, &gameState.dataCount); - Utils::Hook::Set(0x5AC463, &gameState.dataCount); - Utils::Hook::Set(0x5AC471, &gameState.dataCount); - Utils::Hook::Set(0x5AC4C3, &gameState.dataCount); - Utils::Hook::Set(0x5AC4E8, &gameState.dataCount); - Utils::Hook::Set(0x5AC4F8, &gameState.dataCount); - Utils::Hook::Set(0x5AC50F, &gameState.dataCount); - Utils::Hook::Set(0x5AC528, &gameState.dataCount); - Utils::Hook::Set(0x5AC56F, &gameState.dataCount); - Utils::Hook::Set(0x5AC580, &gameState.dataCount); - Utils::Hook::Set(0x5AC592, &gameState.dataCount); - Utils::Hook::Set(0x5AC59F, &gameState.dataCount); - } - void Weapon::PatchLimit() { Utils::Hook::Set(0x403783, WEAPON_LIMIT); @@ -446,21 +220,6 @@ namespace Components Utils::Hook::Set(0x4F76FB, 0x12EC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg4 pointers Utils::Hook::Set(0x4F7630, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); - - - // Reallocate G_ModelIndex - Utils::Hook::Set(0x420654 + 3, cached_models_reallocated); - Utils::Hook::Set(0x43BCE4 + 3, cached_models_reallocated); - Utils::Hook::Set(0x44F27B + 3, cached_models_reallocated); - Utils::Hook::Set(0x479087 + 1, cached_models_reallocated); - Utils::Hook::Set(0x48069D + 3, cached_models_reallocated); - Utils::Hook::Set(0x48F088 + 3, cached_models_reallocated); - Utils::Hook::Set(0x4F457C + 3, cached_models_reallocated); - Utils::Hook::Set(0x5FC762 + 3, cached_models_reallocated); - Utils::Hook::Set(0x5FC7BE + 3, cached_models_reallocated); - Utils::Hook::Set(0x44F256 + 2, G_MODELINDEX_LIMIT); - - GModelIndexHasBeenReallocated = true; } void* Weapon::LoadNoneWeaponHook() @@ -652,7 +411,6 @@ namespace Components Weapon::Weapon() { PatchLimit(); - PatchConfigStrings(); // BG_LoadWEaponCompleteDef_FastFile Utils::Hook(0x57B650, LoadWeaponCompleteDef, HOOK_JUMP).install()->quick(); diff --git a/src/Components/Modules/Weapon.hpp b/src/Components/Modules/Weapon.hpp index 3065a1e0..fb0a238b 100644 --- a/src/Components/Modules/Weapon.hpp +++ b/src/Components/Modules/Weapon.hpp @@ -1,16 +1,6 @@ #pragma once -#define BASEGAME_WEAPON_LIMIT 1200 -#define BASEGAME_MAX_CONFIGSTRINGS 4139 -// Increase the weapon limit -#define WEAPON_LIMIT 2400 -#define MAX_CONFIGSTRINGS (BASEGAME_MAX_CONFIGSTRINGS - BASEGAME_WEAPON_LIMIT + WEAPON_LIMIT) - -// Double the limit to allow loading of some heavy-duty MW3 maps -#define ADDITIONAL_GMODELS 512 - -#define G_MODELINDEX_LIMIT (512 + WEAPON_LIMIT - BASEGAME_WEAPON_LIMIT + ADDITIONAL_GMODELS) namespace Components { @@ -18,9 +8,12 @@ namespace Components { public: Weapon(); - static Game::XModel* cached_models_reallocated[G_MODELINDEX_LIMIT]; + static const int BASEGAME_WEAPON_LIMIT = 1200; - static bool GModelIndexHasBeenReallocated; + // Increase the weapon limit + static const int WEAPON_LIMIT = 2400; + + static const int ADDED_WEAPONS = WEAPON_LIMIT - BASEGAME_WEAPON_LIMIT; private: static const Game::dvar_t* BGWeaponOffHandFix; @@ -29,15 +22,9 @@ namespace Components static void PatchLimit(); static void* LoadNoneWeaponHook(); static void LoadNoneWeaponHookStub(); - static void PatchConfigStrings(); - static const char* GetWeaponConfigString(int index); static void SaveRegisteredWeapons(); - static void ParseConfigStrings(); - static int ParseWeaponConfigStrings(); - static int ClearConfigStrings(void* dest, int value, int size); - static void CG_UpdatePrimaryForAltModeWeapon_Stub(); static void CG_SelectWeaponIndex_Stub(); diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 92048912..8b6bbcd7 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -22,7 +22,7 @@ namespace Game CG_ScrollScoreboardUp_t CG_ScrollScoreboardUp = CG_ScrollScoreboardUp_t(0x47A5C0); CG_ScrollScoreboardDown_t CG_ScrollScoreboardDown = CG_ScrollScoreboardDown_t(0x493B50); CG_GetTeamName_t CG_GetTeamName = CG_GetTeamName_t(0x4B6210); - CG_SetupWeaponDef_t CG_SetupWeaponDef = CG_SetupWeaponDef_t(0x4BD520); + CG_SetupWeaponConfigString_t CG_SetupWeaponConfigString = CG_SetupWeaponConfigString_t(0x4BD520); Cmd_AddCommand_t Cmd_AddCommand = Cmd_AddCommand_t(0x470090); Cmd_AddServerCommand_t Cmd_AddServerCommand = Cmd_AddServerCommand_t(0x4DCE00); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 090d669c..69626d39 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -51,8 +51,8 @@ namespace Game typedef const char*(*CG_GetTeamName_t)(team_t team); extern CG_GetTeamName_t CG_GetTeamName; - typedef void(*CG_SetupWeaponDef_t)(int localClientNum, unsigned int weapIndex); - extern CG_SetupWeaponDef_t CG_SetupWeaponDef; + typedef void(*CG_SetupWeaponConfigString_t)(int localClientNum, unsigned int weapIndex); + extern CG_SetupWeaponConfigString_t CG_SetupWeaponConfigString; typedef void(*Cmd_AddCommand_t)(const char* cmdName, void(*function), cmd_function_s* allocedCmd, int isKey); extern Cmd_AddCommand_t Cmd_AddCommand; diff --git a/src/Game/Game.hpp b/src/Game/Game.hpp index b51e2538..ad47733d 100644 --- a/src/Game/Game.hpp +++ b/src/Game/Game.hpp @@ -53,6 +53,10 @@ namespace Game constexpr std::size_t ENTITYNUM_NONE = MAX_GENTITIES - 1; extern gentity_s* g_entities; + // This does not belong anywhere else + NetField* clientStateFields = reinterpret_cast(0x741E40); + size_t clientStateFieldsCount = Utils::Hook::Get(0x7433C8); + extern const char* origErrorMsg; extern XModel* G_GetModel(int index); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index d1233fb5..d661f1de 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1,6 +1,6 @@ #pragma once -#define PROTOCOL 0x96 +#define PROTOCOL 0x97 #define NUM_CUSTOM_CLASSES 15 #define FX_ELEM_FIELD_COUNT 90 @@ -527,8 +527,14 @@ namespace Game CS_VOTE_NO = 0x14, CS_VOTE_MAPNAME = 0x15, CS_VOTE_GAMETYPE = 0x16, + CS_MODELS = 0x465, // Models (confirmed) 1125 + // 1580<>1637 models not cached (something's going on, not sure what) + CS_MODELS_LAST = 0x664, // End of models CS_SHELLSHOCKS = 0x985, - CS_ITEMS = 0x102A, + CS_WEAPONFILES = 0xAF5, // 2805 Confirmed + CS_WEAPONFILES_LAST = 0xFA3, // Confirmed too // 4003 + CS_ITEMS = 4138, // Correct! CS_ITEMS is actually an item **COUNT** + MAX_CONFIGSTRINGS = 4139 }; // Incomplete enum conChannel_t @@ -1906,6 +1912,26 @@ namespace Game TEAM_NUM_TEAMS = 0x4, }; + struct clientState_s_borked + { + team_t team; + int modelindex; + int dualWielding; + int riotShieldNext; + int attachModelIndex[6]; + int attachTagIndex[6]; + char name[16]; + float maxSprintTimeMultiplier; + int rank; + int prestige; + unsigned int perks[2]; + int diveState; + int voiceConnectivityBits; + unsigned int playerCardIcon; + unsigned int playerCardTitle; + unsigned int playerCardNameplate; + }; + struct clientState_s { int clientIndex; @@ -4938,6 +4964,14 @@ namespace Game float colors[5][4]; }; + struct NetField + { + char* name; + int offset; + int bits; + char changeHints; + }; + struct __declspec(align(4)) WeaponDef { const char* szOverlayName; @@ -8718,7 +8752,7 @@ namespace Game char* skelMemoryStart; bool allowedAllocSkel; int serverId; - gameState_t gameState; + gameState_t cl_gameState; clSnapshot_t noDeltaSnapshot; int nextNoDeltaEntity; entityState_s noDeltaEntities[1024]; From b6fbc92db37fd07f0ab42422a58bf8b7f56eb2bc Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Wed, 31 Jan 2024 01:12:45 +0100 Subject: [PATCH 34/71] woops --- src/Game/Structs.hpp | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index d661f1de..cd68331b 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1912,26 +1912,6 @@ namespace Game TEAM_NUM_TEAMS = 0x4, }; - struct clientState_s_borked - { - team_t team; - int modelindex; - int dualWielding; - int riotShieldNext; - int attachModelIndex[6]; - int attachTagIndex[6]; - char name[16]; - float maxSprintTimeMultiplier; - int rank; - int prestige; - unsigned int perks[2]; - int diveState; - int voiceConnectivityBits; - unsigned int playerCardIcon; - unsigned int playerCardTitle; - unsigned int playerCardNameplate; - }; - struct clientState_s { int clientIndex; From 5bab1ba0cc5f9b578d340d308b1d986d1e201715 Mon Sep 17 00:00:00 2001 From: Edo Date: Wed, 31 Jan 2024 01:14:17 +0100 Subject: [PATCH 35/71] use gsl --- src/Utils/Utils.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index daf68be9..141a0fcc 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -131,9 +131,10 @@ namespace Utils return; } + const auto _0 = gsl::finally([&] { std::free(buffer); }); + SetCurrentDirectoryA(buffer); SetDllDirectoryA(buffer); - std::free(buffer); } /** @@ -148,16 +149,16 @@ namespace Utils return {}; } + const auto _0 = gsl::finally([&] { std::free(buffer); }); + try { std::filesystem::path result = buffer; - std::free(buffer); return result; } catch (const std::exception& ex) { printf("Failed to convert '%s' to native file system path. Got error '%s'\n", buffer, ex.what()); - std::free(buffer); return {}; } } From 13f06d3c9b630c179049c020a94477b8c831b4be Mon Sep 17 00:00:00 2001 From: Roxanne Date: Wed, 31 Jan 2024 10:03:29 +0100 Subject: [PATCH 36/71] Fix debug comp --- src/Components/Modules/ModelCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/ModelCache.cpp b/src/Components/Modules/ModelCache.cpp index 98c5a136..0893303c 100644 --- a/src/Components/Modules/ModelCache.cpp +++ b/src/Components/Modules/ModelCache.cpp @@ -63,7 +63,7 @@ namespace Components if (fieldsToUpdate.contains(field->name)) { - assert(field->bits == oldBitLength); + assert(static_cast(field->bits) == oldBitLength); Utils::Hook::Set(&field->bits, newBitLength); currentBitOffset++; From 96cc6f2f12d934aed778e2f1a4e5e5ae34b779ee Mon Sep 17 00:00:00 2001 From: Roxanne Date: Wed, 31 Jan 2024 10:17:49 +0100 Subject: [PATCH 37/71] fix warning --- src/Components/Modules/ConfigStrings.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Components/Modules/ConfigStrings.cpp b/src/Components/Modules/ConfigStrings.cpp index c4fd2e0e..020093d5 100644 --- a/src/Components/Modules/ConfigStrings.cpp +++ b/src/Components/Modules/ConfigStrings.cpp @@ -249,9 +249,6 @@ namespace Components const auto R_RegisterModel = 0x50FA00; index = index - EXTRA_MODELCACHE_FIRST + ModelCache::BASE_GMODEL_COUNT; - - const auto test = ModelCache::gameModels_reallocated[index]; - ModelCache::gameModels_reallocated[index] = Utils::Hook::Call(R_RegisterModel)(name); } else From 5a967f3004300f88efc59569586d94cf7109d893 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Wed, 31 Jan 2024 17:17:29 +0100 Subject: [PATCH 38/71] Fix whoopsie --- src/Game/Game.cpp | 3 +++ src/Game/Game.hpp | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 7e0ad53b..2887e3a1 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -18,6 +18,9 @@ namespace Game gentity_s* g_entities = reinterpret_cast(0x18835D8); + NetField* clientStateFields = reinterpret_cast(0x741E40); + size_t clientStateFieldsCount = Utils::Hook::Get(0x7433C8); + const char* origErrorMsg = reinterpret_cast(0x79B124); XModel* G_GetModel(const int index) diff --git a/src/Game/Game.hpp b/src/Game/Game.hpp index ad47733d..06315fad 100644 --- a/src/Game/Game.hpp +++ b/src/Game/Game.hpp @@ -54,8 +54,8 @@ namespace Game extern gentity_s* g_entities; // This does not belong anywhere else - NetField* clientStateFields = reinterpret_cast(0x741E40); - size_t clientStateFieldsCount = Utils::Hook::Get(0x7433C8); + extern NetField* clientStateFields; + extern size_t clientStateFieldsCount; extern const char* origErrorMsg; From 0d222e30cf2a3911fd0dfc734a03ce341ae2174d Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Wed, 31 Jan 2024 20:53:41 +0100 Subject: [PATCH 39/71] Important fixes for zonebuilder & filter servers on protocol --- src/Components/Modules/Node.cpp | 72 +++++++++++++++------------ src/Components/Modules/ServerList.cpp | 15 +++++- src/Components/Modules/ServerList.hpp | 1 + 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index 4f170bb4..898f5b85 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -342,12 +342,12 @@ namespace Components for (const auto& nodeListData : nodeListReponseMessages) { Scheduler::Once([=] - { + { #ifdef NODE_SYSTEM_DEBUG - Logger::Debug("Sending {} nodeListResponse length to {}\n", nodeListData.length(), address.getCString()); + Logger::Debug("Sending {} nodeListResponse length to {}\n", nodeListData.length(), address.getCString()); #endif - Session::Send(address, "nodeListResponse", nodeListData); - }, Scheduler::Pipeline::MAIN, NODE_SEND_RATE * i++); + Session::Send(address, "nodeListResponse", nodeListData); + }, Scheduler::Pipeline::MAIN, NODE_SEND_RATE * i++); } } @@ -397,48 +397,54 @@ namespace Components Node::Node() { + if (ZoneBuilder::IsEnabled()) + { + return; + } + net_natFix = Game::Dvar_RegisterBool("net_natFix", false, 0, "Fix node registration for certain firewalls/routers"); Scheduler::Loop([] - { - StoreNodes(false); - }, Scheduler::Pipeline::ASYNC, 5min); + { + StoreNodes(false); + }, Scheduler::Pipeline::ASYNC, 5min); Scheduler::Loop(RunFrame, Scheduler::Pipeline::MAIN); - Session::Handle("nodeListResponse", HandleResponse); - Session::Handle("nodeListRequest", [](const Network::Address& address, [[maybe_unused]] const std::string& data) - { - SendList(address); - }); - Scheduler::OnGameInitialized([] - { - Migrate(); - LoadNodePreset(); - LoadNodes(); - }, Scheduler::Pipeline::MAIN); + { + + Session::Handle("nodeListResponse", HandleResponse); + Session::Handle("nodeListRequest", [](const Network::Address& address, [[maybe_unused]] const std::string& data) + { + SendList(address); + }); + + Migrate(); + LoadNodePreset(); + LoadNodes(); + }, Scheduler::Pipeline::MAIN); Command::Add("listNodes", [](const Command::Params*) - { - Logger::Print("Nodes: {}\n", Nodes.size()); - - std::lock_guard _(Mutex); - for (const auto& node : Nodes) { - Logger::Print("{}\t({})\n", node.address.getString(), node.isValid() ? "Valid" : "Invalid"); - } - }); + Logger::Print("Nodes: {}\n", Nodes.size()); + + std::lock_guard _(Mutex); + for (const auto& node : Nodes) + { + Logger::Print("{}\t({})\n", node.address.getString(), node.isValid() ? "Valid" : "Invalid"); + } + }); Command::Add("addNode", [](const Command::Params* params) - { - if (params->size() < 2) return; - auto address = Network::Address{ params->get(1) }; - if (address.isValid()) { - Add(address); - } - }); + if (params->size() < 2) return; + auto address = Network::Address{ params->get(1) }; + if (address.isValid()) + { + Add(address); + } + }); } void Node::preDestroy() diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index fd496edb..bcef4c6f 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -322,6 +322,19 @@ namespace Components continue; } + if (!entry.HasMember("ip") || !entry["protocol"].IsInt()) + { + continue; + } + + const auto protocol = entry["protocol"].GetInt(); + + if (protocol != PROTOCOL) + { + // We can't connect to it anyway + continue; + } + // Using VA because it's faster Network::Address server(Utils::String::VA("%s:%u", entry["ip"].GetString(), entry["port"].GetInt())); server.setType(Game::NA_IP); // Just making sure... @@ -371,7 +384,7 @@ namespace Components Toast::Show("cardicon_headshot", "Server Browser", "Fetching servers...", 3000); - const auto* url = "http://iw4x.plutools.pw/v1/servers/iw4x"; + const auto url = std::format("http://iw4x.plutools.pw/v1/servers/iw4x?protocol={}", PROTOCOL); const auto reply = Utils::WebIO("IW4x", url).setTimeout(5000)->get(); if (reply.empty()) { diff --git a/src/Components/Modules/ServerList.hpp b/src/Components/Modules/ServerList.hpp index abe61896..193b789b 100644 --- a/src/Components/Modules/ServerList.hpp +++ b/src/Components/Modules/ServerList.hpp @@ -23,6 +23,7 @@ namespace Components int ping; int matchType; int securityLevel; + int protocol; bool hardcore; bool svRunning; bool aimassist; From 67bd07949d47e4c70808c2a3e4487066c8f2a069 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 00:31:03 +0100 Subject: [PATCH 40/71] GUID On runtime --- .../Modules/AssetInterfaces/IXModel.cpp | 5 - src/Components/Modules/Auth.cpp | 109 +++++++------ src/Components/Modules/Download.cpp | 2 +- src/STDInclude.hpp | 3 + src/Utils/Cryptography.cpp | 146 ++++++++++++------ src/Utils/Cryptography.hpp | 9 +- 6 files changed, 164 insertions(+), 110 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index ac22bc5e..5daf43fc 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -873,11 +873,6 @@ namespace Assets void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) { - if (model->name == "body_airport_com_a"s) - { - printf(""); - } - std::string requiredBonesForHumanoid[] = { "j_spinelower", "j_spineupper", diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 2cae7191..c3642b70 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -19,14 +19,15 @@ namespace Components std::vector Auth::BannedUids = { - 0xf4d2c30b712ac6e3, + // No longer necessary + /* 0xf4d2c30b712ac6e3, 0xf7e33c4081337fa3, 0x6f5597f103cc50e9, - 0xecd542eee54ffccf, + 0xecd542eee54ffccf,*/ }; bool Auth::HasAccessToReservedSlot; - + void Auth::Frame() { if (TokenContainer.generating) @@ -45,7 +46,7 @@ namespace Components if (mseconds < 0) mseconds = 0; } - Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", Utils::String::VA("Increasing security level from %d to %d (est. %s)",GetSecurityLevel(), TokenContainer.targetLevel, Utils::String::FormatTimeSpan(static_cast(mseconds)).data())); + Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", Utils::String::VA("Increasing security level from %d to %d (est. %s)", GetSecurityLevel(), TokenContainer.targetLevel, Utils::String::FormatTimeSpan(static_cast(mseconds)).data())); } else if (TokenContainer.thread.joinable()) { @@ -53,7 +54,7 @@ namespace Components TokenContainer.generating = false; StoreKey(); - Logger::Debug("Security level is {}",GetSecurityLevel()); + Logger::Debug("Security level is {}", GetSecurityLevel()); Command::Execute("closemenu security_increase_popmenu", false); if (!TokenContainer.cancel) @@ -212,7 +213,7 @@ namespace Components SteamID guid; guid.bits = xuid; - if (Bans::IsBanned({guid, address.getIP()})) + if (Bans::IsBanned({ guid, address.getIP() })) { Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(address)); Network::Send(address, "error\nEXE_ERR_BANNED_PERM"); @@ -304,15 +305,15 @@ namespace Components xor eax, eax jmp safeContinue - noAccess: - mov eax, dword ptr [edx + 0x10] + noAccess : + mov eax, dword ptr[edx + 0x10] - safeContinue: - // Game code skipped by hook - add esp, 0xC + safeContinue : + // Game code skipped by hook + add esp, 0xC - push 0x460FB3 - ret + push 0x460FB3 + ret } } @@ -342,6 +343,8 @@ namespace Components void Auth::StoreKey() { + // We write the key as a decoy I suppose - it's really no longer needed + // TODO Remove this part if (!Dedicated::IsEnabled() && !ZoneBuilder::IsEnabled() && GuidKey.isValid()) { Proto::Auth::Certificate cert; @@ -366,23 +369,31 @@ namespace Components if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled()) return; if (!force && GuidKey.isValid()) return; + // We no longer read the key from disk + // While having obvious advantages to palliate the fact that some users are not playing on Steam, + // it is creating a lot of issues because GUID files get packaged with the game when people share it + // and it makes it harder for server owners to identify players uniquely + // Note that we could store it in Appdata, but then it would be dissociated from the rest of player files, + // so for now we're doing something else: the key is generated uniquely from the machine's characteristics + // It is not (necessarily) stored and therefore, not loaded, so it could make it harder to evade bans without + // using a custom client that would need regeneration at each update. +#if false Proto::Auth::Certificate cert; if (cert.ParseFromString(::Utils::IO::ReadFile("players/guid.dat"))) { GuidKey.deserialize(cert.privatekey()); GuidToken = cert.token(); ComputeToken = cert.ctoken(); - } + } else { GuidKey.free(); } if (!GuidKey.isValid()) - { +#endif Auth::GenerateKey(); - } - } +} uint32_t Auth::GetSecurityLevel() { @@ -404,18 +415,18 @@ namespace Components // Start thread TokenContainer.thread = std::thread([&level]() - { - TokenContainer.generating = true; - TokenContainer.hashes = 0; - TokenContainer.startTime = Game::Sys_Milliseconds(); - IncrementToken(GuidToken, ComputeToken, GuidKey.getPublicKey(), TokenContainer.targetLevel, &TokenContainer.cancel, &TokenContainer.hashes); - TokenContainer.generating = false; - - if (TokenContainer.cancel) { - Logger::Print("Token incrementation thread terminated\n"); - } - }); + TokenContainer.generating = true; + TokenContainer.hashes = 0; + TokenContainer.startTime = Game::Sys_Milliseconds(); + IncrementToken(GuidToken, ComputeToken, GuidKey.getPublicKey(), TokenContainer.targetLevel, &TokenContainer.cancel, &TokenContainer.hashes); + TokenContainer.generating = false; + + if (TokenContainer.cancel) + { + Logger::Print("Token incrementation thread terminated\n"); + } + }); } } @@ -521,36 +532,36 @@ namespace Components // Guid command Command::Add("guid", [] - { - Logger::Print("Your guid: {:#X}\n", Steam::SteamUser()->GetSteamID().bits); - }); + { + Logger::Print("Your guid: {:#X}\n", Steam::SteamUser()->GetSteamID().bits); + }); if (!Dedicated::IsEnabled() && !ZoneBuilder::IsEnabled()) { Command::Add("securityLevel", [](const Command::Params* params) - { - if (params->size() < 2) { - const auto level = GetZeroBits(GuidToken, GuidKey.getPublicKey()); - Logger::Print("Your current security level is {}\n", level); - Logger::Print("Your security token is: {}\n", Utils::String::DumpHex(GuidToken.toString(), "")); - Logger::Print("Your computation token is: {}\n", Utils::String::DumpHex(ComputeToken.toString(), "")); + if (params->size() < 2) + { + const auto level = GetZeroBits(GuidToken, GuidKey.getPublicKey()); + Logger::Print("Your current security level is {}\n", level); + Logger::Print("Your security token is: {}\n", Utils::String::DumpHex(GuidToken.toString(), "")); + Logger::Print("Your computation token is: {}\n", Utils::String::DumpHex(ComputeToken.toString(), "")); - Toast::Show("cardicon_locked", "^5Security Level", Utils::String::VA("Your security level is %d", level), 3000); - } - else - { - const auto level = std::strtoul(params->get(1), nullptr, 10); - IncreaseSecurityLevel(level); - } - }); + Toast::Show("cardicon_locked", "^5Security Level", Utils::String::VA("Your security level is %d", level), 3000); + } + else + { + const auto level = std::strtoul(params->get(1), nullptr, 10); + IncreaseSecurityLevel(level); + } + }); } UIScript::Add("security_increase_cancel", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - TokenContainer.cancel = true; - Logger::Print("Token incrementation process canceled!\n"); - }); + { + TokenContainer.cancel = true; + Logger::Print("Token incrementation process canceled!\n"); + }); } Auth::~Auth() diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 237c3ef9..0123504e 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -463,7 +463,7 @@ namespace Components void Download::Reply(mg_connection* connection, const std::string& contentType, const std::string& data) { - const auto formatted = std::format("Content-Type: {}\r\n", contentType); + const auto formatted = std::format("Content-Type: {}\r\nAccess-Control-Allow-Origin: *\r\n", contentType); mg_http_reply(connection, 200, formatted.c_str(), "%s", data.c_str()); } diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 03aad46b..56d3e7b1 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -56,6 +56,9 @@ #include #pragma comment (lib, "dwmapi.lib") +#include +#pragma comment (lib, "iphlpapi.lib") + // Ignore the warnings #pragma warning(push) #pragma warning(disable: 4100) diff --git a/src/Utils/Cryptography.cpp b/src/Utils/Cryptography.cpp index d07525ff..cbfe3c82 100644 --- a/src/Utils/Cryptography.cpp +++ b/src/Utils/Cryptography.cpp @@ -8,10 +8,80 @@ namespace Utils { void Initialize() { - DES3::Initialize(); Rand::Initialize(); } + std::string GetEntropy() + { + DWORD volumeID; + if (GetVolumeInformationA("C:\\", + NULL, + NULL, + &volumeID, + NULL, + NULL, + NULL, + NULL + )) + { + // Drive info + return std::to_string(volumeID); + } + else + { + // Resort to mac address + unsigned long outBufLen = 0; + DWORD dwResult = GetAdaptersInfo(NULL, &outBufLen); + if (dwResult == ERROR_BUFFER_OVERFLOW) // This is what we're expecting + { + // Now allocate a structure of the required size. + PIP_ADAPTER_INFO pIpAdapterInfo = reinterpret_cast(malloc(outBufLen)); + dwResult = GetAdaptersInfo(pIpAdapterInfo, &outBufLen); + if (dwResult == ERROR_SUCCESS) + { + while (pIpAdapterInfo) + { + switch (pIpAdapterInfo->Type) + { + default: + pIpAdapterInfo = pIpAdapterInfo->Next; + continue; + + case IF_TYPE_IEEE80211: + case MIB_IF_TYPE_ETHERNET: + { + + std::string macAddress{}; + for (size_t i = 0; i < ARRAYSIZE(pIpAdapterInfo->Address); i++) + { + macAddress += std::to_string(pIpAdapterInfo->Address[i]); + } + + free(pIpAdapterInfo); + return macAddress; + } + } + } + } + else + { + // :( + // Continue to fallback + } + + // Free before going next because clearly this is not working + free(pIpAdapterInfo); + } + else + { + // No MAC, no C: drive? Come on + } + + // ultimate fallback + return std::to_string(Rand::GenerateInt()); + } + } + #pragma region Rand prng_state Rand::State; @@ -51,8 +121,29 @@ namespace Utils Key key; ltc_mp = ltm_desc; - register_prng(&sprng_desc); - ecc_make_key(nullptr, find_prng("sprng"), bits / 8, key.getKeyPtr()); + int descriptorIndex = register_prng(&chacha20_prng_desc); + + // allocate state + { + prng_state* state = new prng_state(); + + chacha20_prng_start(state); + + const auto entropy = Cryptography::GetEntropy(); + chacha20_prng_add_entropy(reinterpret_cast(entropy.data()), entropy.size(), state); + + chacha20_prng_ready(state); + + const auto result = ecc_make_key(state, descriptorIndex, bits / 8, key.getKeyPtr()); + + if (result != CRYPT_OK) + { + Components::Logger::PrintError(Game::conChannel_t::CON_CHANNEL_ERROR, "There was an issue generating your unique player ID! Please contact support"); + } + + // Deallocate state + delete state; + } return key; } @@ -79,8 +170,8 @@ namespace Utils int result = 0; return (ecc_verify_hash(reinterpret_cast(signature.data()), signature.size(), - reinterpret_cast(message.data()), message.size(), - &result, key.getKeyPtr()) == CRYPT_OK && result != 0); + reinterpret_cast(message.data()), message.size(), + &result, key.getKeyPtr()) == CRYPT_OK && result != 0); } #pragma endregion @@ -115,7 +206,7 @@ namespace Utils ltc_mp = ltm_desc; rsa_sign_hash_ex(reinterpret_cast(hash.data()), hash.size(), - buffer, &length, LTC_PKCS_1_V1_5, nullptr, 0, hash_index, 0, key.getKeyPtr()); + buffer, &length, LTC_PKCS_1_V1_5, nullptr, 0, hash_index, 0, key.getKeyPtr()); return std::string{ reinterpret_cast(buffer), length }; } @@ -133,47 +224,8 @@ namespace Utils auto result = 0; return (rsa_verify_hash_ex(reinterpret_cast(signature.data()), signature.size(), - reinterpret_cast(hash.data()), hash.size(), LTC_PKCS_1_V1_5, - hash_index, 0, &result, key.getKeyPtr()) == CRYPT_OK && result != 0); - } - -#pragma endregion - -#pragma region DES3 - - void DES3::Initialize() - { - register_cipher(&des3_desc); - } - - std::string DES3::Encrypt(const std::string& text, const std::string& iv, const std::string& key) - { - std::string encData; - encData.resize(text.size()); - - symmetric_CBC cbc; - const auto des3 = find_cipher("3des"); - - cbc_start(des3, reinterpret_cast(iv.data()), reinterpret_cast(key.data()), static_cast(key.size()), 0, &cbc); - cbc_encrypt(reinterpret_cast(text.data()), reinterpret_cast(encData.data()), text.size(), &cbc); - cbc_done(&cbc); - - return encData; - } - - std::string DES3::Decrpyt(const std::string& data, const std::string& iv, const std::string& key) - { - std::string decData; - decData.resize(data.size()); - - symmetric_CBC cbc; - const auto des3 = find_cipher("3des"); - - cbc_start(des3, reinterpret_cast(iv.data()), reinterpret_cast(key.data()), static_cast(key.size()), 0, &cbc); - cbc_decrypt(reinterpret_cast(data.data()), reinterpret_cast(decData.data()), data.size(), &cbc); - cbc_done(&cbc); - - return decData; + reinterpret_cast(hash.data()), hash.size(), LTC_PKCS_1_V1_5, + hash_index, 0, &result, key.getKeyPtr()) == CRYPT_OK && result != 0); } #pragma endregion diff --git a/src/Utils/Cryptography.hpp b/src/Utils/Cryptography.hpp index 46ac5e14..36fbdd28 100644 --- a/src/Utils/Cryptography.hpp +++ b/src/Utils/Cryptography.hpp @@ -5,6 +5,7 @@ namespace Utils namespace Cryptography { void Initialize(); + std::string GetEntropy(); class Token { @@ -327,14 +328,6 @@ namespace Utils static bool VerifyMessage(Key key, const std::string& message, const std::string& signature); }; - class DES3 - { - public: - static void Initialize(); - static std::string Encrypt(const std::string& text, const std::string& iv, const std::string& key); - static std::string Decrpyt(const std::string& text, const std::string& iv, const std::string& key); - }; - class Tiger { public: From a1b024ba679e8beb9430dabd0edb6666e6588853 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 12:33:53 +0100 Subject: [PATCH 41/71] Filter CSV while building zonebuilder zone --- src/Components/Modules/ZoneBuilder.cpp | 158 ++++++++++++++++++------- src/Components/Modules/ZoneBuilder.hpp | 24 +++- 2 files changed, 135 insertions(+), 47 deletions(-) diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 753ea594..1ede93dc 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -14,7 +14,7 @@ namespace Components { std::string ZoneBuilder::TraceZone; - std::vector> ZoneBuilder::TraceAssets; + std::vector ZoneBuilder::TraceAssets; bool ZoneBuilder::MainThreadInterrupted; DWORD ZoneBuilder::InterruptingThreadId; @@ -733,11 +733,11 @@ namespace Components ZoneBuilder::TraceZone = zone; } - std::vector> ZoneBuilder::EndAssetTrace() + std::vector ZoneBuilder::EndAssetTrace() { ZoneBuilder::TraceZone.clear(); - std::vector> AssetTrace; + std::vector AssetTrace; Utils::Merge(&AssetTrace, ZoneBuilder::TraceAssets); ZoneBuilder::TraceAssets.clear(); @@ -1164,20 +1164,15 @@ namespace Components ZoneBuilder::DumpingZone = zone; ZoneBuilder::RefreshExporterWorkDirectory(); - std::vector> assets{}; + std::vector assets{}; const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); Logger::Print("Dumping zone '{}'...\n", zone); { Utils::IO::CreateDir(GetDumpingZonePath()); - std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); - csv - << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) - << "\n\n"; // Order the CSV around - // TODO: Trim asset list using IW4OF dependencies constexpr Game::XAssetType typeOrder[] = { Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, @@ -1197,11 +1192,13 @@ namespace Components Game::XAssetType::ASSET_TYPE_PIXELSHADER, Game::XAssetType::ASSET_TYPE_VERTEXSHADER, Game::XAssetType::ASSET_TYPE_VERTEXDECL, + Game::XAssetType::ASSET_TYPE_LIGHT_DEF, Game::XAssetType::ASSET_TYPE_IMAGE, Game::XAssetType::ASSET_TYPE_SOUND, Game::XAssetType::ASSET_TYPE_LOADED_SOUND, Game::XAssetType::ASSET_TYPE_SOUND_CURVE, Game::XAssetType::ASSET_TYPE_PHYSPRESET, + Game::XAssetType::ASSET_TYPE_LOCALIZE_ENTRY, }; std::unordered_map typePriority{}; @@ -1211,25 +1208,35 @@ namespace Components typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); } - std::map> invalidAssets{}; + enum AssetCategory + { + EXPLICIT, + IMPLICIT, + NOT_SUPPORTED, + DISAPPEARED, + + COUNT + }; + + std::vector assetsToList[AssetCategory::COUNT]{}; std::sort(assets.begin(), assets.end(), [&]( - const std::pair& a, - const std::pair& b + const NamedAsset& a, + const NamedAsset& b ) { - if (a.first == b.first) + if (a.type == b.type) { - return a.second.compare(b.second) < 0; + return a.name.compare(b.name) < 0; } else { - const auto priorityA = typePriority[a.first]; - const auto priorityB = typePriority[b.first]; + const auto priorityA = typePriority[a.type]; + const auto priorityB = typePriority[b.type]; if (priorityA == priorityB) { - return a.second.compare(b.second) < 0; + return a.name.compare(b.name) < 0; } else { @@ -1238,47 +1245,112 @@ namespace Components } }); - // Used to format the CSV - Game::XAssetType lastTypeEncountered{}; + std::unordered_set assetsToSkip{}; + const std::function)> recursivelyAddChildren = + [&](std::unordered_set children) + { + for (const auto& k : children) + { + const auto type = static_cast(k.type); + Game::XAsset asset = { type, {k.data} }; + const auto insertionInfo = assetsToSkip.insert({ type, Game::DB_GetXAssetName(&asset) }); + if (insertionInfo.second) + { + // True means it was inserted, so we might need to check children aswell + if (ExporterAPI.is_type_supported(k.type)) + { + const auto deps = ExporterAPI.get_children(k.type, k.data); + recursivelyAddChildren(deps); + } + } + } + }; + + Logger::Print("Filtering asset list for '{}.csv'...\n", zone); + std::vector explicitAssetsToFilter{}; + + // Filter implicit and assets that don't need dumping for (const auto& asset : assets) { - const auto type = asset.first; - const auto name = asset.second; + const auto type = asset.type; + const auto& name = asset.name; + + if (assetsToSkip.contains({ type, name.data() })) + { + assetsToList[AssetCategory::IMPLICIT].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + continue; + } + if (ExporterAPI.is_type_supported(type) && name[0] != ',') { const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); if (assetHeader.data) { - ExporterAPI.write(type, assetHeader.data); - const auto typeName = Game::DB_GetXAssetTypeName(type); + const auto children = ExporterAPI.get_children(type, assetHeader.data); - if (type != lastTypeEncountered) - { - csv << "\n### " << typeName << "\n"; - lastTypeEncountered = type; - } - - csv << typeName << "," << name << "\n"; + recursivelyAddChildren(children); + explicitAssetsToFilter.push_back({ type, assetHeader }); } else { Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); - invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + assetsToList[AssetCategory::DISAPPEARED].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); } } else { - invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + assetsToList[AssetCategory::NOT_SUPPORTED].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); } } - for (const auto& kv : invalidAssets) + // Write explicit assets + Logger::Print("Dumping remaining assets...\n"); + for (auto& asset : explicitAssetsToFilter) { - csv << "\n### " << kv.first << "\n"; - for (const auto& line : kv.second) + const auto& name = Game::DB_GetXAssetName(&asset); + if (!assetsToSkip.contains({ asset.type, name })) { - csv << "#" << line << "\n"; + ExporterAPI.write(asset.type, asset.header.data); + Logger::Print("."); + assetsToList[AssetCategory::EXPLICIT].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(asset.type), name)); + } + } + + Logger::Print("\n"); + + // Write zone source + Logger::Print("Writing zone source...\n"); + std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); + csv + << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) + << "\n\n"; + + for (size_t i = 0; i < AssetCategory::COUNT; i++) + { + if (assetsToList[i].size() == 0) + { + continue; + } + + switch (static_cast(i)) + { + case AssetCategory::EXPLICIT: + break; + case AssetCategory::IMPLICIT: + csv << "\n### The following assets are implicitly built with previous assets:\n"; + break; + case AssetCategory::NOT_SUPPORTED: + csv << "\n### The following assets are not supported for dumping:\n"; + break; + case AssetCategory::DISAPPEARED: + csv << "\n### The following assets disappeared while dumping but are mentioned in the zone:\n"; + break; + } + + for (const auto& asset : assetsToList[i]) + { + csv << (i == AssetCategory::EXPLICIT ? "" : "#") << asset << "\n"; } } @@ -1292,7 +1364,7 @@ namespace Components } - std::function ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector>& assets) + std::function ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector& assets) { ZoneBuilder::BeginAssetTrace(zone); @@ -1433,7 +1505,7 @@ namespace Components { if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) { - ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); + ZoneBuilder::TraceAssets.push_back({ type, name }); } }); @@ -1451,13 +1523,13 @@ namespace Components if (params->size() < 2) return; std::string zone = params->get(1); - std::vector> assets{}; + std::vector assets{}; const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); int count = 0; for (auto i = assets.begin(); i != assets.end(); ++i, ++count) { - Logger::Print(" {}: {}: {}\n", count, Game::DB_GetXAssetTypeName(i->first), i->second); + Logger::Print(" {}: {}: {}\n", count, Game::DB_GetXAssetTypeName(i->type), i->name); } Logger::Print("\n"); @@ -1547,8 +1619,8 @@ namespace Components #endif } } - } - }); + } + }); Command::Add("listassets", [](const Command::Params* params) { @@ -1564,8 +1636,8 @@ namespace Components }, &type, false); } }); - } } +} ZoneBuilder::~ZoneBuilder() { diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index ad4a049b..dfa35924 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -32,7 +32,7 @@ namespace Components private: Zone* builder; }; - + Zone(const std::string& zoneName, const std::string& sourceName, const std::string& destination); Zone(const std::string& zoneName); ~Zone(); @@ -123,6 +123,22 @@ namespace Components size_t assetDepth; }; + struct NamedAsset + { + Game::XAssetType type; + std::string name; + + bool operator==(const NamedAsset& other) const { + return type == other.type && name == other.name; + }; + + struct Hash { + size_t operator()(const NamedAsset& k) const { + return static_cast(k.type) ^ std::hash{}(k.name); + } + }; + }; + ZoneBuilder(); ~ZoneBuilder(); @@ -130,10 +146,10 @@ namespace Components static bool IsDumpingZone() { return DumpingZone.length() > 0; }; static std::string TraceZone; - static std::vector> TraceAssets; + static std::vector TraceAssets; static void BeginAssetTrace(const std::string& zone); - static std::vector> EndAssetTrace(); + static std::vector EndAssetTrace(); static Dvar::Var zb_sp_to_mp; @@ -164,7 +180,7 @@ namespace Components static void DumpZone(const std::string& zone); - static std::function LoadZoneWithTrace(const std::string& zone, OUT std::vector> &assets); + static std::function LoadZoneWithTrace(const std::string& zone, OUT std::vector& assets); static void Com_Quitf_t(); From ec6a4af6e4867e404b7b796e2bcca64fe6a69a46 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 12:36:44 +0100 Subject: [PATCH 42/71] Bump IW4OF --- deps/iw4-open-formats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/iw4-open-formats b/deps/iw4-open-formats index 4f7041b9..9c245e79 160000 --- a/deps/iw4-open-formats +++ b/deps/iw4-open-formats @@ -1 +1 @@ -Subproject commit 4f7041b9f259d09bc6ca06c3bc182a66c961f2e4 +Subproject commit 9c245e794c7c452de82aa9fd07e6b3edb27be9b7 From e9c6d79a3854ca2e95f5fdc615451a75bd3a8035 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 14:31:20 +0100 Subject: [PATCH 43/71] Remove unused very old debugging stuff --- src/Components/Modules/FastFiles.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Components/Modules/FastFiles.cpp b/src/Components/Modules/FastFiles.cpp index 0e7e6dc8..a1a116ad 100644 --- a/src/Components/Modules/FastFiles.cpp +++ b/src/Components/Modules/FastFiles.cpp @@ -689,17 +689,5 @@ namespace Components Logger::Print("Waiting for database...\n"); while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(100ms); }); - -#ifdef _DEBUG - // ZoneBuilder debugging - Utils::IO::WriteFile("userraw/logs/iw4_reads.log", "", false); - Utils::Hook(0x4A8FA0, FastFiles::LogStreamRead, HOOK_JUMP).install()->quick(); - Utils::Hook(0x4BCB62, [] - { - FastFiles::StreamRead = true; - Utils::Hook::Call(0x4B8DB0)(true); // currently set to Load_GfxWorld - FastFiles::StreamRead = false; - }, HOOK_CALL).install()/*->quick()*/; -#endif } } From 3162fceefd47123d0531bed834aaaa10271dde28 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 15:34:00 +0100 Subject: [PATCH 44/71] Fix security level 1 breaking connection process --- src/Components/Modules/Auth.cpp | 34 +++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index c3642b70..f9b50dda 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -128,6 +128,13 @@ namespace Components Game::SV_Cmd_EndTokenizedString(); + if (GuidToken.toString().empty()) + { + Game::SV_Cmd_EndTokenizedString(); + Logger::Error(Game::ERR_SERVERDISCONNECT, "Connecting failed: Empty GUID token!"); + return; + } + Proto::Auth::Connect connectData; connectData.set_token(GuidToken.toString()); connectData.set_publickey(GuidKey.getPublicKey()); @@ -343,8 +350,6 @@ namespace Components void Auth::StoreKey() { - // We write the key as a decoy I suppose - it's really no longer needed - // TODO Remove this part if (!Dedicated::IsEnabled() && !ZoneBuilder::IsEnabled() && GuidKey.isValid()) { Proto::Auth::Certificate cert; @@ -377,7 +382,6 @@ namespace Components // so for now we're doing something else: the key is generated uniquely from the machine's characteristics // It is not (necessarily) stored and therefore, not loaded, so it could make it harder to evade bans without // using a custom client that would need regeneration at each update. -#if false Proto::Auth::Certificate cert; if (cert.ParseFromString(::Utils::IO::ReadFile("players/guid.dat"))) { @@ -390,14 +394,28 @@ namespace Components GuidKey.free(); } - if (!GuidKey.isValid()) -#endif + if (GuidKey.isValid()) + { + auto machineKey = Utils::Cryptography::ECC::GenerateKey(512); + if (GetKeyHash(machineKey.getPublicKey()) == GetKeyHash()) + { + //All good, nothing to do + } + else + { + // kill! The user has changed machine or copied files from another + Auth::GenerateKey(); + } + } + else + { Auth::GenerateKey(); -} + } + } uint32_t Auth::GetSecurityLevel() { - return GetZeroBits(GuidToken, GuidKey.getPublicKey()); + return GuidToken.toString().empty() ? 0 : GetZeroBits(GuidToken, GuidKey.getPublicKey()); } void Auth::IncreaseSecurityLevel(uint32_t level, const std::string& command) @@ -470,7 +488,7 @@ namespace Components } // Check if we already have the desired security level - uint32_t lastLevel = GetZeroBits(token, publicKey); + uint32_t lastLevel = token.toString().empty() ? 0 : GetZeroBits(token, publicKey); uint32_t level = lastLevel; if (level >= zeroBits) return; From 8a9fe6d78f8f65eff7c354d1ae5d825671e3d290 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 16:00:26 +0100 Subject: [PATCH 45/71] sneaky --- src/Components/Modules/ServerList.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index bcef4c6f..d069decf 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -623,7 +623,22 @@ namespace Components std::hash hashFn; server.hash = hashFn(server); +#if false + // more secure server.hostname = TextRenderer::StripMaterialTextIcons(server.hostname); +#else + // more fun ! + constexpr auto MAX_SERVER_NAME_LENGTH = 48; + server.hostname = server.hostname.substr(0, MAX_SERVER_NAME_LENGTH); + +#endif + + if (server.hostname.empty() || TextRenderer::StripMaterialTextIcons(server.hostname).empty()) + { + // Invalid server name containing only emojis + return; + } + server.mapname = TextRenderer::StripMaterialTextIcons(server.mapname); server.gametype = TextRenderer::StripMaterialTextIcons(server.gametype); server.mod = TextRenderer::StripMaterialTextIcons(server.mod); From 5bc79bac59a5a2578fcd2dca54db88a9d716eb62 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 16:04:46 +0100 Subject: [PATCH 46/71] Strip servers with empty name --- src/Components/Modules/ServerList.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index d069decf..f3fc8f8b 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -632,8 +632,8 @@ namespace Components server.hostname = server.hostname.substr(0, MAX_SERVER_NAME_LENGTH); #endif - - if (server.hostname.empty() || TextRenderer::StripMaterialTextIcons(server.hostname).empty()) + const auto strippedHostName = TextRenderer::StripMaterialTextIcons(server.hostname); + if (server.hostname.empty() || strippedHostName.empty() || std::all_of(strippedHostName.begin(), strippedHostName.end(), isspace)) { // Invalid server name containing only emojis return; From 14b52dbb51fd5bfdf3e9b7baa73e462ada284012 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 16:05:03 +0100 Subject: [PATCH 47/71] Reformat server list --- src/Components/Modules/ServerList.cpp | 216 +++++++++++++------------- 1 file changed, 108 insertions(+), 108 deletions(-) diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index f3fc8f8b..aa673a95 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -270,7 +270,7 @@ namespace Components if ((ui_browserMod == 0 && static_cast(serverInfo->mod.size())) || (ui_browserMod == 1 && serverInfo->mod.empty())) continue; // Filter by gametype - if (ui_joinGametype > 0 && (ui_joinGametype - 1) < *Game::gameTypeCount && Game::gameTypes[(ui_joinGametype - 1)].gameType != serverInfo->gametype) continue; + if (ui_joinGametype > 0 && (ui_joinGametype - 1) < *Game::gameTypeCount && Game::gameTypes[(ui_joinGametype - 1)].gameType != serverInfo->gametype) continue; VisibleList.push_back(i); } @@ -410,7 +410,7 @@ namespace Components void ServerList::StoreFavourite(const std::string& server) { std::vector servers; - + const auto parseData = Utils::IO::ReadFile(FavouriteFile); if (!parseData.empty()) { @@ -493,7 +493,7 @@ namespace Components auto* list = GetList(); if (list) list->clear(); - + RefreshVisibleListInternal(UIScript::Token(), nullptr); } @@ -548,7 +548,7 @@ namespace Components container.target = address; auto alreadyInserted = false; - for (auto &server : RefreshContainer.servers) + for (auto& server : RefreshContainer.servers) { if (server.target == container.target) { @@ -750,36 +750,36 @@ namespace Components if (!IsServerListOpen()) return; std::ranges::stable_sort(VisibleList, [](const unsigned int& server1, const unsigned int& server2) -> bool - { - ServerInfo* info1 = nullptr; - ServerInfo* info2 = nullptr; - - auto* list = GetList(); - if (!list) return false; - - if (list->size() > server1) info1 = &(*list)[server1]; - if (list->size() > server2) info2 = &(*list)[server2]; - - if (!info1) return false; - if (!info2) return false; - - // Numerical comparisons - if (SortKey == static_cast>(Column::Ping)) { - return info1->ping < info2->ping; - } + ServerInfo* info1 = nullptr; + ServerInfo* info2 = nullptr; - if (SortKey == static_cast>(Column::Players)) - { - return info1->clients < info2->clients; - } + auto* list = GetList(); + if (!list) return false; - auto text1 = Utils::String::ToLower(TextRenderer::StripColors(GetServerInfoText(info1, SortKey, true))); - auto text2 = Utils::String::ToLower(TextRenderer::StripColors(GetServerInfoText(info2, SortKey, true))); + if (list->size() > server1) info1 = &(*list)[server1]; + if (list->size() > server2) info2 = &(*list)[server2]; - // ASCII-based comparison - return text1.compare(text2) < 0; - }); + if (!info1) return false; + if (!info2) return false; + + // Numerical comparisons + if (SortKey == static_cast>(Column::Ping)) + { + return info1->ping < info2->ping; + } + + if (SortKey == static_cast>(Column::Players)) + { + return info1->clients < info2->clients; + } + + auto text1 = Utils::String::ToLower(TextRenderer::StripColors(GetServerInfoText(info1, SortKey, true))); + auto text2 = Utils::String::ToLower(TextRenderer::StripColors(GetServerInfoText(info2, SortKey, true))); + + // ASCII-based comparison + return text1.compare(text2) < 0; + }); if (!SortAsc) std::ranges::reverse(VisibleList); } @@ -938,17 +938,17 @@ namespace Components VisibleList.clear(); Events::OnDvarInit([] - { - UIServerSelected = Dvar::Register("ui_serverSelected", false, + { + UIServerSelected = Dvar::Register("ui_serverSelected", false, Game::DVAR_NONE, "Whether a server has been selected in the serverlist"); - UIServerSelectedMap = Dvar::Register("ui_serverSelectedMap", "mp_afghan", - Game::DVAR_NONE, "Map of the selected server"); + UIServerSelectedMap = Dvar::Register("ui_serverSelectedMap", "mp_afghan", + Game::DVAR_NONE, "Map of the selected server"); - NETServerQueryLimit = Dvar::Register("net_serverQueryLimit", 1, - 1, 10, Dedicated::IsEnabled() ? Game::DVAR_NONE : Game::DVAR_ARCHIVE, "Amount of server queries per frame"); - NETServerFrames = Dvar::Register("net_serverFrames", 30, - 1, 60, Dedicated::IsEnabled() ? Game::DVAR_NONE : Game::DVAR_ARCHIVE, "Amount of server query frames per second"); - }); + NETServerQueryLimit = Dvar::Register("net_serverQueryLimit", 1, + 1, 10, Dedicated::IsEnabled() ? Game::DVAR_NONE : Game::DVAR_ARCHIVE, "Amount of server queries per frame"); + NETServerFrames = Dvar::Register("net_serverFrames", 30, + 1, 60, Dedicated::IsEnabled() ? Game::DVAR_NONE : Game::DVAR_ARCHIVE, "Amount of server query frames per second"); + }); // Fix ui_netsource dvar Utils::Hook::Nop(0x4CDEEC, 5); // Don't reset the netsource when gametypes aren't loaded @@ -956,35 +956,35 @@ namespace Components Localization::Set("MPUI_SERVERQUERIED", "Servers: 0\nPlayers: 0 (0)"); Network::OnClientPacket("getServersResponse", [](const Network::Address& address, [[maybe_unused]] const std::string& data) - { - if (RefreshContainer.host != address) return; // Only parse from host we sent to - - RefreshContainer.awatingList = false; - - std::lock_guard _(RefreshContainer.mutex); - - auto offset = 0; - const auto count = RefreshContainer.servers.size(); - MasterEntry* entry; - - // Find first entry - do { - entry = reinterpret_cast(const_cast(data.data()) + offset++); - } while (!entry->HasSeparator() && !entry->IsEndToken()); + if (RefreshContainer.host != address) return; // Only parse from host we sent to - for (int i = 0; !entry[i].IsEndToken() && entry[i].HasSeparator(); ++i) - { - Network::Address serverAddr = address; - serverAddr.setIP(entry[i].ip); - serverAddr.setPort(ntohs(entry[i].port)); - serverAddr.setType(Game::NA_IP); + RefreshContainer.awatingList = false; - InsertRequest(serverAddr); - } + std::lock_guard _(RefreshContainer.mutex); - Logger::Print("Parsed {} servers from master\n", RefreshContainer.servers.size() - count); - }); + auto offset = 0; + const auto count = RefreshContainer.servers.size(); + MasterEntry* entry; + + // Find first entry + do + { + entry = reinterpret_cast(const_cast(data.data()) + offset++); + } while (!entry->HasSeparator() && !entry->IsEndToken()); + + for (int i = 0; !entry[i].IsEndToken() && entry[i].HasSeparator(); ++i) + { + Network::Address serverAddr = address; + serverAddr.setIP(entry[i].ip); + serverAddr.setPort(ntohs(entry[i].port)); + serverAddr.setType(Game::NA_IP); + + InsertRequest(serverAddr); + } + + Logger::Print("Parsed {} servers from master\n", RefreshContainer.servers.size() - count); + }); // Set default masterServerName + port and save it Utils::Hook::Set(0x60AD92, "server.alterware.dev"); @@ -1001,69 +1001,69 @@ namespace Components UIScript::Add("RefreshServers", Refresh); UIScript::Add("JoinServer", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - auto* serverInfo = GetServer(CurrentServer); - if (serverInfo) { - Party::Connect(serverInfo->addr); - } - }); + auto* serverInfo = GetServer(CurrentServer); + if (serverInfo) + { + Party::Connect(serverInfo->addr); + } + }); UIScript::Add("ServerSort", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - const auto key = token.get(); - if (SortKey == key) { - SortAsc = !SortAsc; - } - else - { - SortKey = key; - SortAsc = true; - } + const auto key = token.get(); + if (SortKey == key) + { + SortAsc = !SortAsc; + } + else + { + SortKey = key; + SortAsc = true; + } - Logger::Print("Sorting server list by token: {}\n", SortKey); - SortList(); - }); + Logger::Print("Sorting server list by token: {}\n", SortKey); + SortList(); + }); UIScript::Add("CreateListFavorite", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - auto* serverInfo = GetCurrentServer(); - if (info && serverInfo && serverInfo->addr.isValid()) { - StoreFavourite(serverInfo->addr.getString()); - } - }); + auto* serverInfo = GetCurrentServer(); + if (info && serverInfo && serverInfo->addr.isValid()) + { + StoreFavourite(serverInfo->addr.getString()); + } + }); UIScript::Add("CreateFavorite", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - const auto value = Dvar::Var("ui_favoriteAddress").get(); - if (!value.empty()) { - StoreFavourite(value); - } - }); + const auto value = Dvar::Var("ui_favoriteAddress").get(); + if (!value.empty()) + { + StoreFavourite(value); + } + }); UIScript::Add("CreateCurrentServerFavorite", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - if (Game::CL_IsCgameInitialized()) { - const auto addressText = Network::Address(*Game::connectedHost).getString(); - if (addressText != "0.0.0.0:0"s && addressText != "loopback"s) + if (Game::CL_IsCgameInitialized()) { - StoreFavourite(addressText); + const auto addressText = Network::Address(*Game::connectedHost).getString(); + if (addressText != "0.0.0.0:0"s && addressText != "loopback"s) + { + StoreFavourite(addressText); + } } - } - }); + }); UIScript::Add("DeleteFavorite", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) - { - auto* serverInfo = GetCurrentServer(); - if (serverInfo) { - RemoveFavourite(serverInfo->addr.getString()); - } - }); + auto* serverInfo = GetCurrentServer(); + if (serverInfo) + { + RemoveFavourite(serverInfo->addr.getString()); + } + }); // Add required ownerDraws UIScript::AddOwnerDraw(220, UpdateSource); From 0218ff618c52d37602a94fc59fc042ff16656c6a Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 17:11:23 +0100 Subject: [PATCH 48/71] Fix a typo from 2000 BC --- src/Components/Modules/ServerList.cpp | 9 +-------- src/Components/Modules/TextRenderer.cpp | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index aa673a95..afc64295 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -623,17 +623,10 @@ namespace Components std::hash hashFn; server.hash = hashFn(server); -#if false // more secure server.hostname = TextRenderer::StripMaterialTextIcons(server.hostname); -#else - // more fun ! - constexpr auto MAX_SERVER_NAME_LENGTH = 48; - server.hostname = server.hostname.substr(0, MAX_SERVER_NAME_LENGTH); -#endif - const auto strippedHostName = TextRenderer::StripMaterialTextIcons(server.hostname); - if (server.hostname.empty() || strippedHostName.empty() || std::all_of(strippedHostName.begin(), strippedHostName.end(), isspace)) + if (server.hostname.empty() || std::all_of(server.hostname.begin(), server.hostname.end(), isspace)) { // Invalid server name containing only emojis return; diff --git a/src/Components/Modules/TextRenderer.cpp b/src/Components/Modules/TextRenderer.cpp index 91d9ff52..8eb0fe05 100644 --- a/src/Components/Modules/TextRenderer.cpp +++ b/src/Components/Modules/TextRenderer.cpp @@ -1414,7 +1414,7 @@ namespace Components std::string TextRenderer::StripMaterialTextIcons(const std::string& in) { char buffer[1000]{}; // Should be more than enough - StripAllTextIcons(in.data(), buffer, sizeof(buffer)); + StripMaterialTextIcons(in.data(), buffer, sizeof(buffer)); return std::string{ buffer }; } From 229861cde9bb1b66914e1071ab1e90cd203e97ad Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 3 Feb 2024 17:56:56 +0100 Subject: [PATCH 49/71] Do not check GUID in loopback private match --- src/Components/Modules/Auth.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index f9b50dda..97e8e3b4 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -128,7 +128,7 @@ namespace Components Game::SV_Cmd_EndTokenizedString(); - if (GuidToken.toString().empty()) + if (GuidToken.toString().empty() && adr.type != Game::NA_LOOPBACK) { Game::SV_Cmd_EndTokenizedString(); Logger::Error(Game::ERR_SERVERDISCONNECT, "Connecting failed: Empty GUID token!"); From 4ff13373589f961d25ba485b1c582fa4a1671925 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 4 Feb 2024 19:13:47 +0100 Subject: [PATCH 50/71] New GUID generation & migration --- src/Components/Modules/Auth.cpp | 125 +++++++++++++++++++++----- src/Components/Modules/Auth.hpp | 2 + src/Components/Modules/FileSystem.cpp | 2 +- src/Utils/Cryptography.cpp | 96 +++++--------------- src/Utils/Cryptography.hpp | 3 +- 5 files changed, 130 insertions(+), 98 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 97e8e3b4..08931f79 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -20,10 +20,12 @@ namespace Components std::vector Auth::BannedUids = { // No longer necessary - /* 0xf4d2c30b712ac6e3, + 0xf4d2c30b712ac6e3, 0xf7e33c4081337fa3, 0x6f5597f103cc50e9, - 0xecd542eee54ffccf,*/ + 0xecd542eee54ffccf, + 0xA46B84C54694FD5B, + 0xECD542EEE54FFCCF, }; bool Auth::HasAccessToReservedSlot; @@ -365,7 +367,7 @@ namespace Components { GuidToken.clear(); ComputeToken.clear(); - GuidKey = Utils::Cryptography::ECC::GenerateKey(512); + GuidKey = Utils::Cryptography::ECC::GenerateKey(512, GetMachineEntropy()); StoreKey(); } @@ -374,21 +376,32 @@ namespace Components if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled()) return; if (!force && GuidKey.isValid()) return; - // We no longer read the key from disk - // While having obvious advantages to palliate the fact that some users are not playing on Steam, - // it is creating a lot of issues because GUID files get packaged with the game when people share it - // and it makes it harder for server owners to identify players uniquely - // Note that we could store it in Appdata, but then it would be dissociated from the rest of player files, - // so for now we're doing something else: the key is generated uniquely from the machine's characteristics - // It is not (necessarily) stored and therefore, not loaded, so it could make it harder to evade bans without - // using a custom client that would need regeneration at each update. + const auto appdata = Components::FileSystem::GetAppdataPath(); + Utils::IO::CreateDir(appdata.string()); + + const auto guidPath = appdata / "guid.dat"; + +#ifndef REGENERATE_INVALID_KEY + // Migrate old file + const auto oldGuidPath = "players/guid.dat"; + if (Utils::IO::FileExists(oldGuidPath)) + { + if (MoveFileA(oldGuidPath, guidPath.string().data())) + { + Utils::IO::RemoveFile(oldGuidPath); + } + } +#endif + + const auto guidFile = Utils::IO::ReadFile(guidPath.string()); + Proto::Auth::Certificate cert; - if (cert.ParseFromString(::Utils::IO::ReadFile("players/guid.dat"))) + if (cert.ParseFromString(guidFile)) { GuidKey.deserialize(cert.privatekey()); GuidToken = cert.token(); ComputeToken = cert.ctoken(); - } + } else { GuidKey.free(); @@ -396,16 +409,15 @@ namespace Components if (GuidKey.isValid()) { - auto machineKey = Utils::Cryptography::ECC::GenerateKey(512); - if (GetKeyHash(machineKey.getPublicKey()) == GetKeyHash()) +#ifdef REGENERATE_INVALID_KEY + auto machineKey = Utils::Cryptography::ECC::GenerateKey(512, GetMachineEntropy()); + if (GetKeyHash(machineKey.getPublicKey()) != GetKeyHash()) { - //All good, nothing to do - } - else - { - // kill! The user has changed machine or copied files from another - Auth::GenerateKey(); + // kill! The user has changed machine or copied files from another + Auth::GenerateKey(); } +#endif + //All good, nothing to do } else { @@ -512,6 +524,77 @@ namespace Components token = computeToken; } + // A somewhat hardware tied 48 bit value + std::string Auth::GetMachineEntropy() + { + std::string entropy{}; + DWORD volumeID; + if (GetVolumeInformationA("C:\\", + NULL, + NULL, + &volumeID, + NULL, + NULL, + NULL, + NULL + )) + { + // Drive info + entropy += std::to_string(volumeID); + } + + // MAC Address + { + unsigned long outBufLen = 0; + DWORD dwResult = GetAdaptersInfo(NULL, &outBufLen); + if (dwResult == ERROR_BUFFER_OVERFLOW) // This is what we're expecting + { + // Now allocate a structure of the required size. + PIP_ADAPTER_INFO pIpAdapterInfo = reinterpret_cast(malloc(outBufLen)); + dwResult = GetAdaptersInfo(pIpAdapterInfo, &outBufLen); + if (dwResult == ERROR_SUCCESS) + { + while (pIpAdapterInfo) + { + switch (pIpAdapterInfo->Type) + { + default: + pIpAdapterInfo = pIpAdapterInfo->Next; + continue; + + case IF_TYPE_IEEE80211: + case MIB_IF_TYPE_ETHERNET: + { + + std::string macAddress{}; + for (size_t i = 0; i < ARRAYSIZE(pIpAdapterInfo->Address); i++) + { + entropy += std::to_string(pIpAdapterInfo->Address[i]); + } + + break; + } + } + } + } + + // Free before going next because clearly this is not working + free(pIpAdapterInfo); + } + + } + + if (entropy.empty()) + { + // ultimate fallback + return std::to_string(Utils::Cryptography::Rand::GenerateLong()); + } + else + { + return entropy; + } + } + Auth::Auth() { TokenContainer.cancel = false; diff --git a/src/Components/Modules/Auth.hpp b/src/Components/Modules/Auth.hpp index 5ab438ce..4644281c 100644 --- a/src/Components/Modules/Auth.hpp +++ b/src/Components/Modules/Auth.hpp @@ -24,6 +24,8 @@ namespace Components static uint32_t GetZeroBits(Utils::Cryptography::Token token, const std::string& publicKey); static void IncrementToken(Utils::Cryptography::Token& token, Utils::Cryptography::Token& computeToken, const std::string& publicKey, uint32_t zeroBits, bool* cancel = nullptr, uint64_t* count = nullptr); + static std::string GetMachineEntropy(); + private: class TokenIncrementing diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index eae77d1a..3880a6d2 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -153,7 +153,7 @@ namespace Components CoTaskMemFree(path); }); - return std::filesystem::path(path) / "xlabs"; + return std::filesystem::path(path) / "iw4x"; } std::vector FileSystem::GetFileList(const std::string& path, const std::string& extension) diff --git a/src/Utils/Cryptography.cpp b/src/Utils/Cryptography.cpp index cbfe3c82..06cbe1e9 100644 --- a/src/Utils/Cryptography.cpp +++ b/src/Utils/Cryptography.cpp @@ -11,77 +11,6 @@ namespace Utils Rand::Initialize(); } - std::string GetEntropy() - { - DWORD volumeID; - if (GetVolumeInformationA("C:\\", - NULL, - NULL, - &volumeID, - NULL, - NULL, - NULL, - NULL - )) - { - // Drive info - return std::to_string(volumeID); - } - else - { - // Resort to mac address - unsigned long outBufLen = 0; - DWORD dwResult = GetAdaptersInfo(NULL, &outBufLen); - if (dwResult == ERROR_BUFFER_OVERFLOW) // This is what we're expecting - { - // Now allocate a structure of the required size. - PIP_ADAPTER_INFO pIpAdapterInfo = reinterpret_cast(malloc(outBufLen)); - dwResult = GetAdaptersInfo(pIpAdapterInfo, &outBufLen); - if (dwResult == ERROR_SUCCESS) - { - while (pIpAdapterInfo) - { - switch (pIpAdapterInfo->Type) - { - default: - pIpAdapterInfo = pIpAdapterInfo->Next; - continue; - - case IF_TYPE_IEEE80211: - case MIB_IF_TYPE_ETHERNET: - { - - std::string macAddress{}; - for (size_t i = 0; i < ARRAYSIZE(pIpAdapterInfo->Address); i++) - { - macAddress += std::to_string(pIpAdapterInfo->Address[i]); - } - - free(pIpAdapterInfo); - return macAddress; - } - } - } - } - else - { - // :( - // Continue to fallback - } - - // Free before going next because clearly this is not working - free(pIpAdapterInfo); - } - else - { - // No MAC, no C: drive? Come on - } - - // ultimate fallback - return std::to_string(Rand::GenerateInt()); - } - } - #pragma region Rand prng_state Rand::State; @@ -98,6 +27,13 @@ namespace Utils return std::string{ buffer, static_cast(pos) }; } + std::uint64_t Rand::GenerateLong() + { + std::uint64_t number = 0; + fortuna_read(reinterpret_cast(&number), sizeof(number), &Rand::State); + return number; + } + std::uint32_t Rand::GenerateInt() { std::uint32_t number = 0; @@ -116,20 +52,30 @@ namespace Utils #pragma region ECC - ECC::Key ECC::GenerateKey(int bits) + ECC::Key ECC::GenerateKey(int bits, const std::string& entropy) { Key key; ltc_mp = ltm_desc; - int descriptorIndex = register_prng(&chacha20_prng_desc); - // allocate state + if (entropy.empty()) { + register_prng(&sprng_desc); + const auto result = ecc_make_key(nullptr, find_prng("sprng"), bits / 8, key.getKeyPtr()); + if (result != CRYPT_OK) + { + Components::Logger::PrintError(Game::conChannel_t::CON_CHANNEL_ERROR, "There was an issue generating a secured random key! Please contact support"); + } + } + else + { + int descriptorIndex = register_prng(&chacha20_prng_desc); + + // allocate state prng_state* state = new prng_state(); chacha20_prng_start(state); - const auto entropy = Cryptography::GetEntropy(); chacha20_prng_add_entropy(reinterpret_cast(entropy.data()), entropy.size(), state); chacha20_prng_ready(state); diff --git a/src/Utils/Cryptography.hpp b/src/Utils/Cryptography.hpp index 36fbdd28..67734f81 100644 --- a/src/Utils/Cryptography.hpp +++ b/src/Utils/Cryptography.hpp @@ -132,6 +132,7 @@ namespace Utils { public: static std::string GenerateChallenge(); + static std::uint64_t GenerateLong(); static std::uint32_t GenerateInt(); static void Initialize(); @@ -236,7 +237,7 @@ namespace Utils std::shared_ptr keyStorage; }; - static Key GenerateKey(int bits); + static Key GenerateKey(int bits, const std::string& entropy = {}); static std::string SignMessage(Key key, const std::string& message); static bool VerifyMessage(Key key, const std::string& message, const std::string& signature); }; From 6b780150a312cf8653a3bcb56e11a8575f080275 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 4 Feb 2024 19:21:50 +0100 Subject: [PATCH 51/71] Factorize GetGuidFilePath --- src/Components/Modules/Auth.cpp | 24 ++++++++++++++++-------- src/Components/Modules/Auth.hpp | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 08931f79..36b49af3 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -326,6 +326,16 @@ namespace Components } } + std::string Auth::GetGUIDFilePath() + { + const auto appdata = Components::FileSystem::GetAppdataPath(); + Utils::IO::CreateDir(appdata.string()); + + const auto guidPath = appdata / "guid.dat"; + + return guidPath.string(); + } + void ClientConnectFailedStub(Game::netsrc_t sock, Game::netadr_t adr, const char* data) { Logger::PrintFail2Ban("Failed connect attempt from IP address: {}\n", Network::AdrToString(adr)); @@ -358,8 +368,9 @@ namespace Components cert.set_token(GuidToken.toString()); cert.set_ctoken(ComputeToken.toString()); cert.set_privatekey(GuidKey.serialize(PK_PRIVATE)); - - Utils::IO::WriteFile("players/guid.dat", cert.SerializeAsString()); + + const auto guidPath = GetGUIDFilePath(); + Utils::IO::WriteFile(guidPath, cert.SerializeAsString()); } } @@ -376,24 +387,21 @@ namespace Components if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled()) return; if (!force && GuidKey.isValid()) return; - const auto appdata = Components::FileSystem::GetAppdataPath(); - Utils::IO::CreateDir(appdata.string()); - - const auto guidPath = appdata / "guid.dat"; + const auto guidPath = GetGUIDFilePath(); #ifndef REGENERATE_INVALID_KEY // Migrate old file const auto oldGuidPath = "players/guid.dat"; if (Utils::IO::FileExists(oldGuidPath)) { - if (MoveFileA(oldGuidPath, guidPath.string().data())) + if (MoveFileA(oldGuidPath, guidPath.data())) { Utils::IO::RemoveFile(oldGuidPath); } } #endif - const auto guidFile = Utils::IO::ReadFile(guidPath.string()); + const auto guidFile = Utils::IO::ReadFile(guidPath); Proto::Auth::Certificate cert; if (cert.ParseFromString(guidFile)) diff --git a/src/Components/Modules/Auth.hpp b/src/Components/Modules/Auth.hpp index 4644281c..1d56c1b0 100644 --- a/src/Components/Modules/Auth.hpp +++ b/src/Components/Modules/Auth.hpp @@ -55,6 +55,8 @@ namespace Components static char* Info_ValueForKeyStub(const char* s, const char* key); static void DirectConnectPrivateClientStub(); + static std::string GetGUIDFilePath(); + static void Frame(); }; } From c067b099f1320230b368f6397c69eb5890c55e3a Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 10 Feb 2024 11:06:39 +0100 Subject: [PATCH 52/71] Do not run party discovery in zonebuilder --- src/Components/Modules/Party.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Components/Modules/Party.cpp b/src/Components/Modules/Party.cpp index fadefcb1..a52c915f 100644 --- a/src/Components/Modules/Party.cpp +++ b/src/Components/Modules/Party.cpp @@ -193,6 +193,11 @@ namespace Components Party::Party() { + if (ZoneBuilder::IsEnabled()) + { + return; + } + PartyEnable = Dvar::Register("party_enable", Dedicated::IsEnabled(), Game::DVAR_NONE, "Enable party system"); Dvar::Register("xblive_privatematch", true, Game::DVAR_INIT, ""); From e4a5cd056eb03745e295dd523d2dc1b05bd00f06 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 11 Feb 2024 15:42:51 +0100 Subject: [PATCH 53/71] Fix MAC-based machine entropy generation --- src/Components/Modules/Auth.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 36b49af3..0c5dc938 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -566,23 +566,21 @@ namespace Components { switch (pIpAdapterInfo->Type) { - default: - pIpAdapterInfo = pIpAdapterInfo->Next; - continue; - - case IF_TYPE_IEEE80211: - case MIB_IF_TYPE_ETHERNET: - { - - std::string macAddress{}; - for (size_t i = 0; i < ARRAYSIZE(pIpAdapterInfo->Address); i++) + case IF_TYPE_IEEE80211: + case MIB_IF_TYPE_ETHERNET: { - entropy += std::to_string(pIpAdapterInfo->Address[i]); - } - break; - } + std::string macAddress{}; + for (size_t i = 0; i < ARRAYSIZE(pIpAdapterInfo->Address); i++) + { + entropy += std::to_string(pIpAdapterInfo->Address[i]); + } + + break; + } } + + pIpAdapterInfo = pIpAdapterInfo->Next; } } From 89c4a9d7a5181574086940ba6ed01c43a4231305 Mon Sep 17 00:00:00 2001 From: WantedDV <122710241+WantedDV@users.noreply.github.com> Date: Thu, 15 Feb 2024 18:55:17 -0330 Subject: [PATCH 54/71] fix: removed hook that can crash when loading zone --- src/Components/Modules/Maps.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index c8fd5a54..5300c66f 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -847,11 +847,9 @@ namespace Components // Always refresh arena when loading or unloading a zone Utils::Hook::Nop(0x485017, 2); - Utils::Hook::Nop(0x4FD8C7, 2); // Gametypes Utils::Hook::Nop(0x4BDFB7, 2); // Unknown Utils::Hook::Nop(0x45ED6F, 2); // loadGameInfo Utils::Hook::Nop(0x4A5888, 2); // UI_InitOnceForAllClients - // Allow hiding specific smodels Utils::Hook(0x50E67C, Maps::HideModelStub, HOOK_CALL).install()->quick(); From 4d0abb3ec2e4976290df9fca07d04e071953bb41 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sat, 9 Mar 2024 17:46:02 +0100 Subject: [PATCH 55/71] Replace plutools.pw with getserve.rs --- src/Components/Modules/ServerList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index afc64295..38c66564 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -384,7 +384,7 @@ namespace Components Toast::Show("cardicon_headshot", "Server Browser", "Fetching servers...", 3000); - const auto url = std::format("http://iw4x.plutools.pw/v1/servers/iw4x?protocol={}", PROTOCOL); + const auto url = std::format("http://iw4x.getserve.rs/v1/servers/iw4x?protocol={}", PROTOCOL); const auto reply = Utils::WebIO("IW4x", url).setTimeout(5000)->get(); if (reply.empty()) { From fbc939f484512122b9744c6b3a0ecd2695a4e00a Mon Sep 17 00:00:00 2001 From: Future Date: Sat, 30 Mar 2024 10:54:19 +0100 Subject: [PATCH 56/71] RCon: remove ECC & related functions + fix logic bug --- src/Components/Modules/RCon.cpp | 151 +------------------------------- src/Components/Modules/RCon.hpp | 14 --- 2 files changed, 1 insertion(+), 164 deletions(-) diff --git a/src/Components/Modules/RCon.cpp b/src/Components/Modules/RCon.cpp index 87a20462..792f11bf 100644 --- a/src/Components/Modules/RCon.cpp +++ b/src/Components/Modules/RCon.cpp @@ -11,9 +11,6 @@ namespace Components std::vector RCon::RConAddresses; - RCon::Container RCon::RConContainer; - Utils::Cryptography::ECC::Key RCon::RConKey; - std::string RCon::Password; Dvar::Var RCon::RConPassword; @@ -96,25 +93,6 @@ namespace Components Network::SendCommand(target, "rconSafe", directive.SerializeAsString()); }); - Command::Add("remoteCommand", [](const Command::Params* params) - { - if (params->size() < 2) return; - - RConContainer.command = params->get(1); - - auto* addr = reinterpret_cast(0xA5EA44); - Network::Address target(addr); - if (!target.isValid() || target.getIP().full == 0) - { - target = Party::Target(); - } - - if (target.isValid()) - { - Network::SendCommand(target, "rconRequest"); - } - }); - Command::AddSV("RconWhitelistAdd", [](const Command::Params* params) { if (params->size() < 2) @@ -261,47 +239,9 @@ namespace Components if (!Dedicated::IsEnabled()) { - Network::OnClientPacket("rconAuthorization", [](const Network::Address& address, [[maybe_unused]] const std::string& data) - { - if (RConContainer.command.empty()) - { - return; - } - - const auto& key = CryptoKeyECC::Get(); - const auto signedMsg = Utils::Cryptography::ECC::SignMessage(key, data); - - Proto::RCon::Command rconExec; - rconExec.set_command(RConContainer.command); - rconExec.set_signature(signedMsg); - - Network::SendCommand(address, "rconExecute", rconExec.SerializeAsString()); - }); - return; } - // Load public key - static std::uint8_t publicKey[] = - { - 0x04, 0x01, 0x9D, 0x18, 0x7F, 0x57, 0xD8, 0x95, 0x4C, 0xEE, 0xD0, 0x21, - 0xB5, 0x00, 0x53, 0xEC, 0xEB, 0x54, 0x7C, 0x4C, 0x37, 0x18, 0x53, 0x89, - 0x40, 0x12, 0xF7, 0x08, 0x8D, 0x9A, 0x8D, 0x99, 0x9C, 0x79, 0x79, 0x59, - 0x6E, 0x32, 0x06, 0xEB, 0x49, 0x1E, 0x00, 0x99, 0x71, 0xCB, 0x4A, 0xE1, - 0x90, 0xF1, 0x7C, 0xB7, 0x4D, 0x60, 0x88, 0x0A, 0xB7, 0xF3, 0xD7, 0x0D, - 0x4F, 0x08, 0x13, 0x7C, 0xEB, 0x01, 0xFF, 0x00, 0x32, 0xEE, 0xE6, 0x23, - 0x07, 0xB1, 0xC2, 0x9E, 0x45, 0xD6, 0xD7, 0xBD, 0xED, 0x05, 0x23, 0xB5, - 0xE7, 0x83, 0xEF, 0xD7, 0x8E, 0x36, 0xDC, 0x16, 0x79, 0x74, 0xD1, 0xD5, - 0xBA, 0x2C, 0x4C, 0x28, 0x61, 0x29, 0x5C, 0x49, 0x7D, 0xD4, 0xB6, 0x56, - 0x17, 0x75, 0xF5, 0x2B, 0x58, 0xCD, 0x0D, 0x76, 0x65, 0x10, 0xF7, 0x51, - 0x69, 0x1D, 0xB9, 0x0F, 0x38, 0xF6, 0x53, 0x3B, 0xF7, 0xCE, 0x76, 0x4F, - 0x08 - }; - - RConKey.set(std::string(reinterpret_cast(publicKey), sizeof(publicKey))); - - RConContainer.timestamp = 0; - Events::OnDvarInit([] { RConPassword = Dvar::Register("rcon_password", "", Game::DVAR_NONE, "The password for rcon"); @@ -359,6 +299,7 @@ namespace Components if (!key.isValid()) { Logger::PrintError(Game::CON_CHANNEL_NETWORK, "RSA public key is invalid\n"); + return; } Proto::RCon::Command directive; @@ -382,96 +323,6 @@ namespace Components RConSafeExecutor(address, s); }, Scheduler::Pipeline::MAIN); }); - - Network::OnClientPacket("rconRequest", [](const Network::Address& address, [[maybe_unused]] const std::string& data) - { - RConContainer.address = address; - RConContainer.challenge = Utils::Cryptography::Rand::GenerateChallenge(); - RConContainer.timestamp = Game::Sys_Milliseconds(); - - Network::SendCommand(address, "rconAuthorization", RConContainer.challenge); - }); - - Network::OnClientPacket("rconExecute", [](const Network::Address& address, [[maybe_unused]] const std::string& data) - { - if (address != RConContainer.address) return; // Invalid IP - if (!RConContainer.timestamp || (Game::Sys_Milliseconds() - RConContainer.timestamp) > (1000 * 10)) return; // Timeout - - RConContainer.timestamp = 0; - - Proto::RCon::Command rconExec; - rconExec.ParseFromString(data); - - if (!Utils::Cryptography::ECC::VerifyMessage(RConKey, RConContainer.challenge, rconExec.signature())) - { - return; - } - - RConContainer.output.clear(); - Logger::PipeOutput([](const std::string& output) - { - RConContainer.output.append(output); - }); - - Command::Execute(rconExec.command(), true); - - Logger::PipeOutput(nullptr); - - Network::SendCommand(address, "print", RConContainer.output); - RConContainer.output.clear(); - }); - } - - bool RCon::CryptoKeyECC::LoadKey(Utils::Cryptography::ECC::Key& key) - { - std::string data; - if (!Utils::IO::ReadFile("./private.key", &data)) - { - return false; - } - - key.deserialize(data); - return key.isValid(); - } - - Utils::Cryptography::ECC::Key RCon::CryptoKeyECC::GenerateKey() - { - auto key = Utils::Cryptography::ECC::GenerateKey(512); - if (!key.isValid()) - { - throw std::runtime_error("Failed to generate server key!"); - } - - if (!Utils::IO::WriteFile("./private.key", key.serialize())) - { - throw std::runtime_error("Failed to write server key!"); - } - - return key; - } - - Utils::Cryptography::ECC::Key RCon::CryptoKeyECC::LoadOrGenerateKey() - { - Utils::Cryptography::ECC::Key key; - if (LoadKey(key)) - { - return key; - } - - return GenerateKey(); - } - - Utils::Cryptography::ECC::Key RCon::CryptoKeyECC::GetKeyInternal() - { - auto key = LoadOrGenerateKey(); - Utils::IO::WriteFile("./public.key", key.getPublicKey()); - return key; - } - - Utils::Cryptography::ECC::Key& RCon::CryptoKeyECC::Get() - { - static auto key = GetKeyInternal(); - return key; } Utils::Cryptography::RSA::Key RCon::CryptoKeyRSA::LoadPublicKey() diff --git a/src/Components/Modules/RCon.hpp b/src/Components/Modules/RCon.hpp index ef691ce1..ad4136db 100644 --- a/src/Components/Modules/RCon.hpp +++ b/src/Components/Modules/RCon.hpp @@ -18,17 +18,6 @@ namespace Components Network::Address address{}; }; - class CryptoKeyECC - { - public: - static Utils::Cryptography::ECC::Key& Get(); - private: - static bool LoadKey(Utils::Cryptography::ECC::Key& key); - static Utils::Cryptography::ECC::Key GenerateKey(); - static Utils::Cryptography::ECC::Key LoadOrGenerateKey(); - static Utils::Cryptography::ECC::Key GetKeyInternal(); - }; - class CryptoKeyRSA { public: @@ -52,9 +41,6 @@ namespace Components static std::vector RConAddresses; - static Container RConContainer; - static Utils::Cryptography::ECC::Key RConKey; - static std::string Password; static std::string RConOutputBuffer; From 01b87374887d7a848bd876ffad7006beb157ade4 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sat, 30 Mar 2024 17:41:34 +0100 Subject: [PATCH 57/71] fix command line arguments for alterware-launcher.exe --- src/Components/Modules/QuickPatch.cpp | 2 +- src/Utils/Library.cpp | 26 +++++++++++++------------- src/Utils/Library.hpp | 2 +- src/Utils/Utils.cpp | 22 ++++++++++++++++++++++ src/Utils/Utils.hpp | 2 ++ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index e8a7ded9..ef455f9a 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -249,7 +249,7 @@ namespace Components const std::string command = binary == "iw4x-sp.exe" ? "iw4x-sp" : "iw4x"; SetEnvironmentVariableA("MW2_INSTALL", workingDir.data()); - Utils::Library::LaunchProcess(binary, std::format("{} --pass \"{}\"", command, GetCommandLineA()), workingDir); + Utils::Library::LaunchProcess(Utils::String::Convert(binary), std::format(L"{} --pass \"{}\"", Utils::String::Convert(command), Utils::GetLaunchParameters()), workingDir); } __declspec(naked) void QuickPatch::SND_GetAliasOffset_Stub() diff --git a/src/Utils/Library.cpp b/src/Utils/Library.cpp index 70d661de..fae1bbcc 100644 --- a/src/Utils/Library.cpp +++ b/src/Utils/Library.cpp @@ -102,22 +102,22 @@ namespace Utils this->module_ = nullptr; } - void Library::LaunchProcess(const std::string& process, const std::string& commandLine, const std::string& currentDir) + void Library::LaunchProcess(const std::wstring& process, const std::wstring& commandLine, const std::filesystem::path& currentDir) { - STARTUPINFOA startupInfo; - PROCESS_INFORMATION processInfo; + STARTUPINFOW startup_info; + PROCESS_INFORMATION process_info; - ZeroMemory(&startupInfo, sizeof(startupInfo)); - ZeroMemory(&processInfo, sizeof(processInfo)); - startupInfo.cb = sizeof(startupInfo); + ZeroMemory(&startup_info, sizeof(startup_info)); + ZeroMemory(&process_info, sizeof(process_info)); + startup_info.cb = sizeof(startup_info); - CreateProcessA(process.data(), const_cast(commandLine.data()), nullptr, - nullptr, false, NULL, nullptr, currentDir.data(), - &startupInfo, &processInfo); + CreateProcessW(process.data(), const_cast(commandLine.data()), nullptr, + nullptr, false, NULL, nullptr, currentDir.wstring().data(), + &startup_info, &process_info); - if (processInfo.hThread && processInfo.hThread != INVALID_HANDLE_VALUE) - CloseHandle(processInfo.hThread); - if (processInfo.hProcess && processInfo.hProcess != INVALID_HANDLE_VALUE) - CloseHandle(processInfo.hProcess); + if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) + CloseHandle(process_info.hThread); + if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) + CloseHandle(process_info.hProcess); } } diff --git a/src/Utils/Library.hpp b/src/Utils/Library.hpp index 367d9d08..3ff72663 100644 --- a/src/Utils/Library.hpp +++ b/src/Utils/Library.hpp @@ -65,7 +65,7 @@ namespace Utils return T(); } - static void LaunchProcess(const std::string& process, const std::string& commandLine, const std::string& currentDir); + static void LaunchProcess(const std::wstring& process, const std::wstring& commandLine, const std::filesystem::path& currentDir); private: HMODULE module_; diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 141a0fcc..60e06a5f 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -163,6 +163,28 @@ namespace Utils } } + std::wstring GetLaunchParameters() + { + std::wstring args; + + int numArgs; + auto* const argv = CommandLineToArgvW(GetCommandLineW(), &numArgs); + + if (argv) + { + for (auto i = 1; i < numArgs; ++i) + { + std::wstring arg(argv[i]); + args.append(arg); + args.append(L" "); + } + + LocalFree(argv); + } + + return args; + } + HMODULE GetNTDLL() { return GetModuleHandleA("ntdll.dll"); diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index f864a670..9ec8664b 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,6 +24,8 @@ namespace Utils void OpenUrl(const std::string& url); + std::wstring GetLaunchParameters(); + bool HasIntersection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); template From 0801553556548a2d7dda4b64a52d2ddc7c38c362 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sun, 31 Mar 2024 22:05:20 +0200 Subject: [PATCH 58/71] Fix commandLine for CreateProcessW Remove unnecessary ternary (binary is never iw4x-sp.exe) --- src/Components/Modules/QuickPatch.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index ef455f9a..ca8dfc9d 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -244,12 +244,12 @@ namespace Components return; } - auto workingDir = std::filesystem::current_path().string(); - const std::string binary = *Game::sys_exitCmdLine; - const std::string command = binary == "iw4x-sp.exe" ? "iw4x-sp" : "iw4x"; + const std::filesystem::path workingDir = std::filesystem::current_path(); + const std::wstring binary = Utils::String::Convert(*Game::sys_exitCmdLine); + const std::wstring commandLine = std::format(L"\"{}\" iw4x --pass \"{}\"", (workingDir / binary).wstring(), Utils::GetLaunchParameters()); - SetEnvironmentVariableA("MW2_INSTALL", workingDir.data()); - Utils::Library::LaunchProcess(Utils::String::Convert(binary), std::format(L"{} --pass \"{}\"", Utils::String::Convert(command), Utils::GetLaunchParameters()), workingDir); + SetEnvironmentVariableA("MW2_INSTALL", workingDir.string().data()); + Utils::Library::LaunchProcess(binary, commandLine, workingDir); } __declspec(naked) void QuickPatch::SND_GetAliasOffset_Stub() From a07253fff47892819d89352fd142fb90252d3ac2 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:13:37 +0200 Subject: [PATCH 59/71] format according to review --- src/Utils/Library.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Utils/Library.cpp b/src/Utils/Library.cpp index fae1bbcc..d5220666 100644 --- a/src/Utils/Library.cpp +++ b/src/Utils/Library.cpp @@ -104,20 +104,20 @@ namespace Utils void Library::LaunchProcess(const std::wstring& process, const std::wstring& commandLine, const std::filesystem::path& currentDir) { - STARTUPINFOW startup_info; - PROCESS_INFORMATION process_info; + STARTUPINFOW startupInfo; + PROCESS_INFORMATION processInfo; - ZeroMemory(&startup_info, sizeof(startup_info)); - ZeroMemory(&process_info, sizeof(process_info)); - startup_info.cb = sizeof(startup_info); + ZeroMemory(&startupInfo, sizeof(startupInfo)); + ZeroMemory(&processInfo, sizeof(processInfo)); + startupInfo.cb = sizeof(startupInfo); CreateProcessW(process.data(), const_cast(commandLine.data()), nullptr, nullptr, false, NULL, nullptr, currentDir.wstring().data(), - &startup_info, &process_info); + &startupInfo, &processInfo); - if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) - CloseHandle(process_info.hThread); - if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) - CloseHandle(process_info.hProcess); + if (processInfo.hThread && processInfo.hThread != INVALID_HANDLE_VALUE) + CloseHandle(processInfo.hThread); + if (processInfo.hProcess && processInfo.hProcess != INVALID_HANDLE_VALUE) + CloseHandle(processInfo.hProcess); } } From d97dad0fc1f781759d4a36ab36b7abd2ee13db76 Mon Sep 17 00:00:00 2001 From: ineed bots Date: Thu, 2 May 2024 11:24:06 -0600 Subject: [PATCH 60/71] Add BotRemoteAngles and BotAngles, fixes ads bot button not throwing, adds remote bot button, BotStop assigns current weapon --- src/Components/Modules/Bots.cpp | 55 ++++++++++++++++++++++++++++++--- src/Game/Structs.hpp | 1 + 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index 576bcdc8..ec63221e 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -30,6 +30,8 @@ namespace Components std::uint16_t lastAltWeapon; std::uint8_t meleeDist; float meleeYaw; + std::int8_t remoteAngles[2]; + float angles[3]; bool active; }; @@ -54,10 +56,11 @@ namespace Components { "sprint", Game::CMD_BUTTON_SPRINT }, { "leanleft", Game::CMD_BUTTON_LEAN_LEFT }, { "leanright", Game::CMD_BUTTON_LEAN_RIGHT }, - { "ads", Game::CMD_BUTTON_ADS }, + { "ads", Game::CMD_BUTTON_ADS | Game::CMD_BUTTON_THROW }, { "holdbreath", Game::CMD_BUTTON_BREATH }, { "usereload", Game::CMD_BUTTON_USE_RELOAD }, { "activate", Game::CMD_BUTTON_ACTIVATE }, + { "remote", Game::CMD_BUTTON_REMOTE }, }; void Bots::UpdateBotNames() @@ -214,7 +217,10 @@ namespace Components } ZeroMemory(&g_botai[entref.entnum], sizeof(BotMovementInfo)); - g_botai[entref.entnum].weapon = 1; + g_botai[entref.entnum].weapon = ent->client->ps.weapCommon.weapon; + g_botai[entref.entnum].angles[0] = ent->client->ps.viewangles[0]; + g_botai[entref.entnum].angles[1] = ent->client->ps.viewangles[1]; + g_botai[entref.entnum].angles[2] = ent->client->ps.viewangles[2]; g_botai[entref.entnum].active = true; }); @@ -311,6 +317,43 @@ namespace Components g_botai[entref.entnum].meleeDist = static_cast(dist); g_botai[entref.entnum].active = true; }); + + GSC::Script::AddMethod("BotRemoteAngles", [](const Game::scr_entref_t entref) // Usage: BotRemoteAngles(, ); + { + const auto* ent = GSC::Script::Scr_GetPlayerEntity(entref); + if (!Game::SV_IsTestClient(ent->s.number)) + { + Game::Scr_Error("BotRemoteAngles: Can only call on a bot!"); + return; + } + + const auto pitch = std::clamp(Game::Scr_GetInt(0), std::numeric_limits::min(), std::numeric_limits::max()); + const auto yaw = std::clamp(Game::Scr_GetInt(1), std::numeric_limits::min(), std::numeric_limits::max()); + + g_botai[entref.entnum].remoteAngles[0] = static_cast(pitch); + g_botai[entref.entnum].remoteAngles[1] = static_cast(yaw); + g_botai[entref.entnum].active = true; + }); + + GSC::Script::AddMethod("BotAngles", [](const Game::scr_entref_t entref) // Usage: BotAngles(, , ); + { + const auto* ent = GSC::Script::Scr_GetPlayerEntity(entref); + if (!Game::SV_IsTestClient(ent->s.number)) + { + Game::Scr_Error("BotAngles: Can only call on a bot!"); + return; + } + + const auto pitch = Game::Scr_GetFloat(0); + const auto yaw = Game::Scr_GetFloat(1); + const auto roll = Game::Scr_GetFloat(2); + + g_botai[entref.entnum].angles[0] = pitch; + g_botai[entref.entnum].angles[1] = yaw; + g_botai[entref.entnum].angles[2] = roll; + + g_botai[entref.entnum].active = true; + }); } void Bots::BotAiAction(Game::client_s* cl) @@ -341,10 +384,12 @@ namespace Components userCmd.primaryWeaponForAltMode = g_botai[clientNum].lastAltWeapon; userCmd.meleeChargeYaw = g_botai[clientNum].meleeYaw; userCmd.meleeChargeDist = g_botai[clientNum].meleeDist; + userCmd.remoteControlAngles[0] = g_botai[clientNum].remoteAngles[0]; + userCmd.remoteControlAngles[1] = g_botai[clientNum].remoteAngles[1]; - userCmd.angles[0] = ANGLE2SHORT((cl->gentity->client->ps.viewangles[0] - cl->gentity->client->ps.delta_angles[0])); - userCmd.angles[1] = ANGLE2SHORT((cl->gentity->client->ps.viewangles[1] - cl->gentity->client->ps.delta_angles[1])); - userCmd.angles[2] = ANGLE2SHORT((cl->gentity->client->ps.viewangles[2] - cl->gentity->client->ps.delta_angles[2])); + userCmd.angles[0] = ANGLE2SHORT(g_botai[clientNum].angles[0] - cl->gentity->client->ps.delta_angles[0]); + userCmd.angles[1] = ANGLE2SHORT(g_botai[clientNum].angles[1] - cl->gentity->client->ps.delta_angles[1]); + userCmd.angles[2] = ANGLE2SHORT(g_botai[clientNum].angles[2] - cl->gentity->client->ps.delta_angles[2]); Game::SV_ClientThink(cl, &userCmd); } diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index d2c2e9b1..6778b88b 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1978,6 +1978,7 @@ namespace Game CMD_BUTTON_FRAG = 1 << 14, CMD_BUTTON_OFFHAND_SECONDARY = 1 << 15, CMD_BUTTON_THROW = 1 << 19, + CMD_BUTTON_REMOTE = 1 << 20, }; struct usercmd_s From c249947390c13677bbd7c5191380b146799c722b Mon Sep 17 00:00:00 2001 From: ineed bots Date: Thu, 2 May 2024 11:38:09 -0600 Subject: [PATCH 61/71] fix warning --- src/Components/Modules/Bots.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index ec63221e..974f849e 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -217,7 +217,7 @@ namespace Components } ZeroMemory(&g_botai[entref.entnum], sizeof(BotMovementInfo)); - g_botai[entref.entnum].weapon = ent->client->ps.weapCommon.weapon; + g_botai[entref.entnum].weapon = static_cast(ent->client->ps.weapCommon.weapon); g_botai[entref.entnum].angles[0] = ent->client->ps.viewangles[0]; g_botai[entref.entnum].angles[1] = ent->client->ps.viewangles[1]; g_botai[entref.entnum].angles[2] = ent->client->ps.viewangles[2]; From 231f2c2d69766727446117b0b5deda86065be0a1 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Tue, 21 May 2024 05:29:56 +0200 Subject: [PATCH 62/71] add sv_votesrequired dvar --- src/Components/Modules/Vote.cpp | 23 +++++++++++++++++++++-- src/Components/Modules/Vote.hpp | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Vote.cpp b/src/Components/Modules/Vote.cpp index 9679907d..b149198f 100644 --- a/src/Components/Modules/Vote.cpp +++ b/src/Components/Modules/Vote.cpp @@ -2,11 +2,14 @@ #include "ClientCommand.hpp" #include "MapRotation.hpp" #include "Vote.hpp" +#include "Events.hpp" using namespace Utils::String; namespace Components { + Dvar::Var Vote::SV_VotesRequired; + std::unordered_map Vote::VoteCommands = { {"map_restart", HandleMapRestart}, @@ -37,6 +40,17 @@ namespace Components Game::SV_SetConfigstring(Game::CS_VOTE_NO, VA("%i", Game::level->voteNo)); } + int Vote::VotesRequired() + { + auto votesRequired = Vote::SV_VotesRequired.get(); + + if (votesRequired > 0) + { + return votesRequired; + } + return Game::level->numConnectedClients / 2 + 1; + } + bool Vote::IsInvalidVoteString(const std::string& input) { static const char* separators[] = { "\n", "\r", ";" }; @@ -204,7 +218,7 @@ namespace Components return; } - if (Game::level->numConnectedClients < 2) + if (Game::level->numConnectedClients < VotesRequired()) { Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_VOTINGNOTENOUGHPLAYERS\"", 0x65)); return; @@ -218,7 +232,7 @@ namespace Components return; } - if (ent->client->sess.voteCount >= 3) + if (ent->client->sess.voteCount >= VotesRequired()) { Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_MAXVOTESCALLED\"", 0x65)); return; @@ -321,6 +335,11 @@ namespace Components // Replicate g_allowVote Utils::Hook::Set(0x5E3A4F, Game::DVAR_INTERNAL | Game::DVAR_CODINFO); + Events::OnDvarInit([]() + { + Vote::SV_VotesRequired = Game::Dvar_RegisterInt("sv_votesRequired", 0, 0, 18, Game::DVAR_NONE, ""); + }); + ClientCommand::Add("callvote", Cmd_CallVote_f); ClientCommand::Add("vote", Cmd_Vote_f); diff --git a/src/Components/Modules/Vote.hpp b/src/Components/Modules/Vote.hpp index 82b35d22..ac4f4405 100644 --- a/src/Components/Modules/Vote.hpp +++ b/src/Components/Modules/Vote.hpp @@ -11,11 +11,14 @@ namespace Components using CommandHandler = std::function; static std::unordered_map VoteCommands; + static Dvar::Var SV_VotesRequired; + static constexpr auto* CallVoteDesc = "%c \"GAME_VOTECOMMANDSARE\x15 map_restart, map_rotate, map , g_gametype , typemap , " "kick , tempBanUser \""; static void DisplayVote(const Game::gentity_s* ent); static bool IsInvalidVoteString(const std::string& input); + static int VotesRequired(); static bool HandleMapRestart(const Game::gentity_s* ent, const Command::ServerParams* params); static bool HandleMapRotate(const Game::gentity_s* ent, const Command::ServerParams* params); From 41039f9b19b05ef313e6588342fe06f30c5654cb Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Tue, 21 May 2024 05:35:40 +0200 Subject: [PATCH 63/71] add sv_votesrequired description --- src/Components/Modules/Vote.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Vote.cpp b/src/Components/Modules/Vote.cpp index b149198f..8a2d6c2c 100644 --- a/src/Components/Modules/Vote.cpp +++ b/src/Components/Modules/Vote.cpp @@ -335,9 +335,8 @@ namespace Components // Replicate g_allowVote Utils::Hook::Set(0x5E3A4F, Game::DVAR_INTERNAL | Game::DVAR_CODINFO); - Events::OnDvarInit([]() - { - Vote::SV_VotesRequired = Game::Dvar_RegisterInt("sv_votesRequired", 0, 0, 18, Game::DVAR_NONE, ""); + Events::OnDvarInit([]{ + Vote::SV_VotesRequired = Game::Dvar_RegisterInt("sv_votesRequired", 0, 0, 18, Game::DVAR_NONE, "Set the amount of votes required for a vote to pass.\n0 = (players / 2) + 1"); }); ClientCommand::Add("callvote", Cmd_CallVote_f); From 704a125223d77a7a305e079503a1f1f9825cc41a Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 15 Jun 2024 23:58:11 +0200 Subject: [PATCH 64/71] Better MSS logging when ASI files are missing and causes sound to not be played (also don't spam console) --- src/Components/Modules/Logger.cpp | 23 ++++ src/Game/Game.cpp | 2 + src/Game/Game.hpp | 2 + src/Game/Structs.hpp | 214 ++++++++++++++++++++++++++++-- 4 files changed, 228 insertions(+), 13 deletions(-) diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index 1d90e816..4f54ca63 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -419,8 +419,31 @@ namespace Components }); } + void PrintAliasError(Game::conChannel_t channel, const char * originalMsg, const char* soundName, const char* lastErrorStr) + { + // We add a bit more info and we clear the sound stream when it happens + // to avoid spamming the error + const auto newMsg = std::format("{}Make sure you have the 'miles' folder in your game directory! Otherwise MP3 and other codecs will be unavailable.\n", originalMsg); + Game::Com_PrintError(channel, newMsg.c_str(), soundName, lastErrorStr); + + for (size_t i = 0; i < ARRAYSIZE(Game::milesGlobal->streamReadInfo); i++) + { + if (0 == std::strncmp(Game::milesGlobal->streamReadInfo[i].path, soundName, ARRAYSIZE(Game::milesGlobal->streamReadInfo[i].path))) + { + Game::milesGlobal->streamReadInfo[i].path[0] = '\x00'; // This kills it and make sure it doesn't get played again for now + break; + } + } + } + Logger::Logger() { + // Print sound aliases errors + if (!Dedicated::IsEnabled()) + { + Utils::Hook(0x64BA67, PrintAliasError, HOOK_CALL).install()->quick(); + } + Utils::Hook(0x642139, BuildOSPath_Stub, HOOK_JUMP).install()->quick(); Scheduler::Loop(Frame, Scheduler::Pipeline::SERVER); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 2887e3a1..e2d26a17 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -21,6 +21,8 @@ namespace Game NetField* clientStateFields = reinterpret_cast(0x741E40); size_t clientStateFieldsCount = Utils::Hook::Get(0x7433C8); + MssLocal* milesGlobal = reinterpret_cast(0x649A1A0); + const char* origErrorMsg = reinterpret_cast(0x79B124); XModel* G_GetModel(const int index) diff --git a/src/Game/Game.hpp b/src/Game/Game.hpp index 06315fad..12d81a94 100644 --- a/src/Game/Game.hpp +++ b/src/Game/Game.hpp @@ -56,6 +56,8 @@ namespace Game // This does not belong anywhere else extern NetField* clientStateFields; extern size_t clientStateFieldsCount; + extern MssLocal* milesGlobal; + extern const char* origErrorMsg; diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 6778b88b..257044a1 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1139,11 +1139,11 @@ namespace Game unsigned __int16 triCount; XSurfaceCollisionTree* collisionTree; }; - + struct DObjSkelMat { - float axis[3][4]; - float origin[4]; + float axis[3][4]; + float origin[4]; }; struct XSurface @@ -2426,7 +2426,7 @@ namespace Game float scale; unsigned int noScalePartBits[6]; unsigned __int16* boneNames; - unsigned char *parentList; + unsigned char* parentList; short* quats; float* trans; unsigned char* partClassification; @@ -4021,7 +4021,7 @@ namespace Game GfxSurfaceBounds* surfacesBounds; GfxStaticModelDrawInst* smodelDrawInsts; GfxDrawSurf* surfaceMaterials; - unsigned int* surfaceCastsSunShadow; + unsigned int* surfaceCastsSunShadow; volatile int usageCount; }; @@ -4032,7 +4032,7 @@ namespace Game unsigned __int16 materialSortedIndex : 12; unsigned __int16 visDataRefCountLessOne : 4; }; - + union GfxSModelSurfHeader { GfxSModelSurfHeaderFields fields; @@ -5519,6 +5519,32 @@ namespace Game StructuredDataEnumEntry* entries; }; + enum LookupResultDataType + { + LOOKUP_RESULT_INT = 0x0, + LOOKUP_RESULT_BOOL = 0x1, + LOOKUP_RESULT_STRING = 0x2, + LOOKUP_RESULT_FLOAT = 0x3, + LOOKUP_RESULT_SHORT = 0x4, + }; + + enum LookupState + { + LOOKUP_IN_PROGRESS = 0x0, + LOOKUP_FINISHED = 0x1, + LOOKUP_ERROR = 0x2, + }; + + enum LookupError + { + LOOKUP_ERROR_NONE = 0x0, + LOOKUP_ERROR_WRONG_DATA_TYPE = 0x1, + LOOKUP_ERROR_INDEX_OUTSIDE_BOUNDS = 0x2, + LOOKUP_ERROR_INVALID_STRUCT_PROPERTY = 0x3, + LOOKUP_ERROR_INVALID_ENUM_VALUE = 0x4, + LOOKUP_ERROR_COUNT = 0x5, + }; + enum StructuredDataTypeCategory { DATA_INT = 0x0, @@ -5594,6 +5620,15 @@ namespace Game unsigned int size; }; + + struct StructuredDataLookup + { + StructuredDataDef* def; + StructuredDataType* type; + unsigned int offset; + LookupError error; + }; + struct StructuredDataDefSet { const char* name; @@ -7932,8 +7967,8 @@ namespace Game struct GfxCmdBufContext { - GfxCmdBufSourceState *source; - GfxCmdBufState *state; + GfxCmdBufSourceState* source; + GfxCmdBufState* state; }; struct GfxDrawGroupSetupFields @@ -8385,18 +8420,18 @@ namespace Game int timeStamp; DObjAnimMat* mat; }; - + struct bitarray { - int array[6]; + int array[6]; }; /* 1923 */ struct XAnimCalcAnimInfo { - DObjAnimMat rotTransArray[1152]; - bitarray animPartBits; - bitarray ignorePartBits; + DObjAnimMat rotTransArray[1152]; + bitarray animPartBits; + bitarray ignorePartBits; }; @@ -11101,6 +11136,159 @@ namespace Game FFD_USER_MAP = 0x2, }; + struct ASISTAGE + { + int(__stdcall* ASI_stream_open)(unsigned int, int(__stdcall*)(unsigned int, void*, int, int), unsigned int); + int(__stdcall* ASI_stream_process)(int, void*, int); + int(__stdcall* ASI_stream_seek)(int, int); + int(__stdcall* ASI_stream_close)(int); + int(__stdcall* ASI_stream_property)(int, unsigned int, void*, const void*, void*); + unsigned int INPUT_BIT_RATE; + unsigned int INPUT_SAMPLE_RATE; + unsigned int INPUT_BITS; + unsigned int INPUT_CHANNELS; + unsigned int OUTPUT_BIT_RATE; + unsigned int OUTPUT_SAMPLE_RATE; + unsigned int OUTPUT_BITS; + unsigned int OUTPUT_CHANNELS; + unsigned int OUTPUT_RESERVOIR; + unsigned int POSITION; + unsigned int PERCENT_DONE; + unsigned int MIN_INPUT_BLOCK_SIZE; + unsigned int RAW_RATE; + unsigned int RAW_BITS; + unsigned int RAW_CHANNELS; + unsigned int REQUESTED_RATE; + unsigned int REQUESTED_BITS; + unsigned int REQUESTED_CHANS; + unsigned int STREAM_SEEK_POS; + unsigned int DATA_START_OFFSET; + unsigned int DATA_LEN; + int stream; + }; + + struct _STREAM + { + int block_oriented; + int using_ASI; + ASISTAGE* ASI; + void* samp; + unsigned int fileh; + char* bufs[3]; + unsigned int bufsizes[3]; + int reset_ASI[3]; + int reset_seek_pos[3]; + int bufstart[3]; + void* asyncs[3]; + int loadedbufstart[2]; + int loadedorder[2]; + int loadorder; + int bufsize; + int readsize; + unsigned int buf1; + int size1; + unsigned int buf2; + int size2; + unsigned int buf3; + int size3; + unsigned int datarate; + int filerate; + int filetype; + unsigned int fileflags; + int totallen; + int substart; + int sublen; + int subpadding; + unsigned int blocksize; + int padding; + int padded; + int loadedsome; + unsigned int startpos; + unsigned int totalread; + unsigned int loopsleft; + unsigned int error; + int preload; + unsigned int preloadpos; + int noback; + int alldone; + int primeamount; + int readatleast; + int playcontrol; + void(__stdcall* callback)(_STREAM*); + int user_data[8]; + void* next; + int autostreaming; + int docallback; + }; + + enum SND_EQTYPE + { + SND_EQTYPE_FIRST = 0x0, + SND_EQTYPE_LOWPASS = 0x0, + SND_EQTYPE_HIGHPASS = 0x1, + SND_EQTYPE_LOWSHELF = 0x2, + SND_EQTYPE_HIGHSHELF = 0x3, + SND_EQTYPE_BELL = 0x4, + SND_EQTYPE_LAST = 0x4, + SND_EQTYPE_COUNT = 0x5, + SND_EQTYPE_INVALID = 0x5, + }; + + struct __declspec(align(4)) SndEqParams + { + SND_EQTYPE type; + float gain; + float freq; + float q; + bool enabled; + }; + + struct MssEqInfo + { + SndEqParams params[3][64]; + }; + + struct MssFileHandle + { + unsigned int id; + MssFileHandle* next; + int handle; + char fileName[128]; + unsigned int hashCode; + int offset; + int fileOffset; + int fileLength; + }; + + struct __declspec(align(4)) MssStreamReadInfo + { + char path[256]; + int timeshift; + float fraction; + int startDelay; + _STREAM* handle; + bool readError; + }; + + struct MssLocal + { + struct _DIG_DRIVER* driver; + struct _SAMPLE* handle_sample[40]; + _STREAM* handle_stream[12]; + bool voiceEqDisabled[52]; + MssEqInfo eq[2]; + float eqLerp; + unsigned int eqFilter; + int currentRoomtype; + MssFileHandle fileHandle[12]; + MssFileHandle* freeFileHandle; + bool isMultiChannel; + float realVolume[12]; + int playbackRate[52]; + MssStreamReadInfo streamReadInfo[12]; + }; + + #pragma endregion #ifndef IDA From 21d5b05554838a82406db51f416690a7195a8221 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sat, 15 Jun 2024 23:58:25 +0200 Subject: [PATCH 65/71] Correct Player Data Def class limit & migration! --- src/Components/Modules/StructuredData.cpp | 134 +++++++++++++++------- 1 file changed, 95 insertions(+), 39 deletions(-) diff --git a/src/Components/Modules/StructuredData.cpp b/src/Components/Modules/StructuredData.cpp index 34f9e6c9..ca55b76b 100644 --- a/src/Components/Modules/StructuredData.cpp +++ b/src/Components/Modules/StructuredData.cpp @@ -3,6 +3,8 @@ namespace Components { + constexpr auto BASE_PLAYERSTATS_VERSION = 155; + Utils::Memory::Allocator StructuredData::MemAllocator; const char* StructuredData::EnumTranslation[COUNT] = @@ -26,6 +28,11 @@ namespace Components { if (!data || type >= StructuredData::PlayerDataType::COUNT) return; + // Reallocate them before patching + Game::StructuredDataEnum* newEnums = MemAllocator.allocateArray(data->enumCount); + std::memcpy(newEnums, data->enums, sizeof(Game::StructuredDataEnum) * data->enumCount); + data->enums = newEnums; + Game::StructuredDataEnum* dataEnum = &data->enums[type]; // Build index-sorted data vector @@ -87,23 +94,52 @@ namespace Components void StructuredData::PatchCustomClassLimit(Game::StructuredDataDef* data, int count) { - const int customClassSize = 64; + constexpr auto CLASS_ARRAY = 5; + const auto originalClassCount = data->indexedArrays[CLASS_ARRAY].arraySize; - for (int i = 0; i < data->structs[0].propertyCount; ++i) + if (count != originalClassCount) { - // 3003 is the offset of the customClasses structure - if (data->structs[0].properties[i].offset >= 3643) + const int customClassSize = 64; + + // We need to recreate the struct list cause it's used in order definitions + auto newStructs = StructuredData::MemAllocator.allocateArray(data->structCount); + std::memcpy(newStructs, data->structs, data->structCount * sizeof(Game::StructuredDataStruct)); + data->structs = newStructs; + + constexpr auto structIndex = 0; { - // -10 because 10 is the default amount of custom classes. - data->structs[0].properties[i].offset += ((count - 10) * customClassSize); + auto strct = &data->structs[structIndex]; + + auto newProperties = StructuredData::MemAllocator.allocateArray(strct->propertyCount); + std::memcpy(newProperties, strct->properties, strct->propertyCount * sizeof(Game::StructuredDataStructProperty)); + strct->properties = newProperties; + + for (int propertyIndex = 0; propertyIndex < strct->propertyCount; ++propertyIndex) + { + // 3643 is the offset of the customClasses structure + if (strct->properties[propertyIndex].offset >= 3643) + { + // -10 because 10 is the default amount of custom classes. + strct->properties[propertyIndex].offset += ((count - originalClassCount) * customClassSize); + } + } + } + + // update structure size + data->size += ((count - originalClassCount) * customClassSize); + + // Update amount of custom classes + if (data->indexedArrays[CLASS_ARRAY].arraySize != count) + { + // We need to recreate the whole array - this reference could be reused accross definitions + auto newIndexedArray = StructuredData::MemAllocator.allocateArray(data->indexedArrayCount); + std::memcpy(newIndexedArray, data->indexedArrays, data->indexedArrayCount * sizeof(Game::StructuredDataIndexedArray)); + data->indexedArrays = newIndexedArray; + + // Add classes + data->indexedArrays[CLASS_ARRAY].arraySize = count; } } - - // update structure size - data->size += ((count - 10) * customClassSize); - - // Update amount of custom classes - data->indexedArrays[5].arraySize = count; } void StructuredData::PatchAdditionalData(Game::StructuredDataDef* data, std::unordered_map& patches) @@ -119,26 +155,44 @@ namespace Components bool StructuredData::UpdateVersionOffsets(Game::StructuredDataDefSet* set, Game::StructuredDataBuffer* buffer, Game::StructuredDataDef* whatever) { - Game::StructuredDataDef* newDef = &set->defs[0]; - Game::StructuredDataDef* oldDef = &set->defs[0]; - - for (unsigned int i = 0; i < set->defCount; ++i) + if (set->defCount > 1) { - if (newDef->version < set->defs[i].version) + int bufferVersion = *reinterpret_cast(buffer->data); + + for (size_t i = 0; i < set->defCount; i++) { - newDef = &set->defs[i]; + if (set->defs[i].version == bufferVersion) + { + if (i == 0) + { + // No update to conduct + } + else + { + Game::StructuredDataDef* newDef = &set->defs[i - 1]; + Game::StructuredDataDef* oldDef = &set->defs[i]; + + if (newDef->version == 159 && oldDef->version <= 158) + { + // this should move the data 320 bytes infront + std::memmove(&buffer->data[3963], &buffer->data[3643], oldDef->size - 3643); + } + else if (newDef->version > 159 && false) + { + // 159 cannot be translated upwards and it's hard to say why + // Reading it fails in various ways + // might be the funky class bump...? + + Command::Execute("setPlayerData prestige 10"); + Command::Execute("setPlayerData experience 2516000"); + } + + return Utils::Hook::Call(0x456830)(set, buffer, whatever); + } + } } - if (set->defs[i].version == *reinterpret_cast(buffer->data)) - { - oldDef = &set->defs[i]; - } - } - - if (newDef->version >= 159 && oldDef->version <= 158) - { - // this should move the data 320 bytes infront - std::memmove(&buffer->data[3963], &buffer->data[3643], oldDef->size - 3643); + return Utils::Hook::Call(0x456830)(set, buffer, whatever); } // StructuredData_UpdateVersion @@ -182,7 +236,7 @@ namespace Components return; } - if (data->defs[0].version != 155) + if (data->defs[0].version != BASE_PLAYERSTATS_VERSION) { Logger::Error(Game::ERR_FATAL, "Initial PlayerDataDef is not version 155, patching not possible!"); return; @@ -262,16 +316,10 @@ namespace Components auto* newData = StructuredData::MemAllocator.allocateArray(data->defCount + patchDefinitions.size()); std::memcpy(&newData[patchDefinitions.size()], data->defs, sizeof Game::StructuredDataDef * data->defCount); - // Prepare the buffers + // Set the versions for (unsigned int i = 0; i < patchDefinitions.size(); ++i) { - std::memcpy(&newData[i], data->defs, sizeof Game::StructuredDataDef); - newData[i].version = (patchDefinitions.size() - i) + 155; - - // Reallocate the enum array - auto* newEnums = StructuredData::MemAllocator.allocateArray(data->defs->enumCount); - std::memcpy(newEnums, data->defs->enums, sizeof Game::StructuredDataEnum * data->defs->enumCount); - newData[i].enums = newEnums; + newData[i].version = (patchDefinitions.size() - i) + BASE_PLAYERSTATS_VERSION; } // Apply new data @@ -279,10 +327,18 @@ namespace Components data->defCount += patchDefinitions.size(); // Patch the definition - for (unsigned int i = 0; i < data->defCount; ++i) + for (int i = data->defCount - 1; i >= 0; --i) { // No need to patch version 155 - if (newData[i].version == 155) continue; + if (newData[i].version == BASE_PLAYERSTATS_VERSION) + { + continue; + } + + // We start from the previous one + const auto version = newData[i].version; + data->defs[i] = data->defs[i + 1]; + newData[i].version = version; if (patchDefinitions.contains(newData[i].version)) { From 835f74065cba8faae67c6ab8dc9b91bdbd465e96 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 16 Jun 2024 00:58:16 +0200 Subject: [PATCH 66/71] Fix a crash with arena reloading on custom maps --- src/Components/Modules/Friends.cpp | 5 +- src/Components/Modules/Logger.cpp | 289 +++++++++++++++-------------- src/Components/Modules/Maps.cpp | 172 +++++++++-------- src/Components/Modules/Maps.hpp | 9 +- 4 files changed, 250 insertions(+), 225 deletions(-) diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index 90cf83c7..cc327e1d 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -315,7 +315,10 @@ namespace Components Friends::LoggedOn = (Steam::Proxy::SteamUser_ && Steam::Proxy::SteamUser_->LoggedOn()); if (!Steam::Proxy::SteamFriends) return; - Game::UI_UpdateArenas(); + if (Game::Sys_IsMainThread()) + { + Game::UI_UpdateArenas(); + } int count = Steam::Proxy::SteamFriends->GetFriendCount(4); diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index 4f54ca63..25a50c60 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -38,32 +38,32 @@ namespace Components void Logger::MessagePrint(const int channel, const std::string& msg) { static const auto shouldPrint = []() -> bool - { - return Flags::HasFlag("stdout") || Loader::IsPerformingUnitTests(); - }(); + { + return Flags::HasFlag("stdout") || Loader::IsPerformingUnitTests(); + }(); - if (shouldPrint) - { - (void)std::fputs(msg.data(), stdout); - (void)std::fflush(stdout); - return; - } + if (shouldPrint) + { + (void)std::fputs(msg.data(), stdout); + (void)std::fflush(stdout); + return; + } #ifdef _DEBUG - if (!IsConsoleReady()) - { - OutputDebugStringA(msg.data()); - } + if (!IsConsoleReady()) + { + OutputDebugStringA(msg.data()); + } #endif - if (!Game::Sys_IsMainThread()) - { - EnqueueMessage(msg); - } - else - { - Game::Com_PrintMessage(channel, msg.data(), 0); - } + if (!Game::Sys_IsMainThread()) + { + EnqueueMessage(msg); + } + else + { + Game::Com_PrintMessage(channel, msg.data(), 0); + } } void Logger::DebugInternal(const std::string_view& fmt, std::format_args&& args, [[maybe_unused]] const std::source_location& loc) @@ -120,33 +120,33 @@ namespace Components void Logger::PrintFail2BanInternal(const std::string_view& fmt, std::format_args&& args) { static const auto shouldPrint = []() -> bool - { - return Flags::HasFlag("fail2ban"); - }(); + { + return Flags::HasFlag("fail2ban"); + }(); - if (!shouldPrint) - { - return; - } + if (!shouldPrint) + { + return; + } - auto msg = std::vformat(fmt, args); + auto msg = std::vformat(fmt, args); - static auto log_next_time_stamp = true; - if (log_next_time_stamp) - { - auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - // Convert to local time - std::tm timeInfo = *std::localtime(&now); + static auto log_next_time_stamp = true; + if (log_next_time_stamp) + { + auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + // Convert to local time + std::tm timeInfo = *std::localtime(&now); - std::ostringstream ss; - ss << std::put_time(&timeInfo, "%Y-%m-%d %H:%M:%S "); + std::ostringstream ss; + ss << std::put_time(&timeInfo, "%Y-%m-%d %H:%M:%S "); - msg.insert(0, ss.str()); - } + msg.insert(0, ss.str()); + } - log_next_time_stamp = (msg.find('\n') != std::string::npos); + log_next_time_stamp = (msg.find('\n') != std::string::npos); - Utils::IO::WriteFile(IW4x_fail2ban_location.get(), msg, true); + Utils::IO::WriteFile(IW4x_fail2ban_location.get(), msg, true); } void Logger::Frame() @@ -227,28 +227,28 @@ namespace Components pushad - push [esp + 28h] + push[esp + 28h] call PrintMessagePipe add esp, 4h popad ret - returnPrint: + returnPrint : pushad - push 0 // gLog - push [esp + 2Ch] // data - call NetworkLog - add esp, 8h + push 0 // gLog + push[esp + 2Ch] // data + call NetworkLog + add esp, 8h - popad + popad - push esi - mov esi, [esp + 0Ch] + push esi + mov esi, [esp + 0Ch] - push 4AA835h - ret + push 4AA835h + ret } } @@ -262,13 +262,16 @@ namespace Components { const auto* g_log = (*Game::g_log) ? (*Game::g_log)->current.string : ""; - if (std::strcmp(g_log, file) == 0) + if (g_log) // This can be null - has happened before { - if (std::strcmp(folder, "userraw") != 0) + if (std::strcmp(g_log, file) == 0) { - if (IW4x_one_log.get()) + if (std::strcmp(folder, "userraw") != 0) { - strncpy_s(folder, 256, "userraw", _TRUNCATE); + if (IW4x_one_log.get()) + { + strncpy_s(folder, 256, "userraw", _TRUNCATE); + } } } } @@ -280,8 +283,8 @@ namespace Components { pushad - push [esp + 20h + 8h] - push [esp + 20h + 10h] + push[esp + 20h + 8h] + push[esp + 20h + 10h] call RedirectOSPath add esp, 8h @@ -310,116 +313,116 @@ namespace Components void Logger::AddServerCommands() { Command::AddSV("log_add", [](const Command::Params* params) - { - if (params->size() < 2) return; - - std::unique_lock lock(LoggingMutex); - - Network::Address addr(params->get(1)); - if (std::ranges::find(LoggingAddresses[0], addr) == LoggingAddresses[0].end()) { - LoggingAddresses[0].push_back(addr); - } - }); + if (params->size() < 2) return; + + std::unique_lock lock(LoggingMutex); + + Network::Address addr(params->get(1)); + if (std::ranges::find(LoggingAddresses[0], addr) == LoggingAddresses[0].end()) + { + LoggingAddresses[0].push_back(addr); + } + }); Command::AddSV("log_del", [](const Command::Params* params) - { - if (params->size() < 2) return; - - std::unique_lock lock(LoggingMutex); - - const auto num = std::atoi(params->get(1)); - if (!std::strcmp(VA("%i", num), params->get(1)) && static_cast(num) < LoggingAddresses[0].size()) { - auto addr = Logger::LoggingAddresses[0].begin() + num; - Print("Address {} removed\n", addr->getString()); - LoggingAddresses[0].erase(addr); - } - else - { - Network::Address addr(params->get(1)); + if (params->size() < 2) return; - if (const auto i = std::ranges::find(LoggingAddresses[0], addr); i != LoggingAddresses[0].end()) + std::unique_lock lock(LoggingMutex); + + const auto num = std::atoi(params->get(1)); + if (!std::strcmp(VA("%i", num), params->get(1)) && static_cast(num) < LoggingAddresses[0].size()) { - LoggingAddresses[0].erase(i); - Print("Address {} removed\n", addr.getString()); + auto addr = Logger::LoggingAddresses[0].begin() + num; + Print("Address {} removed\n", addr->getString()); + LoggingAddresses[0].erase(addr); } else { - Print("Address {} not found!\n", addr.getString()); + Network::Address addr(params->get(1)); + + if (const auto i = std::ranges::find(LoggingAddresses[0], addr); i != LoggingAddresses[0].end()) + { + LoggingAddresses[0].erase(i); + Print("Address {} removed\n", addr.getString()); + } + else + { + Print("Address {} not found!\n", addr.getString()); + } } - } - }); + }); Command::AddSV("log_list", []([[maybe_unused]] const Command::Params* params) - { - Print("# ID: Address\n"); - Print("-------------\n"); - - std::unique_lock lock(LoggingMutex); - - for (unsigned int i = 0; i < LoggingAddresses[0].size(); ++i) { - Print("#{:03d}: {}\n", i, LoggingAddresses[0][i].getString()); - } - }); + Print("# ID: Address\n"); + Print("-------------\n"); + + std::unique_lock lock(LoggingMutex); + + for (unsigned int i = 0; i < LoggingAddresses[0].size(); ++i) + { + Print("#{:03d}: {}\n", i, LoggingAddresses[0][i].getString()); + } + }); Command::AddSV("g_log_add", [](const Command::Params* params) - { - if (params->size() < 2) return; - - std::unique_lock lock(LoggingMutex); - - const Network::Address addr(params->get(1)); - if (std::ranges::find(LoggingAddresses[1], addr) == LoggingAddresses[1].end()) { - LoggingAddresses[1].push_back(addr); - } - }); + if (params->size() < 2) return; + + std::unique_lock lock(LoggingMutex); + + const Network::Address addr(params->get(1)); + if (std::ranges::find(LoggingAddresses[1], addr) == LoggingAddresses[1].end()) + { + LoggingAddresses[1].push_back(addr); + } + }); Command::AddSV("g_log_del", [](const Command::Params* params) - { - if (params->size() < 2) return; - - std::unique_lock lock(LoggingMutex); - - const auto num = std::atoi(params->get(1)); - if (!std::strcmp(VA("%i", num), params->get(1)) && static_cast(num) < LoggingAddresses[1].size()) { - const auto addr = LoggingAddresses[1].begin() + num; - Print("Address {} removed\n", addr->getString()); - LoggingAddresses[1].erase(addr); - } - else - { - const Network::Address addr(params->get(1)); - const auto i = std::ranges::find(LoggingAddresses[1].begin(), LoggingAddresses[1].end(), addr); - if (i != LoggingAddresses[1].end()) + if (params->size() < 2) return; + + std::unique_lock lock(LoggingMutex); + + const auto num = std::atoi(params->get(1)); + if (!std::strcmp(VA("%i", num), params->get(1)) && static_cast(num) < LoggingAddresses[1].size()) { - LoggingAddresses[1].erase(i); - Print("Address {} removed\n", addr.getString()); + const auto addr = LoggingAddresses[1].begin() + num; + Print("Address {} removed\n", addr->getString()); + LoggingAddresses[1].erase(addr); } else { - Print("Address {} not found!\n", addr.getString()); + const Network::Address addr(params->get(1)); + const auto i = std::ranges::find(LoggingAddresses[1].begin(), LoggingAddresses[1].end(), addr); + if (i != LoggingAddresses[1].end()) + { + LoggingAddresses[1].erase(i); + Print("Address {} removed\n", addr.getString()); + } + else + { + Print("Address {} not found!\n", addr.getString()); + } } - } - }); + }); Command::AddSV("g_log_list", []([[maybe_unused]] const Command::Params* params) - { - Print("# ID: Address\n"); - Print("-------------\n"); - - std::unique_lock lock(LoggingMutex); - for (std::size_t i = 0; i < LoggingAddresses[1].size(); ++i) { - Print("#{:03d}: {}\n", i, LoggingAddresses[1][i].getString()); - } - }); + Print("# ID: Address\n"); + Print("-------------\n"); + + std::unique_lock lock(LoggingMutex); + for (std::size_t i = 0; i < LoggingAddresses[1].size(); ++i) + { + Print("#{:03d}: {}\n", i, LoggingAddresses[1][i].getString()); + } + }); } - void PrintAliasError(Game::conChannel_t channel, const char * originalMsg, const char* soundName, const char* lastErrorStr) + void PrintAliasError(Game::conChannel_t channel, const char* originalMsg, const char* soundName, const char* lastErrorStr) { // We add a bit more info and we clear the sound stream when it happens // to avoid spamming the error @@ -461,10 +464,10 @@ namespace Components Events::OnSVInit(AddServerCommands); Events::OnDvarInit([] - { - IW4x_one_log = Dvar::Register("iw4x_onelog", false, Game::DVAR_LATCH, "Only write the game log to the 'userraw' OS folder"); - IW4x_fail2ban_location = Dvar::Register("iw4x_fail2ban_location", "/var/log/iw4x.log", Game::DVAR_NONE, "Fail2Ban logfile location"); - }); + { + IW4x_one_log = Dvar::Register("iw4x_onelog", false, Game::DVAR_LATCH, "Only write the game log to the 'userraw' OS folder"); + IW4x_fail2ban_location = Dvar::Register("iw4x_fail2ban_location", "/var/log/iw4x.log", Game::DVAR_NONE, "Fail2Ban logfile location"); + }); } Logger::~Logger() diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 5300c66f..bdc3d45e 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -103,9 +103,9 @@ namespace Components const char* Maps::LoadArenaFileStub(const char* name, char* buffer, int size) { - std::string data = RawFiles::ReadRawFile(name, buffer, size); + std::string data; - if (Maps::UserMap.isValid()) + if (Maps::UserMap.isValid()) { const auto mapname = Maps::UserMap.getName(); const auto arena = GetArenaPath(mapname); @@ -116,6 +116,10 @@ namespace Components data = Utils::IO::ReadFile(arena); } } + else + { + data = RawFiles::ReadRawFile(name, buffer, size); + } strncpy_s(buffer, size, data.data(), _TRUNCATE); return buffer; @@ -141,11 +145,11 @@ namespace Components Maps::CurrentDependencies.clear(); auto dependencies = GetDependenciesForMap(zoneInfo->name); - + std::vector data; Utils::Merge(&data, zoneInfo, zoneCount); Utils::Memory::Allocator allocator; - + if (dependencies.requiresTeamZones) { auto teams = dependencies.requiredTeams; @@ -177,7 +181,7 @@ namespace Components auto patchZone = std::format("patch_{}", zoneInfo->name); if (FastFiles::Exists(patchZone)) { - data.push_back({patchZone.data(), zoneInfo->allocFlags, zoneInfo->freeFlags}); + data.push_back({ patchZone.data(), zoneInfo->allocFlags, zoneInfo->freeFlags }); } return FastFiles::LoadLocalizeZones(data.data(), data.size(), sync); @@ -185,18 +189,18 @@ namespace Components void Maps::OverrideMapEnts(Game::MapEnts* ents) { - auto callback = [] (Game::XAssetHeader header, void* ents) - { - Game::MapEnts* mapEnts = reinterpret_cast(ents); - Game::clipMap_t* clipMap = header.clipMap; - - if (clipMap && mapEnts && !_stricmp(mapEnts->name, clipMap->name)) + auto callback = [](Game::XAssetHeader header, void* ents) { - clipMap->mapEnts = mapEnts; - //*Game::marMapEntsPtr = mapEnts; - //Game::G_SpawnEntitiesFromString(); - } - }; + Game::MapEnts* mapEnts = reinterpret_cast(ents); + Game::clipMap_t* clipMap = header.clipMap; + + if (clipMap && mapEnts && !_stricmp(mapEnts->name, clipMap->name)) + { + clipMap->mapEnts = mapEnts; + //*Game::marMapEntsPtr = mapEnts; + //Game::G_SpawnEntitiesFromString(); + } + }; // Internal doesn't lock the thread, as locking is impossible, due to executing this in the thread that holds the current lock Game::DB_EnumXAssets_Internal(Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, callback, ents, true); @@ -276,7 +280,7 @@ namespace Components Logger::Print("Waiting for database...\n"); while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); - if (!Game::DB_XAssetPool[Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP].gameWorldMp || !Game::DB_XAssetPool[Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP].gameWorldMp->name || + if (!Game::DB_XAssetPool[Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP].gameWorldMp || !Game::DB_XAssetPool[Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP].gameWorldMp->name || !Game::DB_XAssetPool[Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP].gameWorldMp->g_glassData || Maps::SPMap) { return Game::DB_XAssetPool[Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP].gameWorldSp->g_glassData; @@ -294,7 +298,7 @@ namespace Components call Maps::GetWorldData - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax @@ -314,6 +318,22 @@ namespace Components } } + void Maps::ForceRefreshArenas() + { + if (Game::Sys_IsMainThread()) + { + Game::sharedUiInfo_t* uiInfo = reinterpret_cast(0x62E4B78); + uiInfo->updateArenas = 1; + Game::UI_UpdateArenas(); + } + else + { + // Dangerous!! + assert(false, "Arenas refreshed from wrong thread??"); + Logger::Print("Tried to refresh arenas from a thread that is NOT the main thread!! This could have lead to a crash. Please report this bug and how you got it!"); + } + } + // TODO : Remove hook entirely? void Maps::GetBSPName(char* buffer, size_t size, const char* format, const char* mapname) { @@ -459,7 +479,7 @@ namespace Components char hashBuf[100] = { 0 }; unsigned int hash = atoi(Game::MSG_ReadStringLine(msg, hashBuf, sizeof hashBuf)); - if(!Maps::CheckMapInstalled(mapname, false, true) || hash && hash != Maps::GetUsermapHash(mapname)) + if (!Maps::CheckMapInstalled(mapname, false, true) || hash && hash != Maps::GetUsermapHash(mapname)) { // Reconnecting forces the client to download the new map Command::Execute("disconnect", false); @@ -480,12 +500,12 @@ namespace Components push eax pushad - push [esp + 28h] + push[esp + 28h] push ebp call Maps::TriggerReconnectForMap add esp, 8h - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax @@ -495,7 +515,7 @@ namespace Components push 487C50h // Rotate map - skipRotation: + skipRotation : retn } } @@ -506,7 +526,7 @@ namespace Components { pushad - push [esp + 24h] + push[esp + 24h] call Maps::PrepareUsermap pop eax @@ -523,7 +543,7 @@ namespace Components { pushad - push [esp + 24h] + push[esp + 24h] call Maps::PrepareUsermap pop eax @@ -668,7 +688,7 @@ namespace Components Logger::Error(Game::ERR_DISCONNECT, "Missing map file {}.\nYou may have a damaged installation or are attempting to load a non-existent map.", mapname); } - + return false; } @@ -706,7 +726,7 @@ namespace Components void Maps::G_SpawnTurretHook(Game::gentity_s* ent, int unk, int unk2) { - if (Maps::CurrentMainZone == "mp_backlot_sh"s || Maps::CurrentMainZone == "mp_con_spring"s || + if (Maps::CurrentMainZone == "mp_backlot_sh"s || Maps::CurrentMainZone == "mp_con_spring"s || Maps::CurrentMainZone == "mp_mogadishu_sh"s || Maps::CurrentMainZone == "mp_nightshift_sh"s) { return; @@ -736,7 +756,7 @@ namespace Components Logger::Error(Game::errorParm_t::ERR_DROP, "Invalid trigger index ({}) in entities exceeds the maximum trigger count ({}) defined in the clipmap. Check your map ents, or your clipmap!", triggerIndex, ents->trigger.count); return 0; } - else + else { return Utils::Hook::Call(0x4416C0)(triggerIndex, bounds); } @@ -744,29 +764,29 @@ namespace Components return 0; } - + Maps::Maps() { Scheduler::Once([] - { - Dvar::Register("isDlcInstalled_All", false, Game::DVAR_EXTERNAL | Game::DVAR_INIT, ""); - Maps::RListSModels = Dvar::Register("r_listSModels", false, Game::DVAR_NONE, "Display a list of visible SModels"); - - Maps::AddDlc({ 1, "Stimulus Pack", {"mp_complex", "mp_compact", "mp_storm", "mp_overgrown", "mp_crash"} }); - Maps::AddDlc({ 2, "Resurgence Pack", {"mp_abandon", "mp_vacant", "mp_trailerpark", "mp_strike", "mp_fuel2"} }); - Maps::AddDlc({ 3, "IW4x Classics", {"mp_nuked", "mp_cross_fire", "mp_cargoship", "mp_bloc", "mp_killhouse", "mp_bog_sh", "mp_cargoship_sh", "mp_shipment", "mp_shipment_long", "mp_rust_long", "mp_firingrange", "mp_bloc_sh", "mp_crash_tropical", "mp_estate_tropical", "mp_fav_tropical", "mp_storm_spring"} }); - Maps::AddDlc({ 4, "Call Of Duty 4 Pack", {"mp_farm", "mp_backlot", "mp_pipeline", "mp_countdown", "mp_crash_snow", "mp_carentan", "mp_broadcast", "mp_showdown", "mp_convoy", "mp_citystreets"} }); - Maps::AddDlc({ 5, "Modern Warfare 3 Pack", {"mp_dome", "mp_hardhat", "mp_paris", "mp_seatown", "mp_bravo", "mp_underground", "mp_plaza2", "mp_village", "mp_alpha"}}); - - Maps::UpdateDlcStatus(); - - UIScript::Add("downloadDLC", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) { - const auto dlc = token.get(); + Dvar::Register("isDlcInstalled_All", false, Game::DVAR_EXTERNAL | Game::DVAR_INIT, ""); + Maps::RListSModels = Dvar::Register("r_listSModels", false, Game::DVAR_NONE, "Display a list of visible SModels"); - Game::ShowMessageBox(Utils::String::VA("DLC %d does not exist!", dlc), "ERROR"); - }); - }, Scheduler::Pipeline::MAIN); + Maps::AddDlc({ 1, "Stimulus Pack", {"mp_complex", "mp_compact", "mp_storm", "mp_overgrown", "mp_crash"} }); + Maps::AddDlc({ 2, "Resurgence Pack", {"mp_abandon", "mp_vacant", "mp_trailerpark", "mp_strike", "mp_fuel2"} }); + Maps::AddDlc({ 3, "IW4x Classics", {"mp_nuked", "mp_cross_fire", "mp_cargoship", "mp_bloc", "mp_killhouse", "mp_bog_sh", "mp_cargoship_sh", "mp_shipment", "mp_shipment_long", "mp_rust_long", "mp_firingrange", "mp_bloc_sh", "mp_crash_tropical", "mp_estate_tropical", "mp_fav_tropical", "mp_storm_spring"} }); + Maps::AddDlc({ 4, "Call Of Duty 4 Pack", {"mp_farm", "mp_backlot", "mp_pipeline", "mp_countdown", "mp_crash_snow", "mp_carentan", "mp_broadcast", "mp_showdown", "mp_convoy", "mp_citystreets"} }); + Maps::AddDlc({ 5, "Modern Warfare 3 Pack", {"mp_dome", "mp_hardhat", "mp_paris", "mp_seatown", "mp_bravo", "mp_underground", "mp_plaza2", "mp_village", "mp_alpha"} }); + + Maps::UpdateDlcStatus(); + + UIScript::Add("downloadDLC", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) + { + const auto dlc = token.get(); + + Game::ShowMessageBox(Utils::String::VA("DLC %d does not exist!", dlc), "ERROR"); + }); + }, Scheduler::Pipeline::MAIN); // disable turrets on CoD:OL 448+ maps for now Utils::Hook(0x5EE577, Maps::G_SpawnTurretHook, HOOK_CALL).install()->quick(); @@ -782,7 +802,7 @@ namespace Components #endif // - + //#define SORT_SMODELS #if !defined(DEBUG) || !defined(SORT_SMODELS) // Don't sort static models @@ -825,13 +845,13 @@ namespace Components Utils::Hook(0x5B34DD, Maps::LoadMapLoadscreenStub, HOOK_CALL).install()->quick(); Command::Add("delayReconnect", []() - { - Scheduler::Once([] { - Command::Execute("closemenu popup_reconnectingtoparty", false); - Command::Execute("reconnect", false); - }, Scheduler::Pipeline::CLIENT, 10s); - }); + Scheduler::Once([] + { + Command::Execute("closemenu popup_reconnectingtoparty", false); + Command::Execute("reconnect", false); + }, Scheduler::Pipeline::CLIENT, 10s); + }); if (Dedicated::IsEnabled()) { @@ -845,12 +865,6 @@ namespace Components // Load usermap arena file Utils::Hook(0x630A88, Maps::LoadArenaFileStub, HOOK_CALL).install()->quick(); - // Always refresh arena when loading or unloading a zone - Utils::Hook::Nop(0x485017, 2); - Utils::Hook::Nop(0x4BDFB7, 2); // Unknown - Utils::Hook::Nop(0x45ED6F, 2); // loadGameInfo - Utils::Hook::Nop(0x4A5888, 2); // UI_InitOnceForAllClients - // Allow hiding specific smodels Utils::Hook(0x50E67C, Maps::HideModelStub, HOOK_CALL).install()->quick(); @@ -861,33 +875,33 @@ namespace Components // Client only Scheduler::Loop([] - { - auto*& gameWorld = *reinterpret_cast(0x66DEE94); - if (!Game::CL_IsCgameInitialized() || !gameWorld || !Maps::RListSModels.get()) return; - - std::map models; - for (unsigned int i = 0; i < gameWorld->dpvs.smodelCount; ++i) { - if (gameWorld->dpvs.smodelVisData[0][i]) + auto*& gameWorld = *reinterpret_cast(0x66DEE94); + if (!Game::CL_IsCgameInitialized() || !gameWorld || !Maps::RListSModels.get()) return; + + std::map models; + for (unsigned int i = 0; i < gameWorld->dpvs.smodelCount; ++i) { - std::string name = gameWorld->dpvs.smodelDrawInsts[i].model->name; + if (gameWorld->dpvs.smodelVisData[0][i]) + { + std::string name = gameWorld->dpvs.smodelDrawInsts[i].model->name; - if (!models.contains(name)) models[name] = 1; - else models[name]++; + if (!models.contains(name)) models[name] = 1; + else models[name]++; + } } - } - Game::Font_s* font = Game::R_RegisterFont("fonts/smallFont", 0); - auto height = Game::R_TextHeight(font); - auto scale = 0.75f; - float color[4] = {0.0f, 1.0f, 0.0f, 1.0f}; + Game::Font_s* font = Game::R_RegisterFont("fonts/smallFont", 0); + auto height = Game::R_TextHeight(font); + auto scale = 0.75f; + float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; - unsigned int i = 0; - for (auto& model : models) - { - Game::R_AddCmdDrawText(Utils::String::VA("%d %s", model.second, model.first.data()), std::numeric_limits::max(), font, 15.0f, (height * scale + 1) * (i++ + 1) + 15.0f, scale, scale, 0.0f, color, Game::ITEM_TEXTSTYLE_NORMAL); - } - }, Scheduler::Pipeline::RENDERER); + unsigned int i = 0; + for (auto& model : models) + { + Game::R_AddCmdDrawText(Utils::String::VA("%d %s", model.second, model.first.data()), std::numeric_limits::max(), font, 15.0f, (height * scale + 1) * (i++ + 1) + 15.0f, scale, scale, 0.0f, color, Game::ITEM_TEXTSTYLE_NORMAL); + } + }, Scheduler::Pipeline::RENDERER); } Maps::~Maps() diff --git a/src/Components/Modules/Maps.hpp b/src/Components/Modules/Maps.hpp index 356cf6fe..aca7d6bc 100644 --- a/src/Components/Modules/Maps.hpp +++ b/src/Components/Modules/Maps.hpp @@ -13,7 +13,7 @@ namespace Components { ZeroMemory(&this->searchPath, sizeof this->searchPath); this->hash = Maps::GetUsermapHash(this->mapname); - Game::UI_UpdateArenas(); + Maps::ForceRefreshArenas(); } ~UserMapContainer() @@ -29,7 +29,10 @@ namespace Components { bool wasValid = this->isValid(); this->mapname.clear(); - if (wasValid) Game::UI_UpdateArenas(); + if (wasValid) + { + Maps::ForceRefreshArenas(); + } } void loadIwd(); @@ -95,6 +98,8 @@ namespace Components static Dvar::Var RListSModels; + static void ForceRefreshArenas(); + static void GetBSPName(char* buffer, size_t size, const char* format, const char* mapname); static void LoadAssetRestrict(Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* restrict); static void LoadMapZones(Game::XZoneInfo *zoneInfo, unsigned int zoneCount, int sync); From da9b2b415bb2796407ba7dca36f75ef12b1708b6 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 16 Jun 2024 01:06:11 +0200 Subject: [PATCH 67/71] Fix assert & github CI maybe? --- src/Components/Modules/Logger.hpp | 2 +- src/Components/Modules/Maps.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Logger.hpp b/src/Components/Modules/Logger.hpp index 8b43704d..2b23ae8b 100644 --- a/src/Components/Modules/Logger.hpp +++ b/src/Components/Modules/Logger.hpp @@ -83,7 +83,7 @@ namespace Components static void PrintFail2Ban(const std::string_view& fmt) { - PrintFail2BanInternal(fmt, std::make_format_args(0)); + PrintFail2BanInternal(fmt, std::make_format_args()); } template diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index bdc3d45e..1c76073b 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -329,7 +329,7 @@ namespace Components else { // Dangerous!! - assert(false, "Arenas refreshed from wrong thread??"); + assert(false && "Arenas refreshed from wrong thread??"); Logger::Print("Tried to refresh arenas from a thread that is NOT the main thread!! This could have lead to a crash. Please report this bug and how you got it!"); } } From 5ac6b8cd195cb4c0fe496ae3f4b3d77c7f493f6b Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 16 Jun 2024 01:12:49 +0200 Subject: [PATCH 68/71] make_format_args ? --- src/Components/Modules/Logger.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Components/Modules/Logger.hpp b/src/Components/Modules/Logger.hpp index 2b23ae8b..ead3d695 100644 --- a/src/Components/Modules/Logger.hpp +++ b/src/Components/Modules/Logger.hpp @@ -23,12 +23,12 @@ namespace Components static void Print(const std::string_view& fmt) { - PrintInternal(Game::CON_CHANNEL_DONT_FILTER, fmt, std::make_format_args(0)); + PrintInternal(Game::CON_CHANNEL_DONT_FILTER, fmt, std::make_format_args()); } static void Print(Game::conChannel_t channel, const std::string_view& fmt) { - PrintInternal(channel, fmt, std::make_format_args(0)); + PrintInternal(channel, fmt, std::make_format_args()); } template @@ -47,7 +47,7 @@ namespace Components static void Error(Game::errorParm_t error, const std::string_view& fmt) { - ErrorInternal(error, fmt, std::make_format_args(0)); + ErrorInternal(error, fmt, std::make_format_args()); } template @@ -59,7 +59,7 @@ namespace Components static void Warning(Game::conChannel_t channel, const std::string_view& fmt) { - WarningInternal(channel, fmt, std::make_format_args(0)); + WarningInternal(channel, fmt, std::make_format_args()); } template @@ -71,7 +71,7 @@ namespace Components static void PrintError(Game::conChannel_t channel, const std::string_view& fmt) { - PrintErrorInternal(channel, fmt, std::make_format_args(0)); + PrintErrorInternal(channel, fmt, std::make_format_args()); } template From 7d974ecd8278f7fafb10e31ff9d40783f7650327 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 16 Jun 2024 10:07:20 +0200 Subject: [PATCH 69/71] Do not refresh arena if g_quitRequested --- src/Components/Modules/Maps.cpp | 6 ++++++ src/Game/Game.cpp | 1 + src/Game/Game.hpp | 1 + 3 files changed, 8 insertions(+) diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 1c76073b..e4e6c2c0 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -322,6 +322,12 @@ namespace Components { if (Game::Sys_IsMainThread()) { + if (*Game::g_quitRequested) + { + // No need to refresh if we're exiting the game + return; + } + Game::sharedUiInfo_t* uiInfo = reinterpret_cast(0x62E4B78); uiInfo->updateArenas = 1; Game::UI_UpdateArenas(); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index e2d26a17..0b90782c 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -17,6 +17,7 @@ namespace Game G_DebugLineWithDuration_t G_DebugLineWithDuration = G_DebugLineWithDuration_t(0x4C3280); gentity_s* g_entities = reinterpret_cast(0x18835D8); + bool* g_quitRequested = reinterpret_cast(0x649FB61); NetField* clientStateFields = reinterpret_cast(0x741E40); size_t clientStateFieldsCount = Utils::Hook::Get(0x7433C8); diff --git a/src/Game/Game.hpp b/src/Game/Game.hpp index 12d81a94..12b3fe9c 100644 --- a/src/Game/Game.hpp +++ b/src/Game/Game.hpp @@ -52,6 +52,7 @@ namespace Game constexpr std::size_t MAX_GENTITIES = 2048; constexpr std::size_t ENTITYNUM_NONE = MAX_GENTITIES - 1; extern gentity_s* g_entities; + extern bool* g_quitRequested; // This does not belong anywhere else extern NetField* clientStateFields; From 83407f78955d6709a5bedd8d68481321e664277f Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 16 Jun 2024 10:10:26 +0200 Subject: [PATCH 70/71] Bump IW4OF --- deps/iw4-open-formats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/iw4-open-formats b/deps/iw4-open-formats index 9c245e79..f4ff5d53 160000 --- a/deps/iw4-open-formats +++ b/deps/iw4-open-formats @@ -1 +1 @@ -Subproject commit 9c245e794c7c452de82aa9fd07e6b3edb27be9b7 +Subproject commit f4ff5d532aeaac1c3254ef20ace687ab41622ab9 From 8b0c3cd70790406cc9b43e4907c77397eec81be3 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Wed, 19 Jun 2024 14:04:10 +0200 Subject: [PATCH 71/71] Default sv_network_fps is default --- src/Components/Modules/Dvar.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index 74d883d0..ea041d37 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -248,9 +248,10 @@ namespace Components return Name.get(); } - const Game::dvar_t* Dvar::Dvar_RegisterSVNetworkFps(const char* dvarName, int /*value*/, int min, int /*max*/, std::uint16_t /*flags*/, const char* description) + const Game::dvar_t* Dvar::Dvar_RegisterSVNetworkFps(const char* dvarName, int value, int min, int /*max*/, std::uint16_t /*flags*/, const char* description) { - return Game::Dvar_RegisterInt(dvarName, 1000, min, 1000, Game::DVAR_NONE, description); + // bump limit up to 1000 + return Game::Dvar_RegisterInt(dvarName, value, min, 1000, Game::DVAR_NONE, description); } const Game::dvar_t* Dvar::Dvar_RegisterPerkExtendedMeleeRange(const char* dvarName, float value, float min, float /*max*/, std::uint16_t flags, const char* description)