Skip to content

Commit

Permalink
Merge pull request #290 from Xcube-Studio/refactor/quick-launch-service
Browse files Browse the repository at this point in the history
Refactor/quick launch service
  • Loading branch information
natsurainko authored Dec 2, 2024
2 parents 86dd722 + a724b29 commit f146605
Show file tree
Hide file tree
Showing 21 changed files with 499 additions and 319 deletions.
2 changes: 1 addition & 1 deletion FluentLauncher.Localization
13 changes: 5 additions & 8 deletions Natsurainko.FluentLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using Natsurainko.FluentLauncher.Services.Launch;
using Natsurainko.FluentLauncher.Services.UI;
using Natsurainko.FluentLauncher.Services.UI.Messaging;
using Natsurainko.FluentLauncher.Utils.Extensions;
Expand Down Expand Up @@ -42,6 +43,7 @@ void ConfigureApplication()

App.GetService<MessengerService>().SubscribeEvents();
App.GetService<AppearanceService>().RegisterApp(this);
App.GetService<QuickLaunchService>().CleanRemovedJumpListItem();

// Global exception handler
UnhandledException += (_, e) =>
Expand All @@ -58,6 +60,9 @@ protected override async void OnLaunched(LaunchActivatedEventArgs args)
mainInstance.Activated += (object? sender, AppActivationArguments e) =>
{
DispatcherQueue.TryEnqueue(() => MainWindow?.Activate());

if (e.Data is Windows.ApplicationModel.Activation.LaunchActivatedEventArgs redirectedArgs)
App.GetService<QuickLaunchService>().LaunchFromActivatedEventArgs(redirectedArgs.Arguments.Split(' '));
};

if (!mainInstance.IsCurrent)
Expand All @@ -70,14 +75,6 @@ protected override async void OnLaunched(LaunchActivatedEventArgs args)
return;
}

string[] cmdargs = Environment.GetCommandLineArgs();

if (cmdargs.Length > 1 && cmdargs[1].Equals("/quick-launch"))
{
//App.GetService<JumpListService>().LaunchFromJumpListAsync(cmdargs[2]);
return;
}

