FileSystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. 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.
  18. ===========================================================================
  19. */
  20. #ifndef __FILESYSTEM_H__
  21. #define __FILESYSTEM_H__
  22. /*
  23. ===============================================================================
  24. File System
  25. No stdio calls should be used by any part of the game, because of all sorts
  26. of directory and separator char issues. Throughout the game a forward slash
  27. should be used as a separator. The file system takes care of the conversion
  28. to an OS specific separator. The file system treats all file and directory
  29. names as case insensitive.
  30. The following cvars store paths used by the file system:
  31. "fs_basepath" path to local install, read-only
  32. "fs_savepath" path to config, save game, etc. files, read & write
  33. "fs_cdpath" path to cd, read-only
  34. "fs_devpath" path to files created during development, read & write
  35. The base path for file saving can be set to "fs_savepath" or "fs_devpath".
  36. ===============================================================================
  37. */
  38. static const ID_TIME_T FILE_NOT_FOUND_TIMESTAMP = 0xFFFFFFFF;
  39. static const int MAX_PURE_PAKS = 128;
  40. static const int MAX_OSPATH = 256;
  41. // modes for OpenFileByMode. used as bit mask internally
  42. typedef enum {
  43. FS_READ = 0,
  44. FS_WRITE = 1,
  45. FS_APPEND = 2
  46. } fsMode_t;
  47. typedef enum {
  48. PURE_OK, // we are good to connect as-is
  49. PURE_RESTART, // restart required
  50. PURE_MISSING, // pak files missing on the client
  51. PURE_NODLL // no DLL could be extracted
  52. } fsPureReply_t;
  53. typedef enum {
  54. DLTYPE_URL,
  55. DLTYPE_FILE
  56. } dlType_t;
  57. typedef enum {
  58. DL_WAIT, // waiting in the list for beginning of the download
  59. DL_INPROGRESS, // in progress
  60. DL_DONE, // download completed, success
  61. 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
  62. DL_FAILED
  63. } dlStatus_t;
  64. typedef enum {
  65. FILE_EXEC,
  66. FILE_OPEN
  67. } dlMime_t;
  68. typedef enum {
  69. FIND_NO,
  70. FIND_YES,
  71. FIND_ADDON
  72. } findFile_t;
  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. typedef struct fileDownload_s {
  82. int position;
  83. int length;
  84. void * buffer;
  85. } fileDownload_t;
  86. typedef struct backgroundDownload_s {
  87. struct backgroundDownload_s *next; // set by the fileSystem
  88. dlType_t opcode;
  89. idFile * f;
  90. fileDownload_t file;
  91. urlDownload_t url;
  92. volatile bool completed;
  93. } backgroundDownload_t;
  94. // file list for directory listings
  95. class idFileList {
  96. friend class idFileSystemLocal;
  97. public:
  98. const char * GetBasePath( void ) const { return basePath; }
  99. int GetNumFiles( void ) const { return list.Num(); }
  100. const char * GetFile( int index ) const { return list[index]; }
  101. const idStrList & GetList( void ) const { return list; }
  102. private:
  103. idStr basePath;
  104. idStrList list;
  105. };
  106. // mod list
  107. class idModList {
  108. friend class idFileSystemLocal;
  109. public:
  110. int GetNumMods( void ) const { return mods.Num(); }
  111. const char * GetMod( int index ) const { return mods[index]; }
  112. const char * GetDescription( int index ) const { return descriptions[index]; }
  113. private:
  114. idStrList mods;
  115. idStrList descriptions;
  116. };
  117. class idFileSystem {
  118. public:
  119. virtual ~idFileSystem() {}
  120. // Initializes the file system.
  121. virtual void Init( void ) = 0;
  122. // Restarts the file system.
  123. virtual void Restart( void ) = 0;
  124. // Shutdown the file system.
  125. virtual void Shutdown( bool reloading ) = 0;
  126. // Returns true if the file system is initialized.
  127. virtual bool IsInitialized( void ) const = 0;
  128. // Returns true if we are doing an fs_copyfiles.
  129. virtual bool PerformingCopyFiles( void ) const = 0;
  130. // Returns a list of mods found along with descriptions
  131. // 'mods' contains the directory names to be passed to fs_game
  132. // 'descriptions' contains a free form string to be used in the UI
  133. virtual idModList * ListMods( void ) = 0;
  134. // Frees the given mod list
  135. virtual void FreeModList( idModList *modList ) = 0;
  136. // Lists files with the given extension in the given directory.
  137. // Directory should not have either a leading or trailing '/'
  138. // The returned files will not include any directories or '/' unless fullRelativePath is set.
  139. // The extension must include a leading dot and may not contain wildcards.
  140. // If extension is "/", only subdirectories will be returned.
  141. virtual idFileList * ListFiles( const char *relativePath, const char *extension, bool sort = false, bool fullRelativePath = false, const char* gamedir = NULL ) = 0;
  142. // Lists files in the given directory and all subdirectories with the given extension.
  143. // Directory should not have either a leading or trailing '/'
  144. // The returned files include a full relative path.
  145. // The extension must include a leading dot and may not contain wildcards.
  146. virtual idFileList * ListFilesTree( const char *relativePath, const char *extension, bool sort = false, const char* gamedir = NULL ) = 0;
  147. // Frees the given file list.
  148. virtual void FreeFileList( idFileList *fileList ) = 0;
  149. // Converts a relative path to a full OS path.
  150. virtual const char * OSPathToRelativePath( const char *OSPath ) = 0;
  151. // Converts a full OS path to a relative path.
  152. virtual const char * RelativePathToOSPath( const char *relativePath, const char *basePath = "fs_devpath" ) = 0;
  153. // Builds a full OS path from the given components.
  154. virtual const char * BuildOSPath( const char *base, const char *game, const char *relativePath ) = 0;
  155. // Creates the given OS path for as far as it doesn't exist already.
  156. virtual void CreateOSPath( const char *OSPath ) = 0;
  157. // Returns true if a file is in a pak file.
  158. virtual bool FileIsInPAK( const char *relativePath ) = 0;
  159. // Returns a space separated string containing the checksums of all referenced pak files.
  160. // will call SetPureServerChecksums internally to restrict itself
  161. virtual void UpdatePureServerChecksums( void ) = 0;
  162. // setup the mapping of OS -> game pak checksum
  163. virtual bool UpdateGamePakChecksums( void ) = 0;
  164. // 0-terminated list of pak checksums
  165. // if pureChecksums[ 0 ] == 0, all data sources will be allowed
  166. // otherwise, only pak files that match one of the checksums will be checked for files
  167. // with the sole exception of .cfg files.
  168. // the function tries to configure pure mode from the paks already referenced and this new list
  169. // it returns wether the switch was successfull, and sets the missing checksums
  170. // the process is verbosive when fs_debug 1
  171. virtual fsPureReply_t SetPureServerChecksums( const int pureChecksums[ MAX_PURE_PAKS ], int gamePakChecksum, int missingChecksums[ MAX_PURE_PAKS ], int *missingGamePakChecksum ) = 0;
  172. // fills a 0-terminated list of pak checksums for a client
  173. // if OS is -1, give the current game pak checksum. if >= 0, lookup the game pak table (server only)
  174. virtual void GetPureServerChecksums( int checksums[ MAX_PURE_PAKS ], int OS, int *gamePakChecksum ) = 0;
  175. // before doing a restart, force the pure list and the search order
  176. // if the given checksum list can't be completely processed and set, will error out
  177. virtual void SetRestartChecksums( const int pureChecksums[ MAX_PURE_PAKS ], int gamePakChecksum ) = 0;
  178. // equivalent to calling SetPureServerChecksums with an empty list
  179. virtual void ClearPureChecksums( void ) = 0;
  180. // get a mask of supported OSes. if not pure, returns -1
  181. virtual int GetOSMask( void ) = 0;
  182. // Reads a complete file.
  183. // Returns the length of the file, or -1 on failure.
  184. // A null buffer will just return the file length without loading.
  185. // A null timestamp will be ignored.
  186. // As a quick check for existance. -1 length == not present.
  187. // A 0 byte will always be appended at the end, so string ops are safe.
  188. // The buffer should be considered read-only, because it may be cached for other uses.
  189. virtual int ReadFile( const char *relativePath, void **buffer, ID_TIME_T *timestamp = NULL ) = 0;
  190. // Frees the memory allocated by ReadFile.
  191. virtual void FreeFile( void *buffer ) = 0;
  192. // Writes a complete file, will create any needed subdirectories.
  193. // Returns the length of the file, or -1 on failure.
  194. virtual int WriteFile( const char *relativePath, const void *buffer, int size, const char *basePath = "fs_savepath" ) = 0;
  195. // Removes the given file.
  196. virtual void RemoveFile( const char *relativePath ) = 0;
  197. // Opens a file for reading.
  198. virtual idFile * OpenFileRead( const char *relativePath, bool allowCopyFiles = true, const char* gamedir = NULL ) = 0;
  199. // Opens a file for writing, will create any needed subdirectories.
  200. virtual idFile * OpenFileWrite( const char *relativePath, const char *basePath = "fs_savepath" ) = 0;
  201. // Opens a file for writing at the end.
  202. virtual idFile * OpenFileAppend( const char *filename, bool sync = false, const char *basePath = "fs_basepath" ) = 0;
  203. // Opens a file for reading, writing, or appending depending on the value of mode.
  204. virtual idFile * OpenFileByMode( const char *relativePath, fsMode_t mode ) = 0;
  205. // Opens a file for reading from a full OS path.
  206. virtual idFile * OpenExplicitFileRead( const char *OSPath ) = 0;
  207. // Opens a file for writing to a full OS path.
  208. virtual idFile * OpenExplicitFileWrite( const char *OSPath ) = 0;
  209. // Closes a file.
  210. virtual void CloseFile( idFile *f ) = 0;
  211. // Returns immediately, performing the read from a background thread.
  212. virtual void BackgroundDownload( backgroundDownload_t *bgl ) = 0;
  213. // resets the bytes read counter
  214. virtual void ResetReadCount( void ) = 0;
  215. // retrieves the current read count
  216. virtual int GetReadCount( void ) = 0;
  217. // adds to the read count
  218. virtual void AddToReadCount( int c ) = 0;
  219. // look for a dynamic module
  220. virtual void FindDLL( const char *basename, char dllPath[ MAX_OSPATH ], bool updateChecksum ) = 0;
  221. // case sensitive filesystems use an internal directory cache
  222. // the cache is cleared when calling OpenFileWrite and RemoveFile
  223. // in some cases you may need to use this directly
  224. virtual void ClearDirCache( void ) = 0;
  225. // is D3XP installed? even if not running it atm
  226. virtual bool HasD3XP( void ) = 0;
  227. // are we using D3XP content ( through a real d3xp run or through a double mod )
  228. virtual bool RunningD3XP( void ) = 0;
  229. // don't use for large copies - allocates a single memory block for the copy
  230. virtual void CopyFile( const char *fromOSPath, const char *toOSPath ) = 0;
  231. // lookup a relative path, return the size or 0 if not found
  232. virtual int ValidateDownloadPakForChecksum( int checksum, char path[ MAX_STRING_CHARS ], bool isGamePak ) = 0;
  233. virtual idFile * MakeTemporaryFile( void ) = 0;
  234. // make downloaded pak files known so pure negociation works next time
  235. virtual int AddZipFile( const char *path ) = 0;
  236. // look for a file in the loaded paks or the addon paks
  237. // if the file is found in addons, FS's internal structures are ready for a reloadEngine
  238. virtual findFile_t FindFile( const char *path, bool scheduleAddons = false ) = 0;
  239. // get map/addon decls and take into account addon paks that are not on the search list
  240. // the decl 'name' is in the "path" entry of the dict
  241. virtual int GetNumMaps() = 0;
  242. virtual const idDict * GetMapDecl( int i ) = 0;
  243. virtual void FindMapScreenshot( const char *path, char *buf, int len ) = 0;
  244. // ignore case and seperator char distinctions
  245. virtual bool FilenameCompare( const char *s1, const char *s2 ) const = 0;
  246. };
  247. extern idFileSystem * fileSystem;
  248. #endif /* !__FILESYSTEM_H__ */