Skip to content

Commit

Permalink
添加Platform枚举
Browse files Browse the repository at this point in the history
  • Loading branch information
JWJUN233233 committed Dec 9, 2023
1 parent 34522c6 commit ee9f018
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 28 deletions.
16 changes: 16 additions & 0 deletions MinecraftLaunch/Classes/Enums/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinecraftLaunch.Classes.Enums
{
public enum Platform
{
windows,
linux,
osx,
unknown
}
}
8 changes: 5 additions & 3 deletions MinecraftLaunch/Classes/Models/Game/LibraryJsonEntry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using MinecraftLaunch.Classes.Enums;
using MinecraftLaunch.Classes.Models.Download;

namespace MinecraftLaunch.Classes.Models.Game {
Expand All @@ -10,14 +11,15 @@ public record LibraryJsonEntry {
public DownloadsEntry Downloads { get; set; }

[JsonPropertyName("natives")]
public Dictionary<string, string> Natives { get; set; }
public Dictionary<Platform, string> Natives { get; set; }
}

public record RuleModel {
public record RuleModel
{
[JsonPropertyName("action")]
public string Action { get; set; }

[JsonPropertyName("os")]
public Dictionary<string, string> System { get; set; }
public Dictionary<string, Platform> System { get; set; }
}
}
7 changes: 4 additions & 3 deletions MinecraftLaunch/Components/Fetcher/JavaFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Game;
using MinecraftLaunch.Classes.Enums;

namespace MinecraftLaunch.Components.Fetcher {
public class JavaFetcher : IFetcher<ImmutableArray<JavaEntry>> {
Expand Down Expand Up @@ -67,9 +68,9 @@ public ImmutableArray<JavaEntry> Fetch() {

public async ValueTask<ImmutableArray<JavaEntry>> FetchAsync() {
return EnvironmentUtil.GetPlatformName() switch {
"windows" => FetchWindowJava(),
"osx" => await FetchMacJavaAsync(),
"linux" => await FetchLinuxJavaAsync(),
Platform.windows => FetchWindowJava(),
Platform.osx => await FetchMacJavaAsync(),
Platform.linux => await FetchLinuxJavaAsync(),
_ => FetchWindowJava()
};
}
Expand Down
6 changes: 3 additions & 3 deletions MinecraftLaunch/Components/Launcher/ArgumentsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ private string GetClasspath() {
}

private static IEnumerable<string> GetEnvironmentJvmArguments() {
string platformName = EnvironmentUtil.GetPlatformName();
if (!(platformName == "windows")) {
if (platformName == "osx")
Platform platformName = EnvironmentUtil.GetPlatformName();
if (!(platformName == Platform.windows)) {
if (platformName == Platform.osx)
yield return "-XstartOnFirstThread";
} else {
yield return "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump";
Expand Down
19 changes: 10 additions & 9 deletions MinecraftLaunch/Components/Resolver/LibrariesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MinecraftLaunch.Extensions;
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Game;
using MinecraftLaunch.Classes.Enums;

namespace MinecraftLaunch.Components.Resolver;

Expand Down Expand Up @@ -111,13 +112,13 @@ private bool GetLibraryEnable(IEnumerable<RuleModel> rules) {

foreach (var os in item.System) {
switch (os.Value) {
case "windows":
case Platform.windows:
windows = true;
break;
case "linux":
case Platform.linux:
linux = true;
break;
case "osx":
case Platform.osx:
osx = true;
break;
}
Expand All @@ -130,13 +131,13 @@ private bool GetLibraryEnable(IEnumerable<RuleModel> rules) {

foreach (var os in item.System) {
switch (os.Value) {
case "windows":
case Platform.windows:
windows = false;
break;
case "linux":
case Platform.linux:
linux = false;
break;
case "osx":
case Platform.osx:
osx = false;
break;
}
Expand All @@ -145,9 +146,9 @@ private bool GetLibraryEnable(IEnumerable<RuleModel> rules) {
}

return EnvironmentUtil.GetPlatformName() switch {
"windows" => windows,
"linux" => linux,
"osx" => osx,
Platform.windows => windows,
Platform.linux=> linux,
Platform.osx => osx,
_ => false,
};
}
Expand Down
13 changes: 7 additions & 6 deletions MinecraftLaunch/Utilities/EnvironmentUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using MinecraftLaunch.Classes.Enums;
using System.Runtime.InteropServices;

namespace MinecraftLaunch.Utilities {
public class EnvironmentUtil {
Expand All @@ -14,16 +15,16 @@ public static bool IsLinux
public static bool IsWindow
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

public static string GetPlatformName() {
public static Platform GetPlatformName() {
if (IsMac) {
return "osx";
return Platform.osx;
} else if (IsLinux) {
return "linux";
return Platform.linux;
} else if (IsWindow) {
return "windows";
return Platform.windows;
}

return "unknown";
return Platform.unknown;
}
}
}
9 changes: 5 additions & 4 deletions MinecraftLaunch/Utilities/ZipUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Compression;
using MinecraftLaunch.Classes.Enums;
using System.IO.Compression;

namespace MinecraftLaunch.Utilities {
public class ZipUtil {
Expand All @@ -10,9 +11,9 @@ public static void ExtractNatives(string targetFolder, IEnumerable<string> files
DirectoryUtil.DeleteAllFiles(targetFolder);

var extension = EnvironmentUtil.GetPlatformName() switch {
"windows" => ".dll",
"linux" => ".so",
"osx" => ".dylib",
Platform.windows => ".dll",
Platform.linux => ".so",
Platform.osx => ".dylib",
_ => "."
};

Expand Down

0 comments on commit ee9f018

Please sign in to comment.