This repository was archived by the owner on Feb 13, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
389 lines (344 loc) · 13.2 KB
/
MainWindow.xaml.cs
File metadata and controls
389 lines (344 loc) · 13.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System.IO;
using System.Net;
using System.Windows;
using IWshRuntimeLibrary;
using File = System.IO.File;
using Path = System.IO.Path;
using System.IO.Compression;
using System.ComponentModel;
using System.Net.Http;
using MessageBox = ModernWpf.MessageBox;
using System.Security.Cryptography;
using System;
namespace Lithicsoft_AI_Studio_Installer;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool isWorking = false;
private bool isInstalled = false;
private string localFilePath = ".build";
private string url = "https://raw.githubusercontent.com/Lithicsoft/Lithicsoft-Trainer-Studio/refs/heads/main/update.datas";
private string hashUrl = "https://raw.githubusercontent.com/Lithicsoft/Lithicsoft-Trainer-Studio/refs/heads/main/verify.sha256";
public MainWindow()
{
InitializeComponent();
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
try
{
LoadHtml();
if (File.Exists(".build") && Directory.Exists("Lithicsoft AI Studio"))
{
UpdateBuildTitle();
if (CheckForUpdates())
{
ControlButton.Content = "Repair";
isInstalled = true;
}
else
{
ControlButton.Content = "Update";
isInstalled = true;
}
}
else
{
ControlButton.Content = "Install";
isInstalled = false;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error during initialization: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
}
}
private async void LoadHtml()
{
string url = "https://raw.githubusercontent.com/Lithicsoft/Lithicsoft-Trainer-Studio/refs/heads/main/changelog.html";
using (HttpClient client = new HttpClient())
{
try
{
string htmlContent = await client.GetStringAsync(url);
webBrowser.NavigateToString(htmlContent);
}
catch (Exception ex)
{
MessageBox.Show("Error loading page: " + ex.Message);
}
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
ControlButton.IsEnabled = false;
try
{
isWorking = true;
await Task.Run(() => Dispatcher.Invoke(() => AIStudio()));
}
catch (Exception ex)
{
MessageBox.Show($"Update failed: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
}
}
private async Task AIStudio()
{
var progress = new Progress<(int percent, string message)>(report =>
{
Dispatcher.Invoke(() =>
{
ProcessPercent.Content = $"{report.percent}%";
ProcessBar.Value = report.percent;
Information.Content = report.message;
});
});
await Task.Run(() => DownloadAndExtractFiles(progress));
}
private void DownloadAndExtractFiles(IProgress<(int percent, string message)> progress)
{
string destinationFolder = "Lithicsoft AI Studio";
string zipFilePath = "downloaded.zip";
try
{
using var webClient = new WebClient();
string fileContents = webClient.DownloadString(url);
string[] lines = fileContents.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length > 0)
{
string latestBuild = lines[0];
string downloadUrl = lines.Length > 1 ? lines[1] : string.Empty;
File.WriteAllText(localFilePath, latestBuild);
webClient.DownloadProgressChanged += (s, e) =>
{
int percentComplete = (int)(e.ProgressPercentage * 0.5);
progress.Report((percentComplete, $"Downloading... {e.ProgressPercentage}%"));
};
webClient.DownloadFileCompleted += (s, e) =>
{
progress.Report((50, "Download complete. Verifying..."));
VerifyDownloadedFile(zipFilePath);
progress.Report((50, "Verify complete. Extracting..."));
ExtractFiles(zipFilePath, destinationFolder, progress);
};
webClient.DownloadFileAsync(new Uri(downloadUrl), zipFilePath);
}
}
catch (Exception ex)
{
Dispatcher.Invoke(() =>
{
MessageBox.Show($"Error during download: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
});
}
}
private async void VerifyDownloadedFile(string filePath)
{
try
{
using (HttpClient client = new HttpClient())
{
string expectedHash = await client.GetStringAsync(hashUrl);
expectedHash = expectedHash.Trim();
string actualHash = ComputeSHA256(filePath);
if (!actualHash.Equals(expectedHash, StringComparison.OrdinalIgnoreCase))
{
Dispatcher.Invoke(() =>
{
MessageBox.Show($"There is a problem with the downloaded file, please use the repair tool or report to us.", "File download verification failed", MessageBoxButton.OK, MessageBoxImage.Error);
});
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error during verify: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
}
}
static string ComputeSHA256(string filePath)
{
try
{
using (SHA256 sha256 = SHA256.Create())
{
using (FileStream stream = File.OpenRead(filePath))
{
byte[] hashBytes = sha256.ComputeHash(stream);
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error during verify: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
}
return null;
}
private void ExtractFiles(string zipFilePath, string destinationFolder, IProgress<(int percent, string message)> progress)
{
try
{
using (var archive = ZipFile.OpenRead(zipFilePath))
{
int totalFiles = archive.Entries.Count;
int extractedFiles = 0;
foreach (var entry in archive.Entries)
{
string destinationPath = Path.Combine(destinationFolder, entry.FullName);
string directoryPath = Path.GetDirectoryName(destinationPath);
if (!string.IsNullOrEmpty(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
if (entry.Name == "")
{
Directory.CreateDirectory(destinationPath);
}
else
{
entry.ExtractToFile(destinationPath, overwrite: true);
Thread.Sleep(50);
}
extractedFiles++;
int percentComplete = 50 + (int)((double)extractedFiles / totalFiles * 50);
progress.Report((percentComplete, $"Extracting {entry.FullName}..."));
}
}
File.Delete(zipFilePath);
if (!isInstalled)
{
DirectoryPermissionHelper.SetFullControlPermissions(destinationFolder);
CreateShortcut("Lithicsoft AI Studio", Path.GetFullPath(Path.Combine(destinationFolder, "Lithicsoft AI Studio.exe")));
Dispatcher.Invoke(() =>
{
ShowNotification("Installation Complete", "Lithicsoft AI Studio has been installed!");
Information.Content = "Waiting...";
ControlButton.Content = "Repair";
ControlButton.IsEnabled = true;
isWorking = false;
});
}
else
{
Dispatcher.Invoke(() =>
{
ShowNotification("Update Complete", "Lithicsoft AI Studio has been updated!");
Information.Content = "Waiting...";
ControlButton.Content = "Repair";
ControlButton.IsEnabled = true;
isWorking = false;
});
}
UpdateBuildTitle();
}
catch (Exception ex)
{
Dispatcher.Invoke(() =>
{
MessageBox.Show($"Error during extraction: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
});
}
}
private void UpdateBuildTitle()
{
try
{
Dispatcher.Invoke(() =>
{
this.Title = "Lithicsoft AI Studio Installer | Build: " + File.ReadAllText(".build");
});
}
catch (Exception ex)
{
MessageBox.Show($"Error updating build title: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
}
}
private bool CheckForUpdates()
{
try
{
using var webClient = new WebClient();
string fileContents = webClient.DownloadString(url);
string[] lines = fileContents.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length > 0)
{
string latestBuild = lines[0];
if (File.Exists(localFilePath))
{
string localBuild = File.ReadAllText(localFilePath);
if (latestBuild == localBuild)
{
ShowNotification("Up-to-Date", "AI Studio is already up-to-date.");
return true;
}
}
ShowNotification("Update Available", "An update is available. Please update to the new version.");
return false;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error checking for updates: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
}
return false;
}
private void CreateShortcut(string shortcutName, string targetFileLocation)
{
try
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string startMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string desktopShortcutLocation = Path.Combine(desktopPath, $"{shortcutName}.lnk");
string startMenuShortcutLocation = Path.Combine(startMenuPath, $"{shortcutName}.lnk");
WshShell shell = new();
void ConfigureShortcut(string shortcutPath)
{
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.Description = "Shortcut for Lithicsoft AI Studio";
shortcut.TargetPath = targetFileLocation;
string? workingLocation = Path.GetDirectoryName(targetFileLocation);
if (workingLocation != null)
{
shortcut.WorkingDirectory = Path.GetFullPath(workingLocation);
}
shortcut.Save();
}
ConfigureShortcut(desktopShortcutLocation);
ConfigureShortcut(startMenuShortcutLocation);
}
catch (Exception ex)
{
Dispatcher.Invoke(() =>
{
MessageBox.Show($"Error creating shortcut: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Clipboard.SetText(ex.ToString());
});
}
}
private void ShowNotification(string title, string message)
{
MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Information);
}
private void Window_Closing(object sender, CancelEventArgs e)
{
if (isWorking)
{
MessageBox.Show("You cannot close the installer right now!", "Installing AI Studio", MessageBoxButton.OK, MessageBoxImage.Stop);
e.Cancel = true;
}
else if(MessageBox.Show("Do you want to exit Lithicsoft AI Studio Installer?", "Close Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
{
e.Cancel = true;
}
}
}