Skip to content

Commit ffc993c

Browse files
committed
Add more initialization methods.
1 parent 04a596e commit ffc993c

File tree

12 files changed

+118
-52
lines changed

12 files changed

+118
-52
lines changed

src/Flarial.Launcher.Runtime/Client/FlarialClient.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Runtime.Serialization.Json;
54
using System.Security.Cryptography;
65
using System.Threading.Tasks;
76
using Flarial.Launcher.Runtime.Game;
@@ -15,20 +14,20 @@ namespace Flarial.Launcher.Runtime.Client;
1514
sealed class FlarialClientRelease : FlarialClient
1615
{
1716
protected override string Build => nameof(Release);
18-
protected override string Uri => "https://cdn.flarial.xyz/dll/latest.dll";
19-
protected override string Name => $"Flarial.Client.{nameof(Release)}.dll";
17+
protected override string FileName => $"Flarial.Client.{nameof(Release)}.dll";
2018
protected override string Identifer => "34F45015-6EB6-4213-ABEF-F2967818E628";
19+
protected override string DownloadUri => "https://cdn.flarial.xyz/dll/latest.dll";
2120
}
2221

2322
public abstract class FlarialClient
2423
{
2524
internal FlarialClient() { }
2625
static readonly JsonService<Dictionary<string, string>> s_json = JsonService<Dictionary<string, string>>.GetJson();
2726

28-
protected abstract string Uri { get; }
29-
protected abstract string Name { get; }
3027
protected abstract string Build { get; }
28+
protected abstract string FileName { get; }
3129
protected abstract string Identifer { get; }
30+
protected abstract string DownloadUri { get; }
3231

3332
public static readonly FlarialClient Release = new FlarialClientRelease();
3433

@@ -42,15 +41,15 @@ static FlarialClient? Current
4241
}
4342
}
4443

