home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / framework / FileSystem.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  15KB  |  387 lines

  1. // Copyright (C) 2004 Id Software, Inc.
  2. //
  3.  
  4. #ifndef __FILESYSTEM_H__
  5. #define __FILESYSTEM_H__
  6.  
  7. /*
  8. ===============================================================================
  9.  
  10.     File System
  11.  
  12.     No stdio calls should be used by any part of the game, because of all sorts
  13.     of directory and separator char issues. Throughout the game a forward slash
  14.     should be used as a separator. The file system takes care of the conversion
  15.     to an OS specific separator. The file system treats all file and directory
  16.     names as case insensitive.
  17.  
  18.     The following cvars store paths used by the file system:
  19.  
  20.     "fs_basepath"        path to local install, read-only
  21.     "fs_savepath"        path to config, save game, etc. files, read & write
  22.     "fs_cdpath"            path to cd, read-only
  23.     "fs_devpath"        path to files created during development, read & write
  24.  
  25.     The base path for file saving can be set to "fs_savepath" or "fs_devpath".
  26.  
  27. ===============================================================================
  28. */
  29.  
  30. static const unsigned    FILE_NOT_FOUND_TIMESTAMP    = 0xFFFFFFFF;
  31. static const int        MAX_PURE_PAKS                = 128;
  32. static const int        MAX_OSPATH                    = 256;
  33.  
  34. // modes for OpenFileByMode. used as bit mask internally
  35. typedef enum {
  36.     FS_READ        = 0,
  37.     FS_WRITE    = 1,
  38.     FS_APPEND    = 2
  39. } fsMode_t;
  40.  
  41. typedef enum {
  42.     PURE_OK,        // we are good to connect as-is
  43.     PURE_RESTART,    // restart required
  44.     PURE_MISSING,    // pak files missing on the client
  45.     PURE_NODLL,        // no DLL could be extracted
  46.     //    PURE_DIFFERENT, // differing checksums
  47. } fsPureReply_t;
  48.  
  49. typedef enum {
  50.     DLTYPE_URL,
  51.     DLTYPE_FILE
  52. } dlType_t;
  53.  
  54. typedef enum {
  55.     DL_WAIT,        // waiting in the list for beginning of the download
  56.     DL_INPROGRESS,    // in progress
  57.     DL_DONE,        // download completed, success
  58.     DL_ABORTING,    // this one can be set during a download, it will force the next progress callback to abort - then will go to DL_FAILED
  59.     DL_FAILED
  60. } dlStatus_t;
  61.  
  62. typedef enum {
  63.     FILE_EXEC,
  64.     FILE_OPEN
  65. } dlMime_t;
  66.  
  67. typedef enum {
  68.     FIND_NO,
  69.     FIND_YES,
  70.     FIND_ADDON
  71. } findFile_t;
  72.  
  73. typedef struct urlDownload_s {
  74.     idStr                url;
  75.     char                dlerror[ MAX_STRING_CHARS ];
  76.     int                    dltotal;
  77.     int                    dlnow;
  78.     int                    dlstatus;
  79.     dlStatus_t            status;
  80. } urlDownload_t;
  81.  
  82. typedef struct fileDownload_s {
  83.     int                    position;
  84.     int                    length;
  85.     void *                buffer;
  86. } fileDownload_t;
  87.  
  88. // RAVEN BEGIN
  89. // mwhitlock: changes for Xenon to enable us to use texture resources from .xpr
  90. // bundles.
  91. #if defined(_XENON)    
  92. #include "Xtl.h"
  93. #endif
  94. // RAVEN END
  95.  
  96. // RAVEN BEGIN
  97. // mwhitlock: changes for Xenon to enable us to use texture resources from .xpr
  98. // bundles.
  99. #if defined(_XENON)
  100.  
  101. // Define this to enable background streaming via Xenon's native file I/O
  102. // routines (unbuffered reads).
  103. #define _XENON_USE_NATIVE_FILEIO
  104.  
  105. typedef struct backgroundDownload_s
  106. {
  107.     static const int MAX_SEGMENTS = 2;
  108.  
  109.     struct backgroundDownload_s    *next;                    // Set by the fileSystem.
  110.     dlType_t                    opcode;
  111.     idFile *                    f;
  112.     int                            numSegments;
  113.     fileDownload_t                file[MAX_SEGMENTS];
  114.     urlDownload_t                url;
  115.     volatile bool                completed;
  116.     double                        ticksToLoad;            // Used for performance profiling.
  117.  
  118. public:
  119.     void Clear(void)
  120.     {
  121.         numSegments=0;
  122.         completed=false;
  123.         f=0;
  124.     }
  125.  
  126.     void AddSegment(int    position, int length, void* buffer)
  127.     {
  128.         assert(position>0);
  129.         assert(length>0);
  130.         assert(buffer!=0);
  131.         assert(numSegments<MAX_SEGMENTS);
  132.         if(numSegments<MAX_SEGMENTS)
  133.         {
  134.             file[numSegments].position=position;
  135.             file[numSegments].length=length;
  136.             file[numSegments].buffer=buffer;
  137.             numSegments++;
  138.         }
  139. #if defined(_DEBUG)
  140.         // Sanity: segements should not overlap.
  141.         for(int s=0;s<numSegments-1;s++)
  142.         {
  143.             assert(file[s].position+file[s].length-1 < file[s+1].position);
  144.         }
  145. #endif
  146.     }
  147.  
  148.     int DownloadSize(void) const
  149.     {
  150.         int size=0;
  151.         for(int s=0;s<numSegments;s++)
  152.         {
  153.             size+=file[s].length;
  154.         }
  155.         return size;
  156.     }
  157. } backgroundDownload_t;
  158.  
  159. #else
  160.  
  161. typedef struct backgroundDownload_s {
  162.     struct backgroundDownload_s    *next;    // set by the fileSystem
  163.     dlType_t                opcode;
  164.     idFile *                f;
  165.     fileDownload_t            file;
  166.     int                        numSegments;
  167.     urlDownload_t            url;
  168.     volatile bool            completed;
  169. } backgroundDownload_t;
  170.  
  171. #endif
  172. // RAVEN END
  173.  
  174. // file list for directory listings
  175. class idFileList {
  176.     friend class idFileSystemLocal;
  177. public:
  178.     const char *            GetBasePath( void ) const { return basePath; }
  179.     int                        GetNumFiles( void ) const { return list.Num(); }
  180.     const char *            GetFile( int index ) const { return list[index]; }
  181.     const idStrList &        GetList( void ) const { return list; }
  182.  
  183. private:
  184.     idStr                    basePath;
  185.     idStrList                list;
  186. };
  187.  
  188. // mod list
  189. class idModList {
  190.     friend class idFileSystemLocal;
  191. public:
  192.     int                        GetNumMods( void ) const { return mods.Num(); }
  193.     const char *            GetMod( int index ) const { return mods[index]; }
  194.     const char *            GetDescription( int index ) const { return descriptions[index]; }
  195.  
  196. private:
  197.     idStrList                mods;
  198.     idStrList                descriptions;
  199. };
  200.  
  201. class idFileSystem {
  202. public:
  203.     virtual                    ~idFileSystem() {}
  204.                             // Initializes the file system.
  205.     virtual void            Init( void ) = 0;
  206.                             // Restarts the file system.
  207.     virtual void            Restart( void ) = 0;
  208.                             // Shutdown the file system.
  209.     virtual void            Shutdown( bool reloading ) = 0;
  210.                             // Returns true if the file system is initialized.
  211.     virtual bool            IsInitialized( void ) const = 0;
  212.                             // Returns true if we are doing an fs_copyfiles.
  213.     virtual bool            PerformingCopyFiles( void ) const = 0;
  214.                             // Returns a list of mods found along with descriptions
  215.                             // 'mods' contains the directory names to be passed to fs_game
  216.                             // 'descriptions' contains a free form string to be used in the UI
  217.     virtual idModList *        ListMods( void ) = 0;
  218.                             // Frees the given mod list
  219.     virtual void            FreeModList( idModList *modList ) = 0;
  220.                             // Lists files with the given extension in the given directory.
  221.                             // Directory should not have either a leading or trailing '/'
  222.                             // The returned files will not include any directories or '/' unless fullRelativePath is set.
  223.                             // The extension must include a leading dot and may not contain wildcards.
  224.                             // If extension is "/", only subdirectories will be returned.
  225.     virtual idFileList *    ListFiles( const char *relativePath, const char *extension, bool sort = false, bool fullRelativePath = false, const char* gamedir = NULL ) = 0;
  226.                             // Lists files in the given directory and all subdirectories with the given extension.
  227.                             // Directory should not have either a leading or trailing '/'
  228.                             // The returned files include a full relative path.
  229.                             // The extension must include a leading dot and may not contain wildcards.
  230.     virtual idFileList *    ListFilesTree( const char *relativePath, const char *extension, bool sort = false, const char* gamedir = NULL ) = 0;
  231.                             // Frees the given file list.
  232.     virtual void            FreeFileList( idFileList *fileList ) = 0;
  233.                             // Converts a relative path to a full OS path.
  234.     virtual const char *    OSPathToRelativePath( const char *OSPath ) = 0;
  235.                             // Converts a full OS path to a relative path.
  236.     virtual const char *    RelativePathToOSPath( const char *relativePath, const char *basePath = "fs_devpath" ) = 0;
  237.                             // Builds a full OS path from the given components.
  238.     virtual const char *    BuildOSPath( const char *base, const char *game, const char *relativePath ) = 0;
  239.                             // Creates the given OS path for as far as it doesn't exist already.
  240.     virtual void            CreateOSPath( const char *OSPath ) = 0;
  241.                             // Returns true if a file is in a pak file.
  242.     virtual bool            FileIsInPAK( const char *relativePath ) = 0;
  243.                             // Returns a space separated string containing the checksums of all referenced pak files.
  244.                             // will call SetPureServerChecksums internally to restrict itself
  245.     virtual void            UpdatePureServerChecksums( void ) = 0;
  246.                             // setup the mapping of OS -> game pak checksum
  247.     virtual bool            UpdateGamePakChecksums( void ) = 0;
  248.                             // 0-terminated list of pak checksums
  249.                             // if pureChecksums[ 0 ] == 0, all data sources will be allowed
  250.                             // otherwise, only pak files that match one of the checksums will be checked for files
  251.                             // with the sole exception of .cfg files.
  252.                             // the function tries to configure pure mode from the paks already referenced and this new list
  253.                             // it returns wether the switch was successfull, and sets the missing checksums
  254.                             // the process is verbosive when fs_debug 1
  255.     virtual fsPureReply_t    SetPureServerChecksums( const int pureChecksums[ MAX_PURE_PAKS ], int gamePakChecksum, int missingChecksums[ MAX_PURE_PAKS ], int *missingGamePakChecksum ) = 0;
  256.                             // fills a 0-terminated list of pak checksums for a client
  257.                             // if OS is -1, give the current game pak checksum. if >= 0, lookup the game pak table (server only)
  258.     virtual void            GetPureServerChecksums( int checksums[ MAX_PURE_PAKS ], int OS, int *gamePakChecksum ) = 0;
  259.                             // before doing a restart, force the pure list and the search order
  260.                             // if the given checksum list can't be completely processed and set, will error out
  261.     virtual void            SetRestartChecksums( const int pureChecksums[ MAX_PURE_PAKS ], int gamePakChecksum ) = 0;
  262.                             // equivalent to calling SetPureServerChecksums with an empty list
  263.     virtual    void            ClearPureChecksums( void ) = 0;
  264.                             // get a mask of supported OSes. if not pure, returns -1
  265.     virtual unsigned int    GetOSMask( void ) = 0;
  266.                             // Reads a complete file.
  267.                             // Returns the length of the file, or -1 on failure.
  268.                             // A null buffer will just return the file length without loading.
  269.                             // A null timestamp will be ignored.
  270.                             // As a quick check for existance. -1 length == not present.
  271.                             // A 0 byte will always be appended at the end, so string ops are safe.
  272.                             // The buffer should be considered read-only, because it may be cached for other uses.
  273.     virtual int                ReadFile( const char *relativePath, void **buffer, unsigned *timestamp = NULL ) = 0;
  274.                             // Frees the memory allocated by ReadFile.
  275.     virtual void            FreeFile( void *buffer ) = 0;
  276.                             // Writes a complete file, will create any needed subdirectories.
  277.                             // Returns the length of the file, or -1 on failure.
  278.     virtual int                WriteFile( const char *relativePath, const void *buffer, int size, const char *basePath = "fs_savepath" ) = 0;
  279.                             // Removes the given file.
  280. // RAVEN BEGIN
  281. // rjohnson: can specify the path cvar
  282.     virtual void            RemoveFile( const char *relativePath, const char *basePath = "fs_savepath" ) = 0;
  283. // bdube: added method
  284.                             // Removes the given file and returns the filesystem status of the removal
  285.     virtual int                RemoveExplicitFile ( const char *OSPath ) = 0;
  286.  
  287. // mekberg: is file loading allowed?
  288.     virtual void            SetIsFileLoadingAllowed(bool mode) = 0;
  289.  
  290. // dluetscher: returns file loading status
  291.     virtual bool            GetIsFileLoadingAllowed() const = 0;
  292.  
  293. // amccarthy: set the current asset log name.
  294.     virtual void            SetAssetLogName(const char *logName) = 0;
  295. // amccarthy: write out a list of all files loaded.
  296.     virtual void            WriteAssetLog() = 0;
  297. // jnewquist: clear list of all files loaded.
  298.     virtual void            ClearAssetLog() = 0;
  299. // jnewquist: Accessor for asset log name (with filter)
  300.     virtual const char*        GetAssetLogName() = 0;
  301.  
  302. // jscott: new functions for tools
  303.     virtual idFile *        GetNewFileMemory( void ) = 0;
  304.     virtual idFile *        GetNewFilePermanent( void ) = 0;
  305.  
  306. // RAVEN END
  307.                             // Opens a file for reading.
  308.     virtual idFile *        OpenFileRead( const char *relativePath, bool allowCopyFiles = true, const char* gamedir = NULL ) = 0;
  309.                             // Opens a file for writing, will create any needed subdirectories.
  310.     virtual idFile *        OpenFileWrite( const char *relativePath, const char *basePath = "fs_savepath", bool ASCII = false ) = 0;
  311.                             // Opens a file for writing at the end.
  312.     virtual idFile *        OpenFileAppend( const char *filename, bool sync = false, const char *basePath = "fs_basepath" ) = 0;
  313.                             // Opens a file for reading, writing, or appending depending on the value of mode.
  314.     virtual idFile *        OpenFileByMode( const char *relativePath, fsMode_t mode ) = 0;
  315.                             // Opens a file for reading from a full OS path.
  316.     virtual idFile *        OpenExplicitFileRead( const char *OSPath ) = 0;
  317.                             // Opens a file for writing to a full OS path.
  318.     virtual idFile *        OpenExplicitFileWrite( const char *OSPath ) = 0;
  319.                             // Closes a file.
  320.     virtual void            CloseFile( idFile *f ) = 0;
  321.                             // Returns immediately, performing the read from a background thread.
  322.     virtual void            BackgroundDownload( backgroundDownload_t *bgl ) = 0;
  323.                             // resets the bytes read counter
  324.     virtual void            ResetReadCount( void ) = 0;
  325.                             // retrieves the current read count
  326.     virtual int                GetReadCount( void ) = 0;
  327.                             // adds to the read count
  328.     virtual void            AddToReadCount( int c ) = 0;
  329.                             // look for a dynamic module
  330.     virtual void            FindDLL( const char *basename, char dllPath[ MAX_OSPATH ], bool updateChecksum ) = 0;
  331.                             // case sensitive filesystems use an internal directory cache
  332.                             // the cache is cleared when calling OpenFileWrite and RemoveFile
  333.                             // in some cases you may need to use this directly
  334.     virtual void            ClearDirCache( void ) = 0;
  335.  
  336.                             // lookup a relative path, return the size or 0 if not found
  337.     virtual int                ValidateDownloadPakForChecksum( int checksum, char path[ MAX_STRING_CHARS ], bool isGamePak ) = 0;
  338.  
  339.     virtual idFile *        MakeTemporaryFile( void ) = 0;
  340.  
  341.                             // make downloaded pak files known so pure negociation works next time
  342.     virtual int                AddZipFile( const char *path ) = 0;
  343.  
  344. // RAVEN BEGIN
  345. // nrausch: explicit pak add/removal
  346. #ifdef _XENON    
  347.     virtual bool            AddDownloadedPak( const char *path ) = 0;
  348.     virtual void            RemoveDownloadedPak( const char *path ) = 0;
  349. #endif
  350.  
  351.     virtual bool            AddExplicitPak( const char *path ) = 0;
  352.     virtual void            RemoveExplicitPak( const char *path ) = 0;
  353.     virtual bool            IsPakLoaded( const char *path ) = 0;
  354. // RAVEN END
  355.  
  356.                             // look for a file in the loaded paks or the addon paks
  357.                             // if the file is found in addons, FS's internal structures are ready for a reloadEngine
  358.     virtual findFile_t        FindFile( const char *path ) = 0;
  359.  
  360.                             // get map/addon decls and take into account addon paks that are not on the search list
  361.                             // the decl 'name' is in the "path" entry of the dict
  362.     virtual int                GetNumMaps() = 0;
  363.     virtual const idDict *    GetMapDecl( int i ) = 0;
  364.     virtual void            FindMapScreenshot( const char *path, char *buf, int len ) = 0;
  365.  
  366. // RAVEN BEGIN
  367. // rjohnson: added new functions
  368.                             // Converts a full OS path to an import path.
  369.     virtual bool            OSpathToImportPath( const char *osPath, idStr &iPath, bool stripTemp = false ) = 0;
  370.                             // Opens a file for reading from the fs_importpath directory
  371.     virtual idFile *        OpenImportFileRead( const char *filename ) = 0;
  372.                             // Copy a file 
  373.     virtual void            CopyOSFile( const char *fromOSPath, const char *toOSPath ) = 0;
  374.     virtual void            CopyOSFile( idFile *src, const char *toOSPath ) = 0;
  375. // RAVEN END
  376.  
  377.     // demo functions - only for use by the core ( and not DLL boundary / memory safe anyway )
  378.     // ReadDemoHeader: returns -1 if a reloadEngine is requested before trying again, 0 if we failed with no backup, and 1 if we can continue
  379.     // if demo_enforceFS is 0, will always ret 1
  380.     virtual void            WriteDemoHeader( idFile *file ) = 0;
  381.     virtual int                ReadDemoHeader( idFile *file ) = 0;
  382. };
  383.  
  384. extern idFileSystem *        fileSystem;
  385.  
  386. #endif /* !__FILESYSTEM_H__ */
  387.