[Renderer] Fix crash when debugging oilrig

This commit is contained in:
Diavolo 2022-06-30 13:32:19 +02:00
parent dbef5f7a16
commit 16988b24f4
No known key found for this signature in database
GPG Key ID: FA77F074E98D98A5
2 changed files with 42 additions and 33 deletions

View File

@ -177,7 +177,7 @@ namespace Components
auto entities = Game::g_entities; 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]; auto* ent = &entities[i];
@ -229,7 +229,7 @@ namespace Components
auto scene = Game::scene; 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) if (!scene->sceneModel[i].model)
continue; continue;
@ -255,23 +255,27 @@ namespace Components
if (!val) return; if (!val) return;
// Ingame only auto clientNum = Game::CG_GetClientNum();
int clientNum = Game::CG_GetClientNum(); Game::gentity_t* clientEntity = &Game::g_entities[clientNum];
if (!Game::CL_IsCgameInitialized() ||
clientNum >= 18 ||
clientNum < 0 ||
Game::g_entities[clientNum].client == nullptr) {
// Ingame only & player only
if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr)
{
return; return;
} }
Game::gentity_t* clientEntity = &Game::g_entities[clientNum];
float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] };
const auto mapName = Dvar::Var("mapname").get<const char*>(); const auto mapName = Dvar::Var("mapname").get<const char*>();
auto scene = Game::scene; 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<int>(); auto drawDistance = r_playerDrawDebugDistance.get<int>();
auto sqrDist = drawDistance * drawDistance; auto sqrDist = drawDistance * drawDistance;
@ -297,7 +301,6 @@ namespace Components
} }
} }
break; break;
case 2: case 2:
for (auto i = 0; i < scene->sceneDObjCount; i++) for (auto i = 0; i < scene->sceneDObjCount; i++)
{ {
@ -320,7 +323,6 @@ namespace Components
} }
} }
break; break;
case 3: case 3:
// Static models // Static models
for (size_t i = 0; i < world->dpvs.smodelCount; i++) for (size_t i = 0; i < world->dpvs.smodelCount; i++)
@ -344,6 +346,8 @@ namespace Components
} }
} }
break; break;
default:
break;
} }
} }
@ -353,41 +357,45 @@ namespace Components
if (!val) return; if (!val) return;
// Ingame only auto clientNum = Game::CG_GetClientNum();
int clientNum = Game::CG_GetClientNum(); Game::gentity_t* clientEntity = &Game::g_entities[clientNum];
if (!Game::CL_IsCgameInitialized() ||
clientNum >= 18 ||
clientNum < 0 ||
Game::g_entities[clientNum].client == nullptr) {
// Ingame only & player only
if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr)
{
return; return;
} }
Game::gentity_t* clientEntity = &Game::g_entities[clientNum];
float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] };
const auto mapName = Dvar::Var("mapname").get<const char*>(); const auto mapName = Dvar::Var("mapname").get<const char*>();
auto scene = Game::scene; 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<int>(); auto drawDistance = r_playerDrawDebugDistance.get<int>();
auto sqrDist = drawDistance * drawDistance; auto sqrDist = drawDistance * drawDistance;
switch (val) { switch (val)
{
case 1: case 1:
for (auto i = 0; i < scene->sceneModelCount; i++) for (auto i = 0; i < scene->sceneModelCount; i++)
{ {
if (!scene->sceneModel[i].model) if (!scene->sceneModel[i].model)
continue; 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<float>(sqrDist))
{ {
Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name);
} }
} }
break; break;
case 2: case 2:
for (auto i = 0; i < scene->sceneDObjCount; i++) 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++) 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<float>(sqrDist))
{ {
Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); 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; break;
case 3: case 3:
// Static models // Static models
for (size_t i = 0; i < world->dpvs.smodelCount; i++) for (size_t i = 0; i < world->dpvs.smodelCount; i++)
@ -412,13 +419,15 @@ namespace Components
if (staticModel.model) if (staticModel.model)
{ {
const auto dist = Utils::Maths::Vec3SqrDistance(playerPosition, staticModel.placement.origin); const auto dist = Utils::Maths::Vec3SqrDistance(playerPosition, staticModel.placement.origin);
if (dist < sqrDist) if (dist < static_cast<float>(sqrDist))
{ {
Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name);
} }
} }
} }
break; break;
default:
break;
} }
} }
@ -467,14 +476,14 @@ namespace Components
Utils::Hook(0x536A80, Renderer::BackendFrameStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x536A80, Renderer::BackendFrameStub, HOOK_JUMP).install()->quick();
// Begin device recovery (not D3D9Ex) // Begin device recovery (not D3D9Ex)
Utils::Hook(0x508298, []() Utils::Hook(0x508298, []
{ {
Game::DB_BeginRecoverLostDevice(); Game::DB_BeginRecoverLostDevice();
Renderer::BeginRecoverDeviceSignal(); Renderer::BeginRecoverDeviceSignal();
}, HOOK_CALL).install()->quick(); }, HOOK_CALL).install()->quick();
// End device recovery (not D3D9Ex) // End device recovery (not D3D9Ex)
Utils::Hook(0x508355, []() Utils::Hook(0x508355, []
{ {
Renderer::EndRecoverDeviceSignal(); Renderer::EndRecoverDeviceSignal();
Game::DB_EndRecoverLostDevice(); Game::DB_EndRecoverLostDevice();

View File

@ -1143,8 +1143,8 @@ namespace Game
extern int* demoRecording; extern int* demoRecording;
extern int* serverMessageSequence; extern int* serverMessageSequence;
constexpr auto MAX_GENTITIES = 2048u; constexpr std::size_t MAX_GENTITIES = 2048;
constexpr auto ENTITYNUM_NONE = MAX_GENTITIES - 1; constexpr std::size_t ENTITYNUM_NONE = MAX_GENTITIES - 1;
extern gentity_t* g_entities; extern gentity_t* g_entities;
extern netadr_t* connectedHost; extern netadr_t* connectedHost;