From b7201669134a6521e0197e9fa9f6d2fbbc3ed645 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Mon, 13 Jun 2022 00:02:20 +0200 Subject: [PATCH] Revert change to ZoneBuilder & Add simple callback function to add commands --- src/Components/Modules/Auth.cpp | 4 +- src/Components/Modules/Command.cpp | 12 ++- src/Components/Modules/Command.hpp | 5 +- src/Components/Modules/Friends.cpp | 2 +- src/Components/Modules/QuickPatch.cpp | 118 +++++++++---------------- src/Components/Modules/ZoneBuilder.cpp | 22 +++-- src/Components/Modules/ZoneBuilder.hpp | 4 +- 7 files changed, 76 insertions(+), 91 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index a3ff5fb1..226c9e17 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -457,7 +457,7 @@ namespace Components { if (params->size() < 2) { - uint32_t level = Auth::GetZeroBits(Auth::GuidToken, Auth::GuidKey.getPublicKey()); + const auto level = Auth::GetZeroBits(Auth::GuidToken, Auth::GuidKey.getPublicKey()); Logger::Print("Your current security level is {}\n", level); Logger::Print("Your security token is: {}\n", Utils::String::DumpHex(Auth::GuidToken.toString(), "")); Logger::Print("Your computation token is: {}\n", Utils::String::DumpHex(Auth::ComputeToken.toString(), "")); @@ -466,7 +466,7 @@ namespace Components } else { - uint32_t level = static_cast(atoi(params->get(1))); + const auto level = static_cast(atoi(params->get(1))); Auth::IncreaseSecurityLevel(level); } }); diff --git a/src/Components/Modules/Command.cpp b/src/Components/Modules/Command.cpp index 6144d1e6..a0acb4a8 100644 --- a/src/Components/Modules/Command.cpp +++ b/src/Components/Modules/Command.cpp @@ -60,7 +60,15 @@ namespace Components return Game::sv_cmd_args->argv[this->nesting_][index]; } - void Command::Add(const char* name, std::function callback) + void Command::Add(const char* name, const std::function& callback) + { + Add(name, [callback]([[maybe_unused]] const Command::Params* params) + { + callback(); + }); + } + + void Command::Add(const char* name, const std::function& callback) { const auto command = Utils::String::ToLower(name); @@ -72,7 +80,7 @@ namespace Components Command::FunctionMap.insert_or_assign(command, std::move(callback)); } - void Command::AddSV(const char* name, std::function callback) + void Command::AddSV(const char* name, const std::function& callback) { if (Loader::IsPregame()) { diff --git a/src/Components/Modules/Command.hpp b/src/Components/Modules/Command.hpp index 45dd9c0f..cf67995c 100644 --- a/src/Components/Modules/Command.hpp +++ b/src/Components/Modules/Command.hpp @@ -51,8 +51,9 @@ namespace Components static Game::cmd_function_t* Allocate(); - static void Add(const char* name, std::function callback); - static void AddSV(const char* name, std::function callback); + static void Add(const char* name, const std::function& callback); + static void Add(const char* name, const std::function& callback); + static void AddSV(const char* name, const std::function& callback); static void AddRaw(const char* name, void(*callback)(), bool key = false); static void AddRawSV(const char* name, void(*callback)()); static void Execute(std::string command, bool sync = true); diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index 390aee75..ea227ebb 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -589,7 +589,7 @@ namespace Components Command::Add("addFriend", [](Command::Params* params) { - if (params->size() < 2u) + if (params->size() < 2) { Logger::Print("Usage: {} \n", params->get(0)); return; diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index 6b842af2..c453dc36 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -503,10 +503,7 @@ namespace Components // Ignore call to print 'Offhand class mismatch when giving weapon...' Utils::Hook(0x5D9047, 0x4BB9B0, HOOK_CALL).install()->quick(); - Command::Add("unlockstats", [](Command::Params*) - { - QuickPatch::UnlockStats(); - }); + Command::Add("unlockstats", QuickPatch::UnlockStats); Command::Add("dumptechsets", [](Command::Params* param) { @@ -515,23 +512,26 @@ namespace Components Logger::Print("usage: dumptechsets | all\n"); return; } - std::vector fastfiles; + + std::vector fastFiles; if (param->get(1) == "all"s) { - for (std::string f : Utils::IO::ListFiles("zone/english")) - fastfiles.push_back(f.substr(7, f.length() - 10)); - for (std::string f : Utils::IO::ListFiles("zone/dlc")) - fastfiles.push_back(f.substr(3, f.length() - 6)); - for (std::string f : Utils::IO::ListFiles("zone/patch")) - fastfiles.push_back(f.substr(5, f.length() - 8)); + for (const auto& f : Utils::IO::ListFiles("zone/english")) + fastFiles.emplace_back(f.substr(7, f.length() - 10)); + + for (const auto& f : Utils::IO::ListFiles("zone/dlc")) + fastFiles.emplace_back(f.substr(3, f.length() - 6)); + + for (const auto& f : Utils::IO::ListFiles("zone/patch")) + fastFiles.emplace_back(f.substr(5, f.length() - 8)); } else { - fastfiles.push_back(param->get(1)); + fastFiles.emplace_back(param->get(1)); } - int count = 0; + auto count = 0; AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* /*restrict*/) { @@ -553,7 +553,7 @@ namespace Components if (Utils::IO::FileExists(Utils::String::VA(formatString, name.data()))) return; Utils::Stream buffer(0x1000); - Game::MaterialPixelShader* dest = buffer.dest(); + auto* dest = buffer.dest(); buffer.save(asset.pixelShader); if (asset.pixelShader->prog.loadDef.program) @@ -565,31 +565,6 @@ namespace Components Utils::IO::WriteFile(Utils::String::VA(formatString, name.data()), buffer.toBuffer()); } - static std::map pointerMap; - - // Check if the given pointer has already been mapped - std::function hasPointer = [](const void* pointer) - { - return (pointerMap.find(pointer) != pointerMap.end()); - }; - - // Get stored offset for given file pointer - std::function getPointer = [hasPointer](const void* pointer) - { - if (hasPointer(pointer)) - { - return pointerMap[pointer]; - } - - return 0U; - }; - - std::function storePointer = [hasPointer](const void* ptr, unsigned int offset) - { - if (hasPointer(ptr)) return; - pointerMap[ptr] = offset; - }; - if (type == Game::ASSET_TYPE_TECHNIQUE_SET) { Utils::IO::CreateDir("userraw/techsets"); @@ -609,59 +584,54 @@ namespace Components if (technique) { - dest->techniques[i] = reinterpret_cast(getPointer(technique)); - if (!dest->techniques) + // Size-check is obsolete, as the structure is dynamic + buffer.align(Utils::Stream::ALIGN_4); + + Game::MaterialTechnique* destTechnique = buffer.dest(); + buffer.save(technique, 8); + + // Save_MaterialPassArray + Game::MaterialPass* destPasses = buffer.dest(); + buffer.saveArray(technique->passArray, technique->passCount); + + for (std::uint16_t j = 0; j < technique->passCount; ++j) { - // Size-check is obsolete, as the structure is dynamic - buffer.align(Utils::Stream::ALIGN_4); - //storePointer(technique, buffer->); + AssertSize(Game::MaterialPass, 20); - Game::MaterialTechnique* destTechnique = buffer.dest(); - buffer.save(technique, 8); + Game::MaterialPass* destPass = &destPasses[j]; + Game::MaterialPass* pass = &technique->passArray[j]; - // Save_MaterialPassArray - Game::MaterialPass* destPasses = buffer.dest(); - buffer.saveArray(technique->passArray, technique->passCount); - - for (short j = 0; j < technique->passCount; ++j) + if (pass->vertexDecl) { - AssertSize(Game::MaterialPass, 20); - Game::MaterialPass* destPass = &destPasses[j]; - Game::MaterialPass* pass = &technique->passArray[j]; - - if (pass->vertexDecl) - { - - } - - if (pass->args) - { - buffer.align(Utils::Stream::ALIGN_4); - buffer.saveArray(pass->args, pass->perPrimArgCount + pass->perObjArgCount + pass->stableArgCount); - Utils::Stream::ClearPointer(&destPass->args); - } } - if (technique->name) + if (pass->args) { - buffer.saveString(technique->name); - Utils::Stream::ClearPointer(&destTechnique->name); + buffer.align(Utils::Stream::ALIGN_4); + buffer.saveArray(pass->args, pass->perPrimArgCount + pass->perObjArgCount + pass->stableArgCount); + Utils::Stream::ClearPointer(&destPass->args); } - - Utils::Stream::ClearPointer(&dest->techniques[i]); } + + if (technique->name) + { + buffer.saveString(technique->name); + Utils::Stream::ClearPointer(&destTechnique->name); + } + + Utils::Stream::ClearPointer(&dest->techniques[i]); } } } }); - for (std::string fastfile : fastfiles) + for (const auto& fastFile : fastFiles) { - if (!Game::DB_IsZoneLoaded(fastfile.data())) + if (!Game::DB_IsZoneLoaded(fastFile.data())) { Game::XZoneInfo info; - info.name = fastfile.data(); + info.name = fastFile.data(); info.allocFlags = 0x20; info.freeFlags = 0; diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 610122eb..b76b89f5 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -7,7 +7,7 @@ namespace Components bool ZoneBuilder::MainThreadInterrupted; DWORD ZoneBuilder::InterruptingThreadId; - bool ZoneBuilder::Terminate; + volatile bool ZoneBuilder::Terminate = false; std::thread ZoneBuilder::CommandThread; Dvar::Var ZoneBuilder::PreferDiskAssetsDvar; @@ -801,7 +801,12 @@ namespace Components { "localized_ui_mp", Game::DB_ZONE_GAME, 0 } }; - int __stdcall ZoneBuilder::EntryPoint(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nShowCmd*/) + void ZoneBuilder::Com_Quitf_t() + { + ExitProcess(0); + } + + int APIENTRY ZoneBuilder::EntryPoint(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nShowCmd*/) { Utils::Hook::Call(0x42F0A0)(); // Com_InitCriticalSections Utils::Hook::Call(0x4301B0)(); // Com_InitMainThread @@ -835,8 +840,7 @@ namespace Components //Utils::Hook::Call(0x464A90)(); // Com_ParseCommandLine Utils::Hook::Call(0x43D140)(); // Com_EventLoop - ZoneBuilder::Terminate = false; - ZoneBuilder::CommandThread = std::thread([]() + ZoneBuilder::CommandThread = std::thread([] { while (!ZoneBuilder::Terminate) { @@ -847,10 +851,7 @@ namespace Components } }); - Command::Add("quit", []([[maybe_unused]] Command::Params* params) - { - Game::Com_Quitf_t(); - }); + Command::Add("quit", ZoneBuilder::Com_Quitf_t); // now load default assets and shaders if (FastFiles::Exists("defaults") && FastFiles::Exists("techsets")) @@ -1085,6 +1086,9 @@ namespace Components // set new entry point Utils::Hook(0x4513DA, ZoneBuilder::EntryPoint, HOOK_JUMP).install()->quick(); + // set quit handler + Utils::Hook(0x4D4000, ZoneBuilder::Com_Quitf_t, HOOK_JUMP).install()->quick(); + // handle Com_error Calls Utils::Hook(Game::Com_Error, ZoneBuilder::HandleError, HOOK_JUMP).install()->quick(); @@ -1535,7 +1539,7 @@ namespace Components ZoneBuilder::~ZoneBuilder() { ZoneBuilder::Terminate = true; - if(ZoneBuilder::CommandThread.joinable()) + if (ZoneBuilder::CommandThread.joinable()) { ZoneBuilder::CommandThread.join(); } diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 1275382f..210a028f 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -147,10 +147,12 @@ namespace Components static bool IsThreadMainThreadHook(); + static void Com_Quitf_t(); + static bool MainThreadInterrupted; static DWORD InterruptingThreadId; - static bool Terminate; + static volatile bool Terminate; static std::thread CommandThread; }; }