-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppData.cs
More file actions
82 lines (67 loc) · 2.87 KB
/
Copy pathAppData.cs
File metadata and controls
82 lines (67 loc) · 2.87 KB
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
using System.Collections;
using System.Text.Json;
namespace MAES.Core;
public abstract class AppData
{
JsonSerializerOptions? options;
string filePath { get; set; } = "";
public static T Load<T>(string fileName, JsonSerializerOptions? opts = null, T? newValue = null) where T : AppData, new()
{
T result;
try
{
result = JsonSerializer.Deserialize<T>(File.ReadAllText(fileName), opts) ?? (newValue is T val ? val : new T());
}
catch (Exception)
{
result = newValue is T val ? val : new T();
File.WriteAllText(fileName, JsonSerializer.Serialize(result, opts));
}
result.filePath = fileName;
result.options = opts;
return result;
}
public void SaveChanges() => File.WriteAllText(filePath, JsonSerializer.Serialize(this, GetType(), options));
}
public class AppDataList<T> : AppData, IList<T>
{
public T this[int index] { get => values[index]; set => values[index] = value; }
public int Count => values.Count;
public bool IsReadOnly => false;
readonly List<T> values = [];
public void Add(T item) => values.Add(item);
public void Clear() => values.Clear();
public bool Contains(T item) => values.Contains(item);
public void CopyTo(T[] array, int arrayIndex) => values.CopyTo(array, arrayIndex);
public IEnumerator<T> GetEnumerator() => values.GetEnumerator();
public int IndexOf(T item) => values.IndexOf(item);
public void Insert(int index, T item) => values.Insert(index, item);
public bool Remove(T item) => values.Remove(item);
public void RemoveAt(int index) => values.RemoveAt(index);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class AppDataDictionary<T, U> : AppData, IDictionary<T, U> where T : notnull
{
readonly Dictionary<T, U> values = [];
public U this[T key] { get => values[key]; set => values[key] = value; }
public ICollection<T> Keys => values.Keys;
public ICollection<U> Values => values.Values;
public int Count => values.Count;
public bool IsReadOnly => false;
public void Add(T key, U value) => values.Add(key, value);
public void Add(KeyValuePair<T, U> item) => values.Add(item.Key, item.Value);
public void Clear() => values.Clear();
public bool Contains(KeyValuePair<T, U> item) => values.Contains(item);
public bool ContainsKey(T key) => values.ContainsKey(key);
public void CopyTo(KeyValuePair<T, U>[] array, int arrayIndex) => values.ToArray().CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<T, U>> GetEnumerator() => values.GetEnumerator();
public bool Remove(T key) => values.Remove(key);
public bool Remove(KeyValuePair<T, U> item) => values.Remove(item.Key);
public bool TryGetValue(T key, out U value)
{
bool result = values.TryGetValue(key, out var val);
value = val;
return result;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}