-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cs
executable file
·186 lines (175 loc) · 7.58 KB
/
Settings.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Micro.WinForms;
namespace FRESH {
public static class Settings {
public static readonly ConfigTable configFeeds = new ConfigTable(@"feeds.cfg");
public static readonly List<Feed> feeds = new List<Feed>();
public static readonly Options options = new Options(@"options.cfg");
static void Load<T>(ConfigTable table, List<T> list) where T : IExposable, new() {
table.Load();
list.Clear();
list.AddRange(table.data.Select(a => {
var s = new T();
for (int i = 0; i < s.RealValuesCount && i < a.Length; i++)
s.ParseAndSet(a[i], i, false);
return s;
}));
}
static void Save<T>(ConfigTable table, List<T> list, IEnumerable<T> newValues = null) where T : IExposable, new() {
var raw = table.data;
if (newValues != null) {
list.Clear();
list.AddRange(newValues);
}
raw.Clear();
foreach (var entry in list)
raw.Add(entry.TextValues(false).Select(o => o?.ToString() ?? "").ToArray());
table.Save();
}
public static void LoadFeeds() {
Load(configFeeds, feeds);
feeds.ForEach(f => f.LoadCache());
}
public static void SaveFeeds(IEnumerable<Feed> newValues = null) {
Save(configFeeds, feeds, newValues);
Directory.CreateDirectory(Program.CacheDir);
feeds.ForEach(f => f.SaveCache());
}
}
public class ConfigTable : IEnumerable<string[]> {
public const char BACKSLASH = '\\';
//public static readonly Regex rgxLine = new Regex(@"\s+");
public readonly string path;
public readonly List<string[]> data;
public ConfigTable(string name) {
path = Path.Combine(Program.AppDir, name);
data = new List<string[]>();
}
public void Load() {
var f = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read);
var r = new StreamReader(f);
data.Clear();
string l;
while ((l = r.ReadLine()) != null)
data.Add(l.Split('\t').Select(p => Unescape(p)).ToArray());
//data.Add(rgxLine.Split(l).Select(p => Unescape(p)).ToArray());
r.Close();
f.Dispose();
}
public void Save() {
var f = File.Open(path, FileMode.Create, FileAccess.Write);
var w = new StreamWriter(f);
foreach (var l in data)
w.WriteLine(string.Join("\t", l.Select(p => Escape(p)).ToArray()));
w.Flush();
w.Close();
f.Dispose();
}
public static IEnumerable<char> Escape(IEnumerable<char> input) {
foreach (var c in input) {
switch (c) {
case BACKSLASH: yield return BACKSLASH; yield return BACKSLASH; break;
case '/': yield return BACKSLASH; yield return c; break;
case ' ': yield return BACKSLASH; yield return 's'; break;
case '|': yield return BACKSLASH; yield return 'p'; break;
case '\a': yield return BACKSLASH; yield return 'a'; break;
case '\b': yield return BACKSLASH; yield return 'b'; break;
case '\f': yield return BACKSLASH; yield return 'f'; break;
case '\n': yield return BACKSLASH; yield return 'n'; break;
case '\r': yield return BACKSLASH; yield return 'r'; break;
case '\t': yield return BACKSLASH; yield return 't'; break;
case '\v': yield return BACKSLASH; yield return 'v'; break;
default: yield return c; break;
}
}
}
public static IEnumerable<char> Unescape(IEnumerable<char> input) {
var e = input.GetEnumerator();
while (e.MoveNext()) {
char c1 = e.Current;
if (c1 == BACKSLASH) {
if (!e.MoveNext())
throw new FormatException();
char c2 = e.Current;
switch (c2) {
case BACKSLASH: yield return BACKSLASH; break;
case '/': yield return c2; break;
case 's': yield return ' '; break;
case 'p': yield return '|'; break;
case 'a': yield return '\a'; break;
case 'b': yield return '\b'; break;
case 'f': yield return '\f'; break;
case 'n': yield return '\n'; break;
case 'r': yield return '\r'; break;
case 't': yield return '\t'; break;
case 'v': yield return '\v'; break;
default: throw new FormatException();
}
continue;
}
yield return c1;
}
}
public static string Escape(string txt)
=> new string(Escape((IEnumerable<char>)txt).ToArray());
public static string Unescape(string txt)
=> new string(Unescape((IEnumerable<char>)txt).ToArray());
public IEnumerator<string[]> GetEnumerator()
=> data.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> data.GetEnumerator();
}
public class Options {
const string SECTION = "General";
public readonly ValueWrapper
defaultSoundEffectPath,
maxElementsPerFeed,
disableNotification,
disableSound,
lastFeedCheck;
public readonly INIFile file;
readonly ValueWrapper[] bindings;
public Options(string name) {
file = new INIFile(Path.Combine(Program.AppDir, name));
bindings = new[] {
defaultSoundEffectPath = new ValueWrapper(TypeCode.String, nameof(defaultSoundEffectPath), ""),
maxElementsPerFeed = new ValueWrapper(TypeCode.Int32, nameof(maxElementsPerFeed), 0),
disableNotification = new ValueWrapper(TypeCode.Boolean, nameof(disableNotification), false),
disableSound = new ValueWrapper(TypeCode.Boolean, nameof(disableSound), false),
lastFeedCheck = new ValueWrapper(TypeCode.DateTime, nameof(lastFeedCheck), Program.FastNow),
};
}
public void Load() {
foreach (var b in bindings)
b.value = file.Read(SECTION, b.name, TypeCode.String);
}
public void Save() {
foreach (var b in bindings)
file.Write(SECTION, b.name, b.value);
}
}
public class ValueWrapper {
public readonly TypeCode type;
public readonly string name;
public readonly object @default;
public object value {
get => _v == null ? @default : _v;
set => _v = value == null ? @default : Convert.ChangeType(value, type);
}
object _v;
public ValueWrapper(TypeCode type, string name, object @default) {
this.type = type;
this.name = name;
this.@default = _v = @default;
}
public T cast<T>()
=> (T)value;
}
}