-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShared.cs
More file actions
139 lines (129 loc) · 5.64 KB
/
Shared.cs
File metadata and controls
139 lines (129 loc) · 5.64 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
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StreamStartingTimer.StreamerApps;
using System;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
namespace StreamStartingTimer {
public static class Shared {
public static CSettings CurSettings = new CSettings();
public static List<TimerEvent> TimerEvents = new();
public const string Version = "v1.0-RC3";
public const string TimeFormat = @"mm\:ss";
public const string MutexName = "uk.lum.streamstartingtimer";
private const string MMFName = "uk.lum.streamstartingtimer.seconds";
private const int MMFSize = 4;
private static MemoryMappedFile mmf;
private static MemoryMappedViewAccessor mmfAccess = null;
public static Clock frmClock = null;
private static EventWaitHandle s_event;
public static VNyanConnector VNyanConnector = new VNyanConnector();
public static MIUConnector MIUConnector = new MIUConnector();
public static JsonSerializerSettings SerialSettings = new JsonSerializerSettings();
static Shared() {
SerialSettings.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii;
}
public static int SecondsToGo {
get {
int temp = 0;
if (mmfAccess == null) {
InitialiseMMF();
}
mmfAccess.Read<int>(0, out temp);
return temp;
}
set {
if (mmfAccess == null) { InitialiseMMF(); }
mmfAccess.Write<int>(0, ref value);
}
}
private static void InitialiseMMF() {
bool Created = false;
try {
mmf = MemoryMappedFile.OpenExisting(MMFName);
} catch (FileNotFoundException) {
mmf = MemoryMappedFile.CreateOrOpen(MMFName, MMFSize);
Created = true;
}
mmfAccess = mmf.CreateViewAccessor(0, MMFSize);
if (Created) {
int value = 0;
mmfAccess.Write<int>(0, ref value);
}
}
public static List<TimerEvent> LoadEvents(string filename) {
int FileVersion = 0;
List<TimerEvent> result = new List<TimerEvent>();
dynamic JSON = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(filename));
if (!(Int32.TryParse(JSON.FileVersion.ToString(), out FileVersion))) { FileVersion = 0; }
switch (FileVersion) {
case 0:
MessageBox.Show("Unfortunately we can't read event lists from beta 1.0", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case 1:
foreach (dynamic JSONEvent in JSON.Events) {
switch (JSONEvent.EventType.ToString().ToLower()) {
case "vnyan": result.Add(new VNyanEvent(JSONEvent)); break;
case "mixitup": result.Add(new MIUEvent(JSONEvent)); break;
case "exe": result.Add(new ExeEvent(JSONEvent)); break;
}
}
break;
}
return result;
}
public static bool GetROStatus() {
if (frmClock != null) {
return frmClock.TimerRunning;
} else {
return false;
}
}
public static void SaveEvents(string filename, List<TimerEvent> TimerEvents) {
JArray EventsJson = new JArray();
foreach (TimerEvent timerEvent in TimerEvents) {
EventsJson.Add(timerEvent.JSON);
}
JObject Config = new JObject(
new JProperty("FileVersion", 1),
new JProperty("Events", EventsJson)
);
File.WriteAllText(filename, Config.ToString());
}
public class AutoClosingMessageBox {
System.Threading.Timer _timeoutTimer;
string _caption;
AutoClosingMessageBox(string text, string caption, int timeout) {
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
using (_timeoutTimer)
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout) {
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state) {
IntPtr mbWnd = FindWindow("#32770", _caption); // lpClassName is #32770 for MessageBox
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
public static bool IsSingleInstance() {
bool created;
s_event = new EventWaitHandle(false,
EventResetMode.ManualReset, MutexName, out created);
if (!created) {
s_event.Reset();
s_event.Dispose();
}
return created;
}
}
}