-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
199 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,101 @@ | ||
using MinecraftLaunch.Classes.Enums; | ||
using Avalonia.Controls.Notifications; | ||
using CommunityToolkit.Mvvm.Messaging; | ||
using MinecraftLaunch.Classes.Models.Download; | ||
using MinecraftLaunch.Classes.Models.Install; | ||
using MinecraftLaunch.Components.Installer; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using WonderLab.Infrastructure.Models; | ||
using WonderLab.Infrastructure.Models.Messaging; | ||
using WonderLab.Services.Launch; | ||
using WonderLab.ViewModels.Tasks; | ||
|
||
namespace WonderLab.Services.Download; | ||
|
||
public sealed class DownloadService { | ||
private readonly TaskService _taskService; | ||
private readonly GameService _gameService; | ||
private readonly ConfigService _configService; | ||
|
||
public DownloadService(ConfigService configService, GameService gameService) { | ||
public DownloadService(ConfigService configService, GameService gameService, TaskService taskService) { | ||
_taskService = taskService; | ||
_gameService = gameService; | ||
_configService = configService; | ||
} | ||
|
||
public async Task InstallMinecraftAsync(LoaderType type, string gameId, string customGameId = default) { | ||
public Task InstallMinecraftTaskAsync(string gameId, bool isInstallOptifine = false, object installEntry = default, string customGameId = default) { | ||
var id = string.IsNullOrEmpty(customGameId) ? gameId : customGameId; | ||
InstallMinecraftTaskViewModel task = new() { | ||
JobName = $"游戏实例 {id} 的安装任务" | ||
}; | ||
|
||
task.Completed += (_, _) => { | ||
WeakReferenceMessenger.Default.Send(new NotificationMessage($"游戏实例 {id} 安装完成!", NotificationType.Information)); | ||
|
||
_gameService.RefreshGames(); | ||
}; | ||
|
||
_taskService.QueueJob(task); | ||
return Task.Run(async () => await InstallMinecraftAsync(gameId, task, task.TaskCancellationToken, customGameId, installEntry, isInstallOptifine)); | ||
} | ||
|
||
public async Task InstallMinecraftAsync( | ||
string gameId, | ||
IProgress<TaskProgress> progress, | ||
CancellationToken cancellationToken, | ||
string customGameId = default, | ||
object installEntry = default, | ||
bool isInstallOptifine = false) { | ||
var dc = new DownloaderConfiguration { | ||
IsEnableFragmentedDownload = true, | ||
MaxThread = _configService.Entries.ThreadCount | ||
}; | ||
//CompositionInstaller | ||
//var installer = type switch { | ||
// LoaderType.Any => new VanlliaInstaller(_gameService.GameResolver, gameId, dc), | ||
// LoaderType.Forge => new ForgeInstaller(_gameService.GameResolver.GetGameEntity()) | ||
//}; | ||
|
||
try { | ||
var mainInstaller = new VanlliaInstaller(_gameService.GameResolver, gameId, dc); | ||
InstallerBase installer = installEntry is null | ||
? mainInstaller | ||
: installEntry switch { | ||
ForgeInstallEntry => new CompositionInstaller(mainInstaller, new ForgeInstaller((ForgeInstallEntry)installEntry, _configService.Entries.ActiveJava.JavaPath, customGameId, dc), customGameId), | ||
FabricBuildEntry => new CompositionInstaller(mainInstaller, new FabricInstaller((FabricBuildEntry)installEntry, customGameId, dc), customGameId), | ||
QuiltBuildEntry => new CompositionInstaller(mainInstaller, new QuiltInstaller((QuiltBuildEntry)installEntry, customGameId, dc), customGameId), | ||
_ => throw new NotSupportedException() | ||
}; | ||
|
||
progress.Report(new(1, 1d)); | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
if (installer is CompositionInstaller composition) { | ||
bool isSub1Installed = false; | ||
composition.SubInstallerCompleted += (_, _) => { | ||
progress.Report(new(2, 1d)); | ||
isSub1Installed = true; | ||
}; | ||
|
||
composition.ProgressChanged += (_, arg) => { | ||
if (isSub1Installed) { | ||
progress.Report(new(3, arg.Progress, Speed: arg.Speed)); | ||
} else { | ||
progress.Report(new(2, arg.Progress, Speed: arg.Speed)); | ||
} | ||
}; | ||
|
||
composition.Completed += (_, arg) => progress.Report(new(4, 1d)); | ||
} else { | ||
installer.ProgressChanged += (_, arg) => { | ||
progress.Report(new(2, arg.Progress, Speed: arg.Speed)); | ||
}; | ||
|
||
installer.Completed += (_, arg) => { | ||
progress.Report(new(3, 1d)); | ||
progress.Report(new(4, 1d)); | ||
}; | ||
} | ||
|
||
await installer.InstallAsync(cancellationToken); | ||
} catch (Exception ex) { | ||
progress.Report(new(-1, 1d, ex)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
WonderLab/ViewModels/Tasks/InstallMinecraftTaskViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Avalonia.Threading; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using MinecraftLaunch.Components.Installer; | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using WonderLab.Infrastructure.Interfaces; | ||
using WonderLab.Infrastructure.Models; | ||
|
||
namespace WonderLab.ViewModels.Tasks; | ||
|
||
public sealed partial class InstallMinecraftTaskViewModel : ObservableObject, ITaskJob<TaskProgress> { | ||
public readonly CancellationTokenSource InstallCancellationTokenSource = new(); | ||
|
||
public double MaxProgress => 1d; | ||
public string ProgressText => Progress.ToString("P2"); | ||
|
||
public ImmutableArray<TaskStep> TaskSteps { get; } = [ | ||
new TaskStep { StepName = "Check if the specified id exists", TaskStatus = TaskStatus.Running }, | ||
new TaskStep { StepName = "Download minecraft" }, | ||
new TaskStep { StepName = "Install mod loader" }, | ||
]; | ||
|
||
public event EventHandler Completed; | ||
|
||
public Exception Exception { get; set; } | ||
public required string JobName { get; set; } | ||
|
||
[ObservableProperty] private TaskStatus _taskStatus; | ||
[ObservableProperty] private bool _isIndeterminate = true; | ||
|
||
[ObservableProperty] | ||
[NotifyPropertyChangedFor(nameof(ProgressText))] | ||
private double _progress; | ||
|
||
public CancellationToken TaskCancellationToken => InstallCancellationTokenSource.Token; | ||
|
||
public void Report(TaskProgress value) => Dispatcher.UIThread.InvokeAsync(() => { | ||
switch (value.Step) { | ||
case 1: | ||
TaskStatus = TaskStatus.Running; | ||
IsIndeterminate = false; | ||
TaskSteps[0].Progress = value.Progress; | ||
break; | ||
case 2: | ||
TaskSteps[0].TaskStatus = TaskStatus.RanToCompletion; | ||
TaskSteps[1].TaskStatus = TaskStatus.Running; | ||
|
||
TaskSteps[1].Speed = value.Speed; | ||
TaskSteps[1].Progress = value.Progress; | ||
break; | ||
case 3: | ||
TaskSteps[1].TaskStatus = TaskStatus.RanToCompletion; | ||
TaskSteps[2].TaskStatus = TaskStatus.Running; | ||
|
||
TaskSteps[2].Speed = value.Speed; | ||
TaskSteps[2].Progress = value.Progress; | ||
break; | ||
case 4: | ||
Completed?.Invoke(this, EventArgs.Empty); | ||
break; | ||
} | ||
|
||
Progress = TaskSteps.Select(x => x.Progress).Sum() / (double)TaskSteps.Length; | ||
}, DispatcherPriority.ApplicationIdle); | ||
|
||
[RelayCommand] | ||
private void CancelTask() { | ||
InstallCancellationTokenSource.Cancel(); | ||
} | ||
} |
Submodule MinecraftLaunch
updated
8 files