From 7230ed54fadf97311d4759391cf01b6ed2ecf049 Mon Sep 17 00:00:00 2001 From: YangSpring114 Date: Fri, 9 Feb 2024 20:52:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=87=A0=E4=B8=AABug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MinecraftLaunch.Test/Program.cs | 18 +++++++++++- .../Classes/Interfaces/IInstaller.cs | 2 -- .../Models/Download/DownloadRequest.cs | 4 +-- .../Components/Downloader/BatchDownloader.cs | 15 +++++----- .../Components/Fetcher/ModrinthFetcher.cs | 19 ++++--------- .../Components/Installer/ForgeInstaller.cs | 4 +-- .../Components/Installer/InstallerBase.cs | 6 +++- .../Components/Installer/VanlliaInstaller.cs | 14 +++++----- .../Components/Resolver/ModResolver.cs | 28 +++++++++---------- .../Components/Resolver/TomlResolver.cs | 6 +--- .../Extensions/DownloadEntryExtension.cs | 3 +- .../Extensions/DownloadExtension.cs | 1 + MinecraftLaunch/Extensions/StringExtension.cs | 5 ++-- MinecraftLaunch/MinecraftLaunch.csproj | 2 +- 14 files changed, 65 insertions(+), 62 deletions(-) diff --git a/MinecraftLaunch.Test/Program.cs b/MinecraftLaunch.Test/Program.cs index e4f8dac..97d4fbe 100644 --- a/MinecraftLaunch.Test/Program.cs +++ b/MinecraftLaunch.Test/Program.cs @@ -1,12 +1,28 @@ -using MinecraftLaunch.Components.Launcher; +using MinecraftLaunch; +using MinecraftLaunch.Components.Launcher; using MinecraftLaunch.Components.Resolver; using MinecraftLaunch.Classes.Models.Launch; using MinecraftLaunch.Components.Authenticator; using MinecraftLaunch.Components.Fetcher; +using MinecraftLaunch.Components.Installer; +using MinecraftLaunch.Extensions; + +MirrorDownloadManager.IsUseMirrorDownloadSource = true; var account = new OfflineAuthenticator("Yang114").Authenticate(); var resolver = new GameResolver("C:\\Users\\w\\Desktop\\总整包\\MC\\mc启动器\\LauncherX\\.minecraft"); +var installer = new VanlliaInstaller(resolver, "1.19.4", MirrorDownloadManager.Bmcl); +installer.ProgressChanged += (_, args) => { + Console.WriteLine($"{args.ProgressStatus} - {args.Progress * 100:0.00}%"); +}; + +installer.Completed += (_, _) => { + Console.WriteLine("Completed!"); +}; + +await installer.InstallAsync(); + var config = new LaunchConfig { Account = account, IsEnableIndependencyCore = true, diff --git a/MinecraftLaunch/Classes/Interfaces/IInstaller.cs b/MinecraftLaunch/Classes/Interfaces/IInstaller.cs index 9a9adb4..90a8067 100644 --- a/MinecraftLaunch/Classes/Interfaces/IInstaller.cs +++ b/MinecraftLaunch/Classes/Interfaces/IInstaller.cs @@ -5,8 +5,6 @@ namespace MinecraftLaunch.Classes.Interfaces; public interface IInstaller { ValueTask InstallAsync(); - void ReportProgress(double progress, string progressStatus, TaskStatus status); - event EventHandler Completed; event EventHandler ProgressChanged; diff --git a/MinecraftLaunch/Classes/Models/Download/DownloadRequest.cs b/MinecraftLaunch/Classes/Models/Download/DownloadRequest.cs index 6d2d374..dd6b559 100644 --- a/MinecraftLaunch/Classes/Models/Download/DownloadRequest.cs +++ b/MinecraftLaunch/Classes/Models/Download/DownloadRequest.cs @@ -12,9 +12,7 @@ public sealed record DownloadRequest { public required string Url { get; init; } - public required string Name { get; init; } - - public required string Path { get; init; } + public required FileInfo FileInfo { get; set; } public bool IsPartialContentSupported { get; set; } } \ No newline at end of file diff --git a/MinecraftLaunch/Components/Downloader/BatchDownloader.cs b/MinecraftLaunch/Components/Downloader/BatchDownloader.cs index dd66535..145c80a 100644 --- a/MinecraftLaunch/Components/Downloader/BatchDownloader.cs +++ b/MinecraftLaunch/Components/Downloader/BatchDownloader.cs @@ -132,8 +132,8 @@ public async ValueTask DownloadAsync() { // Clean incomplete files foreach (var item in _downloadItems) { - if (!item.IsCompleted && File.Exists(Path.Combine(item.Path, item.Name))) { - File.Delete(Path.Combine(item.Path, item.Name)); + if (!item.IsCompleted && item.FileInfo.Exists) { + item.FileInfo.Delete(); } } @@ -174,13 +174,12 @@ private async ValueTask DownloadItemAsync(DownloadRequest item, int retryT } // Make sure directory exists - if (Directory.Exists(item.Path)) { - Directory.CreateDirectory(item.Path); + if (!item.FileInfo.Directory.Exists) { + item.FileInfo.Directory.Create(); } - var filePath = Path.Combine(item.Path, item.Name); - if (!File.Exists(filePath)) { - using (File.Create(filePath)) { } + if (!item.FileInfo.Exists) { + using var r = item.FileInfo.Create(); } byte[] buffer = _bufferPool.Rent(BUFFER_SIZE); @@ -220,7 +219,7 @@ private async ValueTask DownloadItemAsync(DownloadRequest item, int retryT response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, _userCts.Token); await using var httpStream = await response.Content.ReadAsStreamAsync(); - await using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Write, FileShare.Write); + await using var fileStream = new FileStream(item.FileInfo.FullName, FileMode.Open, FileAccess.Write, FileShare.Write); fileStream.Position = chunkStart; int bytesRead; diff --git a/MinecraftLaunch/Components/Fetcher/ModrinthFetcher.cs b/MinecraftLaunch/Components/Fetcher/ModrinthFetcher.cs index a725d99..3c394d3 100644 --- a/MinecraftLaunch/Components/Fetcher/ModrinthFetcher.cs +++ b/MinecraftLaunch/Components/Fetcher/ModrinthFetcher.cs @@ -1,16 +1,11 @@ -using MinecraftLaunch.Classes.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; +using Flurl.Http; using System.Text; -using System.Threading.Tasks; -using Flurl.Http; -using MinecraftLaunch.Classes.Models.Download; -using MinecraftLaunch.Extensions; using System.Text.Json; -using System.Text.Json.Nodes; -using MinecraftLaunch.Classes.Enums; using MinecraftLaunch.Utilities; +using MinecraftLaunch.Extensions; +using MinecraftLaunch.Classes.Enums; +using MinecraftLaunch.Classes.Interfaces; +using MinecraftLaunch.Classes.Models.Download; namespace MinecraftLaunch.Components.Fetcher; public sealed class ModrinthFetcher : IFetcher> { @@ -29,8 +24,7 @@ public async ValueTask> FetchAsync() { public async ValueTask> SearchResourcesAsync( string searchFilter, string version = default, - ModrinthResourceType? resourceType = ModrinthResourceType.Mod) - { + ModrinthResourceType? resourceType = ModrinthResourceType.Mod) { var stringBuilder = new StringBuilder(_api); stringBuilder.Append($"search?query={searchFilter}"); @@ -54,7 +48,6 @@ public async ValueTask> SearchResourcesAsync( } var jNode = (await stringBuilder.ToString().GetStringAsync()).AsNode(); - return jNode?.Select("hits")?.GetEnumerable() .Deserialize>(JsonConverterUtil.DefaultJsonOptions); } diff --git a/MinecraftLaunch/Components/Installer/ForgeInstaller.cs b/MinecraftLaunch/Components/Installer/ForgeInstaller.cs index 481e327..71cb114 100644 --- a/MinecraftLaunch/Components/Installer/ForgeInstaller.cs +++ b/MinecraftLaunch/Components/Installer/ForgeInstaller.cs @@ -32,7 +32,7 @@ public override async ValueTask InstallAsync() { string packagePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(packageUrl)); - var request = packageUrl.ToDownloadRequest(packagePath); + var request = packageUrl.ToDownloadRequest(packagePath.ToFileInfo()); await request.DownloadAsync(x => { ReportProgress(x.ToPercentage().ToPercentage(0.0d, 0.15d), "Downloading Forge installation package", @@ -43,7 +43,7 @@ await request.DownloadAsync(x => { * Parse package */ ReportProgress(0.15d, "Start parse package", TaskStatus.Created); - var packageArchive = ZipFile.OpenRead(request.Path); + var packageArchive = ZipFile.OpenRead(request.FileInfo.FullName); var installProfile = packageArchive.GetEntry("install_profile.json") .ReadAsString() .AsNode(); diff --git a/MinecraftLaunch/Components/Installer/InstallerBase.cs b/MinecraftLaunch/Components/Installer/InstallerBase.cs index d048548..7e68789 100644 --- a/MinecraftLaunch/Components/Installer/InstallerBase.cs +++ b/MinecraftLaunch/Components/Installer/InstallerBase.cs @@ -10,7 +10,11 @@ public abstract class InstallerBase : IInstaller { public abstract ValueTask InstallAsync(); - public virtual void ReportProgress(double progress, string progressStatus, TaskStatus status) { + public void ReportCompleted() { + Completed?.Invoke(this, EventArgs.Empty); + } + + internal virtual void ReportProgress(double progress, string progressStatus, TaskStatus status) { ProgressChanged?.Invoke(this, new(status, progress, progressStatus)); } } \ No newline at end of file diff --git a/MinecraftLaunch/Components/Installer/VanlliaInstaller.cs b/MinecraftLaunch/Components/Installer/VanlliaInstaller.cs index d1c9259..9fc0c21 100644 --- a/MinecraftLaunch/Components/Installer/VanlliaInstaller.cs +++ b/MinecraftLaunch/Components/Installer/VanlliaInstaller.cs @@ -13,7 +13,6 @@ namespace MinecraftLaunch.Components.Installer; /// public sealed class VanlliaInstaller(IGameResolver gameFoloder, string gameId, MirrorDownloadSource source = default) : InstallerBase { private readonly string _gameId = gameId; - private ResourceChecker _resourceChecker; private readonly MirrorDownloadSource _source = source; private readonly IGameResolver _gameResolver = gameFoloder; @@ -39,7 +38,7 @@ public override async ValueTask InstallAsync() { var versionJsonFile = Path.Combine(_gameResolver.Root.FullName, "versions", _gameId, $"{_gameId}.json").ToFileInfo(); - if (!versionJsonFile.Directory.Exists) { + if (versionJsonFile.Directory is { Exists: false }) { versionJsonFile.Directory.Create(); } @@ -50,10 +49,10 @@ await File.WriteAllTextAsync(versionJsonFile.FullName, * Download dependent resources */ ReportProgress(0.45d, "Start downloading dependent resources", TaskStatus.WaitingToRun); - _resourceChecker = new(_gameResolver.GetGameEntity(_gameId)); - await _resourceChecker.CheckAsync(); + ResourceChecker resourceChecker = new(_gameResolver.GetGameEntity(_gameId)); + await resourceChecker.CheckAsync(); - await _resourceChecker.MissingResources.DownloadResourceEntrysAsync(_source, + await resourceChecker.MissingResources.DownloadResourceEntrysAsync(_source, x => { ReportProgress(x.ToPercentage().ToPercentage(0.45d, 0.95d), $"Downloading dependent resources:{x.CompletedCount}/{x.TotalCount}", @@ -62,15 +61,16 @@ await _resourceChecker.MissingResources.DownloadResourceEntrysAsync(_source, ReportProgress(1.0d, "Installation is complete", TaskStatus.Canceled); + ReportCompleted(); return true; } public static async ValueTask> EnumerableGameCoreAsync(MirrorDownloadSource source = default) { - string url = string.Empty; + string url; if (MirrorDownloadManager.IsUseMirrorDownloadSource && source is not null) { url = source.VersionManifestUrl; } else { - url = "http://launchermeta.mojang.com/mc/game/version_manifest.json"; + url = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; } var node = (await url.GetStringAsync()) diff --git a/MinecraftLaunch/Components/Resolver/ModResolver.cs b/MinecraftLaunch/Components/Resolver/ModResolver.cs index 314d055..ed3fd03 100644 --- a/MinecraftLaunch/Components/Resolver/ModResolver.cs +++ b/MinecraftLaunch/Components/Resolver/ModResolver.cs @@ -132,21 +132,21 @@ private void OfForgeModEntry(ZipArchive zipArchive, ref ModEntry entry) { private void OfQulitModEntry(ZipArchive zipArchive, ref ModEntry entry) { var zipEntry = zipArchive.GetEntry("quilt.mod.json"); using var stream = zipEntry.Open(); - using (var reader = new StreamReader(stream)) { - try { - var jsonNode = reader.ReadToEnd().AsNode(); - jsonNode = jsonNode["quilt_loader"]["metadata"]; + using var reader = new StreamReader(stream); + + try { + var jsonNode = reader.ReadToEnd().AsNode(); + jsonNode = jsonNode.Select("quilt_loader").Select("metadata"); - entry.DisplayName = jsonNode?.GetString("name"); - entry.Version = jsonNode?.GetString("version"); - entry.Description = jsonNode?.GetString("description"); - entry.Authors = jsonNode["authors"] - .GetEnumerable() - .ToImmutableArray(); - } - catch (Exception) { - entry.IsError = true; - } + entry.DisplayName = jsonNode?.GetString("name"); + entry.Version = jsonNode?.GetString("version"); + entry.Description = jsonNode?.GetString("description"); + entry.Authors = jsonNode["authors"] + .GetEnumerable() + .ToImmutableArray(); + } + catch (Exception) { + entry.IsError = true; } } } \ No newline at end of file diff --git a/MinecraftLaunch/Components/Resolver/TomlResolver.cs b/MinecraftLaunch/Components/Resolver/TomlResolver.cs index 61cf86d..8f687b4 100644 --- a/MinecraftLaunch/Components/Resolver/TomlResolver.cs +++ b/MinecraftLaunch/Components/Resolver/TomlResolver.cs @@ -18,11 +18,7 @@ public TomlResolver(string content) : this() { public string this[string key] => GetString(key); public string GetString(string key) { - if (_data.TryGetValue(key, out var value)) { - return value; - } - - return null; + return _data.GetValueOrDefault(key); } private void Parse(string content) { diff --git a/MinecraftLaunch/Extensions/DownloadEntryExtension.cs b/MinecraftLaunch/Extensions/DownloadEntryExtension.cs index 7f6d192..a90fb82 100644 --- a/MinecraftLaunch/Extensions/DownloadEntryExtension.cs +++ b/MinecraftLaunch/Extensions/DownloadEntryExtension.cs @@ -11,11 +11,10 @@ public static class DownloadEntryExtension { public static DownloadRequest ToDownloadRequest(this IDownloadEntry entry) { return new DownloadRequest { Url = entry.Url, - Path = entry.Path, Size = entry.Size, IsCompleted = false, DownloadedBytes = 0, - Name = Path.GetFileName(entry.Path), + FileInfo = entry.Path.ToFileInfo() }; } diff --git a/MinecraftLaunch/Extensions/DownloadExtension.cs b/MinecraftLaunch/Extensions/DownloadExtension.cs index 65a2fa5..0a863cf 100644 --- a/MinecraftLaunch/Extensions/DownloadExtension.cs +++ b/MinecraftLaunch/Extensions/DownloadExtension.cs @@ -50,6 +50,7 @@ public static ValueTask DownloadResourceEntryAsync(this .OfMirrorSource(source) .ToDownloadRequest(), 1)); + Console.WriteLine(downloadEntry.Path); return DefaultDownloader.DownloadAsync(); } diff --git a/MinecraftLaunch/Extensions/StringExtension.cs b/MinecraftLaunch/Extensions/StringExtension.cs index 33dc789..b51375c 100644 --- a/MinecraftLaunch/Extensions/StringExtension.cs +++ b/MinecraftLaunch/Extensions/StringExtension.cs @@ -26,13 +26,12 @@ public static string Replace(this string text, Dictionary keyVal return replacedText; } - public static DownloadRequest ToDownloadRequest(this string url, string path) { + public static DownloadRequest ToDownloadRequest(this string url, FileInfo path) { return new DownloadRequest { Url = url, - Path = path, + FileInfo = path, IsCompleted = false, DownloadedBytes = 0, - Name = Path.GetFileName(path), }; } } \ No newline at end of file diff --git a/MinecraftLaunch/MinecraftLaunch.csproj b/MinecraftLaunch/MinecraftLaunch.csproj index f45a629..e0739f6 100644 --- a/MinecraftLaunch/MinecraftLaunch.csproj +++ b/MinecraftLaunch/MinecraftLaunch.csproj @@ -1,6 +1,6 @@  - 3.0.0 + 3.0.1