-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAddGames.xaml.cs
346 lines (330 loc) · 14.7 KB
/
AddGames.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
using GameLauncher.Properties;
using GameLauncher.ViewModels;
using IWshRuntimeLibrary;
using MaterialDesignThemes.Wpf;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
namespace GameLauncher
{
public partial class AddGames : Page
{
private LoadAllGames lag = new LoadAllGames();
public string alltitles;
public int offset = 0;
public AddGames()
{
InitializeComponent();
if (Settings.Default.theme.ToString() == "Dark")
{
ThemeAssist.SetTheme(this, BaseTheme.Dark);
}
else if (Settings.Default.theme.ToString() == "Light")
{
ThemeAssist.SetTheme(this, BaseTheme.Light);
}
}
private void AddGame_OnClick(object sender, RoutedEventArgs e)
{
if (NewGameTitle.Text != "")
{
//This part repairs the link so it launches properly
string ngl = NewGameLink.Text;
if (!ngl.Contains("http") && (ngl != ""))
{
UriBuilder uriBuilder = new UriBuilder
{
Scheme = "http",
Host = NewGameLink.Text
};
Uri uri = uriBuilder.Uri;
NewGameLink.Text = uri.ToString();
}
if (System.IO.File.Exists("./Resources/GamesList.txt"))
{
string[] allgames = System.IO.File.ReadAllLines("./Resources/GamesList.txt");
string[] columns = new string[0];
int numofgames = 0;
foreach (var item in allgames)
{
columns = allgames[numofgames].Split('|');
string gametitle = columns[0];
gametitle = columns[0];
gametitle = gametitle.Trim().ToLower();
alltitles = alltitles + " | " + gametitle + " | ";
numofgames++;
}
if (alltitles.Contains(" | " + NewGameTitle.Text.Trim().ToLower() + " | "))
{
MessageBox.Show("A game with this title already exists");
NewGameTitle.Text = "";
}
else
{
try
{
TextWriter tsw = new StreamWriter(@"./Resources/GamesList.txt", true);
Guid gameGuid = Guid.NewGuid();
tsw.WriteLine(NewGameTitle.Text + "|" +
NewGameGenre.Text + "|" +
NewGamePath.Text + "|" +
NewGameLink.Text + "|" +
NewGameIcon.Text + "|" +
NewGamePoster.Text + "|" +
NewGameBanner.Text + "|" +
gameGuid);
tsw.Close();
}
catch (Exception ex)
{
Trace.WriteLine(DateTime.Now + ": AddGameOnClick: " + ex.Message);
}
Trace.WriteLine(DateTime.Now + ": Added Game manually: " + NewGameTitle.Text);
ClearGenreBoxes();
clearFields();
((MainWindow)Application.Current.MainWindow)?.RefreshGames();
((MainWindow)Application.Current.MainWindow).isDialogOpen = false;
AddGameDialog.IsOpen = false;
}
}
else
{
try
{
TextWriter tsw = new StreamWriter(@"./Resources/GamesList.txt", true);
Guid gameGuid = Guid.NewGuid();
tsw.WriteLine(NewGameTitle.Text + "|" +
NewGameGenre.Text + "|" +
NewGamePath.Text + "|" +
NewGameLink.Text + "|" +
NewGameIcon.Text + "|" +
NewGamePoster.Text + "|" +
NewGameBanner.Text + "|" +
gameGuid);
tsw.Close();
}
catch (Exception ex)
{
Trace.WriteLine(DateTime.Now + ": AddGameOnClick2: " + ex.Message);
}
ClearGenreBoxes();
clearFields();
((MainWindow)Application.Current.MainWindow)?.RefreshGames();
AddGameDialog.IsOpen = false;
}
}
else
{
MessageBox.Show("Please enter a game title first.");
}
}
private void CancelAddGame_OnClick(object sender, RoutedEventArgs e)
{
ClearGenreBoxes();
((MainWindow)Application.Current.MainWindow).isDialogOpen = false;
AddGameDialog.IsOpen = false;
clearFields();
}
private void clearFields()
{
NewGameTitle.Text = "";
NewGamePath.Text = "";
NewGameGenre.Text = "";
NewGameLink.Text = "";
NewGameIcon.Text = "";
NewGamePoster.Text = "";
NewGameBanner.Text = "";
}
private void TitleTextChanged(object sender, EventArgs e)
{
if (NewGameTitle.Text.IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
{
MessageBox.Show("Unfortunately your title must be valid to save files. This means it cannot contain characters like : ? \\ / etc.");
}
}
private void AddGenre_OnClick(object sender, RoutedEventArgs e)
{
string genrePlaceHolder = null;
for (int i = 0; i < GenreAGList.Items.Count; i++)
{
ContentPresenter c = (ContentPresenter)GenreAGList.ItemContainerGenerator.ContainerFromItem(GenreAGList.Items[i]);
try
{
CheckBox cb = c.ContentTemplate.FindName("genreCheckBox", c) as CheckBox;
if (cb.IsChecked.Value)
{
genrePlaceHolder += cb.Content.ToString() + ";";
}
}
catch (Exception ex) { Trace.WriteLine(DateTime.Now + ": AddGenre: " + ex); }
}
NewGameGenre.Text = genrePlaceHolder;
return;
}
private void ClearGenreSelection_OnClick(object sender, RoutedEventArgs e)
{
ClearGenreBoxes();
}
private void ClearGenreBoxes()
{
for (int i = 0; i < GenreAGList.Items.Count; i++)
{
ContentPresenter c = (ContentPresenter)GenreAGList.ItemContainerGenerator.ContainerFromItem(GenreAGList.Items[i]);
if (c != null)
{
try
{
CheckBox cb = c.ContentTemplate.FindName("genreCheckBox", c) as CheckBox;
if (cb.IsChecked.Value)
{
cb.IsChecked = false;
}
}
catch (Exception e) { Trace.WriteLine(DateTime.Now + ": ClearGenre: " + e); }
}
}
}
private void AttachLauncher_OnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = false,
RestoreDirectory = true,
Filter = "Executable Files (*.exe) | *.exe;*.lnk;*.url"
};
var dialogResult = fileDialog.ShowDialog();
if (dialogResult == true && NewGameTitle.Text != "")
{
newgametitle = NewGameTitle.Text.Replace(":", " -");
CreateShortcut(fileDialog.FileName);
string installPath = AppDomain.CurrentDomain.BaseDirectory;
installPath = installPath.Replace("\\", "/");
string ngNewShortcut = installPath + "Resources/shortcuts/" + newgametitle + ".lnk";
NewGamePath.Text = newgametitle + ".lnk";
}
else if (dialogResult == true && NewGameTitle.Text == "")
{
MessageBox.Show("Please enter a game title first.");
}
}
public string newgametitle;
private void AttachIcon_OnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = false,
RestoreDirectory = true,
Filter = "Images (*.jpg;*.png;*.bmp) | *.jpg;*.png;*.bmp"
};
var dialogResult = fileDialog.ShowDialog();
if (dialogResult == true && NewGameTitle.Text != "")
{
string installPath = AppDomain.CurrentDomain.BaseDirectory;
installPath = installPath.Replace("\\", "/");
string ngIconFile = fileDialog.FileName;
newgametitle = NewGameTitle.Text.Replace(":", " -");
System.IO.File.Copy(ngIconFile, @"./Resources/img/" + newgametitle + "-icon.png", true);
NewGameIcon.Text = newgametitle + "-icon.png";
}
else if (dialogResult == true && newgametitle == "")
{
MessageBox.Show("Please enter a game title first.");
}
}
private void AttachPoster_OnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = false,
RestoreDirectory = true,
Filter = "Images (*.jpg;*.png;*.bmp) | *.jpg;*.png;*.bmp"
};
var dialogResult = fileDialog.ShowDialog();
if (dialogResult == true && NewGameTitle.Text != "")
{
string installPath = AppDomain.CurrentDomain.BaseDirectory;
installPath = installPath.Replace("\\", "/");
string ngPosterFile = fileDialog.FileName;
newgametitle = NewGameTitle.Text.Replace(":", " -"); //this line needs to be used to block any chars that cant be used
System.IO.File.Copy(ngPosterFile, @"./Resources/img/" + newgametitle + "-poster.png", true);
NewGamePoster.Text = newgametitle + "-poster.png";
}
else if (dialogResult == true && NewGameTitle.Text == "")
{
MessageBox.Show("Please enter a game title first.");
}
}
private void AttachBanner_OnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
RestoreDirectory = true,
Multiselect = false,
Filter = "Images (*.jpg;*.png;*.bmp) | *.jpg;*.png;*.bmp"
};
var dialogResult = fileDialog.ShowDialog();
if (dialogResult == true && NewGameTitle.Text != "")
{
string installPath = AppDomain.CurrentDomain.BaseDirectory;
installPath = installPath.Replace("\\", "/");
string ngBannerFile = fileDialog.FileName;
System.IO.File.Copy(ngBannerFile, @"./Resources/img/" + newgametitle + "-banner.png", true);
NewGameBanner.Text = newgametitle + "-banner.png";
}
else if (dialogResult == true && NewGameTitle.Text == "")
{
MessageBox.Show("Please enter a game title first.");
}
}
private void CreateShortcut(string linkname)
{
newgametitle = NewGameTitle.Text.Replace(":", " -");
string installPath = AppDomain.CurrentDomain.BaseDirectory;
if (!Directory.Exists(installPath + "\\Resources\\shortcuts"))
{
Directory.CreateDirectory(installPath + "\\Resources\\shortcuts");
}
//create shortcut from linkname, place shortut in dir
WshShell wsh = new WshShell();
IWshShortcut shortcut = wsh.CreateShortcut(
installPath + "\\Resources\\shortcuts" + "\\" + newgametitle + ".lnk") as IWshShortcut;
shortcut.Arguments = "";
shortcut.TargetPath = linkname;
shortcut.WindowStyle = 1;
shortcut.Description = "Shortcut to " + newgametitle;
shortcut.WorkingDirectory = "C:\\App";
shortcut.IconLocation = linkname;
shortcut.Save();
}
private void SearchIcon_OnClick(object sender, RoutedEventArgs e)
{
LoadSearch ls = new LoadSearch();
string gametitle = NewGameTitle.Text;
string imagetype = "icon";
string searchstring = gametitle + " game icon";
ls.SearchLinks(gametitle, imagetype, searchstring, offset);
((MainWindow)Application.Current.MainWindow)?.OpenImageDL(gametitle, searchstring, imagetype);
}
private void SearchPoster_OnClick(object sender, RoutedEventArgs e)
{
LoadSearch ls = new LoadSearch();
string gametitle = NewGameTitle.Text;
string imagetype = "poster";
string searchstring = gametitle + " game poster";
ls.SearchLinks(gametitle, imagetype, searchstring, offset);
((MainWindow)Application.Current.MainWindow)?.OpenImageDL(gametitle, searchstring, imagetype);
}
private void SearchBanner_OnClick(object sender, RoutedEventArgs e)
{
LoadSearch ls = new LoadSearch();
string gametitle = NewGameTitle.Text;
string imagetype = "banner";
string searchstring = gametitle + " game banner";
ls.SearchLinks(gametitle, imagetype, searchstring, offset);
((MainWindow)Application.Current.MainWindow)?.OpenImageDL(gametitle, searchstring, imagetype);
}
}
}