-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
300 lines (266 loc) · 10.6 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Windows.Threading;
using TD_Loader.Classes;
namespace TD_Loader
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public event ProcFinished_BoolEventHandler FinishedGameHandling;
bool checkedNKH = false;
bool finishedLoading = false;
public static bool doingWork = false;
public static bool exit = false;
public static bool resetGameFiles = false;
public static string workType = "";
public static MainWindow instance;
public static Mods_UserControl mods_User;
public static Plugins_UserControl plugin_User;
public MainWindow()
{
InitializeComponent();
instance = this;
Startup();
BTD5_Image.IsMouseDirectlyOverChanged += BTD5_Image_IsMouseDirectlyOverChanged;
BTDB_Image.IsMouseDirectlyOverChanged += BTDB_Image_IsMouseDirectlyOverChanged;
BMC_Image.IsMouseDirectlyOverChanged += BMC_Image_IsMouseDirectlyOverChanged;
Main.Closing += Main_Closing;
JetReader.FinishedStagingMods += JetReader_FinishedStagingMods;
}
private void Startup()
{
Settings.LoadSettings();
if (Settings.game == null)
Settings.SetGameFile(Settings.settings.GameName);
Log.Output("Program initializing...");
ResetGamePictures();
CreateModsTab();
ShowHidePlugins();
UpdateHandler.CheckForUpdates();
FinishedGameHandling += MainWindow_FinishedGameHandling;
}
private void FinishedLoading()
{
if (!Guard.IsStringValid(Settings.game.GameDir) || !Guard.IsStringValid(Settings.game.GameName))
{
Settings.settings.GameName = "";
Settings.SaveSettings();
Settings.game = null;
return;
}
switch (Settings.game.GameName)
{
case "BTD5":
BTD5_Image.Source = new BitmapImage(new Uri("Resources/btd5.png", UriKind.Relative));
break;
case "BTDB":
BTDB_Image.Source = new BitmapImage(new Uri("Resources/btdb 2.png", UriKind.Relative));
break;
case "BMC":
BMC_Image.Source = new BitmapImage(new Uri("Resources/bmc.png", UriKind.Relative));
break;
}
GameHandling();
}
private void GameHandling()
{
if (Settings.game == null)
return;
Settings.SaveGameFile();
doingWork = true;
Log.Output("Game: " + Settings.game.GameName);
workType = "Initializing mod loader for " + Settings.game.GameName;
ShowHidePlugins();
new Thread(() =>
{
Game.CloseGameIfOpen(Settings.game.GameName);
Game.ValidateGameDir();
Game.WasGameUpdated();
if (!Guard.IsStringValid(Settings.game.ModsDir))
Game.SetModsDir(Settings.game.GameName);
Game.ValidateBackup();
if(NKHook.CanUseNKH() && !checkedNKH)
{
NKHook nkh = new NKHook();
nkh.DoInitialNKH();
}
doingWork = false;
workType = "";
if (FinishedGameHandling != null)
FinishedGameHandling(this, new ProcFinished_BoolEventArgs(true));
}).Start();
}
private void MainWindow_FinishedGameHandling(object source, ProcFinished_BoolEventArgs e)
{
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => {
ShowHidePlugins();
Mods_UserControl.instance.PopulateMods(Settings.game.GameName);
Mods_UserControl.instance.Mods_TextBlock.Text = Settings.game.GameName + " Mods";
if (NKHook.CanUseNKH() && plugin_User == null)
CreatePluginsTab();
}));
}
//
//Main events
//
public static async Task Wait(int time)
{
await Task.Delay(time);
}
private void Main_Activated(object sender, EventArgs e)
{
if (finishedLoading == false)
{
finishedLoading = true;
FinishedLoading();
}
}
private void Main_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Settings.SaveSettings();
Process.GetCurrentProcess().Kill();
}
private void Launch_Button_Clicked(object sender, RoutedEventArgs e)
{
if (Guard.IsDoingWork(workType))
return;
Game.DoLaunchWithMods();
}
private void JetReader_FinishedStagingMods(object sender, EventArgs e)
{
Game.LaunchGame();
}
//
//UI events
//
private void CreateModsTab()
{
mods_User = new Mods_UserControl();
var tab = new TabItem();
tab.Header = " Mods ";
tab.Padding = new Thickness(5);
tab.FontSize = 25;
tab.Content = mods_User;
Main_TabController.Items[1] = tab;
}
private void CreatePluginsTab()
{
plugin_User = new Plugins_UserControl()
{
isPlugins = true
};
Plugins_Tab.Content = plugin_User;
}
private void ShowHidePlugins()
{
if (NKHook.CanUseNKH() && Settings.game.GameName == "BTD5" && Settings.game != null)
{
Plugins_Tab.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { Plugins_Tab.Visibility = Visibility.Visible; }));
LaunchGrid.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { LaunchGrid.MinHeight = 195; }));
}
else
{
Plugins_Tab.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { Plugins_Tab.Visibility = Visibility.Collapsed; }));
LaunchGrid.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { LaunchGrid.MinHeight = 165; }));
}
}
private void ResetGameFiles_CB_Click(object sender, RoutedEventArgs e)
{
resetGameFiles = !resetGameFiles;
}
public void ResetGamePictures()
{
workType = ""; //We're doing this to clear the last opperation, if it wasnt cleared
BTD5_Image.Source = new BitmapImage(new Uri("Resources/btd5_not loaded.png", UriKind.Relative));
BTDB_Image.Source = new BitmapImage(new Uri("Resources/btdb 2_not loaded.png", UriKind.Relative));
BMC_Image.Source = new BitmapImage(new Uri("Resources/bmc_not loaded.png", UriKind.Relative));
}
private void BTD5_Image_MouseDown(object sender, MouseButtonEventArgs e)
{
if (Guard.IsDoingWork(workType))
return;
if (Settings.game == null || Settings.game.GameName != "BTD5")
{
ResetGamePictures();
Settings.SaveGameFile();
Settings.SetGameFile("BTD5");
BTD5_Image.Source = new BitmapImage(new Uri("Resources/btd5.png", UriKind.Relative));
GameHandling();
}
}
private void BTDB_Image_MouseDown(object sender, MouseButtonEventArgs e)
{
if (Guard.IsDoingWork(workType))
return;
if (Settings.game == null || Settings.game.GameName != "BTDB")
{
ResetGamePictures();
Settings.SaveGameFile();
Settings.SetGameFile("BTDB");
BTDB_Image.Source = new BitmapImage(new Uri("Resources/btdb 2.png", UriKind.Relative));
GameHandling();
}
}
private void BMC_Image_MouseDown(object sender, MouseButtonEventArgs e)
{
if (Guard.IsDoingWork(workType))
return;
if (Settings.game == null || Settings.game.GameName != "BMC")
{
ResetGamePictures();
Settings.SaveGameFile();
Settings.SetGameFile("BMC");
BMC_Image.Source = new BitmapImage(new Uri("Resources/bmc.png", UriKind.Relative));
GameHandling();
}
}
private void BMC_Image_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (Settings.game == null || Settings.game.GameName != "BMC")
{
if (!BMC_Image.IsMouseOver)
BMC_Image.Source = new BitmapImage(new Uri("Resources/bmc_not loaded.png", UriKind.Relative));
else
BMC_Image.Source = new BitmapImage(new Uri("Resources/bmc.png", UriKind.Relative));
}
}
private void BTDB_Image_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (Settings.game == null || Settings.game.GameName != "BTDB")
{
if (!BTDB_Image.IsMouseOver)
BTDB_Image.Source = new BitmapImage(new Uri("Resources/btdb 2_not loaded.png", UriKind.Relative));
else
BTDB_Image.Source = new BitmapImage(new Uri("Resources/btdb 2.png", UriKind.Relative));
}
}
private void BTD5_Image_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (Settings.game == null || Settings.game.GameName != "BTD5")
{
if (!BTD5_Image.IsMouseOver)
BTD5_Image.Source = new BitmapImage(new Uri("Resources/btd5_not loaded.png", UriKind.Relative));
else
BTD5_Image.Source = new BitmapImage(new Uri("Resources/btd5.png", UriKind.Relative));
}
}
}
}