45-
public bool Launch(bool initialized)
44+
public bool Launch(bool? initialized)
4645
{
4746
if (Current is { } client)
4847
{
4948
if (!ReferenceEquals(this, client)) return false;
50-
return Minecraft.Current.Launch(false) is { };
49+
return Minecraft.Current.Launch(null) is { };
5150
}
5251

53-
if (Injector.Launch(initialized, new(Name)) is not { } processId)
52+
if (Injector.Launch(initialized, new(FileName)) is not { } processId)
5453
return false;
5554

5655
using NativeMutex mutex = new(Identifer);
@@ -75,7 +74,7 @@ async Task<string> GetLocalHashAsync() => await Task.Run(() =>
7574
{
7675
lock (_lock)
7776
{
78-
using var stream = File.OpenRead(Name);
77+
using var stream = File.OpenRead(FileName);
7978
var value = _algorithm.ComputeHash(stream);
8079
var @string = BitConverter.ToString(value);
8180
return @string.Replace("-", string.Empty);
@@ -93,10 +92,10 @@ public async Task<bool> DownloadAsync(Action<int> callback)
9392
if ((await localHashTask).Equals(await remoteHashTask, OrdinalIgnoreCase))
9493
return true;
9594

96-
try { File.Delete(Name); }
95+
try { File.Delete(FileName); }
9796
catch { return false; }
9897

99-
await HttpService.DownloadAsync(Uri, Name, callback);
98+
await HttpService.DownloadAsync(DownloadUri, FileName, callback);
10099
return true;
101100
}
102101
}

src/Flarial.Launcher.Runtime/Client/FlarialLauncher.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Reflection;
6-
using System.Runtime.Serialization.Json;
76
using System.Text;
87
using System.Threading.Tasks;
9-
using Flarial.Launcher.Runtime.Game;
108
using Flarial.Launcher.Runtime.Services;
119
using static System.Environment;
12-
using static System.Environment.SpecialFolder;
13-
using static Windows.Win32.PInvoke;
1410

1511
namespace Flarial.Launcher.Runtime.Client;
1612

src/Flarial.Launcher.Runtime/Game/Minecraft.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal Minecraft() { }
3030
internal static string Version { get { var _ = Package.Id.Version; return $"{_.Major}.{_.Minor}.{_.Build / 100}"; } }
3131

3232
protected abstract uint? Activate();
33-
public abstract uint? Launch(bool initialized);
33+
internal abstract uint? Launch(bool? initialized);
3434

3535
/*
3636
- The static overloads find windows & processes belonging to the game.

src/Flarial.Launcher.Runtime/Game/MinecraftGDK.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static MinecraftGDK()
5656
return GetProcessId();
5757
}
5858

59-
public override uint? Launch(bool initialized)
59+
internal override uint? Launch(bool? initialized)
6060
{
6161
/*
6262
- Unlike UWP builds, we are bootstrapping the game manually.
@@ -89,7 +89,7 @@ static MinecraftGDK()
8989
- Instead, wait for the game's window to be visible.
9090
*/
9191

92-
if (!IsPackaged)
92+
if (initialized is null || !IsPackaged)
9393
{
9494
while (process.Wait(1))
9595
{
@@ -109,7 +109,7 @@ static MinecraftGDK()
109109

110110
var handle = CreateEvent(null, true, false, null); try
111111
{
112-
using FileSystemWatcher watcher = new(CreateDirectory(s_path).FullName, initialized ? "*resource_init_lock" : "*menu_load_lock")
112+
using FileSystemWatcher watcher = new(CreateDirectory(s_path).FullName, (bool)initialized ? "*resource_init_lock" : "*menu_load_lock")
113113
{
114114
InternalBufferSize = 0,
115115
EnableRaisingEvents = true,

src/Flarial.Launcher.Runtime/Modding/Injector.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ static Injector()
2828
}
2929
}
3030

31-
public static uint? Launch(bool initialized, Library library)
31+
public static uint? Launch(bool? initialized, Library library)
3232
{
33-
if (!library.IsLoadable) throw new FileLoadException(null, library._path);
34-
if (Minecraft.Current.Launch(initialized) is not { } processId) return null;
35-
if (Open(PROCESS_ALL_ACCESS, processId) is not { } process) return null;
33+
if (!library.IsLoadable)
34+
throw new FileLoadException(null, library._path);
35+
36+
if (Minecraft.Current.Launch(initialized) is not { } processId)
37+
return null;
38+
39+
if (Open(PROCESS_ALL_ACCESS, processId) is not { } process)
40+
return null;
3641

3742
using (process)
3843
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Flarial.Launcher.Controls;
2+
3+
abstract class ContentItem<T>
4+
{
5+
internal abstract T Value { get; }
6+
protected abstract string String { get; }
7+
public override string ToString() => String;
8+
}

src/Flarial.Launcher/Controls/CustomDllPickerButton.cs renamed to src/Flarial.Launcher/Controls/CustomDllButton.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Flarial.Launcher.Controls;
77

8-
sealed class CustomDllPickerButton : Grid
8+
sealed class CustomDllButton : Grid
99
{
1010
readonly OpenFileDialog _dialog = new()
1111
{
@@ -46,7 +46,7 @@ void OnButtonClick(object sender, RoutedEventArgs args)
4646

4747
readonly ApplicationSettings _settings;
4848

49-
internal CustomDllPickerButton(ApplicationSettings settings)
49+
internal CustomDllButton(ApplicationSettings settings)
5050
{
5151
_settings = settings;
5252

src/Flarial.Launcher/Controls/DllSelectionBox.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Flarial.Launcher.Management;
23
using Windows.UI.Xaml;
34
using Windows.UI.Xaml.Controls;
@@ -14,13 +15,13 @@ sealed class DllSelectionBox : Grid
1415
HorizontalAlignment = HorizontalAlignment.Stretch
1516
};
1617

17-
readonly CustomDllPickerButton _button;
18+
readonly CustomDllButton _button;
1819

1920
void OnListBoxSelectionChanged(object sender, RoutedEventArgs args)
2021
{
2122
var listBox = (ListBox)sender;
2223

23-
var item = (DllItem)listBox.SelectedItem;
24+
var item = (ContentItem<DllSelection>)listBox.SelectedItem;
2425
_settings.DllSelection = item.Value;
2526

2627
var enabled = item.Value is DllSelection.Custom;
@@ -45,8 +46,8 @@ internal DllSelectionBox(ApplicationSettings settings)
4546
Children.Add(_listBox);
4647
Children.Add(_button);
4748

48-
_listBox.Items.Add(new ClientDllItem());
49-
_listBox.Items.Add(new CustomDllItem());
49+
_listBox.Items.Add(new ClientDll());
50+
_listBox.Items.Add(new CustomDll());
5051

5152
_listBox.SelectionChanged += OnListBoxSelectionChanged;
5253

@@ -56,20 +57,21 @@ internal DllSelectionBox(ApplicationSettings settings)
5657
_listBox.SelectedIndex = (int)_settings.DllSelection;
5758
}
5859

60+
[Obsolete("", true)]
5961
abstract class DllItem
6062
{
6163
protected abstract string String { get; }
6264
public override string ToString() => String;
6365
internal abstract DllSelection Value { get; }
6466
}
6567

66-
sealed class ClientDllItem : DllItem
68+
sealed class ClientDll : ContentItem<DllSelection>
6769
{
6870
protected override string String => "Use the client's DLL with the game.";
6971
internal override DllSelection Value => DllSelection.Client;
7072
}
7173

72-
sealed class CustomDllItem : DllItem
74+
sealed class CustomDll : ContentItem<DllSelection>
7375
{
7476
protected override string String => "Use a specified custom DLL with the game.";
7577
internal override DllSelection Value => DllSelection.Custom;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using Flarial.Launcher.Management;
3+
using Windows.UI.Xaml;
4+
using Windows.UI.Xaml.Controls;
5+
6+
namespace Flarial.Launcher.Controls;
7+
8+
sealed class InitializationTypeBox : Grid
9+
{
10+
readonly ApplicationSettings _settings;
11+
12+
readonly ListBox _listBox = new()
13+
{
14+
VerticalAlignment = VerticalAlignment.Stretch,
15+
HorizontalAlignment = HorizontalAlignment.Stretch
16+
};
17+
18+
void OnListBoxSelectionChanged(object sender, RoutedEventArgs args)
19+
{
20+
var item = ((ListBox)sender).SelectedItem;
21+
_settings.WaitForInitialization = ((ContentItem<bool?>)item).Value;
22+
}
23+
24+
internal InitializationTypeBox(ApplicationSettings settings)
25+
{
26+
_settings = settings;
27+
28+
RowDefinitions.Add(new());
29+
RowDefinitions.Add(new() { Height = GridLength.Auto });
30+
31+
SetRow(_listBox, 0);
32+
SetColumn(_listBox, 0);
33+
34+
Children.Add(_listBox);
35+
36+
_listBox.Items.Add(new WaitForWindow());
37+
_listBox.Items.Add(new WaitForTitleScreen());
38+
_listBox.Items.Add(new WaitForGlobalResources());
39+
40+
_listBox.SelectionChanged += OnListBoxSelectionChanged;
41+
_listBox.SelectedIndex = _settings.WaitForInitialization switch { null => 0, false => 1, true => 2 };
42+
43+
_listBox.SetValue(VirtualizingStackPanel.IsVirtualizingProperty, true);
44+
VirtualizingStackPanel.SetVirtualizationMode(_listBox, VirtualizationMode.Recycling);
45+
}
46+
47+
sealed class WaitForWindow : ContentItem<bool?>
48+
{
49+
internal override bool? Value => null;
50+
protected override string String => "Wait for window, unsafe & fast.";
51+
}
52+
53+
sealed class WaitForTitleScreen : ContentItem<bool?>
54+
{
55+
internal override bool? Value => false;
56+
protected override string String => "Wait for title screen, risky & moderate.";
57+
}
58+
59+
sealed class WaitForGlobalResources : ContentItem<bool?>
60+
{
61+
internal override bool? Value => true;
62+
protected override string String => "Wait for global resources, safe & slow.";
63+
}
64+
65+
}

src/Flarial.Launcher/Management/ApplicationSettings.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
namespace Flarial.Launcher.Management;
77

8-
enum DllSelection { Client, Custom }
8+
enum DllSelection
9+
{
10+
Client,
11+
Custom
12+
}
913

1014
[DataContract]
1115
sealed class ApplicationSettings
@@ -20,7 +24,7 @@ sealed class ApplicationSettings
2024
internal string CustomDllPath { get; set; } = string.Empty;
2125

2226
[DataMember]
23-
internal bool WaitForInitialization { get; set; } = true;
27+
internal bool? WaitForInitialization { get; set; } = true;
2428

2529
[OnDeserializing]
2630
void OnDeserializing(StreamingContext context)

0 commit comments

Comments
 (0)