Skip to content

Commit 428d1fe

Browse files
committed
Reduce reliance on reflection for config persisting. This fixes the last violations of IDE0051 and IDE0052.
1 parent e726472 commit 428d1fe

File tree

20 files changed

+362
-157
lines changed

20 files changed

+362
-157
lines changed

src/BizHawk.Client.Common/config/ConfigPersistAttribute.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel;
3+
using System.Globalization;
4+
5+
namespace BizHawk.Client.Common
6+
{
7+
public interface IConfigPersist
8+
{
9+
public class Provider
10+
{
11+
private Dictionary<string, object> _data;
12+
13+
public Provider(Dictionary<string, object> data)
14+
{
15+
_data = data;
16+
}
17+
18+
public bool Get<T>(string name, ref T value)
19+
{
20+
if (_data.TryGetValue(name, out var val))
21+
{
22+
if (val is string str && typeof(T) != typeof(string))
23+
{
24+
// if a type has a TypeConverter, and that converter can convert to string,
25+
// that will be used in place of object markup by JSON.NET
26+
27+
// but that doesn't work with $type metadata, and JSON.NET fails to fall
28+
// back on regular object serialization when needed. so try to undo a TypeConverter
29+
// operation here
30+
var converter = TypeDescriptor.GetConverter(typeof(T));
31+
val = converter.ConvertFromString(null, CultureInfo.InvariantCulture, str);
32+
}
33+
else if (val is not bool && typeof(T).IsPrimitive)
34+
{
35+
// numeric constants are similarly hosed
36+
val = Convert.ChangeType(val, typeof(T), CultureInfo.InvariantCulture);
37+
}
38+
39+
value = (T)val;
40+
return true;
41+
}
42+
return false;
43+
}
44+
}
45+
46+
/// <summary>
47+
/// Load the provided configuration.
48+
/// </summary>
49+
void LoadConfig(Provider provider);
50+
51+
/// <summary>
52+
/// Return a dictionary representing the current configuration.
53+
/// </summary>
54+
Dictionary<string, object> SaveConfig();
55+
}
56+
}

