-
Notifications
You must be signed in to change notification settings - Fork 12
/
Settings.cs
194 lines (187 loc) · 5.51 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
187
188
189
190
191
192
193
194
using System;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.Threading;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
namespace DiscordOverlay
{
static class Settings
{
private static readonly string OLD_SAVE_FILE = "props.bin";
private static readonly string SAVE_FILE = "settings.json";
[Obsolete]
[Serializable]
#pragma warning disable 0649 // variable is never assigned to
private struct Data
{
public bool overlayClickable;
public bool savePositions;
public bool topMost;
public Rect overlayRect;
public Rect containerRect;
public int frameRate;
public double hostOpacity;
public Color transparencyKey;
}
#pragma warning restore 0649 // variable is never assigned to
[DataContract]
private struct JsonData
{
[DataMember] public bool overlayClickable;
[DataMember] public bool savePositions;
[DataMember] public bool topMost;
[DataMember] public bool showTaskbar;
[DataMember] public Rect overlayRect;
[DataMember] public Rect containerRect;
[DataMember] public int frameRate;
[DataMember] public double hostOpacity;
[DataMember] public double overlayOpacity;
[IgnoreDataMember]
public Color transparencyKey
{
get => ColorTranslator.FromHtml(transparencyKeyString);
set => transparencyKeyString = ColorTranslator.ToHtml(value);//Convert.ToString(value.ToArgb(), 16).Substring(2);
}
[DataMember(Name = "transparencyKey")] private string transparencyKeyString;
}
private static AutoResetEvent gate = new AutoResetEvent(false);
private static JsonData data;
public static bool overlayClickable
{
get => data.overlayClickable;
set => data.overlayClickable = value;
}
public static bool savePositions
{
get => data.savePositions;
set => data.savePositions = value;
}
public static bool topMost
{
get => data.topMost;
set => data.topMost = value;
}
public static bool showTaskbar
{
get => data.showTaskbar;
set => data.showTaskbar = value;
}
public static Rect overlayRect
{
get => data.overlayRect;
set => data.overlayRect = value;
}
public static Rect containerRect
{
get => data.containerRect;
set => data.containerRect = value;
}
public static Color transparencyKey
{
get => data.transparencyKey;
set => data.transparencyKey = value;
}
public static int frameRate
{
get => data.frameRate;
set => data.frameRate = value;
}
public static double hostOpacity
{
get => data.hostOpacity;
set => data.hostOpacity = value;
}
public static double overlayOpacity
{
get
{
if (data.overlayOpacity < 0.25 || data.overlayOpacity > 1)
data.overlayOpacity = 1;
return data.overlayOpacity;
}
set => data.overlayOpacity = value;
}
public static bool isHostTransparent => hostOpacity < 1;
public static async Task Load() => await Task.Run(() =>
{
if (File.Exists(OLD_SAVE_FILE))
{
using (FileStream stream = new FileStream(OLD_SAVE_FILE, FileMode.OpenOrCreate))
{
#pragma warning disable CS0612 // Type or member is obsolete
var oldData = (Data)new BinaryFormatter().Deserialize(stream);
#pragma warning restore CS0612 // Type or member is obsolete
// Migrate data for new property
if (oldData.frameRate == 0)
oldData.frameRate = 10;
oldData.topMost = true;
data = new JsonData
{
overlayClickable = oldData.overlayClickable,
savePositions = oldData.savePositions,
topMost = oldData.topMost,
overlayRect = oldData.overlayRect,
containerRect = oldData.containerRect,
transparencyKey = oldData.transparencyKey,
frameRate = oldData.frameRate,
hostOpacity = oldData.hostOpacity,
showTaskbar = false
};
}
File.Delete(OLD_SAVE_FILE);
Save();
return;
}
if (!File.Exists(SAVE_FILE))
{
data = new JsonData
{
overlayClickable = true,
savePositions = true,
topMost = true,
overlayRect = new Rect { Size = new Size(Constants.OverlayStartWidth, Constants.OverlayStartHeight), Point = Point.Empty },
containerRect = new Rect { Size = new Size(Constants.StartWidth, Constants.StartHeight), Point = Point.Empty },
transparencyKey = Constants.DefaultTransparencyKey,
frameRate = 10,
hostOpacity = 1,
overlayOpacity = 1,
showTaskbar = false
};
return;
}
using (FileStream stream = new FileStream(SAVE_FILE, FileMode.OpenOrCreate))
{
data = (JsonData)new DataContractJsonSerializer(typeof(JsonData)).ReadObject(stream);
}
});
public static void Save() => gate.Set();
public static bool Running { get; private set; }
public static void Close() { Running = false; gate.Set(); }
static Settings()
{
Running = true;
Task.Run(() =>
{
while (gate.WaitOne() && Running)
{
using (FileStream stream = new FileStream(SAVE_FILE, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
{
// Truncate the file before writing out the new contents
stream.SetLength(0);
new DataContractJsonSerializer(typeof(JsonData)).WriteObject(stream, data);
}
}
});
}
[Serializable]
public class Rect
{
public Point Point;
public Size Size;
public static Rect Empty => new Rect { Size = Size.Empty, Point = Point.Empty };
}
}
}