Skip to content

Commit

Permalink
修复了 Forge 安装器的几个恶性 Bug,更新版本号
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Apr 17, 2024
1 parent 0794af9 commit 8796a05
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 37 deletions.
32 changes: 27 additions & 5 deletions MinecraftLaunch.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
using MinecraftLaunch.Components.Resolver;
using MinecraftLaunch;
using MinecraftLaunch.Components.Resolver;
using MinecraftLaunch.Components.Installer;

GameResolver gameResolver = new("C:\\Users\\w\\Downloads\\.minecraft");

var result = gameResolver.GetGameEntitys();
foreach (var item in result) {
Console.WriteLine(item.Id);
}
VanlliaInstaller vanlliaInstaller = new(gameResolver, "1.12.2");
vanlliaInstaller.ProgressChanged += (_, args) => {
Console.WriteLine($"{args.Progress * 100:0.00} - {args.Status} - {args.ProgressStatus}");
};

await vanlliaInstaller.InstallAsync();

Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();

ForgeInstaller forgeInstaller = new(gameResolver.GetGameEntity("1.12.2"),
(await ForgeInstaller.EnumerableFromVersionAsync("1.12.2")).First(),
"C:\\Program Files\\Java\\jdk1.8.0_301\\bin\\javaw.exe",
"1.12.2-forge-114514");

forgeInstaller.ProgressChanged += (_, args) => {
Console.WriteLine($"{args.Progress * 100:0.00} - {args.Status} - {args.ProgressStatus}");
};

