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.

265 lines
7.7 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. #include "movieobjects/dmeusersettings.h"
  3. #include "datamodel/dmelementfactoryhelper.h"
  4. #include "datamodel/dmattributevar.h"
  5. #if !defined( _X360 )
  6. #define WIN32_LEAN_AND_MEAN
  7. #define OEMRESOURCE
  8. #include <windows.h>
  9. #endif
  10. #include "tier0/memdbgon.h" // memdbgon must be the last include file in a .cpp file!!!
  11. //-----------------------------------------------------------------------------
  12. IMPLEMENT_ELEMENT_FACTORY( DmeUserSettings, CDmeUserSettings);
  13. static CUtlMap< const char *, CDmAttribute * >s_RegistryPathToAttribute( DefLessFunc( const char * ) );
  14. CUtlVector< IDmeUserSettingsChangedListener * > CDmeUserSettings::s_UserSettingsChangedListeners;
  15. //-----------------------------------------------------------------------------
  16. CDmeUserSettings *CDmeUserSettings::SharedUserSettings()
  17. {
  18. static CDmeUserSettings *s_UserSettings;
  19. if( !s_UserSettings )
  20. {
  21. s_UserSettings = CreateElement< CDmeUserSettings >( "userSettings", DMFILEID_INVALID );
  22. }
  23. return s_UserSettings;
  24. }
  25. //-----------------------------------------------------------------------------
  26. void CDmeUserSettings::OnConstruction()
  27. {
  28. }
  29. void CDmeUserSettings::OnDestruction()
  30. {
  31. }
  32. //-----------------------------------------------------------------------------
  33. void CDmeUserSettings::AddUserSettingsChangedListener( IDmeUserSettingsChangedListener *pListener )
  34. {
  35. s_UserSettingsChangedListeners.AddToTail( pListener );
  36. }
  37. //-----------------------------------------------------------------------------
  38. void CDmeUserSettings::OnAttributeChanged( CDmAttribute *pAttribute )
  39. {
  40. BaseClass::OnAttributeChanged( pAttribute );
  41. const char *pRegistryPath = FindRegistryPathForAttribute( pAttribute );
  42. if( pRegistryPath )
  43. {
  44. SetRegistryFromAttribute( pAttribute, pRegistryPath );
  45. KeyValues *pMessage = new KeyValues( "OnUserSettingsChanged", NameKey, pAttribute->GetName() );
  46. pMessage->SetPtr( AttributeKey, pAttribute );
  47. pMessage->SetString( RegistryPathKey, pRegistryPath );
  48. int nListenerCount = s_UserSettingsChangedListeners.Count();
  49. for( int i=nListenerCount - 1; i>=0 ; i-- )
  50. {
  51. s_UserSettingsChangedListeners[ i ]->OnUserSettingsChanged( pMessage );
  52. }
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. void CDmeUserSettings::SetAttributeForRegistryPathInDatabase( CDmAttribute *pAttribute, const char *pRegistryPath )
  57. {
  58. s_RegistryPathToAttribute.InsertOrReplace( pRegistryPath, pAttribute );
  59. }
  60. CDmAttribute *CDmeUserSettings::FindAttributeForRegistryPath( const char *pRegistryPath )
  61. {
  62. int nIndex = s_RegistryPathToAttribute.Find( pRegistryPath );
  63. if( s_RegistryPathToAttribute.IsValidIndex( nIndex ) )
  64. {
  65. return s_RegistryPathToAttribute.Element( nIndex );
  66. }
  67. else
  68. {
  69. return NULL;
  70. }
  71. }
  72. const char *CDmeUserSettings::FindRegistryPathForAttribute( CDmAttribute *pAttribute )
  73. {
  74. const char *pReturnValue = NULL;
  75. for ( int i = s_RegistryPathToAttribute.FirstInorder(); i != s_RegistryPathToAttribute.InvalidIndex(); i = s_RegistryPathToAttribute.NextInorder( i ) )
  76. {
  77. CDmAttribute *pCurrentAttribute = s_RegistryPathToAttribute[i];
  78. if( pCurrentAttribute == pAttribute )
  79. {
  80. pReturnValue = s_RegistryPathToAttribute.Key( i );
  81. break;
  82. }
  83. }
  84. return pReturnValue;
  85. }
  86. //-----------------------------------------------------------------------------
  87. void CDmeUserSettings::GetAttributeNameFromRegistryPath( const char *pRegistryPath, char *pAttributeName, int nAttributeNameLength )
  88. {
  89. CUtlVector<char*, CUtlMemory<char*, int> > attributeStrings;
  90. V_SplitString( pRegistryPath, "\\", attributeStrings );
  91. Q_snprintf( pAttributeName, nAttributeNameLength, "%s", attributeStrings.Tail());
  92. attributeStrings.PurgeAndDeleteElements();
  93. }
  94. CDmeUserSettings *CDmeUserSettings::GetUserSettingsForRegistryPath( const char *pRegistryPath )
  95. {
  96. CDmeUserSettings *pReturnSettings = this;
  97. CDmeUserSettings *pCurrentSettings = this;
  98. CUtlVector<char*, CUtlMemory<char*, int> > attributeStrings;
  99. V_SplitString( pRegistryPath, "\\", attributeStrings );
  100. if( attributeStrings.Count() > 1 )
  101. {
  102. for( int i = 0; i < attributeStrings.Count() - 1; i++ )
  103. {
  104. const char *currentName = attributeStrings[i];
  105. pCurrentSettings = pReturnSettings->GetValueElement< CDmeUserSettings >(currentName );
  106. if( !pCurrentSettings )
  107. {
  108. pCurrentSettings = CreateElement< CDmeUserSettings >( currentName, pReturnSettings->GetFileId() );
  109. pReturnSettings->SetValue( currentName, pCurrentSettings, true );
  110. }
  111. pReturnSettings = pCurrentSettings;
  112. }
  113. }
  114. attributeStrings.PurgeAndDeleteElements();
  115. return pReturnSettings;
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool CDmeUserSettings::RegistryPathHasValue( const char *pRegistryPath )
  119. {
  120. char pValueString[1024] = "";
  121. bool bKeyIsAvailable = GetRegistryString( pRegistryPath, pValueString, sizeof( pValueString ) );
  122. return bKeyIsAvailable;
  123. }
  124. void CDmeUserSettings::SetAttributeFromRegistry( CDmAttribute *pAttribute, const char *pRegistryPath )
  125. {
  126. char pValueString[1024];
  127. GetRegistryString( pRegistryPath, pValueString, sizeof( pValueString ) );
  128. pAttribute->SetValueFromString( pValueString );
  129. }
  130. void CDmeUserSettings::SetRegistryFromAttribute( CDmAttribute *pAttribute, const char *pRegistryPath )
  131. {
  132. char pValueString[1024];
  133. pAttribute->GetValueAsString( pValueString, sizeof( pValueString ) );
  134. SetRegistryString( pRegistryPath, pValueString );
  135. }
  136. //-----------------------------------------------------------------------------
  137. // [10/23/2009 Stefan] Replace the following methods by something in tierX when it is available
  138. void CDmeUserSettings::CreateRegistryEntryAndValueKey( const char *pRegistryPath, char *pEntryKey, int nEntryKeyLength, char *pValueKey, int nValueKeyLength )
  139. {
  140. char pFullPath[1024];
  141. V_snprintf(
  142. pFullPath,
  143. sizeof(pFullPath),
  144. "Software\\Valve\\SourceFilmMaker\\%s",
  145. pRegistryPath );
  146. int nFullPathLength = V_strlen( pFullPath );
  147. int nSplitPosition = -1;
  148. for( int i = nFullPathLength - 1; i >= 0; i-- )
  149. {
  150. if( pFullPath[i]=='\\')
  151. {
  152. nSplitPosition = i + 1;
  153. break;
  154. }
  155. }
  156. if( nSplitPosition >= 0 && nSplitPosition < nFullPathLength )
  157. {
  158. V_StrRight( pFullPath, nFullPathLength - nSplitPosition, pValueKey, nValueKeyLength );
  159. V_StrLeft( pFullPath, nSplitPosition, pEntryKey, nEntryKeyLength );
  160. }
  161. else
  162. {
  163. V_snprintf( pEntryKey, nEntryKeyLength, "%s", pFullPath );
  164. V_snprintf( pValueKey, nValueKeyLength, "" );
  165. }
  166. }
  167. bool CDmeUserSettings::SetRegistryString(const char *pRegistryPath, const char *pStringValue)
  168. {
  169. #ifndef _X360
  170. HKEY hKey;
  171. HKEY hSlot = HKEY_CURRENT_USER;
  172. char pEntryKey[1024];
  173. char pValueKey[1024];
  174. CreateRegistryEntryAndValueKey( pRegistryPath, pEntryKey, sizeof( pEntryKey), pValueKey, sizeof( pValueKey ) );
  175. if ( RegCreateKeyEx( hSlot, pEntryKey ,NULL, NULL, REG_OPTION_NON_VOLATILE, pStringValue ? KEY_WRITE : KEY_ALL_ACCESS, NULL, &hKey , NULL) != ERROR_SUCCESS )
  176. {
  177. return false;
  178. }
  179. if ( RegSetValueEx( hKey, pValueKey, NULL, REG_SZ, (unsigned char *)pStringValue, strlen(pStringValue) + 1 ) == ERROR_SUCCESS )
  180. {
  181. RegCloseKey(hKey);
  182. return true;
  183. }
  184. RegCloseKey(hKey);
  185. #endif
  186. return false;
  187. }
  188. bool CDmeUserSettings::GetRegistryString(const char *pRegistryPath, char *pStringValue, int nStringValueLen)
  189. {
  190. #ifndef _X360
  191. pStringValue[0] = 0;
  192. HKEY hKey;
  193. HKEY hSlot = HKEY_CURRENT_USER;
  194. char pEntryKey[1024];
  195. char pValueKey[1024];
  196. CreateRegistryEntryAndValueKey( pRegistryPath, pEntryKey, sizeof( pEntryKey), pValueKey, sizeof( pValueKey ) );
  197. if ( RegOpenKeyEx( hSlot, pEntryKey, NULL, KEY_READ, &hKey ) != ERROR_SUCCESS )
  198. {
  199. return false;
  200. }
  201. unsigned long len = nStringValueLen;
  202. if ( RegQueryValueEx( hKey, pValueKey, NULL, NULL, (unsigned char *)pStringValue, &len ) == ERROR_SUCCESS )
  203. {
  204. RegCloseKey(hKey);
  205. return true;
  206. }
  207. RegCloseKey(hKey);
  208. #endif
  209. return false;
  210. }