-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deat: 添加 Forge 版本获取 Co-Authored-By: StarLight-Core <starlight@inkmarks.studio>
- Loading branch information
1 parent
ece10a0
commit 7ed5679
Showing
8 changed files
with
150 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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; } | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |