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.

421 lines
11 KiB

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