registry.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // Registry.cpp
  24. // Simple interface for storing/retreiving registry values
  25. // Author: Matthew D. Campbell, December 2001
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "Common/Registry.h"
  28. #ifdef _INTERNAL
  29. // for occasional debugging...
  30. //#pragma optimize("", off)
  31. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  32. #endif
  33. Bool getStringFromRegistry(HKEY root, AsciiString path, AsciiString key, AsciiString& val)
  34. {
  35. HKEY handle;
  36. unsigned char buffer[256];
  37. unsigned long size = 256;
  38. unsigned long type;
  39. int returnValue;
  40. if ((returnValue = RegOpenKeyEx( root, path.str(), 0, KEY_READ, &handle )) == ERROR_SUCCESS)
  41. {
  42. returnValue = RegQueryValueEx(handle, key.str(), NULL, &type, (unsigned char *) &buffer, &size);
  43. RegCloseKey( handle );
  44. }
  45. if (returnValue == ERROR_SUCCESS)
  46. {
  47. val = (char *)buffer;
  48. return TRUE;
  49. }
  50. return FALSE;
  51. }
  52. Bool getUnsignedIntFromRegistry(HKEY root, AsciiString path, AsciiString key, UnsignedInt& val)
  53. {
  54. HKEY handle;
  55. unsigned char buffer[4];
  56. unsigned long size = 4;
  57. unsigned long type;
  58. int returnValue;
  59. if ((returnValue = RegOpenKeyEx( root, path.str(), 0, KEY_READ, &handle )) == ERROR_SUCCESS)
  60. {
  61. returnValue = RegQueryValueEx(handle, key.str(), NULL, &type, (unsigned char *) &buffer, &size);
  62. RegCloseKey( handle );
  63. }
  64. if (returnValue == ERROR_SUCCESS)
  65. {
  66. val = *(UnsignedInt *)buffer;
  67. return TRUE;
  68. }
  69. return FALSE;
  70. }
  71. Bool setStringInRegistry( HKEY root, AsciiString path, AsciiString key, AsciiString val)
  72. {
  73. HKEY handle;
  74. unsigned long type;
  75. unsigned long returnValue;
  76. int size;
  77. if ((returnValue = RegCreateKeyEx( root, path.str(), 0, "REG_NONE", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &handle, NULL )) == ERROR_SUCCESS)
  78. {
  79. type = REG_SZ;
  80. size = val.getLength()+1;
  81. returnValue = RegSetValueEx(handle, key.str(), 0, type, (unsigned char *)val.str(), size);
  82. RegCloseKey( handle );
  83. }
  84. return (returnValue == ERROR_SUCCESS);
  85. }
  86. Bool setUnsignedIntInRegistry( HKEY root, AsciiString path, AsciiString key, UnsignedInt val)
  87. {
  88. HKEY handle;
  89. unsigned long type;
  90. unsigned long returnValue;
  91. int size;
  92. if ((returnValue = RegCreateKeyEx( root, path.str(), 0, "REG_NONE", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &handle, NULL )) == ERROR_SUCCESS)
  93. {
  94. type = REG_DWORD;
  95. size = 4;
  96. returnValue = RegSetValueEx(handle, key.str(), 0, type, (unsigned char *)&val, size);
  97. RegCloseKey( handle );
  98. }
  99. return (returnValue == ERROR_SUCCESS);
  100. }
  101. Bool GetStringFromRegistry(AsciiString path, AsciiString key, AsciiString& val)
  102. {
  103. AsciiString fullPath = "SOFTWARE\\Electronic Arts\\EA Games\\Generals";
  104. fullPath.concat(path);
  105. DEBUG_LOG(("GetStringFromRegistry - looking in %s for key %s\n", fullPath.str(), key.str()));
  106. if (getStringFromRegistry(HKEY_LOCAL_MACHINE, fullPath.str(), key.str(), val))
  107. {
  108. return TRUE;
  109. }
  110. return getStringFromRegistry(HKEY_CURRENT_USER, fullPath.str(), key.str(), val);
  111. }
  112. Bool GetUnsignedIntFromRegistry(AsciiString path, AsciiString key, UnsignedInt& val)
  113. {
  114. AsciiString fullPath = "SOFTWARE\\Electronic Arts\\EA Games\\Generals";
  115. fullPath.concat(path);
  116. DEBUG_LOG(("GetUnsignedIntFromRegistry - looking in %s for key %s\n", fullPath.str(), key.str()));
  117. if (getUnsignedIntFromRegistry(HKEY_LOCAL_MACHINE, fullPath.str(), key.str(), val))
  118. {
  119. return TRUE;
  120. }
  121. return getUnsignedIntFromRegistry(HKEY_CURRENT_USER, fullPath.str(), key.str(), val);
  122. }
  123. AsciiString GetRegistryLanguage(void)
  124. {
  125. static Bool cached = FALSE;
  126. static AsciiString val = "english";
  127. if (cached) {
  128. return val;
  129. } else {
  130. cached = TRUE;
  131. }
  132. GetStringFromRegistry("", "Language", val);
  133. return val;
  134. }
  135. AsciiString GetRegistryGameName(void)
  136. {
  137. AsciiString val = "GeneralsMPTest";
  138. GetStringFromRegistry("", "SKU", val);
  139. return val;
  140. }
  141. UnsignedInt GetRegistryVersion(void)
  142. {
  143. UnsignedInt val = 65536;
  144. GetUnsignedIntFromRegistry("", "Version", val);
  145. return val;
  146. }
  147. UnsignedInt GetRegistryMapPackVersion(void)
  148. {
  149. UnsignedInt val = 65536;
  150. GetUnsignedIntFromRegistry("", "MapPackVersion", val);
  151. return val;
  152. }