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.

149 lines
4.5 KiB

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