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.

467 lines
14 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include <windows.h>
  7. #include <io.h>
  8. #include <sys/stat.h>
  9. #include <vgui/isystem.h>
  10. #include "min_footprint_files.h"
  11. #include "filesystem_tools.h"
  12. #include "KeyValues.h"
  13. #include "sdklauncher_main.h"
  14. #include "ModConfigsHelper.h"
  15. #include "vgui_controls/Frame.h"
  16. #include <vgui_controls/Label.h>
  17. #include <vgui_controls/MessageBox.h>
  18. #include <vgui_controls/ProgressBar.h>
  19. #include <vgui/iinput.h>
  20. #include <vgui/ivgui.h>
  21. #include "SourceAppInfo.h"
  22. extern void OpenLocalizedURL( const char *lpszLocalName );
  23. extern FSReturnCode_t LoadGameInfoFile( const char *pDirectoryName,
  24. KeyValues *&pMainFile,
  25. KeyValues *&pFileSystemInfo,
  26. KeyValues *&pSearchPaths );
  27. using namespace vgui;
  28. #define SHOW_MIGRATION_MARKER "show_migration_marker.txt"
  29. #define SHOW_DEPRECATED_APP_ID_MARKER "show_deprecatedappid_marker.txt"
  30. #define MIN_FOOTPRINT_VERSION_FILENAME "min_footprint_version.txt"
  31. #define MIN_FOOTPRINT_FILES_FILENAME "min_footprint_file_list.txt"
  32. #define WRITABLE_PREFIX "[make_writable]"
  33. #define ALWAYS_COPY_PREFIX "[always_copy]"
  34. class CTempDirName
  35. {
  36. public:
  37. char m_SrcDirName[MAX_PATH];
  38. char m_DestDirName[MAX_PATH];
  39. };
  40. #define MF_MAKE_WRITABLE 0x0001
  41. #define MF_ALWAYS_COPY 0x0002
  42. class CMinFootprintFilename
  43. {
  44. public:
  45. char m_SrcFilename[MAX_PATH];
  46. char m_DestFilename[MAX_PATH];
  47. int m_Flags; // Combination of MF_ flags.
  48. };
  49. void SafeCopy( const char *pSrcFilename, const char *pDestFilename )
  50. {
  51. FileHandle_t fpSrc = g_pFullFileSystem->Open( pSrcFilename, "rb" );
  52. if ( fpSrc )
  53. {
  54. FILE *fpDest = fopen( pDestFilename, "wb" );
  55. if ( fpDest )
  56. {
  57. while ( 1 )
  58. {
  59. char tempData[4096];
  60. int nBytesRead = g_pFullFileSystem->Read( tempData, sizeof( tempData ), fpSrc );
  61. if ( nBytesRead )
  62. fwrite( tempData, 1, nBytesRead, fpDest );
  63. if ( nBytesRead < sizeof( tempData ) )
  64. break;
  65. }
  66. fclose( fpDest );
  67. }
  68. else
  69. {
  70. Warning( "SafeCopy: can't open %s for writing.", pDestFilename );
  71. }
  72. g_pFullFileSystem->Close( fpSrc );
  73. }
  74. else
  75. {
  76. Warning( "SafeCopy: can't open %s for reading.", pSrcFilename );
  77. }
  78. }
  79. class CMinFootprintFilesPanel : public Frame
  80. {
  81. public:
  82. typedef Frame BaseClass;
  83. CMinFootprintFilesPanel( Panel *parent, const char *panelName )
  84. : BaseClass( parent, panelName )
  85. {
  86. m_pProgressBar = new ProgressBar( this, "CopyProgressBar" );
  87. m_pProgressBar->SetProgress( 0 );
  88. LoadControlSettings( "MinFootprintFilesPanel.res");
  89. SetSizeable( false );
  90. SetMenuButtonVisible( false );
  91. SetMenuButtonResponsive( false );
  92. SetCloseButtonVisible( false );
  93. // Take the focus.
  94. m_PrevAppFocusPanel = input()->GetAppModalSurface();
  95. input()->SetAppModalSurface( GetVPanel() );
  96. }
  97. ~CMinFootprintFilesPanel()
  98. {
  99. input()->SetAppModalSurface( m_PrevAppFocusPanel );
  100. }
  101. void StartDumpingFiles()
  102. {
  103. m_pProgressBar->SetProgress( 0 );
  104. m_iCurCopyFile = 0;
  105. ivgui()->AddTickSignal( GetVPanel() );
  106. MoveToCenterOfScreen();
  107. Activate();
  108. }
  109. void OnTick()
  110. {
  111. int nCopied = 0;
  112. while ( nCopied < 10 )
  113. {
  114. if ( m_iCurCopyFile >= m_Filenames.Count() )
  115. {
  116. // Done! Write the output version and disappear.
  117. OnFinished();
  118. return;
  119. }
  120. CMinFootprintFilename *pFilename = &m_Filenames[m_iCurCopyFile++];
  121. // If the dest file doesn't exist or it's read-only, copy our file over it.
  122. if ( (pFilename->m_Flags & MF_ALWAYS_COPY) || (_access( pFilename->m_DestFilename, 2 ) != 0) )
  123. {
  124. // Get rid of the old version of the file.
  125. _chmod( pFilename->m_DestFilename, _S_IWRITE | _S_IREAD ); // Read-only.
  126. _unlink( pFilename->m_DestFilename );
  127. // Copy the new version in.
  128. SafeCopy( pFilename->m_SrcFilename, pFilename->m_DestFilename );
  129. // Set its access permissions.
  130. if ( pFilename->m_Flags & MF_MAKE_WRITABLE )
  131. _chmod( pFilename->m_DestFilename, _S_IREAD | _S_IWRITE );
  132. else
  133. _chmod( pFilename->m_DestFilename, _S_IREAD ); // Read-only.
  134. }
  135. ++nCopied;
  136. }
  137. m_pProgressBar->SetProgress( (float)m_iCurCopyFile / m_Filenames.Count() );
  138. }
  139. void OnFinished()
  140. {
  141. m_pProgressBar->SetProgress( 1 );
  142. //
  143. // Write out that we are at the latest version.
  144. //
  145. KeyValues *pVersionKV = new KeyValues( MIN_FOOTPRINT_VERSION_FILENAME );
  146. pVersionKV->SetInt( "Version", m_iOutputVersionNumber );
  147. pVersionKV->SaveToFile( g_pFullFileSystem, MIN_FOOTPRINT_VERSION_FILENAME );
  148. pVersionKV->deleteThis();
  149. // Don't tick any more.
  150. ivgui()->RemoveTickSignal( GetVPanel() );
  151. // Remove our panel.
  152. PostMessage(this, new KeyValues("Close"));
  153. ShowDeprecatedAppIDNotice();
  154. ShowContentMigrationNotice();
  155. }
  156. void ShowDeprecatedAppIDNotice()
  157. {
  158. // Try to open the file that indicates that we've already been through this check
  159. FileHandle_t fp = g_pFullFileSystem->Open( SHOW_DEPRECATED_APP_ID_MARKER, "rb" );
  160. // If the file exists then we've run the check already, don't do anything.
  161. if ( fp )
  162. {
  163. g_pFullFileSystem->Close( fp );
  164. }
  165. // We have not checked for outdated mods yet...
  166. else
  167. {
  168. // Write file that ensures we never perform this check again
  169. fp = g_pFullFileSystem->Open( SHOW_DEPRECATED_APP_ID_MARKER, "wb" );
  170. g_pFullFileSystem->Close( fp );
  171. //
  172. // Look for all of the mods under 'SourceMods', check if the SteamAppId is out of date, and warn the user if
  173. // this is the case
  174. //
  175. ModConfigsHelper mch;
  176. const CUtlVector<char *> &modDirs = mch.getModDirsVector();
  177. vgui::MessageBox *alert = NULL;
  178. char szProblemMods[5*MAX_PATH];
  179. bool bProblemModExists = false;
  180. // Set up the error string
  181. Q_strncpy(szProblemMods, "The SteamAppId values for the following mods should be changed from 220 to 215:\r\n\r\n", 5*MAX_PATH);
  182. // Iterate through the mods and check which ones have outdated SteamAppIds
  183. for ( int i = 0; i < modDirs.Count(); i++ )
  184. {
  185. // Construct full path to mod directory
  186. char szDirPath[MAX_PATH];
  187. char szGameConfigPath[MAX_PATH];
  188. Q_strncpy( szDirPath, mch.getSourceModBaseDir(), MAX_PATH );
  189. Q_strncat( szDirPath, "\\", MAX_PATH , COPY_ALL_CHARACTERS );
  190. Q_strncat( szDirPath, modDirs[i], MAX_PATH , COPY_ALL_CHARACTERS );
  191. // Check for the existence of gameinfo.txt
  192. Q_strncpy( szGameConfigPath, szDirPath, MAX_PATH );
  193. Q_strncat( szGameConfigPath, "\\gameinfo.txt", MAX_PATH , COPY_ALL_CHARACTERS );
  194. FileHandle_t fpGameInfo = g_pFullFileSystem->Open( szGameConfigPath, "rb" );
  195. // GameInfo.txt exists so let's inspect it...
  196. if ( fpGameInfo )
  197. {
  198. // Close the file handle
  199. g_pFullFileSystem->Close( fpGameInfo );
  200. // Load up the "gameinfo.txt"
  201. KeyValues *pMainFile, *pFileSystemInfo, *pSearchPaths;
  202. LoadGameInfoFile( szDirPath, pMainFile, pFileSystemInfo, pSearchPaths );
  203. //!! bug? check return code of loadgameinfofile??
  204. if (NULL != pFileSystemInfo)
  205. {
  206. const int iAppId = pFileSystemInfo->GetInt( "SteamAppId", -1 );
  207. // This is the one that needs replacing add this mod to the list of suspect mods
  208. if ( GetAppSteamAppId( k_App_HL2 ) == iAppId)
  209. {
  210. bProblemModExists = true;
  211. Q_strncat( szProblemMods, modDirs[i], MAX_PATH , COPY_ALL_CHARACTERS );
  212. Q_strncat( szProblemMods, "\r\n", MAX_PATH , COPY_ALL_CHARACTERS );
  213. }
  214. }
  215. }
  216. }
  217. // If necessary, pop up a message box informing the user that
  218. if (bProblemModExists)
  219. {
  220. // Amend the warning message text
  221. Q_strncat( szProblemMods, "\r\nIf you did not author any of the above mods you can ignore this message.\r\nIf you did author any of the above mods you should change the SteamAppId\nvalue in the gameinfo.txt file for each mod listed above.\r\n", 5*MAX_PATH , COPY_ALL_CHARACTERS );
  222. // Pop up a message box
  223. alert = new vgui::MessageBox( "Warning", szProblemMods );
  224. alert->SetOKButtonText("Close");
  225. alert->DoModal();
  226. }
  227. }
  228. }
  229. void ShowContentMigrationNotice()
  230. {
  231. // If we've shown it already, don't do anything.
  232. FileHandle_t fp = g_pFullFileSystem->Open( SHOW_MIGRATION_MARKER, "rb" );
  233. if ( fp )
  234. {
  235. g_pFullFileSystem->Close( fp );
  236. return;
  237. }
  238. // Remember that we showed it.
  239. fp = g_pFullFileSystem->Open( SHOW_MIGRATION_MARKER, "wb" );
  240. g_pFullFileSystem->Close( fp );
  241. OpenLocalizedURL( "URL_Content_Migration_Notice" );
  242. }
  243. public:
  244. CUtlVector<CMinFootprintFilename> m_Filenames;
  245. int m_iOutputVersionNumber;
  246. private:
  247. vgui::VPANEL m_PrevAppFocusPanel;
  248. ProgressBar *m_pProgressBar;
  249. int m_iCurCopyFile;
  250. };
  251. void AddMinFootprintFile( CUtlVector<CMinFootprintFilename> &filenames, const char *pSrcFilename, const char *pDestFilename, int flags )
  252. {
  253. // Just copy this one file.
  254. CMinFootprintFilename *pOutputFilename = &filenames[filenames.AddToTail()];
  255. Q_strncpy( pOutputFilename->m_SrcFilename, pSrcFilename, sizeof( pOutputFilename->m_SrcFilename ) );
  256. Q_strncpy( pOutputFilename->m_DestFilename, pDestFilename, sizeof( pOutputFilename->m_DestFilename ) );
  257. pOutputFilename->m_Flags = flags;
  258. }
  259. void GetMinFootprintFiles_R( CUtlVector<CMinFootprintFilename> &filenames, const char *pSrcDirName, const char *pDestDirName )
  260. {
  261. // pDirName\*.*
  262. char wildcard[MAX_PATH];
  263. Q_strncpy( wildcard, pSrcDirName, sizeof( wildcard ) );
  264. Q_AppendSlash( wildcard, sizeof( wildcard ) );
  265. Q_strncat( wildcard, "*.*", sizeof( wildcard ), COPY_ALL_CHARACTERS );
  266. CUtlVector<CTempDirName> subDirs;
  267. // Make sure the dest directory exists for when we're copying files into it.
  268. CreateDirectory( pDestDirName, NULL );
  269. // Look at all the files.
  270. FileFindHandle_t findHandle;
  271. const char *pFilename = g_pFullFileSystem->FindFirstEx( wildcard, SDKLAUNCHER_MAIN_PATH_ID, &findHandle );
  272. while ( pFilename )
  273. {
  274. if ( Q_stricmp( pFilename, "." ) != 0 && Q_stricmp( pFilename, ".." ) != 0 )
  275. {
  276. char fullSrcFilename[MAX_PATH], fullDestFilename[MAX_PATH];
  277. Q_snprintf( fullSrcFilename, sizeof( fullSrcFilename ), "%s%c%s", pSrcDirName, CORRECT_PATH_SEPARATOR, pFilename );
  278. Q_snprintf( fullDestFilename, sizeof( fullDestFilename ), "%s%c%s", pDestDirName, CORRECT_PATH_SEPARATOR, pFilename );
  279. if ( g_pFullFileSystem->FindIsDirectory( findHandle ) )
  280. {
  281. CTempDirName *pOut = &subDirs[subDirs.AddToTail()];
  282. Q_strncpy( pOut->m_SrcDirName, fullSrcFilename, sizeof( pOut->m_SrcDirName ) );
  283. Q_strncpy( pOut->m_DestDirName, fullDestFilename, sizeof( pOut->m_DestDirName ) );
  284. }
  285. else
  286. {
  287. AddMinFootprintFile( filenames, fullSrcFilename, fullDestFilename, 0 );
  288. }
  289. }
  290. pFilename = g_pFullFileSystem->FindNext( findHandle );
  291. }
  292. g_pFullFileSystem->FindClose( findHandle );
  293. // Recurse.
  294. for ( int i=0; i < subDirs.Count(); i++ )
  295. {
  296. GetMinFootprintFiles_R( filenames, subDirs[i].m_SrcDirName, subDirs[i].m_DestDirName );
  297. }
  298. }
  299. void DumpMinFootprintFiles( bool bForceRefresh )
  300. {
  301. if ( !g_pFullFileSystem->IsSteam() )
  302. return;
  303. // What version are we at now?
  304. int curVersion = 0;
  305. if ( !bForceRefresh )
  306. {
  307. KeyValues *kv = new KeyValues( "" );
  308. if ( kv->LoadFromFile( g_pFullFileSystem, MIN_FOOTPRINT_VERSION_FILENAME ) )
  309. {
  310. curVersion = kv->GetInt( "Version", 0 );
  311. }
  312. kv->deleteThis();
  313. }
  314. // What is the current version?
  315. int latestVersion = 0;
  316. KeyValues *kv = new KeyValues( "" );
  317. bool bValidFile = true;
  318. if ( !kv->LoadFromFile( g_pFullFileSystem, MIN_FOOTPRINT_FILES_FILENAME ) )
  319. bValidFile = false;
  320. KeyValues *pFileList = NULL;
  321. if ( bValidFile && (pFileList = kv->FindKey( "files" )) == NULL )
  322. bValidFile = false;
  323. if ( !bValidFile )
  324. {
  325. VGUIMessageBox( (vgui::Frame *) g_pMainFrame, "#Warning", "#CantLoadMinFootprintFiles" );
  326. kv->deleteThis();
  327. return;
  328. }
  329. latestVersion = kv->GetInt( "Version", 0 );
  330. bool bVersionChanged = ( curVersion != latestVersion );
  331. CMinFootprintFilesPanel *pDisplay = new CMinFootprintFilesPanel( (vgui::Frame *) g_pMainFrame, "MinFootprintFilesPanel" );
  332. pDisplay->m_iOutputVersionNumber = latestVersion;
  333. // Our version is out of date, let's copy out all the min footprint files.
  334. // NOTE: copy files that are set to always_copy whether the version changed or not.
  335. for ( KeyValues *pCur=pFileList->GetFirstSubKey(); pCur; pCur=pCur->GetNextKey() )
  336. {
  337. if ( ( Q_stricmp( pCur->GetName(), "mapping" ) == 0 ) && bVersionChanged )
  338. {
  339. const char *pSrcMapping = pCur->GetString( "src" );
  340. const char *pDestMapping = pCur->GetString( "dest" );
  341. char destDir[MAX_PATH], destDirTemp[MAX_PATH];
  342. Q_snprintf( destDirTemp, sizeof( destDirTemp ), "%s%c%s", GetSDKLauncherBaseDirectory(), CORRECT_PATH_SEPARATOR, pDestMapping );
  343. Q_MakeAbsolutePath( destDir, sizeof( destDir ), destDirTemp );
  344. GetMinFootprintFiles_R( pDisplay->m_Filenames, pSrcMapping, destDir );
  345. }
  346. else if ( Q_stricmp( pCur->GetName(), "single_file" ) == 0 )
  347. {
  348. const char *pDestMapping = pCur->GetString();
  349. // If the filename is preceded by the right prefix, then make it writable.
  350. int flags = 0;
  351. if ( Q_stristr( pDestMapping, WRITABLE_PREFIX ) == pDestMapping )
  352. {
  353. flags |= MF_MAKE_WRITABLE;
  354. pDestMapping += strlen( WRITABLE_PREFIX );
  355. }
  356. // Check for [always_copy]
  357. if ( Q_stristr( pDestMapping, ALWAYS_COPY_PREFIX ) == pDestMapping )
  358. {
  359. flags |= MF_ALWAYS_COPY;
  360. pDestMapping += strlen( ALWAYS_COPY_PREFIX );
  361. }
  362. char destFile[MAX_PATH], destFileTemp[MAX_PATH];
  363. Q_snprintf( destFileTemp, sizeof( destFileTemp ), "%s%c%s", GetSDKLauncherBaseDirectory(), CORRECT_PATH_SEPARATOR, pDestMapping );
  364. Q_MakeAbsolutePath( destFile, sizeof( destFile ), destFileTemp );
  365. if ( bVersionChanged || ( flags & MF_ALWAYS_COPY ) )
  366. {
  367. AddMinFootprintFile( pDisplay->m_Filenames, pDestMapping, destFile, flags );
  368. }
  369. }
  370. else if ( ( Q_stricmp( pCur->GetName(), "create_directory" ) == 0 ) && bVersionChanged )
  371. {
  372. // Create an empty directory?
  373. char destFile[MAX_PATH], destFileTemp[MAX_PATH];
  374. Q_snprintf( destFileTemp, sizeof( destFileTemp ), "%s%c%s", GetSDKLauncherBaseDirectory(), CORRECT_PATH_SEPARATOR, pCur->GetString() );
  375. Q_MakeAbsolutePath( destFile, sizeof( destFile ), destFileTemp );
  376. CreateDirectory( destFile, NULL );
  377. }
  378. }
  379. pDisplay->StartDumpingFiles();
  380. }