forked from lucasgiovannibr/CanaryLauncherUpdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
268 lines (241 loc) · 9.07 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
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Diagnostics;
using Newtonsoft.Json;
using System.Net.Http;
namespace CanaryLauncherUpdate
{
public partial class MainWindow : Window
{
HttpClient httpClient = new HttpClient();
WebClient webClient = new WebClient();
bool clientDownloaded = false;
bool needUpdate = false;
string clientName = "funeral.exe";
string urlClient = "https://github.com/lucasgiovannibr/clientlauncherupdate/archive/refs/heads/main.zip";
string urlVersion = "https://raw.githubusercontent.com/lucasgiovannibr/clientlauncherupdate/main/version.txt";
string currentVersion = "";
string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
public MainWindow()
{
InitializeComponent();
}
private void TibiaLauncher_Load(object sender, RoutedEventArgs e)
{
ImageLogoServer.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/logo.png"));
ImageLogoCompany.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/logo_company.png"));
currentVersion = httpClient.GetStringAsync(urlVersion).Result;
progressbarDownload.Visibility = Visibility.Collapsed;
labelClientVersion.Visibility = Visibility.Collapsed;
labelDownloadPercent.Visibility = Visibility.Collapsed;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (Directory.Exists(path + "/clientlauncherupdate-main"))
{
clientDownloaded = true;
}
if (File.Exists(path + "/clientlauncherupdate-main/version.txt"))
{
StreamReader reader = new StreamReader(path + "/clientlauncherupdate-main/version.txt");
string? myVersion = reader.ReadLine();
reader.Close();
labelVersion.Text = "v" + currentVersion;
if (currentVersion == myVersion)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_play.png")));
buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/icon_play.png"));
labelClientVersion.Content = GetClientVersion(path);
labelClientVersion.Visibility = Visibility.Visible;
buttonPlay_tooltip.Text = "Play Game";
needUpdate = false;
}
if (currentVersion != myVersion)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_update.png")));
buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/icon_update.png"));
labelClientVersion.Content = "Download";
labelClientVersion.Visibility = Visibility.Visible;
buttonPlay.Visibility = Visibility.Visible;
buttonPlay_tooltip.Text = "Update";
needUpdate = true;
}
}
if (!File.Exists(path + "/clientlauncherupdate-main/version.txt"))
{
labelVersion.Text = "v" + currentVersion;
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_update.png")));
buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/icon_update.png"));
labelClientVersion.Content = "Download";
labelClientVersion.Visibility = Visibility.Visible;
buttonPlay.Visibility = Visibility.Visible;
buttonPlay_tooltip.Text = "Download";
needUpdate = true;
}
}
static string GetClientVersion(string path)
{
string json = path + "/clientlauncherupdate-main/package.json";
StreamReader stream = new StreamReader(json);
dynamic jsonString = stream.ReadToEnd();
dynamic versionclient = JsonConvert.DeserializeObject(jsonString);
foreach (string version in versionclient)
{
return version;
}
return "Play Game";
}
private void buttonPlay_Click(object sender, RoutedEventArgs e)
{
if (needUpdate == true)
{
try
{
labelDownloadPercent.Visibility = Visibility.Visible;
progressbarDownload.Visibility = Visibility.Visible;
labelClientVersion.Visibility = Visibility.Collapsed;
buttonPlay.Visibility = Visibility.Collapsed;
webClient.DownloadProgressChanged += Client_DownloadProgressChanged;
webClient.DownloadFileCompleted += Client_DownloadFileCompleted;
webClient.DownloadFileAsync(new Uri(urlClient), path + "/tibia.zip");
}
catch (Exception ex)
{
labelVersion.Text = ex.ToString();
}
}
else
{
if (clientDownloaded == true)
{
Process.Start(path + "/clientlauncherupdate-main/bin/" + clientName);
this.Close();
}
else
{
try
{
labelDownloadPercent.Visibility = Visibility.Visible;
progressbarDownload.Visibility = Visibility.Visible;
labelClientVersion.Visibility = Visibility.Collapsed;
buttonPlay.Visibility = Visibility.Collapsed;
webClient.DownloadProgressChanged += Client_DownloadProgressChanged;
webClient.DownloadFileCompleted += Client_DownloadFileCompleted;
webClient.DownloadFileAsync(new Uri(urlClient), path + "/tibia.zip");
}
catch (Exception ex)
{
labelVersion.Text = ex.ToString();
}
}
}
}
private void Client_DownloadFileCompleted(object? sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_play.png")));
buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/icon_play.png"));
Directory.CreateDirectory(path);
ZipFile.ExtractToDirectory(path + "/tibia.zip", path, true);
File.Delete(path + "/tibia.zip");
progressbarDownload.Value = 100;
needUpdate = false;
clientDownloaded = true;
labelClientVersion.Content = "Play Game";
buttonPlay_tooltip.Text = "Play Game";
labelClientVersion.Visibility = Visibility.Visible;
buttonPlay.Visibility = Visibility.Visible;
progressbarDownload.Visibility = Visibility.Collapsed;
labelDownloadPercent.Visibility = Visibility.Collapsed;
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressbarDownload.Value = e.ProgressPercentage;
labelDownloadPercent.Content = SizeSuffix(e.BytesReceived) + " / " + SizeSuffix(e.TotalBytesToReceive);
}
static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
static string SizeSuffix(Int64 value, int decimalPlaces = 1)
{
if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
int mag = (int)Math.Log(value, 1024);
decimal adjustedSize = (decimal)value / (1L << (mag * 10));
if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
{
mag += 1;
adjustedSize /= 1024;
}
return string.Format("{0:n" + decimalPlaces + "} {1}",
adjustedSize,
SizeSuffixes[mag]);
}
private void buttonPlay_MouseEnter(object sender, MouseEventArgs e)
{
if (File.Exists(path + "/clientlauncherupdate-main/version.txt"))
{
StreamReader reader = new StreamReader(path + "/clientlauncherupdate-main/version.txt");
string? myVersion = reader.ReadLine();
reader.Close();
if (currentVersion != myVersion)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_hover_update.png")));
}
if (currentVersion == myVersion)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_hover_play.png")));
}
}
else
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_hover_update.png")));
}
}
private void buttonPlay_MouseLeave(object sender, MouseEventArgs e)
{
if (File.Exists(path + "/clientlauncherupdate-main/version.txt"))
{
StreamReader reader = new StreamReader(path + "/clientlauncherupdate-main/version.txt");
string? myVersion = reader.ReadLine();
reader.Close();
if (currentVersion != myVersion)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_update.png")));
}
if (currentVersion == myVersion)
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_play.png")));
}
}
else
{
buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Assets/button_update.png")));
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void RestoreButton_Click(object sender, RoutedEventArgs e)
{
if (ResizeMode != ResizeMode.NoResize)
{
if (WindowState == WindowState.Normal)
WindowState = WindowState.Maximized;
else
WindowState = WindowState.Normal;
}
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
}
}