src/BizHawk.Client.EmuHawk/CoreFeatureAnalysis.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace BizHawk.Client.EmuHawk
1313
{
14-
public partial class CoreFeatureAnalysis : ToolFormBase, IToolFormAutoConfig
14+
public partial class CoreFeatureAnalysis : ToolFormBase, IToolFormAutoConfig, IConfigPersist
1515
{
1616
private class CoreInfo
1717
{
@@ -93,8 +93,20 @@ public FunctionInfo(MethodInfo m, object service)
9393
public static Icon ToolIcon
9494
=> Properties.Resources.Logo;
9595

96-
[ConfigPersist]
97-
private Dictionary<string, CoreInfo> KnownCores { get; set; }
96+
private Dictionary<string, CoreInfo> KnownCores;
97+
98+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
99+
{
100+
provider.Get(nameof(KnownCores), ref KnownCores);
101+
}
102+
103+
Dictionary<string, object> IConfigPersist.SaveConfig()
104+
{
105+
return new()
106+
{
107+
[nameof(KnownCores)] = KnownCores,
108+
};
109+
}
98110

99111
// ReSharper disable once UnusedAutoPropertyAccessor.Local
100112
[RequiredService]

src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace BizHawk.Client.EmuHawk
1515
{
16-
public sealed partial class BasicBot : ToolFormBase, IToolFormAutoConfig
16+
public sealed partial class BasicBot : ToolFormBase, IToolFormAutoConfig, IConfigPersist
1717
{
1818
private static readonly FilesystemFilterSet BotFilesFSFilterSet = new(new FilesystemFilter("Bot files", new[] { "bot" }));
1919

@@ -70,8 +70,19 @@ private string CurrentFileName
7070
[RequiredService]
7171
private IMemoryDomains MemoryDomains { get; set; }
7272

73-
[ConfigPersist]
74-
public BasicBotSettings Settings { get; set; }
73+
private BasicBotSettings Settings;
74+
75+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
76+
{
77+
provider.Get(nameof(Settings), ref Settings);
78+
}
79+
Dictionary<string, object> IConfigPersist.SaveConfig()
80+
{
81+
return new()
82+
{
83+
[nameof(Settings)] = Settings,
84+
};
85+
}
7586

7687
public class BasicBotSettings
7788
{

src/BizHawk.Client.EmuHawk/tools/CDL.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
using System.Collections.Generic;
12
using System.Drawing;
23
using System.IO;
34
using System.Windows.Forms;
4-
5-
using BizHawk.Emulation.Common;
6-
75
using BizHawk.Client.Common;
86
using BizHawk.Client.EmuHawk.Properties;
97
using BizHawk.Client.EmuHawk.ToolExtensions;
8+
using BizHawk.Emulation.Common;
109

1110
// TODO - select which memorydomains go out to the CDL file. will this cause a problem when re-importing it?
1211
// perhaps missing domains shouldn't fail a check
@@ -17,7 +16,7 @@
1716
// TODO - context menu should have copy option too
1817
namespace BizHawk.Client.EmuHawk
1918
{
20-
public partial class CDL : ToolFormBase, IToolFormAutoConfig
19+
public partial class CDL : ToolFormBase, IToolFormAutoConfig, IConfigPersist
2120
{
2221
private static readonly FilesystemFilterSet CDLFilesFSFilterSet = new(new FilesystemFilter("Code Data Logger Files", new[] { "cdl" }));
2322

@@ -26,21 +25,35 @@ public static Icon ToolIcon
2625

2726
private RecentFiles _recentFld = new RecentFiles();
2827

29-
[ConfigPersist]
3028
private RecentFiles _recent
3129
{
3230
get => _recentFld;
3331
set => _recentFld = value;
3432
}
3533

36-
[ConfigPersist]
37-
private bool CDLAutoSave { get; set; } = true;
34+
private bool CDLAutoSave = true;
35+
36+
private bool CDLAutoStart = true;
3837

39-
[ConfigPersist]
40-
private bool CDLAutoStart { get; set; } = true;
38+
private bool CDLAutoResume = true;
4139

42-
[ConfigPersist]
43-
private bool CDLAutoResume { get; set; } = true;
40+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
41+
{
42+
provider.Get(nameof(_recentFld), ref _recentFld);
43+
provider.Get(nameof(CDLAutoSave), ref CDLAutoSave);
44+
provider.Get(nameof(CDLAutoStart), ref CDLAutoStart);
45+
provider.Get(nameof(CDLAutoResume), ref CDLAutoResume);
46+
}
47+
Dictionary<string, object> IConfigPersist.SaveConfig()
48+
{
49+
return new()
50+
{
51+
[nameof(_recent)] = _recent,
52+
[nameof(CDLAutoSave)] = CDLAutoSave,
53+
[nameof(CDLAutoStart)] = CDLAutoStart,
54+
[nameof(CDLAutoResume)] = CDLAutoResume,
55+
};
56+
}
4457

4558
private void SetCurrentFilename(string fname)
4659
{

src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
using System.IO;
55
using System.Linq;
66
using System.Windows.Forms;
7-
8-
using BizHawk.Emulation.Common;
97
using BizHawk.Client.Common;
108
using BizHawk.Client.EmuHawk.Properties;
119
using BizHawk.Client.EmuHawk.ToolExtensions;
1210
using BizHawk.Common.CollectionExtensions;
11+
using BizHawk.Emulation.Common;
1312

1413
namespace BizHawk.Client.EmuHawk
1514
{
16-
public partial class Cheats : ToolFormBase, IToolFormAutoConfig, IRestoreDefaults
15+
public partial class Cheats : ToolFormBase, IToolFormAutoConfig, IRestoreDefaults, IConfigPersist
1716
{
1817
private const string NameColumn = "NamesColumn";
1918
private const string AddressColumn = "AddressColumn";
@@ -78,8 +77,19 @@ public Cheats()
7877
[RequiredService]
7978
private IMemoryDomains Core { get; set; }
8079

81-
[ConfigPersist]
82-
public CheatsSettings Settings { get; set; }
80+
public CheatsSettings Settings;
81+
82+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
83+
{
84+
provider.Get(nameof(Settings), ref Settings);
85+
}
86+
Dictionary<string, object> IConfigPersist.SaveConfig()
87+
{
88+
return new()
89+
{
90+
[nameof(Settings)] = Settings,
91+
};
92+
}
8393

8494
public override void Restart()
8595
{

src/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Drawing;
23
using System.Drawing.Imaging;
34
using System.Text;
@@ -10,7 +11,7 @@
1011

1112
namespace BizHawk.Client.EmuHawk
1213
{
13-
public partial class GbGpuView : ToolFormBase, IToolFormAutoConfig
14+
public partial class GbGpuView : ToolFormBase, IToolFormAutoConfig, IConfigPersist
1415
{
1516
public static Icon ToolIcon
1617
=> Properties.Resources.GambatteIcon;
@@ -58,7 +59,6 @@ private IntPtr ComputeTilesPalFromMemory(IGPUMemoryAreas m)
5859

5960
private Color _spriteback;
6061

61-
[ConfigPersist]
6262
public Color Spriteback
6363
{
6464
get => _spriteback;
@@ -70,6 +70,19 @@ public Color Spriteback
7070
}
7171
}
7272

73+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
74+
{
75+
Color outValue = default;
76+
if (provider.Get(nameof(Spriteback), ref outValue)) Spriteback = outValue;
77+
}
78+
Dictionary<string, object> IConfigPersist.SaveConfig()
79+
{
80+
return new()
81+
{
82+
[nameof(Spriteback)] = Spriteback,
83+
};
84+
}
85+
7386
protected override string WindowTitleStatic => "GPU Viewer";
7487

7588
public GbGpuView()

src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66
using System.Linq;
77
using System.Text;
88
using System.Windows.Forms;
9-
10-
using BizHawk.Common;
11-
using BizHawk.Common.NumberExtensions;
12-
using BizHawk.Common.StringExtensions;
13-
using BizHawk.Emulation.Common;
149
using BizHawk.Client.Common;
1510
using BizHawk.Client.EmuHawk.Properties;
1611
using BizHawk.Client.EmuHawk.ToolExtensions;
12+
using BizHawk.Common;
1713
using BizHawk.Common.CollectionExtensions;
14+
using BizHawk.Common.NumberExtensions;
15+
using BizHawk.Common.StringExtensions;
16+
using BizHawk.Emulation.Common;
1817
using BizHawk.WinForms.Controls;
1918

2019
namespace BizHawk.Client.EmuHawk
2120
{
2221
// int to long TODO: 32 bit domains have more digits than the hex editor can account for and the address covers up the 0 column
23-
public partial class HexEditor : ToolFormBase, IToolFormAutoConfig
22+
public partial class HexEditor : ToolFormBase, IToolFormAutoConfig, IConfigPersist
2423
{
2524
private sealed class N64MatrixDisplayDialog : Form
2625
{
@@ -121,17 +120,13 @@ private bool AreAnyHighlighted
121120
private HexFind _hexFind;
122121
private string _lastRom = "";
123122

124-
[ConfigPersist]
125-
private string LastDomain { get; set; }
123+
private string LastDomain;
126124

127-
[ConfigPersist]
128-
private bool BigEndian { get; set; }
125+
private bool BigEndian;
129126

130-
[ConfigPersist]
131-
private int DataSize { get; set; }
127+
private int DataSize;
132128

133-
[ConfigPersist]
134-
private RecentFiles RecentTables { get; set; }
129+
private RecentFiles RecentTables;
135130

136131
internal class ColorConfig
137132
{
@@ -143,8 +138,27 @@ internal class ColorConfig
143138
public Color HighlightFreeze { get; set; } = Color.Violet;
144139
}
145140

146-
[ConfigPersist]
147-
internal ColorConfig Colors { get; set; } = new ColorConfig();
141+
internal ColorConfig Colors = new ColorConfig();
142+
143+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
144+
{
145+
provider.Get(nameof(LastDomain), ref LastDomain);
146+
provider.Get(nameof(BigEndian), ref BigEndian);
147+
provider.Get(nameof(DataSize), ref DataSize);
148+
provider.Get(nameof(RecentTables), ref RecentTables);
149+
provider.Get(nameof(Colors), ref Colors);
150+
}
151+
Dictionary<string, object> IConfigPersist.SaveConfig()
152+
{
153+
return new()
154+
{
155+
[nameof(LastDomain)] = LastDomain,
156+
[nameof(BigEndian)] = BigEndian,
157+
[nameof(DataSize)] = DataSize,
158+
[nameof(RecentTables)] = RecentTables,
159+
[nameof(Colors)] = Colors,
160+
};
161+
}
148162

149163
private WatchSize WatchSize => (WatchSize)DataSize;
150164

src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace BizHawk.Client.EmuHawk
2020
{
21-
public partial class LuaConsole : ToolFormBase, IToolFormAutoConfig, IRestoreDefaults
21+
public partial class LuaConsole : ToolFormBase, IToolFormAutoConfig, IRestoreDefaults, IConfigPersist
2222
{
2323
private const string IconColumnName = "Icon";
2424
private const string ScriptColumnName = "Script";
@@ -74,8 +74,19 @@ public LuaConsoleSettings()
7474
public bool WarnedOnceOnOverwrite { get; set; }
7575
}
7676

77-
[ConfigPersist]
78-
public LuaConsoleSettings Settings { get; set; }
77+
public LuaConsoleSettings Settings;
78+
79+
void IConfigPersist.LoadConfig(IConfigPersist.Provider provider)
80+
{
81+
provider.Get(nameof(Settings), ref Settings);
82+
}
83+
Dictionary<string, object> IConfigPersist.SaveConfig()
84+
{
85+
return new()
86+
{
87+
[nameof(Settings)] = Settings,
88+
};
89+
}
7990

8091
protected override string WindowTitleStatic => "Lua Console";
8192

0 commit comments

Comments
 (0)