-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
94 lines (74 loc) · 2.01 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using BefunDebug.Helper;
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace BefunDebug
{
using ConfigDict = SerializableStringDictionary<SerializableStringDictionary<object>>;
internal static class Program
{
private const string CONFIG_FILENAME = @"befuncompile.cfg.xml";
private static ConfigDict config;
[STAThread]
private static void Main()
{
LoadConfig();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
private static void LoadConfig()
{
if (!File.Exists(CONFIG_FILENAME))
{
config = new ConfigDict();
return;
}
try
{
var serializer = new XmlSerializer(typeof(ConfigDict));
using (var stream = new FileStream(CONFIG_FILENAME, FileMode.Open, FileAccess.Read, FileShare.Read))
{
config = (ConfigDict)serializer.Deserialize(stream);
}
}
catch (Exception)
{
config = new ConfigDict();
}
}
private static SerializableStringDictionary<object> GetCallerConfig(object caller)
{
string callerName = caller.GetType().Name;
if (! config.ContainsKey(callerName)) config[callerName] = new SerializableStringDictionary<object>();
return config[callerName];
}
public static void SetConfigValue(object caller, string key, object value)
{
var dict = GetCallerConfig(caller);
if (dict.ContainsKey(key) && dict[key] == value) return;
dict[key] = value;
var serializer = new XmlSerializer(typeof (ConfigDict));
using (var stream = new StreamWriter(CONFIG_FILENAME))
{
serializer.Serialize(stream, config);
}
}
public static T GetConfigValue<T>(object caller, string key, T defValue)
{
try
{
var dict = GetCallerConfig(caller);
if (!dict.ContainsKey(key)) SetConfigValue(caller, key, defValue);
var value = dict.GetValueOrDefault(key, null);
if (value is T) return (T)value;
return defValue;
}
catch (Exception)
{
return defValue;
}
}
}
}