DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

File_Manifest.h (7347B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #ifndef __FILE_MANIFEST_H__
     30 #define __FILE_MANIFEST_H__
     31 
     32 /*
     33 ==============================================================
     34 
     35   File and preload manifests. 
     36 
     37 ==============================================================
     38 */
     39 
     40 class idFileManifest {
     41 public:
     42 	idFileManifest() {
     43 		cacheTable.SetGranularity( 4096 );
     44 		cacheHash.SetGranularity( 4096 );
     45 	}
     46 	~idFileManifest(){}
     47 
     48 	bool LoadManifest( const char *fileName );
     49 	bool LoadManifestFromFile( idFile *file );
     50 	void WriteManifestFile( const char *fileName );
     51 
     52 	int NumFiles() {
     53 		return cacheTable.Num();
     54 	}
     55 
     56 	int FindFile( const char *fileName );
     57 
     58 	const idStr & GetFileNameByIndex( int idx ) const;
     59 
     60 
     61 	const char * GetManifestName() {
     62 		return filename;
     63 	}
     64 
     65 	void RemoveAll( const char *filename );
     66 	void AddFile( const char *filename );
     67 
     68 	void PopulateList( idStaticList< idStr, 16384 > &dest ) {
     69 		dest.Clear();
     70 		for ( int i = 0; i < cacheTable.Num(); i++ ) {
     71 			dest.Append( cacheTable[ i ] );
     72 		}
     73 	}
     74 
     75 	void Print() {
     76 		idLib::Printf( "dump for manifest %s\n", GetManifestName() );
     77 		idLib::Printf( "---------------------------------------\n" );
     78 		for ( int i = 0; i < NumFiles(); i++ ) {
     79 			const idStr & name = GetFileNameByIndex( i );
     80 			if ( name.Find( ".idwav", false ) >= 0 ) {
     81 				idLib::Printf( "%s\n", GetFileNameByIndex( i ).c_str() );
     82 			}
     83 		}
     84 	}
     85 
     86 private:
     87 	idStrList cacheTable;
     88 	idHashIndex	cacheHash;
     89 	idStr filename;
     90 };
     91 
     92 // image preload 
     93 struct imagePreload_s {
     94 	imagePreload_s() {
     95 		filter = 0;
     96 		repeat = 0;
     97 		usage = 0;
     98 		cubeMap = 0;
     99 	}
    100 	void Write( idFile *f ) {
    101 		f->WriteBig( filter );
    102 		f->WriteBig( repeat );
    103 		f->WriteBig( usage );
    104 		f->WriteBig( cubeMap );
    105 	}
    106 	void Read( idFile *f ) {
    107 		f->ReadBig( filter );
    108 		f->ReadBig( repeat );
    109 		f->ReadBig( usage );
    110 		f->ReadBig( cubeMap );
    111 	}
    112 	bool operator==( const imagePreload_s &b ) const { 
    113 		return ( filter == b.filter && repeat == b.repeat && usage == b.usage && cubeMap == b.cubeMap );
    114 	}
    115 	int filter;
    116 	int repeat;
    117 	int usage;
    118 	int cubeMap;
    119 };
    120 
    121 enum preloadType_t {
    122 	PRELOAD_IMAGE,
    123 	PRELOAD_MODEL,
    124 	PRELOAD_SAMPLE,
    125 	PRELOAD_ANIM,
    126 	PRELOAD_COLLISION,
    127 	PRELOAD_PARTICLE
    128 };
    129 
    130 // preload
    131 struct preloadEntry_s {
    132 	preloadEntry_s() {
    133 		resType = 0;
    134 	}
    135 	bool operator==( const preloadEntry_s &b ) const { 
    136 		bool ret = ( resourceName.Icmp( b.resourceName ) == 0 );
    137 		if ( ret && resType == PRELOAD_IMAGE ) {
    138 			// this should never matter but...
    139 			ret &= ( imgData == b.imgData );
    140 		}
    141 		return ret;
    142 	}
    143 	void Write( idFile *outFile ) {
    144 		outFile->WriteBig( resType );
    145 		outFile->WriteString( resourceName );
    146 		imgData.Write( outFile );
    147 	}
    148 
    149 	void Read( idFile *inFile ) {
    150 		inFile->ReadBig( resType );
    151 		inFile->ReadString( resourceName );
    152 		imgData.Read( inFile );
    153 	}
    154 
    155 	int				resType;		// type
    156 	idStr			resourceName;	// resource name
    157 	imagePreload_s	imgData;		// image specific data
    158 };
    159 
    160 struct preloadSort_t {
    161 	int idx;
    162 	int ofs;
    163 };
    164 class idSort_Preload : public idSort_Quick< preloadSort_t, idSort_Preload > {
    165 public:
    166 	int Compare( const preloadSort_t & a, const preloadSort_t & b ) const { return a.ofs - b.ofs; }
    167 };
    168 
    169 class idPreloadManifest {
    170 public:
    171 	idPreloadManifest() {
    172 		entries.SetGranularity( 2048 );
    173 	}
    174 	~idPreloadManifest(){}
    175 
    176 	bool LoadManifest( const char *fileName );
    177 	bool LoadManifestFromFile( idFile *file );
    178 
    179 	void WriteManifest( const char *fileName );
    180 	void WriteManifestToFile(  idFile *outFile ) {
    181 		if ( outFile == NULL ) {
    182 			return;
    183 		}
    184 		filename = outFile->GetName();
    185 		outFile->WriteBig ( ( int )entries.Num() );
    186 		outFile->WriteString( filename );
    187 		for ( int i = 0; i < entries.Num(); i++ ) {
    188 			entries[ i ].Write( outFile );
    189 		}
    190 	}
    191 
    192 	int NumResources() const {
    193 		return entries.Num();
    194 	}
    195 
    196 	const preloadEntry_s & GetPreloadByIndex( int idx ) const {
    197 		return entries[ idx ];
    198 	}
    199 
    200 	const idStr & GetResourceNameByIndex( int idx ) const {
    201 		return entries[ idx ].resourceName;
    202 	}
    203 
    204 	const char * GetManifestName() const {
    205 		return filename;
    206 	}
    207 
    208 	void Add( const preloadEntry_s & p ) {
    209 		entries.AddUnique( p );
    210 	}
    211 
    212 	void AddSample( const char *_resourceName ) {
    213 		static preloadEntry_s pe;
    214 		pe.resType = PRELOAD_SAMPLE;
    215 		pe.resourceName = _resourceName;
    216 		entries.Append( pe );
    217 	}
    218 	void AddModel( const char *_resourceName ) {
    219 		static preloadEntry_s pe;
    220 		pe.resType = PRELOAD_MODEL;
    221 		pe.resourceName = _resourceName;
    222 		entries.Append( pe );
    223 	}
    224 	void AddParticle( const char *_resourceName ) {
    225 		static preloadEntry_s pe;
    226 		pe.resType = PRELOAD_PARTICLE;
    227 		pe.resourceName = _resourceName;
    228 		entries.Append( pe );
    229 	}
    230 	void AddAnim( const char *_resourceName ) {
    231 		static preloadEntry_s pe;
    232 		pe.resType = PRELOAD_ANIM;
    233 		pe.resourceName = _resourceName;
    234 		entries.Append( pe );
    235 	}
    236 	void AddCollisionModel( const char *_resourceName ) {
    237 		static preloadEntry_s pe;
    238 		pe.resType = PRELOAD_COLLISION;
    239 		pe.resourceName = _resourceName;
    240 		entries.Append( pe );
    241 	}
    242 	void AddImage( const char *_resourceName, int _filter, int _repeat, int _usage, int _cube ) {
    243 		static preloadEntry_s pe;
    244 		pe.resType = PRELOAD_IMAGE;
    245 		pe.resourceName = _resourceName;
    246 		pe.imgData.filter = _filter;
    247 		pe.imgData.repeat = _repeat;
    248 		pe.imgData.usage = _usage;
    249 		pe.imgData.cubeMap = _cube;
    250 		entries.Append( pe );
    251 	}
    252 
    253 	void Clear() {
    254 		entries.Clear();
    255 	}
    256 
    257 	int FindResource( const char *name ) {
    258 		for ( int i = 0; i < entries.Num(); i++ ) {
    259 			if ( idStr::Icmp( name, entries[ i ].resourceName ) == 0 ) {
    260 				return i;
    261 			}
    262 		}
    263 		return -1;
    264 	}
    265 
    266 	void Print() {
    267 		idLib::Printf( "dump for preload manifest %s\n", GetManifestName() );
    268 		idLib::Printf( "---------------------------------------\n" );
    269 		for ( int i = 0; i < NumResources(); i++ ) {
    270 			idLib::Printf( "%s\n", GetResourceNameByIndex( i ).c_str() );
    271 		}
    272 	}
    273 private:
    274 	idList< preloadEntry_s > entries;
    275 	idStr filename;
    276 };
    277 
    278 
    279 #endif /* !__FILE_MANIFEST_H__ */