From dfb56b66f4c2a87d9412d2bf7ac39dda7f37f1f2 Mon Sep 17 00:00:00 2001 From: Garux Date: Tue, 26 Feb 2019 23:59:22 +0300 Subject: [PATCH] * prtview plugin: support PRT2 and PRT1-AM .prt formats --- contrib/prtview/portals.cpp | 88 ++++++++++++++++++++++--------------- contrib/prtview/portals.h | 11 ++--- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/contrib/prtview/portals.cpp b/contrib/prtview/portals.cpp index 6ae0ba9b..818344fa 100644 --- a/contrib/prtview/portals.cpp +++ b/contrib/prtview/portals.cpp @@ -176,31 +176,64 @@ void CPortals::Load(){ return; } - if ( !fgets( buf, LINE_BUF, in ) ) { + #define GETLINE \ + if ( !fgets( buf, LINE_BUF, in ) ) { \ + fclose( in ); \ + node_count = 0; \ + globalErrorStream() << " ERROR - File ended prematurely.\n"; \ + return; \ + } + + GETLINE; + + if ( strncmp( "PRT1-AM", buf, 7 ) == 0 ) { + format = PRT1AM; + } + else if ( strncmp( "PRT1", buf, 4 ) == 0 ) { + format = PRT1; + } + else if ( strncmp( "PRT2", buf, 4 ) == 0 ) { + format = PRT2; + } + else { fclose( in ); - globalErrorStream() << " ERROR - File ended prematurely.\n"; + globalErrorStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\" or \"PRT2\" or \"PRT1-AM\").\n"; return; } - if ( strncmp( "PRT1", buf, 4 ) != 0 ) { - fclose( in ); + switch ( format ) + { + case PRT1: + { + GETLINE; //leafs count https://github.com/kduske/TrenchBroom/issues/1157 //clusters in q3 + sscanf( buf, "%u", &node_count ); + GETLINE; //portals count + sscanf( buf, "%u", &portal_count ); + } + break; + case PRT2: + { + GETLINE; //leafs count + sscanf( buf, "%u", &node_count ); + GETLINE; //clusters count + GETLINE; //portals count + sscanf( buf, "%u", &portal_count ); - globalErrorStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n"; - - return; + } + break; + case PRT1AM: + { + GETLINE; //clusters count + GETLINE; //portals count + sscanf( buf, "%u", &portal_count ); + GETLINE; //leafs count + sscanf( buf, "%u", &node_count ); + } + break; } - if ( !fgets( buf, LINE_BUF, in ) ) { - fclose( in ); - - globalErrorStream() << " ERROR - File ended prematurely.\n"; - - return; - } - - sscanf( buf, "%u", &node_count ); /* if(node_count > 0xFFFF) { @@ -214,18 +247,6 @@ void CPortals::Load(){ } */ - if ( !fgets( buf, LINE_BUF, in ) ) { - fclose( in ); - - node_count = 0; - - globalErrorStream() << " ERROR - File ended prematurely.\n"; - - return; - } - - sscanf( buf, "%u", &portal_count ); - if ( portal_count > 0xFFFF ) { fclose( in ); @@ -253,7 +274,6 @@ void CPortals::Load(){ portal_sort = new int[portal_count]; unsigned int n; - bool first = true; unsigned test_vals_1, test_vals_2; hint_flags = false; @@ -271,20 +291,18 @@ void CPortals::Load(){ } if ( !portal[n].Build( buf ) ) { - if ( first && sscanf( buf, "%d %d", &test_vals_1, &test_vals_2 ) == 1 ) { // skip additional counts of later data, not needed + if ( 0 == n && sscanf( buf, "%d %d", &test_vals_1, &test_vals_2 ) == 1 ) { // skip additional counts of later data, not needed // We can count on hint flags being in the file hint_flags = true; continue; } - first = false; - fclose( in ); - Purge(); - globalErrorStream() << " ERROR - Information for portal number " << n + 1 << " of " << portal_count << " is not formatted correctly.\n"; + Purge(); + return; } @@ -293,7 +311,7 @@ void CPortals::Load(){ fclose( in ); - globalOutputStream() << " " << node_count << " portals read in.\n"; + globalOutputStream() << " " << portal_count << " portals read in.\n"; } #include "math/matrix.h" diff --git a/contrib/prtview/portals.h b/contrib/prtview/portals.h index 1fd2f037..069d68b6 100644 --- a/contrib/prtview/portals.h +++ b/contrib/prtview/portals.h @@ -65,16 +65,17 @@ typedef guint32 PackedColour; class CPortals { +enum ePrtFormat{ + PRT1, + PRT2, + PRT1AM +} format; + public: CPortals(); ~CPortals(); -protected: - - -public: - void Load(); // use filename in fn void Purge();