Skip to content

Commit

Permalink
Deat: 添加 Forge 版本获取
Browse files Browse the repository at this point in the history
Deat: 添加 Forge 版本获取
Co-Authored-By: StarLight-Core <[email protected]>
  • Loading branch information
zhi-feng2008 and StarLight-Core committed Aug 22, 2024
1 parent ece10a0 commit 7ed5679
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 25 deletions.
10 changes: 2 additions & 8 deletions .idea/.idea.StarLight.Core/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Installer/FabricInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,15 @@ public async Task<FabricInstallResult> InstallAsync(string? customId = null)
return new FabricInstallResult(Status.Succeeded, GameVersion, FabricVersion, customId);
}

public static async Task<IEnumerable<FabricVersionEntity>> GetFabricVersionsAsync(string version)
public static async Task<IEnumerable<FabricVersionEntity>> FetchFabricVersionsAsync(string version)
{
try
{
var json = await HttpUtil.GetJsonAsync(DownloadAPIs.Current.FabricRoot + $"/v2/versions/loader/{version}");
if (string.IsNullOrWhiteSpace(json))
throw new InvalidOperationException("[SL]版本列表为空");

var versionsManifest = JsonSerializer.Deserialize<IEnumerable<FabricVersionJsonEntity>>(json);
var result = versionsManifest.Select(manifest =>

var result = json.ToJsonEntry<IEnumerable<FabricVersionJsonEntity>>().Select(manifest =>
new FabricVersionEntity
{
Intermediary = manifest.Intermediary,
Expand Down
90 changes: 90 additions & 0 deletions Installer/ForgeInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Text.Json;
using StarLight_Core.Enum;
using StarLight_Core.Models.Installer;
using StarLight_Core.Models.Utilities;
using StarLight_Core.Utilities;

namespace StarLight_Core.Installer
{
public class ForgeInstaller
{
private string Root { get; set; }

private string GameVersion { get; set; }

private string FabricVersion { get; set; }

private CancellationToken CancellationToken { get; set; }

public Action<string,int>? OnProgressChanged { get; set; }

public Action<string>? OnSpeedChanged { get; set; }

public ForgeInstaller(string gameVersion, string fabricVersion, string root = ".minecraft", CancellationToken cancellationToken = default, Action<string>? onSpeedChanged = null, Action<string,int>? onProgressChanged = null)
{
GameVersion = gameVersion;
FabricVersion = fabricVersion;
OnSpeedChanged = onSpeedChanged;
OnProgressChanged = onProgressChanged;
CancellationToken = cancellationToken;
Root = FileUtil.IsAbsolutePath(root)
? Path.Combine(root)
: Path.Combine(FileUtil.GetCurrentExecutingDirectory(), root);
}

public ForgeInstaller(string gameVersion, string fabricVersion, CancellationToken cancellationToken = default)
{
GameVersion = gameVersion;
FabricVersion = fabricVersion;
CancellationToken = cancellationToken;
Root = Path.Combine(FileUtil.GetCurrentExecutingDirectory(), ".minecraft");
}

public async Task<FabricInstallResult> InstallAsync(string? customId = null)
{
throw new NotImplementedException();
}

public static async Task<IEnumerable<ForgeVersionEntity>> FetchForgeVersionsAsync(string version)
{
try
{
var json = await HttpUtil.GetJsonAsync($"https://bmclapi2.bangbang93.com/forge/minecraft/{version}");
if (string.IsNullOrWhiteSpace(json))
throw new InvalidOperationException("[SL]版本列表为空");

return json.ToJsonEntry<IEnumerable<ForgeVersionEntity>>();
}
catch (JsonException je)
{
throw new Exception("[SL]版本列表解析失败:" + je.Message, je);
}
catch (HttpRequestException hre)
{
throw new Exception("[SL]下载版本列表失败:" + hre.Message, hre);
}
catch (Exception e)
{
throw new Exception("[SL]获取版本列表时发生未知错误:" + e.Message, e);
}
}

static string CalcMemoryMensurableUnit(double bytes)
{
double kb = bytes / 1024;
double mb = kb / 1024;
double gb = mb / 1024;
double tb = gb / 1024;

string result =
tb > 1 ? $"{tb:0.##}TB" :
gb > 1 ? $"{gb:0.##}GB" :
mb > 1 ? $"{mb:0.##}MB" :
kb > 1 ? $"{kb:0.##}KB" :
$"{bytes:0.##}B";

result = result.Replace("/", ".");
return result;
}
}
}
15 changes: 6 additions & 9 deletions Models/Installer/DownloadAPIs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class DownloadAPIs
DownloadSource.Official
);

public static readonly DownloadAPI BmclApi = new DownloadAPI(
private static readonly DownloadAPI BmclApi = new DownloadAPI(
"https://bmclapi2.bangbang93.com",
"https://bmclapi2.bangbang93.com/mc/game/version_manifest_v2.json",
"https://bmclapi2.bangbang93.com/assets",
Expand All @@ -58,13 +58,10 @@ public static class DownloadAPIs
DownloadSource.BmclApi
);

public static void SwitchDownloadSource(DownloadSource source)
public static void SwitchDownloadSource(DownloadSource source) => Current = source switch
{
Current = source switch
{
DownloadSource.Official => Official,
DownloadSource.BmclApi => BmclApi,
_ => throw new ArgumentException("[SL]未找到下载源")
};
}
DownloadSource.Official => Official,
DownloadSource.BmclApi => BmclApi,
_ => throw new ArgumentException("[SL]未找到下载源")
};
}
22 changes: 22 additions & 0 deletions Models/Installer/ForgeVersionEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;

namespace StarLight_Core.Models.Installer
{
public class ForgeVersionEntity
{
[JsonPropertyName("build")]
public int Build { get; set; }

[JsonPropertyName("branch")]
public string Branch { get; set; }

[JsonPropertyName("mcversion")]
public string McVersion { get; set; }

[JsonPropertyName("version")]
public string ForgeVersion { get; set; }

[JsonPropertyName("modified")]
public DateTime ModifiedTime { get; set; }
}
}
2 changes: 1 addition & 1 deletion Models/StarLightInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public static class StarLightInfo
public const string Version = "1.3.7";

// Preview / Release
public const string Type = "Preview";
public const string Type = "Release";
}
}
6 changes: 3 additions & 3 deletions StarLight.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.3.7-Perview9</Version>
<Version>1.3.7</Version>
<Title>StarLight.Core</Title>
<Authors>Ink Marks Studio</Authors>
<Description>StarLight.Core 一个简单,模块化,全能的启动器核心</Description>
Expand All @@ -20,8 +20,8 @@
<PackageReleaseNotes>无</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyName>StarLight_Core</AssemblyName>
<AssemblyVersion>1.3.7.9</AssemblyVersion>
<FileVersion>1.3.7.9</FileVersion>
<AssemblyVersion>1.3.7.10</AssemblyVersion>
<FileVersion>1.3.7.10</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions Utilities/JsonUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization.Metadata;

namespace StarLight_Core.Utilities
{
public static class JsonUtil
{
private static readonly JsonSerializerOptions Options = new JsonSerializerOptions {
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

public static T Deserialize<T>(this string json, JsonTypeInfo<T> jsonType) => JsonSerializer.Deserialize(json, jsonType);

public static T ToJsonEntry<T>(this string json) => JsonSerializer.Deserialize<T>(json, Options);

public static string Serialize(this object obj) => JsonSerializer.Serialize(obj, Options);

public static JsonNode ToJsonNode(this string json) => JsonNode.Parse(json);
}
}

0 comments on commit 7ed5679

Please sign in to comment.