This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
363 lines (297 loc) · 11.1 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
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
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 System.Net;
using System.Windows.Threading;
using Microsoft.Win32;
namespace OS_Game_Launcher
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
bool LoggedIn = false;
public Pages.library libraryP = new Pages.library();
public Pages.discover discoverP = new Pages.discover();
public Pages.accountSettings accountP = new Pages.accountSettings();
public Pages.settings settingsP = new Pages.settings();
public Pages.aboutOSG aboutP = new Pages.aboutOSG();
public MainWindow()
{
Console.WriteLine("\nNew Session");
Utils.Init();
if (!Utils.CheckForServerConnection())
{
Utils.showMessage("We can't connect to our servers. Please check your Internet connection or contact our support.");
App.Current.Shutdown(1);
}
Utils.CheckVersion();
if (!LoggedIn)
{
Window loginW = new Windows.login();
bool? dialogResult = loginW.ShowDialog();
switch (dialogResult) {
case true:
Console.WriteLine("Successfully logged in.");
break;
case false:
this.Close();
break;
default:
this.Close();
break;
}
}
InitializeComponent();
launcherVersion.Text = Utils.getRunningVersion() + " (alpha)";
_mainFrame.Navigate(new Pages.loading());
}
private bool AllowedSaveWinProps = false;
private async void Window_Initialized(object sender, EventArgs e)
{
this.Visibility = Visibility.Hidden;
var loadingWin = new Windows.loading();
loadingWin.Show();
Utils.setURLProtocol();
await Utils.fixInstallingGames();
await UpdateUserData();
Utils.startPipeServer();
_mainFrame.Navigate(libraryP);
await Account.HandleStartupArgs(App.startupArgs);
loadingWin.Close();
this.Visibility = Visibility.Visible;
}
/*
private async void loaded(object sender, RoutedEventArgs e)
{
await UpdateUserData();
_mainFrame.Navigate(libraryP);
}
*/
/// <summary>
/// TitleBar_MouseDown - Drag if single-click, resize if double-click
/// </summary>
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
if (e.ClickCount == 2)
{
AdjustWindowSize();
}
else
{
Application.Current.MainWindow.DragMove();
}
}
/// <summary>
/// CloseButton_Clicked
/// </summary>
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
/// <summary>
/// MaximizedButton_Clicked
/// </summary>
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
AdjustWindowSize();
}
/// <summary>
/// Minimized Button_Clicked
/// </summary>
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
/// <summary>
/// Adjusts the WindowSize to correct parameters when Maximize button is clicked
/// </summary>
private void AdjustWindowSize(bool justState=false)
{
if (this.WindowState == WindowState.Maximized)
{
if (justState == false)
{
this.WindowState = WindowState.Normal;
MaxButton.Content = "⬜";
Root.Margin = new Thickness(0);
} else
{
MaxButton.Content = "◱";
Root.Margin = new Thickness(8);
}
}
else
{
if (justState == false)
{
this.WindowState = WindowState.Maximized;
MaxButton.Content = "◱";
Root.Margin = new Thickness(8);
} else
{
MaxButton.Content = "⬜";
Root.Margin = new Thickness(0);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(libraryP);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(discoverP);
}
public async Task UpdateUserData()
{
var request = new RestRequest("/user/info/home");
var cTokeS = new CancellationTokenSource();
var response = await Utils.Client.ExecuteGetAsync(request, cTokeS.Token);
var data = JObject.Parse(response.Content);
if (data.ContainsKey("success"))
{
if ((bool)data["success"] == false)
{
if ((string)data["error_code"] == "OSG-U1")
{
Console.WriteLine("Not logged in.");
Utils.RestartApp();
}
new Windows.msgBox(data["error_message"].ToString()).ShowDialog();
}
}
userTag.Text = (string)data["tag"];
userBalance.Text = ((float)data["balance"]).ToString("C");
if (await Utils.CheckUrl((string)data["profile_picture_url"]))
{
Console.WriteLine("User profile picture available");
if (await Utils.UrlIsImage((string)data["profile_picture_url"]))
{
Console.WriteLine("User profile picture is image");
var btmp = new BitmapImage(new Uri((string)data["profile_picture_url"]));
userProfilePicture.ImageSource = btmp;
} else
{
Console.WriteLine("User profile picture is not an image");
}
} else
{
Console.WriteLine("User profile picture is not available");
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Button openBtn = sender as Button;
ContextMenu contextMenu = openBtn.ContextMenu;
contextMenu.PlacementTarget = openBtn;
contextMenu.IsOpen = true;
e.Handled = true;
}
private async void LogoutClick(object sender, RoutedEventArgs e)
{
var loadingWindow = new Windows.loading(true);
loadingWindow.Show();
this.IsEnabled = false;
await Account.Logout();
loadingWindow.Close();
}
private async void gameDownloadCancelButton_Click(object sender, RoutedEventArgs e)
{
await Account.CancelDownload();
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
Console.WriteLine("Window got unintended Maximized");
AdjustWindowSize(true);
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Utils.stopPipServer();
if (AllowedSaveWinProps)
{
Console.WriteLine("Saving window Properties");
saveWindowProperties();
}
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(accountP);
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(settingsP);
}
private void aboutButton_Click(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(aboutP);
}
private void Button_Click_5(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(aboutP);
}
public void saveWindowProperties()
{
var key = Utils.RegistryOpenCreateKey(Registry.CurrentUser, Properties.Settings.Default.regestryPath);
if (this.WindowState == WindowState.Maximized)
{
key.SetValue("WinTop", RestoreBounds.Top);
key.SetValue("WinLeft", RestoreBounds.Left);
key.SetValue("WinHeight", RestoreBounds.Height);
key.SetValue("WinWidth", RestoreBounds.Width);
key.SetValue("WinMax", true);
} else
{
key.SetValue("WinTop", this.Top);
key.SetValue("WinLeft", this.Left);
key.SetValue("WinHeight", this.Height);
key.SetValue("WinWidth", this.Width);
key.SetValue("WinMax", false);
}
}
public void readWindowProperties()
{
var key = Utils.RegistryOpenCreateKey(Registry.CurrentUser, Properties.Settings.Default.regestryPath);
Top = Convert.ToDouble(Utils.RegistryGetSet(key, "WinTop", 20));
Console.WriteLine(Utils.RegistryGetSet(key, "WinTop", 20));
Left = Convert.ToDouble(Utils.RegistryGetSet(key, "WinLeft", 20));
Height = Convert.ToDouble(Utils.RegistryGetSet(key, "WinHeight", 715));
Width = Convert.ToDouble(Utils.RegistryGetSet(key, "WinWidth", 1080));
if (bool.Parse((string)Utils.RegistryGetSet(key, "WinMax", false))) {
WindowState = WindowState.Maximized;
}
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
readWindowProperties();
AllowedSaveWinProps = true;
}
public void SetFocus()
{
if (WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Topmost = true;
Activate();
Topmost = false;
}
}
}