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.

420 lines
11 KiB

  1. //===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #if defined( WIN32 ) && !defined( _X360 )
  8. #include <windows.h>
  9. #endif
  10. #include "tier0/platform.h"
  11. #include "iregistry.h"
  12. #include "tier0/dbg.h"
  13. #include "tier1/strtools.h"
  14. #include <stdio.h>
  15. #if defined( _X360 )
  16. #include "xbox/xbox_win32stubs.h"
  17. #endif
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Exposes registry interface to rest of launcher
  22. //-----------------------------------------------------------------------------
  23. class CRegistry : public IRegistry
  24. {
  25. public:
  26. CRegistry( void );
  27. virtual ~CRegistry( void );
  28. virtual bool Init( const char *platformName );
  29. virtual bool DirectInit( const char *subDirectoryUnderValve );
  30. virtual void Shutdown( void );
  31. virtual int ReadInt( const char *key, int defaultValue = 0);
  32. virtual void WriteInt( const char *key, int value );
  33. virtual const char *ReadString( const char *key, const char *defaultValue = NULL );
  34. virtual void WriteString( const char *key, const char *value );
  35. // Read/write helper methods
  36. virtual int ReadInt( const char *pKeyBase, const char *pKey, int defaultValue = 0 );
  37. virtual void WriteInt( const char *pKeyBase, const char *key, int value );
  38. virtual const char *ReadString( const char *pKeyBase, const char *key, const char *defaultValue );
  39. virtual void WriteString( const char *pKeyBase, const char *key, const char *value );
  40. private:
  41. bool m_bValid;
  42. #ifdef WIN32
  43. HKEY m_hKey;
  44. #endif
  45. };
  46. // Creates it and calls Init
  47. IRegistry *InstanceRegistry( char const *subDirectoryUnderValve )
  48. {
  49. CRegistry *instance = new CRegistry();
  50. instance->DirectInit( subDirectoryUnderValve );
  51. return instance;
  52. }
  53. // Calls Shutdown and deletes it
  54. void ReleaseInstancedRegistry( IRegistry *reg )
  55. {
  56. if ( !reg )
  57. {
  58. Assert( !"ReleaseInstancedRegistry( reg == NULL )!" );
  59. return;
  60. }
  61. reg->Shutdown();
  62. delete reg;
  63. }
  64. // Expose to launcher
  65. static CRegistry g_Registry;
  66. IRegistry *registry = ( IRegistry * )&g_Registry;
  67. //-----------------------------------------------------------------------------
  68. // Read/write helper methods
  69. //-----------------------------------------------------------------------------
  70. int CRegistry::ReadInt( const char *pKeyBase, const char *pKey, int defaultValue )
  71. {
  72. int nLen = strlen( pKeyBase );
  73. int nKeyLen = strlen( pKey );
  74. char *pFullKey = (char*)_alloca( nLen + nKeyLen + 2 );
  75. Q_snprintf( pFullKey, nLen + nKeyLen + 2, "%s\\%s", pKeyBase, pKey );
  76. return ReadInt( pFullKey, defaultValue );
  77. }
  78. void CRegistry::WriteInt( const char *pKeyBase, const char *pKey, int value )
  79. {
  80. int nLen = strlen( pKeyBase );
  81. int nKeyLen = strlen( pKey );
  82. char *pFullKey = (char*)_alloca( nLen + nKeyLen + 2 );
  83. Q_snprintf( pFullKey, nLen + nKeyLen + 2, "%s\\%s", pKeyBase, pKey );
  84. WriteInt( pFullKey, value );
  85. }
  86. const char *CRegistry::ReadString( const char *pKeyBase, const char *pKey, const char *defaultValue )
  87. {
  88. int nLen = strlen( pKeyBase );
  89. int nKeyLen = strlen( pKey );
  90. char *pFullKey = (char*)_alloca( nLen + nKeyLen + 2 );
  91. Q_snprintf( pFullKey, nLen + nKeyLen + 2, "%s\\%s", pKeyBase, pKey );
  92. return ReadString( pFullKey, defaultValue );
  93. }
  94. void CRegistry::WriteString( const char *pKeyBase, const char *pKey, const char *value )
  95. {
  96. int nLen = strlen( pKeyBase );
  97. int nKeyLen = strlen( pKey );
  98. char *pFullKey = (char*)_alloca( nLen + nKeyLen + 2 );
  99. Q_snprintf( pFullKey, nLen + nKeyLen + 2, "%s\\%s", pKeyBase, pKey );
  100. WriteString( pFullKey, value );
  101. }
  102. #ifndef POSIX
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. CRegistry::CRegistry( void )
  107. {
  108. // Assume failure
  109. m_bValid = false;
  110. m_hKey = 0;
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Purpose:
  114. //-----------------------------------------------------------------------------
  115. CRegistry::~CRegistry( void )
  116. {
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose: Read integer from registry
  120. // Input : *key -
  121. // defaultValue -
  122. // Output : int
  123. //-----------------------------------------------------------------------------
  124. int CRegistry::ReadInt( const char *key, int defaultValue /*= 0*/ )
  125. {
  126. LONG lResult; // Registry function result code
  127. DWORD dwType; // Type of key
  128. DWORD dwSize; // Size of element data
  129. int value;
  130. if ( !m_bValid )
  131. {
  132. return defaultValue;
  133. }
  134. dwSize = sizeof( DWORD );
  135. lResult = RegQueryValueEx(
  136. m_hKey, // handle to key
  137. key, // value name
  138. 0, // reserved
  139. &dwType, // type buffer
  140. (LPBYTE)&value, // data buffer
  141. &dwSize ); // size of data buffer
  142. if (lResult != ERROR_SUCCESS) // Failure
  143. return defaultValue;
  144. if (dwType != REG_DWORD)
  145. return defaultValue;
  146. return value;
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose: Save integer to registry
  150. // Input : *key -
  151. // value -
  152. //-----------------------------------------------------------------------------
  153. void CRegistry::WriteInt( const char *key, int value )
  154. {
  155. // Size of element data
  156. DWORD dwSize;
  157. if ( !m_bValid )
  158. {
  159. return;
  160. }
  161. dwSize = sizeof( DWORD );
  162. RegSetValueEx(
  163. m_hKey, // handle to key
  164. key, // value name
  165. 0, // reserved
  166. REG_DWORD, // type buffer
  167. (LPBYTE)&value, // data buffer
  168. dwSize ); // size of data buffer
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose: Read string value from registry
  172. // Input : *key -
  173. // *defaultValue -
  174. // Output : const char
  175. //-----------------------------------------------------------------------------
  176. const char *CRegistry::ReadString( const char *key, const char *defaultValue /* = NULL */ )
  177. {
  178. LONG lResult;
  179. // Type of key
  180. DWORD dwType;
  181. // Size of element data
  182. DWORD dwSize = 512;
  183. static char value[ 512 ];
  184. value[0] = 0;
  185. if ( !m_bValid )
  186. {
  187. return defaultValue;
  188. }
  189. lResult = RegQueryValueEx(
  190. m_hKey, // handle to key
  191. key, // value name
  192. 0, // reserved
  193. &dwType, // type buffer
  194. (unsigned char *)value, // data buffer
  195. &dwSize ); // size of data buffer
  196. if ( lResult != ERROR_SUCCESS )
  197. {
  198. return defaultValue;
  199. }
  200. if ( dwType != REG_SZ )
  201. {
  202. return defaultValue;
  203. }
  204. return value;
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Purpose: Save string to registry
  208. // Input : *key -
  209. // *value -
  210. //-----------------------------------------------------------------------------
  211. void CRegistry::WriteString( const char *key, const char *value )
  212. {
  213. DWORD dwSize; // Size of element data
  214. if ( !m_bValid )
  215. {
  216. return;
  217. }
  218. dwSize = (DWORD)( strlen( value ) + 1 );
  219. RegSetValueEx(
  220. m_hKey, // handle to key
  221. key, // value name
  222. 0, // reserved
  223. REG_SZ, // type buffer
  224. (LPBYTE)value, // data buffer
  225. dwSize ); // size of data buffer
  226. }
  227. bool CRegistry::DirectInit( const char *subDirectoryUnderValve )
  228. {
  229. LONG lResult; // Registry function result code
  230. ULONG dwDisposition; // Type of key opening event
  231. char szModelKey[ 1024 ];
  232. wsprintf( szModelKey, "Software\\Valve\\%s", subDirectoryUnderValve );
  233. lResult = RegCreateKeyEx(
  234. HKEY_CURRENT_USER, // handle of open key
  235. szModelKey, // address of name of subkey to open
  236. 0ul, // DWORD ulOptions, // reserved
  237. NULL, // Type of value
  238. REG_OPTION_NON_VOLATILE, // Store permanently in reg.
  239. KEY_ALL_ACCESS, // REGSAM samDesired, // security access mask
  240. NULL,
  241. &m_hKey, // Key we are creating
  242. &dwDisposition ); // Type of creation
  243. if ( lResult != ERROR_SUCCESS )
  244. {
  245. m_bValid = false;
  246. return false;
  247. }
  248. // Success
  249. m_bValid = true;
  250. return true;
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Purpose: Open default launcher key based on game directory
  254. //-----------------------------------------------------------------------------
  255. bool CRegistry::Init( const char *platformName )
  256. {
  257. char subDir[ 512 ];
  258. wsprintf( subDir, "%s\\Settings", platformName );
  259. return DirectInit( subDir );
  260. }
  261. //-----------------------------------------------------------------------------
  262. // Purpose:
  263. //-----------------------------------------------------------------------------
  264. void CRegistry::Shutdown( void )
  265. {
  266. if ( !m_bValid )
  267. return;
  268. // Make invalid
  269. m_bValid = false;
  270. RegCloseKey( m_hKey );
  271. }
  272. #else
  273. //-----------------------------------------------------------------------------
  274. // Purpose:
  275. //-----------------------------------------------------------------------------
  276. CRegistry::CRegistry( void )
  277. {
  278. // Assume failure
  279. m_bValid = false;
  280. }
  281. //-----------------------------------------------------------------------------
  282. // Purpose:
  283. //-----------------------------------------------------------------------------
  284. CRegistry::~CRegistry( void )
  285. {
  286. }
  287. //-----------------------------------------------------------------------------
  288. // Purpose: Read integer from registry
  289. // Input : *key -
  290. // defaultValue -
  291. // Output : int
  292. //-----------------------------------------------------------------------------
  293. int CRegistry::ReadInt( const char *key, int defaultValue /*= 0*/ )
  294. {
  295. return 0;
  296. }
  297. //-----------------------------------------------------------------------------
  298. // Purpose: Save integer to registry
  299. // Input : *key -
  300. // value -
  301. //-----------------------------------------------------------------------------
  302. void CRegistry::WriteInt( const char *key, int value )
  303. {
  304. }
  305. //-----------------------------------------------------------------------------
  306. // Purpose: Read string value from registry
  307. // Input : *key -
  308. // *defaultValue -
  309. // Output : const char
  310. //-----------------------------------------------------------------------------
  311. const char *CRegistry::ReadString( const char *key, const char *defaultValue /* = NULL */ )
  312. {
  313. return 0;
  314. }
  315. //-----------------------------------------------------------------------------
  316. // Purpose: Save string to registry
  317. // Input : *key -
  318. // *value -
  319. //-----------------------------------------------------------------------------
  320. void CRegistry::WriteString( const char *key, const char *value )
  321. {
  322. }
  323. bool CRegistry::DirectInit( const char *subDirectoryUnderValve )
  324. {
  325. return true;
  326. }
  327. //-----------------------------------------------------------------------------
  328. // Purpose: Open default launcher key based on game directory
  329. //-----------------------------------------------------------------------------
  330. bool CRegistry::Init( const char *platformName )
  331. {
  332. char subDir[ 512 ];
  333. snprintf( subDir, sizeof(subDir), "%s\\Settings", platformName );
  334. return DirectInit( subDir );
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Purpose:
  338. //-----------------------------------------------------------------------------
  339. void CRegistry::Shutdown( void )
  340. {
  341. if ( !m_bValid )
  342. return;
  343. // Make invalid
  344. m_bValid = false;
  345. }
  346. #endif // POSIX