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.

114 lines
3.8 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. #ifndef DMEUSERSETTINGS_H
  3. #define DMEUSERSETTINGS_H
  4. #ifdef _WIN32
  5. #pragma once
  6. #endif
  7. #include "datamodel/dmelement.h"
  8. #include "idmeusersettingschangedlistener.h"
  9. //-----------------------------------------------------------------------------
  10. class CDmeUserSettings : public CDmElement
  11. {
  12. DEFINE_ELEMENT( CDmeUserSettings, CDmElement );
  13. public:
  14. static CDmeUserSettings *SharedUserSettings();
  15. template< class T> static void InitSettingsValue( const char *pRegistryPath, const T& value);
  16. template< class T> static void SetSettingsValue( const char *pRegistryPath, const T& value);
  17. template< class T > static const T& GetSettingsValue( const char *pRegistryPath, const T& defaultValue );
  18. virtual CDmeUserSettings *GetUserSettingsForRegistryPath( const char *pRegistryPath );
  19. virtual void OnAttributeChanged( CDmAttribute *pAttribute );
  20. virtual void AddUserSettingsChangedListener( IDmeUserSettingsChangedListener *pListener );
  21. protected:
  22. private:
  23. CDmAttribute *FindAttributeForRegistryPath( const char *pRegistryPath );
  24. const char *FindRegistryPathForAttribute( CDmAttribute *pAttribute );
  25. bool RegistryPathHasValue( const char *pRegistryPath );
  26. void SetAttributeFromRegistry( CDmAttribute *pAttribute, const char *pRegistryPath );
  27. void SetRegistryFromAttribute( CDmAttribute *pAttribute, const char *pRegistryPath );
  28. void GetAttributeNameFromRegistryPath( const char *pRegistryPath, char *pAttributeName, int nAttributeNameLength );
  29. void SetAttributeForRegistryPathInDatabase( CDmAttribute *pAttribute, const char *pRegistryPath );
  30. void CreateRegistryEntryAndValueKey( const char *pRegistryPath, char *pEntryKey, int nEntryKeyLength, char *pValueKey, int nValueKeyLength );
  31. bool GetRegistryString(const char *pRegistryPath, char *pStringValue, int nStringValueLen);
  32. bool SetRegistryString(const char *pRegistryPath, const char *pStringValue);
  33. static CUtlVector< IDmeUserSettingsChangedListener * > s_UserSettingsChangedListeners;
  34. };
  35. //-----------------------------------------------------------------------------
  36. template< class T >
  37. void CDmeUserSettings::InitSettingsValue( const char *pRegistryPath, const T& value )
  38. {
  39. CDmAttribute *pAttribute = SharedUserSettings()->FindAttributeForRegistryPath( pRegistryPath );
  40. if( pAttribute )
  41. {
  42. SharedUserSettings()->SetAttributeFromRegistry( pAttribute, pRegistryPath );
  43. }
  44. else
  45. {
  46. CDmElement *pUserSettingGroup = SharedUserSettings()->GetUserSettingsForRegistryPath( pRegistryPath );
  47. char pAttributeName[255];
  48. SharedUserSettings()->GetAttributeNameFromRegistryPath( pRegistryPath, pAttributeName, 255);
  49. if( pUserSettingGroup )
  50. {
  51. pAttribute = pUserSettingGroup->InitValue( pAttributeName, value );
  52. if( pAttribute )
  53. {
  54. pAttribute->AddFlag( FATTRIB_HAS_CALLBACK );
  55. SharedUserSettings()->SetAttributeForRegistryPathInDatabase( pAttribute, pRegistryPath );
  56. if( SharedUserSettings()->RegistryPathHasValue( pRegistryPath ) )
  57. {
  58. SharedUserSettings()->SetAttributeFromRegistry( pAttribute, pRegistryPath );
  59. }
  60. else
  61. {
  62. SharedUserSettings()->SetRegistryFromAttribute( pAttribute, pRegistryPath );
  63. }
  64. }
  65. }
  66. }
  67. }
  68. template< class T >
  69. void CDmeUserSettings::SetSettingsValue( const char *pRegistryPath, const T& value )
  70. {
  71. CDmAttribute *pAttribute = SharedUserSettings()->FindAttributeForRegistryPath( pRegistryPath );
  72. if( pAttribute )
  73. {
  74. pAttribute->SetValue( value );
  75. }
  76. else
  77. {
  78. InitSettingsValue( pRegistryPath, value );
  79. }
  80. }
  81. template< class T >
  82. const T& CDmeUserSettings::GetSettingsValue( const char *pRegistryPath, const T& defaultValue )
  83. {
  84. CDmAttribute *pAttribute = SharedUserSettings()->FindAttributeForRegistryPath( pRegistryPath );
  85. if( pAttribute )
  86. {
  87. return pAttribute->GetValue< T >( defaultValue );
  88. }
  89. else
  90. {
  91. return defaultValue;
  92. }
  93. }
  94. #endif // DMEUSERSETTINGS_H