Skip to content

Commit e9a342d

Browse files
committed
Add custom settings provider that saves settings to fixed location to avoid them getting wiped when Hearthstone Deck Tracker is updated
1 parent 56e7176 commit e9a342d

File tree

4 files changed

+110
-12
lines changed

4 files changed

+110
-12
lines changed

Advisor/Advisor.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Reference Include="PresentationCore" />
7171
<Reference Include="PresentationFramework" />
7272
<Reference Include="System" />
73+
<Reference Include="System.Configuration" />
7374
<Reference Include="System.Core" />
7475
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
7576
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
@@ -107,10 +108,10 @@
107108
<DesignTimeSharedInput>True</DesignTimeSharedInput>
108109
<DependentUpon>Settings.settings</DependentUpon>
109110
</Compile>
111+
<Compile Include="AdvisorSettingsProvider.cs" />
110112
<Compile Include="Services\GitHub.cs" />
111113
<Compile Include="Services\MetaStats\SnapshotImporter.cs" />
112114
<Compile Include="Services\TrackerRepository.cs" />
113-
<Compile Include="Properties\Settings.cs" />
114115
<Compile Include="ViewModel\MainViewModel.cs" />
115116
<Compile Include="ViewModel\ViewModelLocator.cs" />
116117
</ItemGroup>

Advisor/AdvisorPlugin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public async void OnLoad()
4848
// Small delay to guarantee all game variables are set correctly by now (especially CoreAPI.Game.IsInMenu)
4949
await Task.Delay(2000);
5050

51+
AdvisorSettingsProvider.Apply();
52+
5153
_advisorOverlay = new AdvisorOverlay();
5254
Core.OverlayCanvas.Children.Add(_advisorOverlay);
5355
_advisor = new Advisor(_advisorOverlay);

Advisor/AdvisorSettingsProvider.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

Advisor/Properties/Settings.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)