try
{
IWindowService mainWindowService = App.GetService<IActivationService>().ActivateWindow("MainWindow");
Expand Down
13 changes: 2 additions & 11 deletions Natsurainko.FluentLauncher/FLSerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
using Natsurainko.FluentLauncher.Models.UI;
using Nrk.FluentCore.Authentication;
using Nrk.FluentCore.GameManagement.Installer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Natsurainko.FluentLauncher;

Expand Down Expand Up @@ -38,15 +33,11 @@ namespace Natsurainko.FluentLauncher;
[JsonSerializable(typeof(string[]))]
[JsonSerializable(typeof(Windows.UI.Color))]
[JsonSerializable(typeof(WinUIEx.WindowState))]
internal partial class FLSerializerContext : JsonSerializerContext
{
}
internal partial class FLSerializerContext : JsonSerializerContext { }

[JsonSerializable(typeof(InstanceConfig))]
[JsonSourceGenerationOptions(
IncludeFields = false,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
internal partial class InstanceConfigSerializerContext : JsonSerializerContext
{
}
internal partial class InstanceConfigSerializerContext : JsonSerializerContext { }
2 changes: 1 addition & 1 deletion Natsurainko.FluentLauncher/Models/Launch/InstanceConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public IEnumerable<string> VmParameters
public DateTime? LastLaunchTime
{
get => lastLaunchTime;
set => App.DispatcherQueue.TryEnqueue(() => SetProperty(ref lastLaunchTime, value));
set => SetProperty(ref lastLaunchTime, value);
}

protected override void OnPropertyChanged(PropertyChangedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<PackageReference Include="NbtToolkit" Version="0.1.2-beta" />
<PackageReference Include="PInvoke.User32" Version="0.7.124" />
<PackageReference Include="ReverseMarkdown" Version="4.6.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Management" Version="8.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
Expand Down
31 changes: 30 additions & 1 deletion Natsurainko.FluentLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Natsurainko.FluentLauncher.Services.UI.Messaging;
using Nrk.FluentCore.Resources;
using System;
using System.CommandLine;
using ViewModels = Natsurainko.FluentLauncher.ViewModels;
using Views = Natsurainko.FluentLauncher.Views;

Expand Down Expand Up @@ -106,6 +107,7 @@
services.AddSingleton<CacheSkinService>();
services.AddSingleton<CacheInterfaceService>();
//services.AddSingleton<JumpListService>();
services.AddSingleton<QuickLaunchService>();
services.AddSingleton<SearchProviderService>();
services.AddSingleton<InstanceConfigService>();

Expand All @@ -122,9 +124,36 @@
var app = builder.Build();
AppHost = app.Host;

await app.RunAsync();
await BuildRootCommand(app).InvokeAsync(args);
//await app.RunAsync();

public partial class Program
{
public static IHost AppHost { get; private set; } = null!;

public static Option<string> MinecraftFolderOption { get; } = new (name: "--minecraftFolder") { IsRequired = true };

public static Option<string> InstanceIdOption { get; } = new(name: "--instanceId") { IsRequired = true };

public static RootCommand BuildRootCommand(WinUIApplication application)
{
var rootCommand = new RootCommand();
rootCommand.SetHandler(async () => await application.RunAsync());
rootCommand.Add(BuildSubCommand());

return rootCommand;
}

public static Command BuildSubCommand()
{
var quickLaunchCommand = new Command("quickLaunch");
quickLaunchCommand.AddOption(MinecraftFolderOption);
quickLaunchCommand.AddOption(InstanceIdOption);

quickLaunchCommand.SetHandler(async (folder, instanceId) =>
await AppHost.Services.GetService<QuickLaunchService>()!.LaunchFromArguments(folder, instanceId),
MinecraftFolderOption, InstanceIdOption);

return quickLaunchCommand;
}
}
9 changes: 6 additions & 3 deletions Natsurainko.FluentLauncher/Services/Launch/LaunchService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Natsurainko.FluentLauncher.Models.Launch;
using CommunityToolkit.Mvvm.Messaging;
using Natsurainko.FluentLauncher.Models.Launch;
using Natsurainko.FluentLauncher.Services.Accounts;
using Natsurainko.FluentLauncher.Services.Network;
using Natsurainko.FluentLauncher.Services.Settings;
using Natsurainko.FluentLauncher.Services.UI.Messaging;
using Natsurainko.FluentLauncher.Utils;
using Natsurainko.FluentLauncher.Utils.Extensions;
using Natsurainko.FluentLauncher.ViewModels.Common;
Expand Down Expand Up @@ -64,7 +66,7 @@ public LaunchService(

public void LaunchFromUI(MinecraftInstance instance)
{
var viewModel = new LaunchTaskViewModel(instance);
var viewModel = new LaunchTaskViewModel(instance, this);
viewModel.PropertyChanged += (_, e) =>
{
if (e.PropertyName == "TaskState")
Expand All @@ -74,6 +76,7 @@ public void LaunchFromUI(MinecraftInstance instance)
App.DispatcherQueue.TryEnqueue(() => LaunchTasks.Insert(0, viewModel));

viewModel.Start();
WeakReferenceMessenger.Default.Send(new GlobalNavigationMessage("Tasks/Launch"));
}

public async Task<MinecraftProcess> LaunchAsync(
Expand All @@ -90,7 +93,7 @@ public async Task<MinecraftProcess> LaunchAsync(
{
InstanceConfig config = instance.GetConfig();
config.LastLaunchTime = DateTime.Now;
//App.DispatcherQueue.TryEnqueue(() => config.LastLaunchTime = DateTime.Now);
await App.GetService<QuickLaunchService>().AddLatestMinecraftInstance(instance);

var preCheckData = await PreCheckLaunchNeeds(instance, config, cancellationToken, progress);

Expand Down
Loading

0 comments on commit f146605

Please sign in to comment.