1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Collections . Specialized ;
4
+ using System . Configuration ;
5
+ using System . IO ;
6
+ using System . Linq ;
7
+ using System . Reflection ;
8
+ using HDT . Plugins . Advisor . Properties ;
9
+ using Hearthstone_Deck_Tracker ;
10
+ using Newtonsoft . Json ;
11
+
12
+ namespace HDT . Plugins . Advisor
13
+ {
14
+ public class AdvisorSettingsProvider : SettingsProvider , IApplicationSettingsProvider
15
+ {
16
+ private static readonly string SettingsPath = Path . Combine ( Config . Instance . ConfigDir , "AdvisorSettings.json" ) ;
17
+
18
+ public override string ApplicationName
19
+ {
20
+ get => Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ;
21
+ set { }
22
+ }
23
+
24
+ public override string Name => nameof ( AdvisorSettingsProvider ) ;
25
+
26
+ public SettingsPropertyValue GetPreviousVersion ( SettingsContext context , SettingsProperty property )
27
+ {
28
+ throw new NotImplementedException ( ) ;
29
+ }
30
+
31
+ public void Reset ( SettingsContext context )
32
+ {
33
+ try
34
+ {
35
+ File . Delete ( SettingsPath ) ;
36
+ }
37
+ catch ( IOException )
38
+ {
39
+ }
40
+ }
41
+
42
+ public void Upgrade ( SettingsContext context , SettingsPropertyCollection properties )
43
+ {
44
+ }
45
+
46
+ public override SettingsPropertyValueCollection GetPropertyValues ( SettingsContext context , SettingsPropertyCollection collection )
47
+ {
48
+ var settingsPropertyValueCollection = new SettingsPropertyValueCollection ( ) ;
49
+
50
+ Dictionary < string , object > values = null ;
51
+ try
52
+ {
53
+ using ( var file = File . OpenText ( SettingsPath ) )
54
+ {
55
+ var serializer = new JsonSerializer ( ) ;
56
+ values = ( Dictionary < string , object > ) serializer . Deserialize ( file , typeof ( Dictionary < string , object > ) ) ;
57
+ }
58
+ }
59
+ catch ( IOException )
60
+ {
61
+ }
62
+
63
+ foreach ( SettingsProperty settingsProperty in collection )
64
+ {
65
+ var value = new SettingsPropertyValue ( settingsProperty ) ;
66
+
67
+ if ( values != null )
68
+ {
69
+ value . SerializedValue = values [ settingsProperty . Name ] ;
70
+ }
71
+
72
+ settingsPropertyValueCollection . Add ( value ) ;
73
+ }
74
+
75
+ return settingsPropertyValueCollection ;
76
+ }
77
+
78
+ public override void SetPropertyValues ( SettingsContext context , SettingsPropertyValueCollection collection )
79
+ {
80
+ var values = collection . Cast < SettingsPropertyValue > ( ) . ToDictionary ( v => v . Name , v => v . SerializedValue ) ;
81
+ using ( var file = File . CreateText ( SettingsPath ) )
82
+ {
83
+ var serializer = new JsonSerializer ( ) ;
84
+ serializer . Serialize ( file , values ) ;
85
+ }
86
+ }
87
+
88
+ public override void Initialize ( string name , NameValueCollection config )
89
+ {
90
+ base . Initialize ( name ?? nameof ( AdvisorSettingsProvider ) , config ) ;
91
+ }
92
+
93
+ public static void Apply ( )
94
+ {
95
+ var provider = new AdvisorSettingsProvider ( ) ;
96
+
97
+ Settings . Default . Providers . Add ( provider ) ;
98
+ foreach ( SettingsProperty prop in Settings . Default . Properties )
99
+ {
100
+ prop . Provider = provider ;
101
+ }
102
+
103
+ Settings . Default . Reload ( ) ;
104
+ }
105
+ }
106
+ }
0 commit comments