-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
372 lines (340 loc) · 11.4 KB
/
MainWindow.xaml.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NAudio;
using NAudio.Midi;
using NAudio.Vorbis;
using NAudio.Lame;
using NAudio.Wave;
using NGraphics;
using NLayer;
using NLayer.NAudioSupport;
using Svg;
using SVGImage;
using Stimulsoft.Svg;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows.Media.Effects;
namespace GameLauncher1
{
enum LauncherStatus
{
ready,
failedDown,
failedIn,
failedUp,
downloadingGame,
downloadingUpdate
}
/// <summary>
/// Credits:
/// Poki - Background
/// NicoDevo - Some Music
/// Kasper_100 - Stuff from Physics gaem
/// C# Design Pro - Some Help with Launcher Stuff and Design
/// icons8 - Close/Minimize Button
///
///
/// Interaction logic for MainWindow.xaml
/// </summary>
/// <summary>
/// Stream for looping playback
/// </summary>
public class LoopStream : WaveStream
{
WaveStream sourceStream;
/// <summary>
/// Creates a new Loop stream
/// </summary>
/// <param name="sourceStream">The stream to read from. Note: the Read method of this stream should return 0 when it reaches the end
/// or else we will not loop to the start again.</param>
public LoopStream(WaveStream sourceStream)
{
this.sourceStream = sourceStream;
this.EnableLooping = true;
}
/// <summary>
/// Use this to turn looping on or off
/// </summary>
public bool EnableLooping { get; set; }
/// <summary>
/// Return source stream's wave format
/// </summary>
public override WaveFormat WaveFormat
{
get { return sourceStream.WaveFormat; }
}
/// <summary>
/// LoopStream simply returns
/// </summary>
public override long Length
{
get { return sourceStream.Length; }
}
/// <summary>
/// LoopStream simply passes on positioning to source stream
/// </summary>
public override long Position
{
get { return sourceStream.Position; }
set { sourceStream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
int totalBytesRead = 0;
while (totalBytesRead < count)
{
int bytesRead = sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead);
if (bytesRead == 0)
{
if (sourceStream.Position == 0 || !EnableLooping)
{
// something wrong with the source stream
break;
}
// loop
sourceStream.Position = 0;
}
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
}
public partial class MainWindow : Window
{
private string rootPath;
private string versionFile;
private string gameZip;
private string gameExe;
private LauncherStatus _status;
internal LauncherStatus Status
{
get => _status;
set
{
_status = value;
switch (_status)
{
case LauncherStatus.ready:
PlayButton.Content = "Play";
break;
case LauncherStatus.failedDown:
PlayButton.Content = "Download Failed - Retry";
break;
case LauncherStatus.failedIn:
PlayButton.Content = "Install Failed - Retry";
break;
case LauncherStatus.failedUp:
PlayButton.Content = "Update Failed - Retry";
break;
case LauncherStatus.downloadingGame:
PlayButton.Content = "Downloading Game";
break;
case LauncherStatus.downloadingUpdate:
PlayButton.Content = "Downloading Update";
break;
default:
break;
}
}
}
private NAudio.Wave.WaveFileReader wave = null;
private WaveOut waveOut;
public void PlayBGM()
{
wave = new NAudio.Wave.WaveFileReader("./Assets/Audio/wav/music/Quantum - NicoDevo (Physics geam by Kasper_100).wav");
LoopStream loop = new LoopStream(wave);
waveOut = new WaveOut();
waveOut.Init(loop);
waveOut.Play();
}
public MainWindow()
{
InitializeComponent();
PlayBGM();
rootPath = Directory.GetCurrentDirectory();
versionFile = System.IO.Path.Combine(rootPath, "Version.txt");
gameZip = System.IO.Path.Combine(rootPath, "Build.zip");
gameExe = System.IO.Path.Combine(rootPath, "Build", "Game Name.exe");
}
private void Window_ContentRendered(object sender, EventArgs e)
{
CheckForUpdates();
}
public double PlayButtonAngleX { get; set; } = -10;
public double TargetPlayButtonAngleX { get; set; } = -10;
void Update()
{
PlayButtonAngleX += (TargetPlayButtonAngleX - PlayButtonAngleX) / 10;
}
private void CheckForUpdates()
{
if (File.Exists(versionFile))
{
Version localVersion = new Version(File.ReadAllText(versionFile));
VersionText.Text = localVersion.ToString();
try
{
WebClient webClient = new WebClient();
Version onlineVersion = new Version(webClient.DownloadString("Version File Link"));
if (onlineVersion.IsDifferentThan(localVersion))
{
InstallGameFiles(true, onlineVersion);
}
else
{
Status = LauncherStatus.ready;
}
}
catch (Exception ex)
{
Status = LauncherStatus.failedUp;
System.Windows.MessageBox.Show($"Error checking for game updates: {ex}");
}
}
else
{
InstallGameFiles(false, Version.zero);
}
}
private void InstallGameFiles(bool _isUpdate, Version _onlineVersion)
{
try
{
WebClient webClient = new WebClient();
if (_isUpdate)
{
Status = LauncherStatus.downloadingUpdate;
}
else
{
Status = LauncherStatus.downloadingGame;
_onlineVersion = new Version(webClient.DownloadString("Version File Link"));
}
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadGameCompletedCallback);
webClient.DownloadFileAsync(new Uri("Game Zip Link"), gameZip, _onlineVersion);
}
catch (Exception ex)
{
Status = LauncherStatus.failedIn;
System.Windows.MessageBox.Show($"Error installing game files: {ex}");
}
}
private void PlayButton_Click(object sender, RoutedEventArgs e)
{
TargetPlayButtonAngleX = 10;
if (File.Exists(gameExe) && Status == LauncherStatus.ready)
{
ProcessStartInfo startInfo = new ProcessStartInfo(gameExe);
startInfo.WorkingDirectory = System.IO.Path.Combine(rootPath, "Build");
Process.Start(startInfo);
Close();
}
else if (Status == LauncherStatus.failedDown || Status == LauncherStatus.failedUp || Status == LauncherStatus.failedIn)
{
CheckForUpdates();
}
}
private void DownloadGameCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
try
{
string onlineVersion = ((Version)e.UserState).ToString();
ZipFile.ExtractToDirectory(gameZip, rootPath);
File.Delete(gameZip);
File.WriteAllText(versionFile, onlineVersion);
VersionText.Text = onlineVersion;
Status = LauncherStatus.ready;
}
catch (Exception ex)
{
Status = LauncherStatus.failedDown;
System.Windows.MessageBox.Show($"Error finishing download: {ex}");
}
}
private void Exit(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
private void Minimize(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
struct Version
{
internal static Version zero = new Version(0, 0, 0);
private short major;
private short minor;
private short subMinor;
internal Version(short _major, short _minor, short _subMinor)
{
major = _major;
minor = _minor;
subMinor = _subMinor;
}
internal Version(string _version)
{
string[] _versionStrings = _version.Split('.');
if (_versionStrings.Length != 3)
{
major = 0;
minor = 0;
subMinor = 0;
return;
}
major = short.Parse(_versionStrings[0]);
minor = short.Parse(_versionStrings[1]);
subMinor = short.Parse(_versionStrings[2]);
}
internal bool IsDifferentThan(Version _otherVersion)
{
if (major != _otherVersion.major)
{
return true;
}
else
{
if (minor != _otherVersion.minor)
{
return true;
}
else
{
if (subMinor != _otherVersion.subMinor)
{
return true;
}
}
}
return false;
}
public override string ToString()
{
return $"{major}.{minor}.{subMinor}";
}
}
}
}