2019-10-28 23:15:36 +00:00
# pragma once
//-----------------------------------------------------------------------------
//
// DESCRIPTION:
// File system I/O functions.
//
//-----------------------------------------------------------------------------
# include <stdexcept>
# include "files.h"
# include "tarray.h"
# include "zstring.h"
// We do not want to expose the resource file interface here.
class FResourceFile ;
struct FResourceLump ;
class FileSystemError : std : : runtime_error
{
public :
FileSystemError ( const char * err ) : std : : runtime_error ( err ) { }
} ;
// A file in memory.
class FileData
{
public :
FileData ( ) ;
FileData ( const FileData & copy ) ;
FileData & operator = ( const FileData & copy ) ;
~ FileData ( ) ;
void * GetMem ( ) { return Block . Len ( ) = = 0 ? NULL : ( void * ) Block . GetChars ( ) ; }
size_t GetSize ( ) { return Block . Len ( ) ; }
FString GetString ( ) { return Block ; }
private :
FileData ( const FString & source ) ;
FString Block ;
friend class FileSystem ;
} ;
struct FolderEntry
{
const char * name ;
unsigned lumpnum ;
} ;
2019-10-31 00:48:10 +00:00
enum class ELookupMode // Todo: Merge with FResourceLump::ENameType
2019-10-28 23:15:36 +00:00
{
2019-10-31 00:48:10 +00:00
FullName ,
NoExtension ,
BaseName ,
BaseWithExtension ,
IdWithType ,
NumLookupModes
2019-10-28 23:15:36 +00:00
} ;
2019-10-31 00:48:10 +00:00
enum
{
NumLookupModes = ( int ) ELookupMode : : NumLookupModes + 1
} ;
2019-10-28 23:15:36 +00:00
class FileSystem
{
public :
FileSystem ( ) = default ;
~ FileSystem ( ) ;
int InitMultipleFiles ( TArray < FString > & filenames , const TArray < FString > & todelete ) ;
void AddFile ( const char * filename , FileReader * wadinfo = NULL ) ;
int CheckIfResourceFileLoaded ( const char * name ) noexcept ;
const char * GetResourceFileName ( int filenum ) const noexcept ;
const char * GetResourceFileFullName ( int filenum ) const noexcept ;
int GetFirstEntry ( int filenum ) const noexcept ;
int GetLastEntry ( int filenum ) const noexcept ;
int GetEntryCount ( int filenum ) const noexcept ;
int FindFile ( const char * name , ELookupMode lookupmode = ELookupMode : : FullName , int filenum = - 1 ) const noexcept ;
int GetFile ( const char * name , ELookupMode lookupmode = ELookupMode : : FullName , int filenum = - 1 ) const ; // Like FindFile, but throws an exception when it cannot find what it looks for.
int FindFile ( const FString & name , ELookupMode lookupmode = ELookupMode : : FullName , int filenum = - 1 ) const noexcept { return FindFile ( name . GetChars ( ) , lookupmode , filenum ) ; }
int GetFile ( const FString & name , ELookupMode lookupmode = ELookupMode : : FullName , int filenum = - 1 ) const { return GetFile ( name . GetChars ( ) , lookupmode , filenum ) ; }
int FindFile ( const std : : string & name , ELookupMode lookupmode = ELookupMode : : FullName , int filenum = - 1 ) const noexcept { return FindFile ( name . c_str ( ) , lookupmode , filenum ) ; }
int GetFile ( const std : : string & name , ELookupMode lookupmode = ELookupMode : : FullName , int filenum = - 1 ) const { return GetFile ( name . c_str ( ) , lookupmode , filenum ) ; }
2019-10-31 00:48:10 +00:00
int FindResource ( int resid , const char * type , int filenum = - 1 ) const noexcept ;
int GetResource ( int resid , const char * type , int filenum = - 1 ) const ; // Like FindFile, but throws an exception when it cannot find what it looks for.
2019-10-28 23:15:36 +00:00
TArray < uint8_t > GetFileData ( int file , int pad = 0 ) ; // reads file into a writable buffer and optionally adds some padding at the end. (FileData isn't writable!)
FileData ReadFile ( int file ) ;
FileData ReadFile ( const char * name ) { return ReadFile ( GetFile ( name ) ) ; }
2019-10-31 00:48:10 +00:00
const void * Lock ( int lump ) ;
void Unlock ( bool mayfree = false ) ;
void * Get ( int lump ) ;
2019-10-28 23:15:36 +00:00
FileReader OpenFileReader ( int file ) ; // opens a reader that redirects to the containing file's one.
FileReader ReopenFileReader ( int file , bool alwayscache = false ) ; // opens an independent reader.
int Iterate ( const char * name , int * lastfile , ELookupMode lookupmode = ELookupMode : : FullName ) ; // [RH] Find files with duplication
static uint32_t FileNameHash ( const char * name ) ; // [RH] Create hash key from a given name
int FileLength ( int file ) const ;
int GetFileOffset ( int file ) ; // [RH] Returns offset of file in the wadfile
int GetFileFlags ( int file ) ; // Return the flags for this file
void GetFileName ( char * to , int file ) const ; // [RH] Copies the file name to to using uppercopy
const char * GetFileName ( int file ) const ;
FString GetFileFullPath ( int file ) const ; // [RH] Returns wad's name + file's full name
int GetFileContainer ( int file ) const ; // [RH] Returns filenum for a specified file
int GetRFFIndexNum ( int file ) const ; // Returns the RFF index number for this file
unsigned GetFilesInFolder ( const char * path , TArray < FolderEntry > & result , bool atomic ) const ;
bool IsEncryptedFile ( int file ) const noexcept ;
2019-10-31 00:48:10 +00:00
int GetResourceId ( int file ) const ;
2019-10-28 23:15:36 +00:00
int GetNumResourceFiles ( ) const { return NumFiles ; }
int GetNumEntries ( ) const { return NumEntries ; }
protected :
struct FileRecord ;
TArray < FResourceFile * > Files ;
TArray < FileRecord > FileInfo ;
TArray < uint32_t > Hashes ; // one allocation for all hash lists.
2019-10-31 00:48:10 +00:00
uint32_t * FirstFileIndex [ NumLookupModes ] ; // Hash information for the base name (no path and no extension)
uint32_t * NextFileIndex [ NumLookupModes ] ;
2019-10-28 23:15:36 +00:00
uint32_t NumFiles = 0 ; // Not necessarily the same as FileInfo.Size()
uint32_t NumEntries ;
void InitHashChains ( ) ; // [RH] Set up the lumpinfo hashing
private :
void DeleteAll ( ) ;
} ;
extern FileSystem Files ;