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.

152 lines
4.6 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Utilities for setting vproject settings
  4. //
  5. //===========================================================================//
  6. #ifdef _WIN32
  7. #if !defined( _X360 )
  8. #include <windows.h>
  9. #endif
  10. #include <direct.h>
  11. #include <io.h> // _chmod
  12. #include <process.h>
  13. #endif
  14. #if defined( _X360 )
  15. #include "xbox/xbox_win32stubs.h"
  16. #endif
  17. #include "vconfig.h"
  18. // NOTE: This has to be the last file included!
  19. #include "tier0/memdbgon.h"
  20. #ifdef _WIN32
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Returns the string value of a registry key
  23. // Input : *pName - name of the subKey to read
  24. // *pReturn - string buffer to receive read string
  25. // size - size of specified buffer
  26. //-----------------------------------------------------------------------------
  27. bool GetVConfigRegistrySetting( const char *pName, char *pReturn, int size )
  28. {
  29. // Open the key
  30. HKEY hregkey;
  31. // Changed to HKEY_CURRENT_USER from HKEY_LOCAL_MACHINE
  32. if ( RegOpenKeyEx( HKEY_CURRENT_USER, VPROJECT_REG_KEY, 0, KEY_QUERY_VALUE, &hregkey ) != ERROR_SUCCESS )
  33. return false;
  34. // Get the value
  35. DWORD dwSize = size;
  36. if ( RegQueryValueEx( hregkey, pName, NULL, NULL,(LPBYTE) pReturn, &dwSize ) != ERROR_SUCCESS )
  37. return false;
  38. // Close the key
  39. RegCloseKey( hregkey );
  40. return true;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Sends a global system message to alert programs to a changed environment variable
  44. //-----------------------------------------------------------------------------
  45. void NotifyVConfigRegistrySettingChanged( void )
  46. {
  47. DWORD_PTR dwReturnValue = 0;
  48. // Propagate changes so that environment variables takes immediate effect!
  49. SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &dwReturnValue );
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose: Set the registry entry to a string value, under the given subKey
  53. // Input : *pName - name of the subKey to set
  54. // *pValue - string value
  55. //-----------------------------------------------------------------------------
  56. void SetVConfigRegistrySetting( const char *pName, const char *pValue, bool bNotify )
  57. {
  58. HKEY hregkey;
  59. // Changed to HKEY_CURRENT_USER from HKEY_LOCAL_MACHINE
  60. // Open the key
  61. if ( RegCreateKeyEx(
  62. HKEY_CURRENT_USER, // base key
  63. VPROJECT_REG_KEY, // subkey
  64. 0, // reserved
  65. 0, // lpClass
  66. 0, // options
  67. (REGSAM)KEY_ALL_ACCESS, // access desired
  68. NULL, // security attributes
  69. &hregkey, // result
  70. NULL // tells if it created the key or not (which we don't care)
  71. ) != ERROR_SUCCESS )
  72. {
  73. return;
  74. }
  75. // Set the value to the string passed in
  76. int nType = strchr( pValue, '%' ) ? REG_EXPAND_SZ : REG_SZ;
  77. RegSetValueEx( hregkey, pName, 0, nType, (const unsigned char *)pValue, (int) strlen(pValue) );
  78. // Notify other programs
  79. if ( bNotify )
  80. {
  81. NotifyVConfigRegistrySettingChanged();
  82. }
  83. // Close the key
  84. RegCloseKey( hregkey );
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose: Removes the obsolete user keyvalue
  88. // Input : *pName - name of the subKey to set
  89. // *pValue - string value
  90. //-----------------------------------------------------------------------------
  91. bool RemoveObsoleteVConfigRegistrySetting( const char *pValueName, char *pOldValue, int size )
  92. {
  93. // Open the key
  94. HKEY hregkey;
  95. if ( RegOpenKeyEx( HKEY_CURRENT_USER, "Environment", 0, (REGSAM)KEY_ALL_ACCESS, &hregkey ) != ERROR_SUCCESS )
  96. return false;
  97. // Return the old state if they've requested it
  98. if ( pOldValue != NULL )
  99. {
  100. DWORD dwSize = size;
  101. // Get the value
  102. if ( RegQueryValueEx( hregkey, pValueName, NULL, NULL,(LPBYTE) pOldValue, &dwSize ) != ERROR_SUCCESS )
  103. return false;
  104. }
  105. // Remove the value
  106. if ( RegDeleteValue( hregkey, pValueName ) != ERROR_SUCCESS )
  107. return false;
  108. // Close the key
  109. RegCloseKey( hregkey );
  110. // Notify other programs
  111. NotifyVConfigRegistrySettingChanged();
  112. return true;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Purpose: Take a user-defined environment variable and swap it out for the internally used one
  116. //-----------------------------------------------------------------------------
  117. bool ConvertObsoleteVConfigRegistrySetting( const char *pValueName )
  118. {
  119. char szValue[MAX_PATH];
  120. if ( RemoveObsoleteVConfigRegistrySetting( pValueName, szValue, sizeof( szValue ) ) )
  121. {
  122. // Set it up the correct way
  123. SetVConfigRegistrySetting( pValueName, szValue );
  124. return true;
  125. }
  126. return false;
  127. }
  128. #endif