Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #if defined( _WIN32 ) && !defined( _X360 )
  7. #include <windows.h>
  8. #include <direct.h>
  9. #include <io.h> // _chmod
  10. #elif _LINUX
  11. #include <unistd.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <sys/stat.h>
  15. #include "tier1/strtools.h"
  16. #include "filesystem_tools.h"
  17. #include "tier0/icommandline.h"
  18. #include "KeyValues.h"
  19. #include "tier2/tier2.h"
  20. #ifdef MPI
  21. #include "vmpi.h"
  22. #include "vmpi_tools_shared.h"
  23. #include "vmpi_filesystem.h"
  24. #endif
  25. // memdbgon must be the last include file in a .cpp file!!!
  26. #include <tier0/memdbgon.h>
  27. // ---------------------------------------------------------------------------------------------------- //
  28. // Module interface.
  29. // ---------------------------------------------------------------------------------------------------- //
  30. IBaseFileSystem *g_pFileSystem = NULL;
  31. // These are only used for tools that need the search paths that the engine's file system provides.
  32. CSysModule *g_pFullFileSystemModule = NULL;
  33. // ---------------------------------------------------------------------------
  34. //
  35. // These are the base paths that everything will be referenced relative to (textures especially)
  36. // All of these directories include the trailing slash
  37. //
  38. // ---------------------------------------------------------------------------
  39. // This is the the path of the initial source file (relative to the cwd)
  40. char qdir[1024];
  41. // This is the base engine + mod-specific game dir (e.g. "c:\tf2\mytfmod\")
  42. char gamedir[1024];
  43. void FileSystem_SetupStandardDirectories( const char *pFilename, const char *pGameInfoPath )
  44. {
  45. // Set qdir.
  46. if ( !pFilename )
  47. {
  48. pFilename = ".";
  49. }
  50. Q_MakeAbsolutePath( qdir, sizeof( qdir ), pFilename, NULL );
  51. Q_StripFilename( qdir );
  52. Q_strlower( qdir );
  53. if ( qdir[0] != 0 )
  54. {
  55. Q_AppendSlash( qdir, sizeof( qdir ) );
  56. }
  57. // Set gamedir.
  58. Q_MakeAbsolutePath( gamedir, sizeof( gamedir ), pGameInfoPath );
  59. Q_AppendSlash( gamedir, sizeof( gamedir ) );
  60. }
  61. bool FileSystem_Init_Normal( const char *pFilename, FSInitType_t initType, bool bOnlyUseDirectoryName )
  62. {
  63. if ( initType == FS_INIT_FULL )
  64. {
  65. // First, get the name of the module
  66. char fileSystemDLLName[MAX_PATH];
  67. bool bSteam;
  68. if ( FileSystem_GetFileSystemDLLName( fileSystemDLLName, MAX_PATH, bSteam ) != FS_OK )
  69. return false;
  70. // Next, load the module, call Connect/Init.
  71. CFSLoadModuleInfo loadModuleInfo;
  72. loadModuleInfo.m_pFileSystemDLLName = fileSystemDLLName;
  73. loadModuleInfo.m_pDirectoryName = pFilename;
  74. loadModuleInfo.m_bOnlyUseDirectoryName = bOnlyUseDirectoryName;
  75. loadModuleInfo.m_ConnectFactory = Sys_GetFactoryThis();
  76. loadModuleInfo.m_bSteam = bSteam;
  77. loadModuleInfo.m_bToolsMode = true;
  78. if ( FileSystem_LoadFileSystemModule( loadModuleInfo ) != FS_OK )
  79. return false;
  80. // Next, mount the content
  81. CFSMountContentInfo mountContentInfo;
  82. mountContentInfo.m_pDirectoryName= loadModuleInfo.m_GameInfoPath;
  83. mountContentInfo.m_pFileSystem = loadModuleInfo.m_pFileSystem;
  84. mountContentInfo.m_bToolsMode = true;
  85. if ( FileSystem_MountContent( mountContentInfo ) != FS_OK )
  86. return false;
  87. // Finally, load the search paths.
  88. CFSSearchPathsInit searchPathsInit;
  89. searchPathsInit.m_pDirectoryName = loadModuleInfo.m_GameInfoPath;
  90. searchPathsInit.m_pFileSystem = loadModuleInfo.m_pFileSystem;
  91. if ( FileSystem_LoadSearchPaths( searchPathsInit ) != FS_OK )
  92. return false;
  93. // Store the data we got from filesystem_init.
  94. g_pFileSystem = g_pFullFileSystem = loadModuleInfo.m_pFileSystem;
  95. g_pFullFileSystemModule = loadModuleInfo.m_pModule;
  96. FileSystem_AddSearchPath_Platform( g_pFullFileSystem, loadModuleInfo.m_GameInfoPath );
  97. FileSystem_SetupStandardDirectories( pFilename, loadModuleInfo.m_GameInfoPath );
  98. }
  99. else
  100. {
  101. if ( !Sys_LoadInterface(
  102. "filesystem_stdio",
  103. FILESYSTEM_INTERFACE_VERSION,
  104. &g_pFullFileSystemModule,
  105. (void**)&g_pFullFileSystem ) )
  106. {
  107. return false;
  108. }
  109. if ( g_pFullFileSystem->Init() != INIT_OK )
  110. return false;
  111. g_pFullFileSystem->RemoveAllSearchPaths();
  112. g_pFullFileSystem->AddSearchPath( "../platform", "PLATFORM" );
  113. g_pFullFileSystem->AddSearchPath( ".", "GAME" );
  114. g_pFileSystem = g_pFullFileSystem;
  115. }
  116. return true;
  117. }
  118. bool FileSystem_Init( const char *pBSPFilename, int maxMemoryUsage, FSInitType_t initType, bool bOnlyUseFilename )
  119. {
  120. Assert( CommandLine()->GetCmdLine() != NULL ); // Should have called CreateCmdLine by now.
  121. // If this app uses VMPI, then let VMPI intercept all filesystem calls.
  122. #if defined( MPI )
  123. if ( g_bUseMPI )
  124. {
  125. if ( g_bMPIMaster )
  126. {
  127. if ( !FileSystem_Init_Normal( pBSPFilename, initType, bOnlyUseFilename ) )
  128. return false;
  129. g_pFileSystem = g_pFullFileSystem = VMPI_FileSystem_Init( maxMemoryUsage, g_pFullFileSystem );
  130. SendQDirInfo();
  131. }
  132. else
  133. {
  134. g_pFileSystem = g_pFullFileSystem = VMPI_FileSystem_Init( maxMemoryUsage, NULL );
  135. RecvQDirInfo();
  136. }
  137. return true;
  138. }
  139. #endif
  140. return FileSystem_Init_Normal( pBSPFilename, initType, bOnlyUseFilename );
  141. }
  142. void FileSystem_Term()
  143. {
  144. #if defined( MPI )
  145. if ( g_bUseMPI )
  146. {
  147. g_pFileSystem = g_pFullFileSystem = VMPI_FileSystem_Term();
  148. }
  149. #endif
  150. if ( g_pFullFileSystem )
  151. {
  152. g_pFullFileSystem->Shutdown();
  153. g_pFullFileSystem = NULL;
  154. g_pFileSystem = NULL;
  155. }
  156. if ( g_pFullFileSystemModule )
  157. {
  158. Sys_UnloadModule( g_pFullFileSystemModule );
  159. g_pFullFileSystemModule = NULL;
  160. }
  161. }
  162. CreateInterfaceFn FileSystem_GetFactory()
  163. {
  164. #if defined( MPI )
  165. if ( g_bUseMPI )
  166. return VMPI_FileSystem_GetFactory();
  167. #endif
  168. return Sys_GetFactory( g_pFullFileSystemModule );
  169. }