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.

129 lines
3.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef _DLC_HELPER_H
  8. #define _DLC_HELPER_H
  9. #pragma once
  10. #include "platform.h"
  11. #include "keyvalues.h"
  12. #include "filesystem.h"
  13. class KeyValues;
  14. // Helper methods for DLC
  15. class DLCHelper
  16. {
  17. public:
  18. static inline uint64 GetInstalledDLCMask( void );
  19. // Loads the key values from all installed DLC files matching the file name plus _dlc[id].
  20. // For example, if fileName is "gamemodes.txt" we will look for "gamemodes_dlc1.txt", "gamemodes_dlc2.txt", etc...
  21. // The key values from the dlc files will then be merged in (with update) to the passed
  22. // in key values.
  23. static inline void AppendDLCKeyValues( KeyValues* pKeyValues, const char* fileName, const char* startDir = NULL );
  24. };
  25. uint64 DLCHelper::GetInstalledDLCMask( void )
  26. {
  27. // Assert( g_pMatchFramework );
  28. // return g_pMatchFramework->GetMatchSystem()->GetDlcManager()->GetDataInfo()->GetUint64( "@info/installed" );
  29. //static ConVarRef mm_dlcs_mask_fake( "mm_dlcs_mask_fake" );
  30. //char const *szFakeDlcsString = mm_dlcs_mask_fake.GetString();
  31. //if ( *szFakeDlcsString )
  32. // return atoi( szFakeDlcsString );
  33. //static ConVarRef mm_dlcs_mask_extras( "mm_dlcs_mask_extras" );
  34. //uint64 uiDLCmask = ( unsigned ) mm_dlcs_mask_extras.GetInt();
  35. uint64 uiDLCmask = 0;
  36. bool bSearchPath = false;
  37. int numDLCs = g_pFullFileSystem->IsAnyDLCPresent( &bSearchPath );
  38. // If we need to, trigger the mounting of DLC
  39. if ( !bSearchPath )
  40. {
  41. g_pFullFileSystem->AddDLCSearchPaths();
  42. }
  43. for ( int j = 0; j < numDLCs; ++ j )
  44. {
  45. unsigned int uiDlcHeader = 0;
  46. if ( !g_pFullFileSystem->GetAnyDLCInfo( j, &uiDlcHeader, NULL, 0 ) )
  47. continue;
  48. int idDLC = DLC_LICENSE_ID( uiDlcHeader );
  49. if ( idDLC < 1 || idDLC >= 31 )
  50. continue; // unsupported DLC id
  51. uiDLCmask |= ( 1ull << idDLC );
  52. }
  53. return uiDLCmask;
  54. }
  55. void DLCHelper::AppendDLCKeyValues( KeyValues* pKeyValues, const char* fileName, const char* startDir )
  56. {
  57. uint64 installedDlc = GetInstalledDLCMask();
  58. if ( installedDlc )
  59. {
  60. KeyValues* pDlcKeyValues = new KeyValues( "" );
  61. char dlcFileName[128] = "";
  62. // We need to insert _dlc[id] into the file name right before the extension
  63. // so filename.txt should become filename_dlc1.txt
  64. // scan backward for '.'
  65. int dotIndex = V_strlen( fileName ) - 1;
  66. while ( dotIndex > 0 && fileName[dotIndex] != '.' )
  67. {
  68. --dotIndex;
  69. }
  70. if ( dotIndex == 0 )
  71. {
  72. Warning( "Invalid file name passed to DLCHelper::AppendDLCKeyValues (%s)\n", fileName );
  73. return;
  74. }
  75. const char* extension = fileName + dotIndex + 1;
  76. V_strncpy( dlcFileName, fileName, 128 );
  77. // For each installed dlc check for an updated file and merge it in
  78. for ( uint64 i = 1; i < 64; i++ )
  79. {
  80. // Don't bother if this dlc isn't installed
  81. if ( installedDlc & ( 1ull << i ) )
  82. {
  83. // Get the filename for this dlc
  84. V_snprintf( dlcFileName + dotIndex, 128 - dotIndex, "_dlc%d.%s", ( int )i, extension );
  85. // Load and merge the keys from this dlc
  86. pDlcKeyValues->Clear();
  87. if ( pDlcKeyValues->LoadFromFile( g_pFullFileSystem, dlcFileName, startDir ) )
  88. {
  89. pKeyValues->MergeFrom( pDlcKeyValues, KeyValues::MERGE_KV_UPDATE );
  90. }
  91. else
  92. {
  93. Warning( "Failed to load %s\n", dlcFileName );
  94. }
  95. }
  96. }
  97. if ( pDlcKeyValues )
  98. {
  99. pDlcKeyValues->deleteThis();
  100. pDlcKeyValues = NULL;
  101. }
  102. }
  103. }
  104. #endif // _DLC_HELPER_H