2016-03-01 15:47:10 +00:00
//-----------------------------------------------------------------------------
//
// DESCRIPTION:
// WAD I/O functions.
//
//-----------------------------------------------------------------------------
# ifndef __W_WAD__
# define __W_WAD__
# include "files.h"
# include "tarray.h"
2020-04-11 11:26:42 +00:00
# include "cmdlib.h"
2019-08-20 20:47:30 +00:00
# include "zstring.h"
2020-04-11 11:36:23 +00:00
# include "resourcefile.h"
2016-03-01 15:47:10 +00:00
class FResourceFile ;
struct FResourceLump ;
class FTexture ;
2020-04-11 11:26:42 +00:00
union LumpShortName
2016-03-01 15:47:10 +00:00
{
2020-04-11 11:26:42 +00:00
char String [ 9 ] ;
2016-03-01 15:47:10 +00:00
2020-04-11 11:26:42 +00:00
uint32_t dword ; // These are for accessing the first 4 or 8 chars of
uint64_t qword ; // Name as a unit without breaking strict aliasing rules
2016-03-01 15:47:10 +00:00
} ;
// A lump in memory.
2020-04-11 11:26:42 +00:00
class FileData
2016-03-01 15:47:10 +00:00
{
public :
2020-04-11 11:26:42 +00:00
FileData ( ) ;
2016-03-01 15:47:10 +00:00
2020-04-11 11:26:42 +00:00
FileData ( const FileData & copy ) ;
FileData & operator = ( const FileData & copy ) ;
~ FileData ( ) ;
2016-03-01 15:47:10 +00:00
void * GetMem ( ) { return Block . Len ( ) = = 0 ? NULL : ( void * ) Block . GetChars ( ) ; }
size_t GetSize ( ) { return Block . Len ( ) ; }
FString GetString ( ) { return Block ; }
private :
2020-04-11 11:26:42 +00:00
FileData ( const FString & source ) ;
2016-03-01 15:47:10 +00:00
FString Block ;
2020-04-11 11:24:34 +00:00
friend class FileSystem ;
2016-03-01 15:47:10 +00:00
} ;
2019-02-07 12:12:39 +00:00
struct FolderEntry
{
const char * name ;
unsigned lumpnum ;
} ;
2020-04-11 11:24:34 +00:00
class FileSystem
2016-03-01 15:47:10 +00:00
{
public :
2020-04-11 11:24:34 +00:00
FileSystem ( ) ;
~ FileSystem ( ) ;
2016-03-01 15:47:10 +00:00
// The wadnum for the IWAD
2017-09-13 08:19:03 +00:00
int GetIwadNum ( ) { return IwadIndex ; }
void SetIwadNum ( int x ) { IwadIndex = x ; }
2016-03-01 15:47:10 +00:00
2019-07-15 18:48:00 +00:00
int GetMaxIwadNum ( ) { return MaxIwadIndex ; }
void SetMaxIwadNum ( int x ) { MaxIwadIndex = x ; }
2020-04-11 11:14:52 +00:00
void InitSingleFile ( const char * filename , bool quiet = false ) ;
2020-04-11 11:26:42 +00:00
void InitMultipleFiles ( TArray < FString > & filenames , bool quiet = false , LumpFilterInfo * filter = nullptr ) ;
2020-04-11 11:21:18 +00:00
void AddFile ( const char * filename , FileReader * wadinfo , bool quiet , LumpFilterInfo * filter ) ;
2020-04-11 11:26:42 +00:00
int CheckIfResourceFileLoaded ( const char * name ) noexcept ;
2016-03-01 15:47:10 +00:00
2020-04-11 11:26:42 +00:00
const char * GetResourceFileName ( int filenum ) const noexcept ;
const char * GetResourceFileFullName ( int wadnum ) const noexcept ;
2016-03-01 15:47:10 +00:00
2020-04-11 11:27:19 +00:00
int GetFirstEntry ( int wadnum ) const noexcept ;
int GetLastEntry ( int wadnum ) const noexcept ;
int GetEntryCount ( int wadnum ) const noexcept ;
2016-03-01 15:47:10 +00:00
int CheckNumForName ( const char * name , int namespc ) ;
int CheckNumForName ( const char * name , int namespc , int wadfile , bool exact = true ) ;
int GetNumForName ( const char * name , int namespc ) ;
2017-03-08 17:47:52 +00:00
inline int CheckNumForName ( const uint8_t * name ) { return CheckNumForName ( ( const char * ) name , ns_global ) ; }
2016-03-01 15:47:10 +00:00
inline int CheckNumForName ( const char * name ) { return CheckNumForName ( name , ns_global ) ; }
inline int CheckNumForName ( const FString & name ) { return CheckNumForName ( name . GetChars ( ) ) ; }
2017-03-08 17:47:52 +00:00
inline int CheckNumForName ( const uint8_t * name , int ns ) { return CheckNumForName ( ( const char * ) name , ns ) ; }
2016-03-01 15:47:10 +00:00
inline int GetNumForName ( const char * name ) { return GetNumForName ( name , ns_global ) ; }
inline int GetNumForName ( const FString & name ) { return GetNumForName ( name . GetChars ( ) , ns_global ) ; }
2017-03-08 17:47:52 +00:00
inline int GetNumForName ( const uint8_t * name ) { return GetNumForName ( ( const char * ) name ) ; }
inline int GetNumForName ( const uint8_t * name , int ns ) { return GetNumForName ( ( const char * ) name , ns ) ; }
2016-03-01 15:47:10 +00:00
2018-03-31 17:20:59 +00:00
int CheckNumForFullName ( const char * name , bool trynormal = false , int namespc = ns_global , bool ignoreext = false ) ;
2016-03-01 15:47:10 +00:00
int CheckNumForFullName ( const char * name , int wadfile ) ;
int GetNumForFullName ( const char * name ) ;
2020-04-11 11:26:42 +00:00
int FindFile ( const char * name )
{
return CheckNumForFullName ( name ) ;
}
LumpShortName & GetShortName ( int i ) ; // may only be called before the hash chains are set up.
2020-04-11 11:27:19 +00:00
bool CreatePathlessCopy ( const char * name , int id , int flags ) ;
2016-03-01 15:47:10 +00:00
inline int CheckNumForFullName ( const FString & name , bool trynormal = false , int namespc = ns_global ) { return CheckNumForFullName ( name . GetChars ( ) , trynormal , namespc ) ; }
inline int CheckNumForFullName ( const FString & name , int wadfile ) { return CheckNumForFullName ( name . GetChars ( ) , wadfile ) ; }
inline int GetNumForFullName ( const FString & name ) { return GetNumForFullName ( name . GetChars ( ) ) ; }
void SetLinkedTexture ( int lump , FTexture * tex ) ;
FTexture * GetLinkedTexture ( int lump ) ;
2020-04-11 11:27:19 +00:00
void ReadFile ( int lump , void * dest ) ;
TArray < uint8_t > GetFileData ( int lump , int pad = 0 ) ; // reads lump into a writable buffer and optionally adds some padding at the end. (FileData isn't writable!)
FileData ReadFile ( int lump ) ;
FileData ReadFile ( const char * name ) { return ReadFile ( GetNumForName ( name ) ) ; }
2016-03-01 15:47:10 +00:00
2020-04-11 11:27:19 +00:00
FileReader OpenFileReader ( int lump ) ; // opens a reader that redirects to the containing file's one.
FileReader ReopenFileReader ( int lump , bool alwayscache = false ) ; // opens an independent reader.
FileReader OpenFileReader ( const char * name ) ;
2018-03-10 12:46:35 +00:00
2016-03-01 15:47:10 +00:00
int FindLump ( const char * name , int * lastlump , bool anyns = false ) ; // [RH] Find lumps with duplication
int FindLumpMulti ( const char * * names , int * lastlump , bool anyns = false , int * nameindex = NULL ) ; // same with multiple possible names
2020-04-11 11:27:19 +00:00
bool CheckFileName ( int lump , const char * name ) ; // [RH] True if lump's name == name
2016-03-01 15:47:10 +00:00
2020-04-11 11:26:42 +00:00
int FindFileWithExtensions ( const char * name , const char * const * exts , int count ) ;
int FindResource ( int resid , const char * type , int filenum ) const noexcept ;
int GetResource ( int resid , const char * type , int filenum ) const ;
2017-03-08 17:47:52 +00:00
static uint32_t LumpNameHash ( const char * name ) ; // [RH] Create hash key from an 8-char name
2016-03-01 15:47:10 +00:00
2020-04-11 11:26:42 +00:00
int FileLength ( int lump ) const ;
int GetFileOffset ( int lump ) ; // [RH] Returns offset of lump in the wadfile
int GetFileFlags ( int lump ) ; // Return the flags for this lump
2020-04-11 11:27:19 +00:00
void GetFileShortName ( char * to , int lump ) const ; // [RH] Copies the lump name to to using uppercopy
void GetFileShortName ( FString & to , int lump ) const ;
const char * GetFileShortName ( int lump ) const ;
const char * GetFileFullName ( int lump , bool returnshort = true ) const ; // [RH] Returns the lump's full name
FString GetFileFullPath ( int lump ) const ; // [RH] Returns wad's name + lump's full name
int GetFileContainer ( int lump ) const ; // [RH] Returns wadnum for a specified lump
int GetFileNamespace ( int lump ) const ; // [RH] Returns the namespace a lump belongs to
void SetFileNamespace ( int lump , int ns ) ;
int GetResourceId ( int lump ) const ; // Returns the RFF index number for this lump
const char * GetResourceType ( int lump ) const ;
bool CheckFileName ( int lump , const char * name ) const ; // [RH] Returns true if the names match
unsigned GetFilesInFolder ( const char * path , TArray < FolderEntry > & result , bool atomic ) const ;
int GetNumEntries ( ) const
2020-04-11 11:14:52 +00:00
{
2020-04-11 11:26:42 +00:00
return NumEntries ;
2020-04-11 11:14:52 +00:00
}
int GetNumWads ( ) const
{
return Files . Size ( ) ;
}
2016-03-01 15:47:10 +00:00
2020-04-11 11:26:42 +00:00
void AddLump ( FResourceLump * lump ) ;
2016-03-01 15:47:10 +00:00
int AddExternalFile ( const char * filename ) ;
2020-04-11 11:26:42 +00:00
int AddFromBuffer ( const char * name , const char * type , char * data , int size , int id , int flags ) ;
FileReader * GetFileReader ( int wadnum ) ; // Gets a FileReader object to the entire WAD
void InitHashChains ( ) ;
2016-03-01 15:47:10 +00:00
protected :
struct LumpRecord ;
TArray < FResourceFile * > Files ;
2020-04-11 11:26:42 +00:00
TArray < LumpRecord > FileInfo ;
2016-03-01 15:47:10 +00:00
2018-11-02 08:51:44 +00:00
TArray < uint32_t > Hashes ; // one allocation for all hash lists.
2017-03-08 17:47:52 +00:00
uint32_t * FirstLumpIndex ; // [RH] Hashing stuff moved out of lumpinfo structure
uint32_t * NextLumpIndex ;
2016-03-01 15:47:10 +00:00
2017-03-08 17:47:52 +00:00
uint32_t * FirstLumpIndex_FullName ; // The same information for fully qualified paths from .zips
uint32_t * NextLumpIndex_FullName ;
2016-03-01 15:47:10 +00:00
2018-03-31 17:20:59 +00:00
uint32_t * FirstLumpIndex_NoExt ; // The same information for fully qualified paths from .zips
uint32_t * NextLumpIndex_NoExt ;
2020-04-11 11:26:42 +00:00
uint32_t * FirstLumpIndex_ResId ; // The same information for fully qualified paths from .zips
uint32_t * NextLumpIndex_ResId ;
uint32_t NumEntries = 0 ; // Not necessarily the same as FileInfo.Size()
2017-03-08 17:47:52 +00:00
uint32_t NumWads ;
2016-03-01 15:47:10 +00:00
2019-07-15 18:48:00 +00:00
int IwadIndex = - 1 ;
int MaxIwadIndex = - 1 ;
2017-09-13 08:19:03 +00:00
2016-03-01 15:47:10 +00:00
private :
void DeleteAll ( ) ;
2019-09-13 11:55:53 +00:00
void MoveLumpsInFolder ( const char * ) ;
2016-03-01 15:47:10 +00:00
} ;
2020-04-11 11:24:34 +00:00
extern FileSystem fileSystem ;
2016-03-01 15:47:10 +00:00
# endif