Skip to content

Commit

Permalink
优化Java安装器的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Oct 29, 2023
1 parent e565224 commit bda774b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion MinecraftLaunch.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.0.0</Version>
<Version>2.0.1</Version>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
17 changes: 11 additions & 6 deletions Modules/Enum/OpenJdkType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.ComponentModel;

namespace MinecraftLaunch.Modules.Enum;

public enum OpenJdkType
{
OpenJdk8,
OpenJdk11,
OpenJdk17,
OpenJdk18
public enum JavaVersion {
[Description("8")]
Java8,

[Description("16")]
Java16,

[Description("17")]
Java17,
}
11 changes: 6 additions & 5 deletions Modules/Installer/JavaInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public JavaInstaller(string path, DownloadJavaInfo javaInfo) {
JavaInfo = javaInfo;
}

public static async IAsyncEnumerable<DownloadJavaInfo> GetJavasByVersionAsync(int javaVersion) {
public static async IAsyncEnumerable<DownloadJavaInfo> GetJavasByVersionAsync(JavaVersion javaVersion) {
string api = $"{(APIManager.Current == APIManager.Mojang ? "https://piston-meta.mojang.com" : APIManager.Current.Host)}" +
$"/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json";

Expand All @@ -58,11 +58,12 @@ public static async IAsyncEnumerable<DownloadJavaInfo> GetJavasByVersionAsync(in
? Convert.ToInt32(version.Split('.', 2)[0])
: Convert.ToInt32(version[0].ToString());

if (firstVersion == javaVersion) {
if (firstVersion == Convert.ToInt32(javaVersion.AsDescription())) {
var manifest = info["manifest"];

yield return new DownloadJavaInfo {
Type = java.Key,
OsType = java.Key,
JavaType = item.Key,
Version = firstVersion,
DetailVersion = version,
Url = manifest["url"].GetValue<string>(),
Expand Down Expand Up @@ -157,9 +158,9 @@ public record DownloadJavaInfo {

public int Version { get; set; }

public string Type { get; set; }
public string OsType { get; set; }

public string JavaPath { get; set; }
public string JavaType { get; set; }

public string DetailVersion { get; set; }
}
Expand Down
32 changes: 11 additions & 21 deletions Modules/Utilities/ExtendUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using System.IO.Compression;
using System.Security.Cryptography;
using System.IO;
using System.ComponentModel;
using System.Reflection;
using DateTimeConverter = MinecraftLaunch.Modules.Models.Download.DateTimeConverter;

namespace MinecraftLaunch.Modules.Utilities;

Expand Down Expand Up @@ -85,27 +88,6 @@ public static T ToJsonEntity<T>(this string json) {
return JsonSerializer.Deserialize<T>(json)!;
}

public static string ToFullJavaPath(this OpenJdkType open, string Save) {
string javapath = null;
switch (open) {
case OpenJdkType.OpenJdk8:
javapath += "OpenJDK 8";
break;
case OpenJdkType.OpenJdk11:
javapath += "OpenJDK 11";
break;
case OpenJdkType.OpenJdk17:
javapath += "OpenJDK 17";
break;
case OpenJdkType.OpenJdk18:
javapath += "OpenJDK 18";
break;
}
string obj = (Save.EndsWith('\\') ? (Save + javapath) : (Save.EndsWith("/") ? (Save + javapath) : (Save + "\\" + javapath)));
Directory.CreateDirectory(obj);
return obj;
}

public static List<ModrinthFileInfo> GetModInfoToVersion(this List<ModrinthProjectInfoItem> ms, string version) {
string version2 = version;
List<ModrinthFileInfo> result = new List<ModrinthFileInfo>();
Expand Down Expand Up @@ -231,4 +213,12 @@ public static FileInfo Find(this DirectoryInfo directory, string file) {

return null;
}

public static string? AsDescription(this System.Enum t) {
var type = t.GetType();
var field = type.GetField(t.ToString());
var description = field?.GetCustomAttribute<DescriptionAttribute>();

return description?.Description;
}
}

0 comments on commit bda774b

Please sign in to comment.