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.

110 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Handle addition and editing of game configurations
  4. //
  5. //=====================================================================================//
  6. #include <windows.h>
  7. #include "interface.h"
  8. #include "tier0/icommandline.h"
  9. #include "filesystem_tools.h"
  10. #include "sdklauncher_main.h"
  11. #include "ConfigManager.h"
  12. #include "KeyValues.h"
  13. #include <io.h>
  14. #include <stdio.h>
  15. #include "configs.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. extern CGameConfigManager g_ConfigManager;
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Copy a character string into a utlvector of characters
  21. //-----------------------------------------------------------------------------
  22. void UtlStrcpy( CUtlVector<char> &dest, const char *pSrc )
  23. {
  24. dest.EnsureCount( strlen( pSrc ) + 1 );
  25. Q_strncpy( dest.Base(), pSrc, dest.Count() );
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Return the path for the gamecfg.INI file
  29. //-----------------------------------------------------------------------------
  30. const char *GetIniFilePath( void )
  31. {
  32. static char iniFilePath[MAX_PATH] = {0};
  33. if ( iniFilePath[0] == 0 )
  34. {
  35. Q_strncpy( iniFilePath, GetSDKLauncherBinDirectory(), sizeof( iniFilePath ) );
  36. Q_strncat( iniFilePath, "\\gamecfg.ini", sizeof( iniFilePath ), COPY_ALL_CHARACTERS );
  37. }
  38. return iniFilePath;
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Add a new configuration with proper paths
  42. // Output : Returns true on success, false on failure.
  43. //-----------------------------------------------------------------------------
  44. // NOTE: This code is fairly fragile, it'd be a lot better to have a solid solution for adding in a new config
  45. bool AddConfig( const char *pModName, const char *pModDirectory, ModType_t modType )
  46. {
  47. // Manager must be loaded
  48. if ( g_ConfigManager.IsLoaded() == false )
  49. return false;
  50. // Set to defaults
  51. defaultConfigInfo_t newInfo;
  52. memset( &newInfo, 0, sizeof( newInfo ) );
  53. // Mod name
  54. Q_strncpy( newInfo.gameName, pModName, sizeof( newInfo.gameName ) );
  55. // Basic FGD
  56. if ( modType == ModType_HL2 )
  57. {
  58. Q_strncpy( newInfo.FGD, "halflife2.fgd", sizeof( newInfo.FGD ) );
  59. }
  60. else if ( modType == ModType_HL2_Multiplayer )
  61. {
  62. Q_strncpy( newInfo.FGD, "hl2mp.fgd", sizeof( newInfo.FGD ) );
  63. }
  64. else
  65. {
  66. Q_strncpy( newInfo.FGD, "base.fgd", sizeof( newInfo.FGD ) );
  67. }
  68. // Get the base directory
  69. Q_FileBase( pModDirectory, newInfo.gameDir, sizeof( newInfo.gameDir ) );
  70. KeyValues *gameBlock = g_ConfigManager.GetGameBlock();
  71. // Default executable
  72. Q_strncpy( newInfo.exeName, "hl2.exe", sizeof( newInfo.exeName ) );
  73. char szPath[MAX_PATH];
  74. Q_strncpy( szPath, pModDirectory, sizeof( szPath ) );
  75. Q_StripLastDir( szPath, sizeof( szPath ) );
  76. Q_StripTrailingSlash( szPath );
  77. char fullDir[MAX_PATH];
  78. g_ConfigManager.GetRootGameDirectory( fullDir, sizeof( fullDir ), g_ConfigManager.GetRootDirectory() );
  79. // Add the config into our file
  80. g_ConfigManager.AddDefaultConfig( newInfo, gameBlock, szPath, fullDir );
  81. // Set this as the currently active configuration
  82. SetVConfigRegistrySetting( GAMEDIR_TOKEN, pModDirectory );
  83. // Save and reload our configs
  84. g_ConfigManager.SaveConfigs();
  85. g_ConfigManager.LoadConfigs();
  86. return true;
  87. }