-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
281 lines (245 loc) · 10.9 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
//using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
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;
namespace StandaloneGeneratorV3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<Game> gamesList;
List<Repo> repoList;
private ObservableCollection<RepoPatch> selectedPatchesList;
private Logger logger;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Initialize logging
this.logger = new Logger(this.Dispatcher, this.uiLogWindow);
gamesList = GamesList.Load();
this.uiGamesList.ItemsSource = gamesList;
selectedPatchesList = new ObservableCollection<RepoPatch>();
uiSelectedPatches.ItemsSource = selectedPatchesList;
IsEnabled = false;
// thcrap.dll will look for its DLLs in its current directory,
// change it so that it can find them.
string curDir = Environment.CurrentDirectory;
Environment.CurrentDirectory = ThcrapDll.THCRAP_DLL_PATH;
ThcrapDll.update_filter_global_wrapper("", IntPtr.Zero);
Environment.CurrentDirectory = curDir;
Task.Run(() =>
{
// Curl will look for bin/cacert.pem, set the current directory
// in a way that it will find it.
Environment.CurrentDirectory = ThcrapDll.THCRAP_DLL_PATH + "..";
var repoList = Repo.Discovery("https://srv.thpatch.net/");
this.Dispatcher.Invoke(() => this.IsEnabled = true);
Environment.CurrentDirectory = curDir;
if (repoList == null)
return;
this.repoList = repoList;
this.Dispatcher.Invoke(() => this.uiRepos.ItemsSource = repoList);
ThcrapDll.log_print("Repo discovery finished\n");
});
}
private async void ReloadGamesList(object sender, RoutedEventArgs e)
{
ThcrapDll.log_print("Reloading games list from " + GamesList.BaseURL + " ...\n");
List<Game> gamesList = null;
try
{
gamesList = await GamesList.Reload();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
if (gamesList == null)
return;
this.gamesList = gamesList;
this.uiGamesList.ItemsSource = gamesList;
ThcrapDll.log_print("Games list reloaded and saved to disk!\n");
}
private string GeneratePatchNameFromStack()
{
bool skip = false;
string ret = "";
// If we have any translation patch, skip everything below that
if (this.selectedPatchesList.Any((RepoPatch patch) => patch.Id.StartsWith("lang_")))
skip = true;
foreach (var patch in this.selectedPatchesList)
{
string patch_id;
if (patch.Id.StartsWith("lang_"))
{
patch_id = patch.Id.Substring(5);
skip = false;
}
else
patch_id = patch.Id;
if (!skip)
{
if (ret.Length != 0)
ret += "-";
ret += patch_id;
}
}
return ret;
}
private void SelectRepo(object sender, MouseButtonEventArgs e)
{
bool updatePatchName = false;
if (GeneratePatchNameFromStack() == uiConfigName.Text)
updatePatchName = true;
var patch = (sender as TextBlock).DataContext as RepoPatch;
selectedPatchesList.Add(patch);
if (updatePatchName)
uiConfigName.Text = GeneratePatchNameFromStack();
}
private void CreateExe(string game_id, string icon_path)
{
string exe_name = game_id + " (" + this.uiConfigName.Text + ").exe";
File.Copy(AppContext.BaseDirectory + @"res\thcrap\thcrap_loader.exe", exe_name);
using (PeResourceUpdater exe = new PeResourceUpdater(exe_name))
{
exe.ReplaceStringTable(new List<string>()
{
@"thcrap\bin\",
string.Format(@"thcrap\bin\thcrap_loader.exe {0}.js {1}", this.uiConfigName.Text, game_id),
"thcrap_loader.exe"
});
if (icon_path != null)
exe.ReplaceIcon(icon_path);
}
}
private async Task CreateStandalonePatchForGame(Game game)
{
ThcrapDll.log_print("Generating standalone for " + game.Name + "...\n");
Directory.CreateDirectory(game.Id);
Directory.CreateDirectory(game.Id + "\\thcrap\\");
Environment.CurrentDirectory = game.Id + "\\thcrap\\";
ZipFile.ExtractToDirectory(@"..\..\thcrap.zip", ".");
foreach (RepoPatch patch in selectedPatchesList)
await Task.Run(() => patch.AddToStack());
await Task.Run(() => ThcrapDll.stack_update_wrapper(
(string fn, IntPtr filter_data) => (fn.Contains('/') == false) || fn.StartsWith(game.Id + "/") ? 1 : 0, IntPtr.Zero,
(IntPtr status_, IntPtr param) =>
{
var status = Marshal.PtrToStructure<ThcrapDll.progress_callback_status_t>(status_);
switch (status.status)
{
case ThcrapDll.get_status_t.GET_DOWNLOADING:
case ThcrapDll.get_status_t.GET_CANCELLED:
break;
case ThcrapDll.get_status_t.GET_OK:
var patch = Marshal.PtrToStructure<ThcrapDll.patch_t>(status.patch);
string patch_id = Marshal.PtrToStringAnsi(patch.id);
ThcrapDll.log_print(string.Format("[{0}/{1}] {2}/{3}: OK ({4}b)\n",
status.nb_files_downloaded, status.nb_files_total,
patch_id, status.fn, status.file_size));
break;
case ThcrapDll.get_status_t.GET_CLIENT_ERROR:
case ThcrapDll.get_status_t.GET_SERVER_ERROR:
case ThcrapDll.get_status_t.GET_SYSTEM_ERROR:
ThcrapDll.log_print(status.url + " : " + status.error + "\n");
break;
case ThcrapDll.get_status_t.GET_CRC32_ERROR:
ThcrapDll.log_print(status.url + " : CRC32 error\n");
break;
}
return true;
}, IntPtr.Zero)
);
var runconfig = new Runconfig();
foreach (RepoPatch patch in selectedPatchesList)
runconfig.patches.Add(new RunconfigPatch(patch.Archive));
runconfig.Save(uiConfigName.Text);
var gamesJs = new Dictionary<string, string>()
{
{ game.Id, "../" + game.Id + ".exe" },
{ game.Id + "_custom", "../custom.exe" },
};
string jsonGamesJs = JsonSerializer.Serialize(gamesJs, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText("config/games.js", jsonGamesJs);
Environment.CurrentDirectory = "..";
CreateExe(game.Id, game.ImagePath);
CreateExe(game.Id + "_custom", null);
ThcrapDll.stack_free();
Environment.CurrentDirectory = "..";
ThcrapDll.log_print("Standalone for " + game.Name + " generated!\n");
}
private async void GenerateStandalonePatch(object sender, RoutedEventArgs e)
{
if (Directory.Exists("out"))
Directory.Delete("out", true);
Directory.CreateDirectory("out");
Environment.CurrentDirectory = "out";
ThcrapDll.log_print("Downloading thcrap...\n");
var webClient = new WebClient();
await webClient.DownloadFileTaskAsync("https://thcrap.thpatch.net/stable/thcrap.zip", "thcrap.zip");
foreach (Game game in gamesList)
if (game.IsSelected)
await CreateStandalonePatchForGame(game);
File.Delete("thcrap.zip");
Environment.CurrentDirectory = "..";
ThcrapDll.log_print("Standalone patches generation finished!\n");
}
private void updatePatchesListFilter(object sender, TextChangedEventArgs e)
{
var textbox = sender as TextBox;
var text = textbox.Text.ToLower();
foreach (Repo repo in this.repoList)
repo.UpdateFilter(text);
this.uiRepos.ItemsSource = this.repoList.Where((Repo repo) => repo.PatchesFiltered.Count() > 0);
}
private void selectedPatches_MoveUp(object sender, RoutedEventArgs e)
{
var elem = uiSelectedPatches.SelectedItem as RepoPatch;
if (elem == null)
return;
int index = selectedPatchesList.IndexOf(elem);
if (index > 0)
selectedPatchesList.Move(index, index - 1);
}
private void selectedPatches_MoveDown(object sender, RoutedEventArgs e)
{
var elem = uiSelectedPatches.SelectedItem as RepoPatch;
if (elem == null)
return;
int index = selectedPatchesList.IndexOf(elem);
if (index != -1 && index < selectedPatchesList.Count - 1)
selectedPatchesList.Move(index, index + 1);
}
private void selectedPatches_Remove(object sender, RoutedEventArgs e)
{
var elem = uiSelectedPatches.SelectedItem as RepoPatch;
if (elem == null)
return;
selectedPatchesList.Remove(elem);
}
}
}