Counter Strike : Global Offensive Source Code
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.

223 lines
6.0 KiB

  1. //===== Copyright � 1996-2005, 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 POSIX
  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. // If we're under Steam we need extra setup to let us find the proper modules
  71. FileSystem_SetupSteamInstallPath();
  72. // Next, load the module, call Connect/Init.
  73. CFSLoadModuleInfo loadModuleInfo;
  74. loadModuleInfo.m_pFileSystemDLLName = fileSystemDLLName;
  75. loadModuleInfo.m_pDirectoryName = pFilename;
  76. loadModuleInfo.m_bOnlyUseDirectoryName = bOnlyUseDirectoryName;
  77. loadModuleInfo.m_ConnectFactory = Sys_GetFactoryThis();
  78. loadModuleInfo.m_bSteam = bSteam;
  79. loadModuleInfo.m_bToolsMode = true;
  80. if ( FileSystem_LoadFileSystemModule( loadModuleInfo ) != FS_OK )
  81. return false;
  82. // Next, mount the content
  83. CFSMountContentInfo mountContentInfo;
  84. mountContentInfo.m_pDirectoryName= loadModuleInfo.m_GameInfoPath;
  85. mountContentInfo.m_pFileSystem = loadModuleInfo.m_pFileSystem;
  86. mountContentInfo.m_bToolsMode = true;
  87. if ( FileSystem_MountContent( mountContentInfo ) != FS_OK )
  88. return false;
  89. // Finally, load the search paths.
  90. CFSSearchPathsInit searchPathsInit;
  91. searchPathsInit.m_pDirectoryName = loadModuleInfo.m_GameInfoPath;
  92. searchPathsInit.m_pFileSystem = loadModuleInfo.m_pFileSystem;
  93. if ( FileSystem_LoadSearchPaths( searchPathsInit ) != FS_OK )
  94. return false;
  95. // Store the data we got from filesystem_init.
  96. g_pFileSystem = g_pFullFileSystem = loadModuleInfo.m_pFileSystem;
  97. g_pFullFileSystemModule = loadModuleInfo.m_pModule;
  98. FileSystem_AddSearchPath_Platform( g_pFullFileSystem, loadModuleInfo.m_GameInfoPath );
  99. FileSystem_SetupStandardDirectories( pFilename, loadModuleInfo.m_GameInfoPath );
  100. }
  101. else
  102. {
  103. if ( !Sys_LoadInterface(
  104. "filesystem_stdio",
  105. FILESYSTEM_INTERFACE_VERSION,
  106. &g_pFullFileSystemModule,
  107. (void**)&g_pFullFileSystem ) )
  108. {
  109. return false;
  110. }
  111. if ( g_pFullFileSystem->Init() != INIT_OK )
  112. return false;
  113. g_pFullFileSystem->RemoveAllSearchPaths();
  114. g_pFullFileSystem->AddSearchPath( "../platform", "PLATFORM" );
  115. g_pFullFileSystem->AddSearchPath( ".", "GAME" );
  116. g_pFileSystem = g_pFullFileSystem;
  117. }
  118. return true;
  119. }
  120. bool FileSystem_Init( const char *pBSPFilename, int maxMemoryUsage, FSInitType_t initType, bool bOnlyUseFilename )
  121. {
  122. Assert( CommandLine()->GetCmdLine() != NULL ); // Should have called CreateCmdLine by now.
  123. // If this app uses VMPI, then let VMPI intercept all filesystem calls.
  124. #if defined( MPI )
  125. if ( g_bUseMPI )
  126. {
  127. if ( g_bMPIMaster )
  128. {
  129. if ( !FileSystem_Init_Normal( pBSPFilename, initType, bOnlyUseFilename ) )
  130. return false;
  131. g_pFileSystem = g_pFullFileSystem = VMPI_FileSystem_Init( maxMemoryUsage, g_pFullFileSystem );
  132. SendQDirInfo();
  133. }
  134. else
  135. {
  136. g_pFileSystem = g_pFullFileSystem = VMPI_FileSystem_Init( maxMemoryUsage, NULL );
  137. RecvQDirInfo();
  138. }
  139. return true;
  140. }
  141. #endif
  142. return FileSystem_Init_Normal( pBSPFilename, initType, bOnlyUseFilename );
  143. }
  144. void FileSystem_Term()
  145. {
  146. #if defined( MPI )
  147. if ( g_bUseMPI )
  148. {
  149. g_pFileSystem = g_pFullFileSystem = VMPI_FileSystem_Term();
  150. }
  151. #endif
  152. if ( g_pFullFileSystem )
  153. {
  154. g_pFullFileSystem->Shutdown();
  155. g_pFullFileSystem = NULL;
  156. g_pFileSystem = NULL;
  157. }
  158. if ( g_pFullFileSystemModule )
  159. {
  160. Sys_UnloadModule( g_pFullFileSystemModule );
  161. g_pFullFileSystemModule = NULL;
  162. }
  163. }
  164. CreateInterfaceFn FileSystem_GetFactory()
  165. {
  166. #if defined( MPI )
  167. if ( g_bUseMPI )
  168. return VMPI_FileSystem_GetFactory();
  169. #endif
  170. return Sys_GetFactory( g_pFullFileSystemModule );
  171. }
  172. bool FileSystem_SetGame( const char *szModDir )
  173. {
  174. g_pFullFileSystem->RemoveAllSearchPaths();
  175. if ( FileSystem_SetBasePaths( g_pFullFileSystem ) != FS_OK )
  176. return false;
  177. CFSSearchPathsInit fsInit;
  178. fsInit.m_pDirectoryName = szModDir;
  179. fsInit.m_pFileSystem = g_pFullFileSystem;
  180. return ( FileSystem_LoadSearchPaths( fsInit ) == FS_OK );
  181. }