/
App.cs
196 lines (157 loc) · 5.43 KB
/
App.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
195
196
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
namespace Polyriser {
public delegate void DelayedAction();
static class App {
static Engine _engine;
public static Settings Settings {get; private set;}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(!CheckSingleInstance())
return;
Settings = new Settings();
Settings.LoadFromFile();
_engine = new Engine();
new Logger(_engine);
// This is created before the engine is init'd so it can receive and
// display messages that happen while the engine is loading
var mainForm = new MainForm(_engine);
new Sounder(_engine);
_engine.RaiseEvent(EngineEvent.Init);
_engine.RaiseEvent(EngineEvent.LoadSettings);
try {
Application.Run(mainForm);
} finally {
_engine.RaiseEvent(EngineEvent.Shutdown);
}
}
static bool CheckSingleInstance() {
var list = Process.GetProcesses();
var current = Process.GetCurrentProcess();
foreach(var existing in list)
try {
if(existing.MainModule.FileName == current.MainModule.FileName
&& existing.Id != current.Id)
return false;
} catch {
// Hey, what can you do...
}
return true;
}
public static void Assert(bool condition) {
Debug.Assert(condition);
}
public static string StoragePath {get {
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
return Path.Combine(path, Strings.DataPath);
}}
public static string SettingsFilename {get {
return Path.Combine(StoragePath, Strings.SettingsFile);
}}
public static string LogFilename {get {
return Path.Combine(StoragePath, Strings.LogFile);
}}
public static string TimeToStringHHMM(TimeSpan time) {
return string.Format("{0}:{1:00.##}",
time.Hours, time.TotalMinutes - time.Hours * 60);
}
public static string TimeToStringHHMMSS(TimeSpan time) {
if(time.Hours > 0)
return string.Format("{0}:{1:D2}:{2:00.##}",
time.Hours, time.Minutes, time.TotalSeconds - time.Hours * 3600 - time.Minutes * 60);
return string.Format("{0}:{1:00.##}",
time.Minutes, time.TotalSeconds - time.Minutes * 60);
}
public static bool TryParseHHMM(string text, out TimeSpan result) {
result = default(TimeSpan);
int seconds = 0;
var colonPos = text.IndexOf(':');
if(colonPos != -1) {
int tmpint;
if(!int.TryParse(text.Substring(0, colonPos), out tmpint))
return false;
seconds += tmpint * 60 * 60;
}
double tmpdbl;
if(!double.TryParse(text.Substring(colonPos + 1), out tmpdbl))
return false;
seconds += (int)(tmpdbl * 60);
result = new TimeSpan(0, 0, seconds);
return true;
}
public static void FlashControl(Control control, string message) {
var oldText = control.Text;
var oldForeColor = control.ForeColor;
var oldFont = control.Font;
try {
control.Text = message;
control.ForeColor = Color.FromArgb(0x00e00000);
var newSize = (float)Math.Pow(oldFont.Size, 1.8) / 5.7; // please don't ask
control.Font = new Font(oldFont.FontFamily, oldFont.Size * 1.25f, FontStyle.Bold);
control.Refresh();
Thread.Sleep(750);
control.Text = oldText;
} finally {
control.Text = oldText;
control.ForeColor = oldForeColor;
control.Font = oldFont;
control.Focus();
}
}
public static void FlashTextBox(TextBox textBox, string message) {
var align = textBox.TextAlign;
try {
textBox.TextAlign = HorizontalAlignment.Center;
FlashControl(textBox, message);
} finally {
textBox.TextAlign = align;
textBox.SelectAll();
}
}
public static string GetCopyrightString() {
var attr = (AssemblyCopyrightAttribute)Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0];
return attr.Copyright;
}
public static string GetProductName() {
var attr = (AssemblyProductAttribute)Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];
return attr.Product;
}
public static string GetVersionString(bool includeBuild) {
var ver = Assembly.GetExecutingAssembly().GetName().Version;
var ret = string.Format("{0}.{1:D2}", ver.Major, ver.Minor);
if(includeBuild)
ret += string.Format(".{0:D4}", ver.Build);
if(ver.Major < 1)
ret += " beta";
return ret;
}
}
static class Strings {
public const string DebugSuffix = " *** DEBUG VERSION ***";
public static string DataPath {get {return App.GetProductName();}}
#if DEBUG
public const string SettingsFile = "settings-testing.ini";
public const string LogFile = "log-testing.txt";
#else
public const string SettingsFile = "settings.ini";
public const string LogFile = "log.txt";
#endif
public const string NapStopPreGrace = "Cancel nap";
public const string NapStopPostGrace = "I'm awake";
public const string ExitCoolingDown = "Not yet...";
public const string ExitIdle = "Exit";
public const string TrayFirstMessage = "I'll stay out of your way!";
public const string InvalidTimeFlash = "h:mm";
public const string CantOpenAudioFile = "Couldn't open file";
public const string FailedTest = "Wake up!!";
}
}