From dbef5f7a1640ea5eaac18b076b4e02a3a5ab702f Mon Sep 17 00:00:00 2001 From: Diavolo Date: Thu, 30 Jun 2022 12:05:56 +0200 Subject: [PATCH 1/2] [Scheduler] Use old hook for renderer pipeline --- src/Components/Modules/Scheduler.cpp | 8 +++----- src/Components/Modules/Scheduler.hpp | 2 +- src/Components/Modules/Toast.cpp | 10 +--------- src/Components/Modules/Toast.hpp | 1 - 4 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/Components/Modules/Scheduler.cpp b/src/Components/Modules/Scheduler.cpp index 858970a4..94a778f4 100644 --- a/src/Components/Modules/Scheduler.cpp +++ b/src/Components/Modules/Scheduler.cpp @@ -67,9 +67,9 @@ namespace Components Pipelines[type].execute(); } - void Scheduler::REndFrame_Hk() + void Scheduler::CL_DrawScreen_Hk() { - Utils::Hook::Call(0x50AB20)(); + Utils::Hook::Call(0x5AC950)(); Execute(Pipeline::RENDERER); } @@ -165,9 +165,7 @@ namespace Components } }); - Utils::Hook(0x4DBE9A, REndFrame_Hk, HOOK_CALL).install()->quick(); - Utils::Hook(0x518D5C, REndFrame_Hk, HOOK_CALL).install()->quick(); - Utils::Hook(0x5ACBA3, REndFrame_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x5ACB99, CL_DrawScreen_Hk, HOOK_CALL).install()->quick(); // Hook G_Glass_Update so we may fix TLS issues Utils::Hook(0x416049, ServerFrame_Hk, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/Scheduler.hpp b/src/Components/Modules/Scheduler.hpp index 5d430a9b..c445e4ad 100644 --- a/src/Components/Modules/Scheduler.hpp +++ b/src/Components/Modules/Scheduler.hpp @@ -59,7 +59,7 @@ namespace Components static void Execute(Pipeline type); - static void REndFrame_Hk(); + static void CL_DrawScreen_Hk(); static void ServerFrame_Hk(); static void ClientFrame_Hk(int localClientNum); static void MainFrame_Hk(); diff --git a/src/Components/Modules/Toast.cpp b/src/Components/Modules/Toast.cpp index 58f07398..4014f043 100644 --- a/src/Components/Modules/Toast.cpp +++ b/src/Components/Modules/Toast.cpp @@ -142,14 +142,6 @@ namespace Components } } - - void Toast::CL_DrawScreen_Stub(int localClientNum) - { - Utils::Hook::Call(0x5AC950)(localClientNum); - - Handler(); - } - Toast::Toast() { if (Dedicated::IsEnabled() || Monitor::IsEnabled() || ZoneBuilder::IsEnabled()) @@ -157,7 +149,7 @@ namespace Components return; } - Utils::Hook(0x5ACB99, CL_DrawScreen_Stub, HOOK_CALL).install()->quick(); + Scheduler::Loop(Handler, Scheduler::Pipeline::RENDERER); #ifdef TEST_TOAST Command::Add("testtoast", []([[maybe_unused]] Command::Params* params) diff --git a/src/Components/Modules/Toast.hpp b/src/Components/Modules/Toast.hpp index f0d805e8..6045d442 100644 --- a/src/Components/Modules/Toast.hpp +++ b/src/Components/Modules/Toast.hpp @@ -23,7 +23,6 @@ namespace Components static void Handler(); static void Draw(UIToast* toast); - static void CL_DrawScreen_Stub(int localClientNum); static std::queue Queue; static std::mutex Mutex; From 16988b24f4b210f36810df89b602de2f83774241 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Thu, 30 Jun 2022 13:32:19 +0200 Subject: [PATCH 2/2] [Renderer] Fix crash when debugging oilrig --- src/Components/Modules/Renderer.cpp | 71 ++++++++++++++++------------- src/Game/Functions.hpp | 4 +- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index dd18f02a..4c369e1e 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -177,7 +177,7 @@ namespace Components auto entities = Game::g_entities; - for (auto i = 0u; i < 0x800u; i++) + for (std::size_t i = 0; i < Game::MAX_GENTITIES; ++i) { auto* ent = &entities[i]; @@ -229,7 +229,7 @@ namespace Components auto scene = Game::scene; - for (auto i = 0; i < scene->sceneModelCount; i++) + for (auto i = 0; i < scene->sceneModelCount; ++i) { if (!scene->sceneModel[i].model) continue; @@ -255,23 +255,27 @@ namespace Components if (!val) return; - // Ingame only - int clientNum = Game::CG_GetClientNum(); - if (!Game::CL_IsCgameInitialized() || - clientNum >= 18 || - clientNum < 0 || - Game::g_entities[clientNum].client == nullptr) { - + auto clientNum = Game::CG_GetClientNum(); + Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; + + // Ingame only & player only + if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr) + { return; } - Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; const auto mapName = Dvar::Var("mapname").get(); auto scene = Game::scene; - auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + auto gfxAsset = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName)); + + if (gfxAsset == nullptr) + { + return; + } + + auto world = gfxAsset->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); auto sqrDist = drawDistance * drawDistance; @@ -297,7 +301,6 @@ namespace Components } } break; - case 2: for (auto i = 0; i < scene->sceneDObjCount; i++) { @@ -320,7 +323,6 @@ namespace Components } } break; - case 3: // Static models for (size_t i = 0; i < world->dpvs.smodelCount; i++) @@ -344,6 +346,8 @@ namespace Components } } break; + default: + break; } } @@ -353,41 +357,45 @@ namespace Components if (!val) return; - // Ingame only - int clientNum = Game::CG_GetClientNum(); - if (!Game::CL_IsCgameInitialized() || - clientNum >= 18 || - clientNum < 0 || - Game::g_entities[clientNum].client == nullptr) { + auto clientNum = Game::CG_GetClientNum(); + Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; + // Ingame only & player only + if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr) + { return; } - Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; const auto mapName = Dvar::Var("mapname").get(); auto scene = Game::scene; - auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + auto gfxAsset = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName)); + + if (gfxAsset == nullptr) + { + return; + } + + auto world = gfxAsset->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); auto sqrDist = drawDistance * drawDistance; - switch (val) { + switch (val) + { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) { if (!scene->sceneModel[i].model) continue; - if (Utils::Maths::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Maths::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < static_cast(sqrDist)) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } } break; - case 2: for (auto i = 0; i < scene->sceneDObjCount; i++) { @@ -395,7 +403,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Maths::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].placement.origin) < sqrDist) + if (Utils::Maths::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].placement.origin) < static_cast(sqrDist)) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -403,7 +411,6 @@ namespace Components } } break; - case 3: // Static models for (size_t i = 0; i < world->dpvs.smodelCount; i++) @@ -412,13 +419,15 @@ namespace Components if (staticModel.model) { const auto dist = Utils::Maths::Vec3SqrDistance(playerPosition, staticModel.placement.origin); - if (dist < sqrDist) + if (dist < static_cast(sqrDist)) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); } } } break; + default: + break; } } @@ -467,14 +476,14 @@ namespace Components Utils::Hook(0x536A80, Renderer::BackendFrameStub, HOOK_JUMP).install()->quick(); // Begin device recovery (not D3D9Ex) - Utils::Hook(0x508298, []() + Utils::Hook(0x508298, [] { Game::DB_BeginRecoverLostDevice(); Renderer::BeginRecoverDeviceSignal(); }, HOOK_CALL).install()->quick(); // End device recovery (not D3D9Ex) - Utils::Hook(0x508355, []() + Utils::Hook(0x508355, [] { Renderer::EndRecoverDeviceSignal(); Game::DB_EndRecoverLostDevice(); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index f043bc0d..0d14d299 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -1143,8 +1143,8 @@ namespace Game extern int* demoRecording; extern int* serverMessageSequence; - constexpr auto MAX_GENTITIES = 2048u; - constexpr auto ENTITYNUM_NONE = MAX_GENTITIES - 1; + constexpr std::size_t MAX_GENTITIES = 2048; + constexpr std::size_t ENTITYNUM_NONE = MAX_GENTITIES - 1; extern gentity_t* g_entities; extern netadr_t* connectedHost;