await forgeInstaller.InstallAsync();
Console.ReadKey();
2 changes: 1 addition & 1 deletion MinecraftLaunch/Classes/Models/Download/DownloadRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed record DownloadRequest {
/// <summary>
/// 下载链接
/// </summary>
public string Url { get; init; }
public string Url { get; set; }

/// <summary>
/// 保存文件信息
Expand Down
16 changes: 10 additions & 6 deletions MinecraftLaunch/Components/Installer/ForgeInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ public override async ValueTask<bool> InstallAsync() {
/*
* Download Forge installation package
*/
var suffix = $"/forge/download/{_installEntry.Build}";
var suffix = $"/net/minecraftforge/forge/{_installEntry.McVersion}-{_installEntry.ForgeVersion}/forge-{_installEntry
.McVersion}-{_installEntry.ForgeVersion}-installer.jar";

var host = MirrorDownloadManager.IsUseMirrorDownloadSource
? MirrorDownloadManager.Bmcl.Host
: _mirrorDownloadSource.Host;
? _mirrorDownloadSource.Host
: "https://files.minecraftforge.net/maven";
var packageUrl = $"{host}{suffix}";

string packagePath = Path.Combine(Path.GetTempPath(),
Path.GetFileName(packageUrl));

var request = packageUrl.ToDownloadRequest(packagePath.ToFileInfo());
await request.DownloadAsync(x => {
ReportProgress(x.ToPercentage().ToPercentage(0.0d, 0.15d),
ReportProgress(x.ToPercentage(0.0d, 0.15d),
"Downloading Forge installation package",
TaskStatus.Running);
});
Expand All @@ -60,8 +62,10 @@ await request.DownloadAsync(x => {
.GetEnumerable("libraries"),
_inheritedFrom.GameFolderPath).ToList();

foreach (var lib in libraries) {
lib.Url = $"https://download.mcbbs.net/maven/{lib.RelativePath.Replace("\\", "/")}";
if (MirrorDownloadManager.IsUseMirrorDownloadSource) {
foreach (var lib in libraries) {
lib.Url = $"https://bmclapi2.bangbang93.com/maven/{lib.RelativePath.Replace("\\", "/")}";
}
}

if (!isLegacyForgeVersion) {
Expand Down
11 changes: 5 additions & 6 deletions MinecraftLaunch/Components/Installer/VanlliaInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,22 @@ public override async ValueTask<bool> InstallAsync() {
versionJsonFile.Directory.Create();
}

await File.WriteAllTextAsync(versionJsonFile.FullName,
await File.WriteAllTextAsync(versionJsonFile.FullName,
await coreInfo.Url.GetStringAsync());

/*
* Download dependent resources
*/
ReportProgress(0.45d, "Start downloading dependent resources", TaskStatus.WaitingToRun);
ResourceChecker resourceChecker = new(_gameResolver.GetGameEntity(_gameId));
await resourceChecker.CheckAsync();

await resourceChecker.MissingResources.DownloadResourceEntrysAsync(_source,
x => {
var hasMissResource = await resourceChecker.CheckAsync();
if (!hasMissResource) {
await resourceChecker.MissingResources.DownloadResourceEntrysAsync(_source, x => {
ReportProgress(x.ToPercentage().ToPercentage(0.45d, 0.95d),
$"Downloading dependent resources:{x.CompletedCount}/{x.TotalCount}",
TaskStatus.Running);
});

}

ReportProgress(1.0d, "Installation is complete", TaskStatus.Canceled);
ReportCompleted();
Expand Down
22 changes: 4 additions & 18 deletions MinecraftLaunch/Extensions/DownloadExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,16 @@ public static IDownloadEntry OfMirrorSource(this IDownloadEntry entry,
return entry;
}

public static ValueTask<bool> DownloadAsync(this DownloadRequest request,
Action<DownloadProgressChangedEventArgs> action = default!) {
//DefaultDownloader.Setup(Enumerable.Repeat(request, 1));

//DefaultDownloader.ProgressChanged += (sender, args) => {
// action(args);
//};

//return DefaultDownloader.DownloadAsync();
throw new NotImplementedException();
public static ValueTask<bool> DownloadAsync(
this DownloadRequest request,
Action<double> action = default!) {
return DownloadUitl.DownloadAsync(request, default, action);
}

public static ValueTask<bool> DownloadResourceEntryAsync(this
IDownloadEntry downloadEntry,
MirrorDownloadSource source = default!) {
return DownloadUitl.DownloadAsync(downloadEntry, DownloadUitl.DefaultDownloadRequest,default,x=>{});

//DefaultDownloader.Setup(Enumerable.Repeat(downloadEntry
// .OfMirrorSource(source)
// .ToDownloadRequest(), 1));

//Console.WriteLine(downloadEntry.Path);
//return DefaultDownloader.DownloadAsync();
//throw new NotImplementedException();
}

public static ValueTask<bool> DownloadResourceEntrysAsync(this
Expand Down
2 changes: 1 addition & 1 deletion MinecraftLaunch/MinecraftLaunch.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>3.0.04</Version>
<Version>3.0.05</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
54 changes: 54 additions & 0 deletions MinecraftLaunch/Utilities/DownloadUitl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,60 @@ public static class DownloadUitl {
MultiPartsCount = 8
};

public static async ValueTask<bool> DownloadAsync(
DownloadRequest downloadRequest,
CancellationTokenSource tokenSource = default,
Action<double> perSecondProgressChangedAction = default) {
Timer timer = default;
downloadRequest ??= DefaultDownloadRequest;
tokenSource ??= new CancellationTokenSource();
perSecondProgressChangedAction ??= x => { };
var responseMessage = (await downloadRequest.Url.GetAsync(cancellationToken: tokenSource.Token))
.ResponseMessage;

if (responseMessage.StatusCode.Equals(HttpStatusCode.Found)) {
downloadRequest.Url = responseMessage.Headers.Location.AbsoluteUri;
return await DownloadAsync(downloadRequest, tokenSource);
}

if (perSecondProgressChangedAction != null) {
timer = new Timer(1000);
}

responseMessage.EnsureSuccessStatusCode();
var contentLength = responseMessage.Content.Headers.ContentLength ?? 0;

//use multipart download method
if (downloadRequest.IsPartialContentSupported && contentLength > downloadRequest.FileSizeThreshold) {
var requestMessage = new HttpRequestMessage(HttpMethod.Get, responseMessage.RequestMessage.RequestUri.AbsoluteUri);
requestMessage.Headers.Range = new RangeHeaderValue(0, 1);

return await MultiPartDownloadAsync(responseMessage, downloadRequest, downloadRequest.FileInfo.FullName, tokenSource)
.AsTask()
.ContinueWith(task => {
return !task.IsFaulted;
});
}

return await WriteFileFromHttpResponseAsync(downloadRequest.FileInfo.FullName, responseMessage, tokenSource, (timer, perSecondProgressChangedAction, contentLength))
.AsTask()
.ContinueWith(task => {
if (timer != null) {
try {
perSecondProgressChangedAction(responseMessage.Content.Headers.ContentLength != null ?
task.Result / (double)responseMessage.Content.Headers.ContentLength : 0);
} catch (Exception) {
}
timer.Stop();
timer.Dispose();
}
return !task.IsFaulted;
});
}

public static async ValueTask<bool> DownloadAsync(
IDownloadEntry downloadEntry,
DownloadRequest downloadRequest = default,
Expand Down

0 comments on commit 8796a05

Please sign in to comment.