-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallationDialog.axaml.cs
More file actions
438 lines (372 loc) · 16.8 KB
/
InstallationDialog.axaml.cs
File metadata and controls
438 lines (372 loc) · 16.8 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using JustLauncher.Services;
namespace JustLauncher;
public partial class InstallationDialog : UserControl
{
public Installation? Result { get; private set; }
public bool DeleteRequested { get; private set; }
private readonly MinecraftService _minecraftService = new();
private Installation? _existingInstallation;
private readonly FabricService _fabricService = new();
private readonly ForgeService _forgeService = new();
private readonly ModManagerService _modManager = new();
public InstallationDialog()
{
InitializeComponent();
var textBox = this.FindControl<TextBox>("GameDirectoryTextBox");
if (textBox != null) textBox.Text = PlatformManager.GetMinecraftDirectory();
var nameBox = this.FindControl<TextBox>("NameTextBox");
if (nameBox != null)
{
nameBox.TextChanged += async (s, e) =>
{
var name = nameBox.Text;
var settings = await ConfigManager.LoadSettingsAsync();
if (settings.UseSeparateGameDir && !string.IsNullOrWhiteSpace(name))
{
var dirBox = this.FindControl<TextBox>("GameDirectoryTextBox");
if (dirBox != null)
{
var safeName = string.Join("_", name.Split(System.IO.Path.GetInvalidFileNameChars()));
var basePath = PlatformManager.GetMinecraftDirectory();
if (string.IsNullOrWhiteSpace(dirBox.Text) || dirBox.Text.StartsWith(basePath))
{
Dispatcher.UIThread.Post(() =>
{
dirBox.Text = System.IO.Path.Combine(basePath, "instances", safeName);
});
}
}
}
};
}
var memorySlider = this.FindControl<Slider>("MemorySlider");
if (memorySlider != null)
{
memorySlider.PropertyChanged += (s, e) => {
if (e.Property == Avalonia.Controls.Primitives.RangeBase.ValueProperty)
{
var memoryLabel = this.FindControl<TextBlock>("MemoryLabel");
if (memoryLabel != null) memoryLabel.Text = $"{(int)memorySlider.Value} GB";
var argsBox = this.FindControl<TextBox>("JavaArgsTextBox");
if (argsBox != null)
{
int memoryGb = (int)memorySlider.Value;
if (memoryGb > 0)
argsBox.Text = $"-Xmx{memoryGb}G -Xms{memoryGb}G";
else
argsBox.Text = "";
}
}
};
}
_ = LoadDataAsync();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
var closeBtn = this.FindControl<Button>("CloseButton");
if (closeBtn != null) closeBtn.Click += (s, e) => OverlayService.Close(null);
var cancelBtn = this.FindControl<Button>("CancelButton");
if (cancelBtn != null) cancelBtn.Click += (s, e) => OverlayService.Close(null);
var createBtn = this.FindControl<Button>("CreateButton");
if (createBtn != null) createBtn.Click += CreateButton_Click;
var removeBtn = this.FindControl<Button>("RemoveButton");
if (removeBtn != null) removeBtn.Click += RemoveButton_Click;
var browseJavaBtn = this.FindControl<Button>("BrowseJavaButton");
if (browseJavaBtn != null) browseJavaBtn.Click += BrowseJavaButton_Click;
var memLow = this.FindControl<Button>("MemLowBtn");
if (memLow != null) memLow.Click += (s, e) => SetMemory(2);
var memMed = this.FindControl<Button>("MemMedBtn");
if (memMed != null) memMed.Click += (s, e) => SetMemory(4);
var memHigh = this.FindControl<Button>("MemHighBtn");
if (memHigh != null) memHigh.Click += (s, e) => SetMemory(8);
var loaderCombo = this.FindControl<ComboBox>("ModLoaderComboBox");
if (loaderCombo != null) loaderCombo.SelectionChanged += LoaderCombo_SelectionChanged;
var versionCombo = this.FindControl<ComboBox>("VersionComboBox");
if (versionCombo != null) versionCombo.SelectionChanged += async (s, e) => await UpdateLoaderVersions();
}
private void SetMemory(double gb)
{
var slider = this.FindControl<Slider>("MemorySlider");
if (slider != null) slider.Value = gb;
}
private async void BrowseJavaButton_Click(object? sender, RoutedEventArgs e)
{
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel == null) return;
var result = await topLevel.StorageProvider.OpenFilePickerAsync(new Avalonia.Platform.Storage.FilePickerOpenOptions
{
Title = "Select Java Executable",
AllowMultiple = false
});
if (result != null && result.Count > 0)
{
var path = result[0].Path.LocalPath;
var combo = this.FindControl<ComboBox>("JavaVersionComboBox");
if (combo != null)
{
var items = new List<object>();
if (combo.ItemsSource is IEnumerable<object> existing) items.AddRange(existing);
var existingItem = items.OfType<JavaInfo>().FirstOrDefault(j => j.Path == path);
if (existingItem != null)
{
combo.SelectedItem = existingItem;
}
else
{
var info = new JavaInfo { Path = path, Version = "Custom", MajorVersion = 0, IsJre = true };
items.Add(info);
combo.ItemsSource = items;
combo.SelectedItem = info;
}
}
}
}
private async Task LoadDataAsync()
{
await LoadVersionsAsync();
await LoadJavaVersionsAsync();
await UpdateLoaderVersions();
}
private async Task LoadJavaVersionsAsync()
{
try
{
var combo = this.FindControl<ComboBox>("JavaVersionComboBox");
if (combo == null) return;
var manager = new JavaManager();
var versions = await manager.GetInstalledJavaVersionsAsync();
var items = new List<object>();
items.Add("Use Global Setting");
foreach (var v in versions) items.Add(v);
combo.ItemsSource = items;
if (_existingInstallation != null && !string.IsNullOrEmpty(_existingInstallation.JavaPath))
{
var match = versions.FirstOrDefault(v => v.Path == _existingInstallation.JavaPath);
if (match != null)
{
combo.SelectedItem = match;
}
else
{
var custom = new JavaInfo { Path = _existingInstallation.JavaPath, Version = "Custom", MajorVersion = 0, DisplayName = $"Custom ({_existingInstallation.JavaPath})" };
items.Add(custom);
combo.ItemsSource = items;
combo.SelectedItem = custom;
}
}
else
{
combo.SelectedIndex = 0;
}
}
catch (Exception ex)
{
Console.WriteLine($"[InstallationDialog] Error loading Java versions: {ex}");
}
}
private async Task LoadVersionsAsync()
{
var versionCombo = this.FindControl<ComboBox>("VersionComboBox");
if (versionCombo == null) return;
try
{
var manifest = await _minecraftService.GetVersionManifestAsync();
var versions = manifest.Versions.Select(v => v.Id).ToList();
versionCombo.ItemsSource = versions;
if (_existingInstallation != null && !string.IsNullOrEmpty(_existingInstallation.Version))
{
int index = versions.IndexOf(_existingInstallation.Version);
versionCombo.SelectedIndex = index >= 0 ? index : 0;
}
else
{
versionCombo.SelectedIndex = 0;
}
}
catch
{
versionCombo.ItemsSource = new[] { "1.21.1", "1.20.1", "1.19.4" };
versionCombo.SelectedIndex = 0;
}
}
public InstallationDialog(Installation existing) : this()
{
_existingInstallation = existing;
var nameBox = this.FindControl<TextBox>("NameTextBox");
var dirBox = this.FindControl<TextBox>("GameDirectoryTextBox");
var argsBox = this.FindControl<TextBox>("JavaArgsTextBox");
var title = this.FindControl<TextBlock>("DialogTitle");
var button = this.FindControl<Button>("CreateButton");
if (nameBox != null) nameBox.Text = existing.Name;
if (dirBox != null) dirBox.Text = existing.GameDirectory;
if (argsBox != null) argsBox.Text = existing.JavaArgs;
if (title != null) title.Text = "Edit Installation";
if (button != null) button.Content = "SAVE CHANGES";
var memorySlider = this.FindControl<Slider>("MemorySlider");
if (memorySlider != null) memorySlider.Value = existing.MemoryAllocationGb;
var removeBtn = this.FindControl<Button>("RemoveButton");
if (removeBtn != null) removeBtn.IsVisible = true;
var loaderCombo = this.FindControl<ComboBox>("ModLoaderComboBox");
if (loaderCombo != null)
{
loaderCombo.SelectedIndex = existing.LoaderType switch
{
ModLoaderType.Fabric => 1,
ModLoaderType.Forge => 2,
_ => 0
};
}
}
private void RemoveButton_Click(object? sender, RoutedEventArgs e)
{
DeleteRequested = true;
OverlayService.Close(this);
}
private async void CreateButton_Click(object? sender, RoutedEventArgs e)
{
var nameBox = this.FindControl<TextBox>("NameTextBox");
var versionCombo = this.FindControl<ComboBox>("VersionComboBox");
var dirBox = this.FindControl<TextBox>("GameDirectoryTextBox");
var argsBox = this.FindControl<TextBox>("JavaArgsTextBox");
var memorySlider = this.FindControl<Slider>("MemorySlider");
Result = new Installation
{
Id = _existingInstallation?.Id ?? Guid.NewGuid().ToString(),
Name = string.IsNullOrWhiteSpace(nameBox?.Text) ? "New Installation" : nameBox.Text,
Version = versionCombo?.SelectedItem?.ToString() ?? "1.21.1",
BaseVersion = versionCombo?.SelectedItem?.ToString() ?? "1.21.1",
GameDirectory = dirBox?.Text ?? PlatformManager.GetMinecraftDirectory(),
JavaArgs = argsBox?.Text ?? "",
MemoryAllocationGb = memorySlider?.Value ?? 0,
Icon = "grass_block",
JavaPath = GetSelectedJavaPath(),
LoaderType = GetSelectedLoaderType(),
ModLoaderVersion = GetSelectedLoaderVersion()
};
if (Result.LoaderType == ModLoaderType.Forge && !string.IsNullOrEmpty(Result.ModLoaderVersion))
{
var btn = this.FindControl<Button>("CreateButton");
if (btn != null)
{
btn.IsEnabled = false;
btn.Content = "Installing Forge...";
}
if (!Directory.Exists(Result.GameDirectory)) Directory.CreateDirectory(Result.GameDirectory);
var versionStr = Result.ModLoaderVersion;
if (versionStr.Contains("(") && versionStr.Contains(")"))
{
var start = versionStr.IndexOf('(') + 1;
var end = versionStr.IndexOf(')');
Result.ModLoaderVersion = versionStr.Substring(start, end - start);
}
await _forgeService.InstallForgeAsync(Result.Version, Result.ModLoaderVersion, Result.GameDirectory);
}
OverlayService.Close(this);
}
private string GetSelectedJavaPath()
{
var combo = this.FindControl<ComboBox>("JavaVersionComboBox");
if (combo?.SelectedItem is JavaInfo info) return info.Path;
return "";
}
private ModLoaderType GetSelectedLoaderType()
{
var combo = this.FindControl<ComboBox>("ModLoaderComboBox");
var item = combo?.SelectedItem as ComboBoxItem;
var tag = item?.Tag?.ToString();
return tag switch
{
"Fabric" => ModLoaderType.Fabric,
"Forge" => ModLoaderType.Forge,
_ => ModLoaderType.Vanilla
};
}
private string? GetSelectedLoaderVersion()
{
if (GetSelectedLoaderType() == ModLoaderType.Vanilla) return null;
var combo = this.FindControl<ComboBox>("ModLoaderVersionComboBox");
return combo?.SelectedItem?.ToString();
}
private async void LoaderCombo_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
await UpdateLoaderVersions();
}
private async Task UpdateLoaderVersions()
{
var loaderCombo = this.FindControl<ComboBox>("ModLoaderComboBox");
var versionCombo = this.FindControl<ComboBox>("VersionComboBox");
var loaderVersionPanel = this.FindControl<StackPanel>("ModLoaderVersionPanel");
var loaderVersionCombo = this.FindControl<ComboBox>("ModLoaderVersionComboBox");
if (loaderCombo == null || versionCombo == null || loaderVersionPanel == null || loaderVersionCombo == null) return;
var type = GetSelectedLoaderType();
loaderVersionPanel.IsVisible = type != ModLoaderType.Vanilla;
if (type == ModLoaderType.Fabric)
{
var gameVersion = versionCombo.SelectedItem?.ToString();
if (string.IsNullOrEmpty(gameVersion)) return;
loaderVersionCombo.ItemsSource = new[] { "Loading..." };
loaderVersionCombo.SelectedIndex = 0;
loaderVersionCombo.IsEnabled = false;
try
{
var versions = await _fabricService.GetLoaderVersionsAsync(gameVersion);
var items = versions.Select(v => v.Loader.Version).ToList();
loaderVersionCombo.ItemsSource = items;
loaderVersionCombo.IsEnabled = true;
if (_existingInstallation != null && _existingInstallation.LoaderType == ModLoaderType.Fabric && !string.IsNullOrEmpty(_existingInstallation.ModLoaderVersion))
{
var existing = items.FirstOrDefault(v => v == _existingInstallation.ModLoaderVersion);
if (existing != null) loaderVersionCombo.SelectedItem = existing;
else if (items.Any()) loaderVersionCombo.SelectedIndex = 0;
}
else if (items.Any())
{
loaderVersionCombo.SelectedIndex = 0;
}
}
catch
{
loaderVersionCombo.ItemsSource = new[] { "Error loading versions" };
}
}
else if (type == ModLoaderType.Forge)
{
var gameVersion = versionCombo.SelectedItem?.ToString();
if (string.IsNullOrEmpty(gameVersion)) return;
loaderVersionCombo.ItemsSource = new[] { "Loading..." };
loaderVersionCombo.SelectedIndex = 0;
loaderVersionCombo.IsEnabled = false;
try
{
var versions = await _forgeService.GetForgeVersionsAsync(gameVersion);
loaderVersionCombo.ItemsSource = versions;
loaderVersionCombo.IsEnabled = true;
if (_existingInstallation != null && _existingInstallation.LoaderType == ModLoaderType.Forge && !string.IsNullOrEmpty(_existingInstallation.ModLoaderVersion))
{
var existing = versions.FirstOrDefault(v => v.ForgeVersionStr == _existingInstallation.ModLoaderVersion);
if (existing != null) loaderVersionCombo.SelectedItem = existing;
else if (versions.Any()) loaderVersionCombo.SelectedIndex = 0;
}
else if (versions.Any())
{
loaderVersionCombo.SelectedIndex = 0;
}
}
catch
{
loaderVersionCombo.ItemsSource = new[] { "Error loading versions" };
}
}
}
}