From 771c359ed0c884ebb27c7a3f31d2f0d9e5d25f67 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 5 May 2022 04:04:24 +0300 Subject: [PATCH] engine: common: make blue-shift map detect more robust --- engine/common/mod_bmodel.c | 44 ++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/engine/common/mod_bmodel.c b/engine/common/mod_bmodel.c index 6edaa606..842a53e8 100644 --- a/engine/common/mod_bmodel.c +++ b/engine/common/mod_bmodel.c @@ -2736,6 +2736,42 @@ static void Mod_LoadLighting( dbspmodel_t *bmod ) } } +/* +================= +Mod_LumpLooksLikePlanes + +================= +*/ +static qboolean Mod_LumpLooksLikePlanes( const byte *in, dlump_t *lump, qboolean fast ) +{ + int numplanes, i; + const dplane_t *planes; + + if( lump->filelen < sizeof( dplane_t ) && + lump->filelen % sizeof( dplane_t ) != 0 ) + return false; + + if( fast ) + return true; + + numplanes = lump->filelen / sizeof( dplane_t ); + planes = (const dplane_t*)(in + lump->fileofs); + + for( i = 0; i < numplanes; i++ ) + { + if( IS_NAN( planes[i].dist ) ) + return false; + + if( VectorIsNAN( planes[i].normal )) + return false; + + if( planes[i].type > 6 ) + return false; + } + + return true; +} + /* ================= Mod_LoadBmodelLumps @@ -2785,8 +2821,8 @@ qboolean Mod_LoadBmodelLumps( const byte *mod_base, qboolean isworld ) if( header->version == HLBSP_VERSION ) { // only relevant for half-life maps - if( header->lumps[LUMP_ENTITIES].fileofs <= 1024 && - (header->lumps[LUMP_ENTITIES].filelen % sizeof( dplane_t )) == 0 ) + if( !Mod_LumpLooksLikePlanes( mod_base, &header->lumps[LUMP_PLANES], false ) && + Mod_LumpLooksLikePlanes( mod_base, &header->lumps[LUMP_ENTITIES], false )) { // blue-shift swapped lumps srclumps[0].lumpnumber = LUMP_PLANES; @@ -2913,8 +2949,8 @@ qboolean Mod_TestBmodelLumps( const char *name, const byte *mod_base, qboolean s if( header->version == HLBSP_VERSION ) { // only relevant for half-life maps - if( header->lumps[LUMP_ENTITIES].fileofs <= 1024 && - (header->lumps[LUMP_ENTITIES].filelen % sizeof( dplane_t )) == 0 ) + if( Mod_LumpLooksLikePlanes( mod_base, &header->lumps[LUMP_ENTITIES], true ) && + !Mod_LumpLooksLikePlanes( mod_base, &header->lumps[LUMP_PLANES], true )) { // blue-shift swapped lumps srclumps[0].lumpnumber = LUMP_PLANES;