From 799c9158c014c3ca3c51fb13cc7b3d75784f6872 Mon Sep 17 00:00:00 2001 From: YangSpring114 Date: Mon, 29 Jan 2024 02:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E4=B8=AABug?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MinecraftLaunch.Test/Program.cs | 12 +++++------ MinecraftLaunch/Classes/Enums/Platform.cs | 8 ------- .../Classes/Interfaces/IGameProcessWatcher.cs | 2 +- .../Classes/Models/Game/LibraryJsonEntry.cs | 4 ++-- .../PlatformEnumToStringJsonConverter.cs | 21 ------------------- .../Components/Fetcher/JavaFetcher.cs | 6 +++--- .../Components/Launcher/ArgumentsBuilder.cs | 6 +++--- .../Components/Launcher/Launcher.cs | 8 +++---- .../Components/Resolver/LibrariesResolver.cs | 18 ++++++++-------- .../Components/Watcher/GameProcessWatcher.cs | 6 ++++-- MinecraftLaunch/Extensions/JsonExtension.cs | 4 ++-- MinecraftLaunch/MinecraftLaunch.csproj | 2 +- MinecraftLaunch/Utilities/EnvironmentUtil.cs | 12 +++++------ .../Utilities/JsonConverterUtil.cs | 2 -- MinecraftLaunch/Utilities/ZipUtil.cs | 6 +++--- 15 files changed, 42 insertions(+), 75 deletions(-) delete mode 100644 MinecraftLaunch/Classes/Enums/Platform.cs delete mode 100644 MinecraftLaunch/Components/Converter/PlatformEnumToStringJsonConverter.cs diff --git a/MinecraftLaunch.Test/Program.cs b/MinecraftLaunch.Test/Program.cs index 187b14b..4bb745d 100644 --- a/MinecraftLaunch.Test/Program.cs +++ b/MinecraftLaunch.Test/Program.cs @@ -1,14 +1,10 @@ -using System.IO.Compression; -using MinecraftLaunch.Extensions; -using MinecraftLaunch.Components.Launcher; +using MinecraftLaunch.Components.Launcher; using MinecraftLaunch.Components.Resolver; using MinecraftLaunch.Classes.Models.Launch; -using MinecraftLaunch.Components.Downloader; -using MinecraftLaunch.Classes.Models.Download; using MinecraftLaunch.Components.Authenticator; var account = new OfflineAuthenticator("Yang114").Authenticate(); -var resolver = new GameResolver(".minecraft"); +var resolver = new GameResolver("C:\\Users\\w\\Desktop\\总整包\\MC\\mc启动器\\LauncherX\\.minecraft"); var config = new LaunchConfig { Account = account, @@ -29,4 +25,6 @@ //检测游戏退出 gameProcessWatcher.Exited += (sender, args) => { Console.WriteLine("exit"); -}; \ No newline at end of file +}; + +Console.ReadKey(); \ No newline at end of file diff --git a/MinecraftLaunch/Classes/Enums/Platform.cs b/MinecraftLaunch/Classes/Enums/Platform.cs deleted file mode 100644 index 6747e99..0000000 --- a/MinecraftLaunch/Classes/Enums/Platform.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace MinecraftLaunch.Classes.Enums; - -public enum Platform { - windows, - linux, - osx, - unknown -} \ No newline at end of file diff --git a/MinecraftLaunch/Classes/Interfaces/IGameProcessWatcher.cs b/MinecraftLaunch/Classes/Interfaces/IGameProcessWatcher.cs index c1cf2ec..6adf4d2 100644 --- a/MinecraftLaunch/Classes/Interfaces/IGameProcessWatcher.cs +++ b/MinecraftLaunch/Classes/Interfaces/IGameProcessWatcher.cs @@ -3,7 +3,7 @@ namespace MinecraftLaunch.Classes.Interfaces; -public class IGameProcessWatcher { +public interface IGameProcessWatcher { public Process Process { get; } public IEnumerable Arguments { get; } diff --git a/MinecraftLaunch/Classes/Models/Game/LibraryJsonEntry.cs b/MinecraftLaunch/Classes/Models/Game/LibraryJsonEntry.cs index c9fafa6..b5d4e40 100644 --- a/MinecraftLaunch/Classes/Models/Game/LibraryJsonEntry.cs +++ b/MinecraftLaunch/Classes/Models/Game/LibraryJsonEntry.cs @@ -12,7 +12,7 @@ public sealed record LibraryJsonEntry { public DownloadsEntry Downloads { get; set; } [JsonPropertyName("natives")] - public Dictionary Natives { get; set; } + public Dictionary Natives { get; set; } } public sealed record RuleModel @@ -21,7 +21,7 @@ public sealed record RuleModel public string Action { get; set; } [JsonPropertyName("os")] - public Dictionary System { get; set; } + public Dictionary System { get; set; } } [JsonSerializable(typeof(LibraryJsonEntry))] diff --git a/MinecraftLaunch/Components/Converter/PlatformEnumToStringJsonConverter.cs b/MinecraftLaunch/Components/Converter/PlatformEnumToStringJsonConverter.cs deleted file mode 100644 index c257cec..0000000 --- a/MinecraftLaunch/Components/Converter/PlatformEnumToStringJsonConverter.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Text.Json; -using MinecraftLaunch.Classes.Enums; -using System.Text.Json.Serialization; - -namespace MinecraftLaunch.Components.Converter; - -public sealed class PlatformEnumToStringJsonConverter : JsonConverter { - public override Platform Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var platform = reader.GetString(); - return platform switch { - "osx" => Platform.osx, - "linux" => Platform.linux, - "windows" => Platform.windows, - _ => Platform.windows, - }; - } - - public override void Write(Utf8JsonWriter writer, Platform value, JsonSerializerOptions options) { - writer.WriteStringValue(value.ToString()); - } -} \ No newline at end of file diff --git a/MinecraftLaunch/Components/Fetcher/JavaFetcher.cs b/MinecraftLaunch/Components/Fetcher/JavaFetcher.cs index 6177dbd..48bcb40 100644 --- a/MinecraftLaunch/Components/Fetcher/JavaFetcher.cs +++ b/MinecraftLaunch/Components/Fetcher/JavaFetcher.cs @@ -72,9 +72,9 @@ public ImmutableArray Fetch() { public async ValueTask> FetchAsync() { return EnvironmentUtil.GetPlatformName() switch { - Platform.windows => FetchWindowJava(), - Platform.osx => await FetchMacJavaAsync(), - Platform.linux => await FetchLinuxJavaAsync(), + "windows" => FetchWindowJava(), + "osx" => await FetchMacJavaAsync(), + "linux" => await FetchLinuxJavaAsync(), _ => FetchWindowJava() }; } diff --git a/MinecraftLaunch/Components/Launcher/ArgumentsBuilder.cs b/MinecraftLaunch/Components/Launcher/ArgumentsBuilder.cs index a7cea80..503a02a 100644 --- a/MinecraftLaunch/Components/Launcher/ArgumentsBuilder.cs +++ b/MinecraftLaunch/Components/Launcher/ArgumentsBuilder.cs @@ -134,9 +134,9 @@ private string GetNativesDirectory() { } private static IEnumerable GetEnvironmentJvmArguments() { - Platform platformName = EnvironmentUtil.GetPlatformName(); - if (!(platformName == Platform.windows)) { - if (platformName == Platform.osx) + string platformName = EnvironmentUtil.GetPlatformName(); + if (!(platformName == "windows")) { + if (platformName == "osx") yield return "-XstartOnFirstThread"; } else { yield return "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump"; diff --git a/MinecraftLaunch/Components/Launcher/Launcher.cs b/MinecraftLaunch/Components/Launcher/Launcher.cs index a6a06e3..7bb5ce8 100644 --- a/MinecraftLaunch/Components/Launcher/Launcher.cs +++ b/MinecraftLaunch/Components/Launcher/Launcher.cs @@ -28,12 +28,13 @@ public async ValueTask LaunchAsync(string id) { var process = CreateProcess(arguments, versionPath); LibrariesResolver librariesResolver = new(gameEntry); - await ExtractNativesAndStartProcess(versionPath, librariesResolver, process); + await ExtractNatives(versionPath, librariesResolver, process); return new GameProcessWatcher(process, arguments); } private Process CreateProcess(IEnumerable arguments, string versionPath) { return new Process { + EnableRaisingEvents = true, StartInfo = new ProcessStartInfo { FileName = LaunchConfig.JvmConfig.JavaPath.FullName, Arguments = string.Join(' '.ToString(), arguments), @@ -42,18 +43,15 @@ private Process CreateProcess(IEnumerable arguments, string versionPath) RedirectStandardError = true, WorkingDirectory = versionPath }, - - EnableRaisingEvents = true }; } - private async Task ExtractNativesAndStartProcess(string versionPath, LibrariesResolver librariesResolver, Process process) { + private async Task ExtractNatives(string versionPath, LibrariesResolver librariesResolver, Process process) { var libraries = librariesResolver.GetLibraries() .Where(x => ((x as LibraryEntry)?.IsNative) != null) .Select(x => x.Path) .ToList(); await Task.Run(() => ZipUtil.ExtractNatives(Path.Combine(versionPath, "natives"), libraries)); - process.Start(); } } \ No newline at end of file diff --git a/MinecraftLaunch/Components/Resolver/LibrariesResolver.cs b/MinecraftLaunch/Components/Resolver/LibrariesResolver.cs index 4995b2b..88b7023 100644 --- a/MinecraftLaunch/Components/Resolver/LibrariesResolver.cs +++ b/MinecraftLaunch/Components/Resolver/LibrariesResolver.cs @@ -130,13 +130,13 @@ private bool GetLibraryEnable(IEnumerable rules) { foreach (var os in item.System) { switch (os.Value) { - case Platform.windows: + case "windows": windows = true; break; - case Platform.linux: + case "linux": linux = true; break; - case Platform.osx: + case "osx": osx = true; break; } @@ -149,13 +149,13 @@ private bool GetLibraryEnable(IEnumerable rules) { foreach (var os in item.System) { switch (os.Value) { - case Platform.windows: + case "windows": windows = false; break; - case Platform.linux: + case "linux": linux = false; break; - case Platform.osx: + case "osx": osx = false; break; } @@ -164,9 +164,9 @@ private bool GetLibraryEnable(IEnumerable rules) { } return EnvironmentUtil.GetPlatformName() switch { - Platform.windows => windows, - Platform.linux=> linux, - Platform.osx => osx, + "windows" => windows, + "linux" => linux, + "osx" => osx, _ => false, }; } diff --git a/MinecraftLaunch/Components/Watcher/GameProcessWatcher.cs b/MinecraftLaunch/Components/Watcher/GameProcessWatcher.cs index 0712949..e57aa0c 100644 --- a/MinecraftLaunch/Components/Watcher/GameProcessWatcher.cs +++ b/MinecraftLaunch/Components/Watcher/GameProcessWatcher.cs @@ -7,7 +7,7 @@ namespace MinecraftLaunch.Components.Watcher; /// /// 游戏进程监视器 /// -public sealed class GameProcessWatcher : IGameProcessWatcher { +public class GameProcessWatcher : IGameProcessWatcher { public Process Process { get; } public IEnumerable Arguments { get; } @@ -19,9 +19,11 @@ public sealed class GameProcessWatcher : IGameProcessWatcher { public GameProcessWatcher(Process process, IEnumerable arguments) { Process = process; Arguments = arguments; - process.Exited += OnExited; + process.Exited += OnExited; process.ErrorDataReceived += OnOutputDataReceived; process.OutputDataReceived += OnOutputDataReceived; + process.Start(); + process.BeginErrorReadLine(); process.BeginOutputReadLine(); } diff --git a/MinecraftLaunch/Extensions/JsonExtension.cs b/MinecraftLaunch/Extensions/JsonExtension.cs index 31b3885..366aa4c 100644 --- a/MinecraftLaunch/Extensions/JsonExtension.cs +++ b/MinecraftLaunch/Extensions/JsonExtension.cs @@ -47,11 +47,11 @@ public static bool GetBool(this JsonNode node, string name) { } public static string GetString(this JsonNode node) { - return node.GetValue(); + return node?.GetValue(); } public static string GetString(this JsonNode node, string name) { - return node[name].GetValue(); + return node[name]?.GetValue(); } public static JsonArray GetEnumerable(this JsonNode node) { diff --git a/MinecraftLaunch/MinecraftLaunch.csproj b/MinecraftLaunch/MinecraftLaunch.csproj index 4fd288d..300e428 100644 --- a/MinecraftLaunch/MinecraftLaunch.csproj +++ b/MinecraftLaunch/MinecraftLaunch.csproj @@ -1,6 +1,6 @@  - 3.0.0-preview9 + 3.0.0-preview10 diff --git a/MinecraftLaunch/Utilities/EnvironmentUtil.cs b/MinecraftLaunch/Utilities/EnvironmentUtil.cs index 0c21bd1..e0ed30d 100644 --- a/MinecraftLaunch/Utilities/EnvironmentUtil.cs +++ b/MinecraftLaunch/Utilities/EnvironmentUtil.cs @@ -16,19 +16,19 @@ public static bool IsLinux public static bool IsWindow => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - public readonly static Dictionary, Platform> PlatformDirectory = new() { - { () => IsMac, Platform.osx }, - { () => IsLinux, Platform.linux }, - { () => IsWindow, Platform.windows } + public readonly static Dictionary, string> PlatformDirectory = new() { + { () => IsMac, "osx" }, + { () => IsLinux, "linux" }, + { () => IsWindow, "windows" } }; - public static Platform GetPlatformName() { + public static string GetPlatformName() { foreach (var item in PlatformDirectory) { if (item.Key.Invoke()) { return item.Value; } } - return Platform.unknown; + return ""; } } \ No newline at end of file diff --git a/MinecraftLaunch/Utilities/JsonConverterUtil.cs b/MinecraftLaunch/Utilities/JsonConverterUtil.cs index 7a505cf..f308297 100644 --- a/MinecraftLaunch/Utilities/JsonConverterUtil.cs +++ b/MinecraftLaunch/Utilities/JsonConverterUtil.cs @@ -16,8 +16,6 @@ private static JsonSerializerOptions Get() { }; options.Converters.Add(new AccountJsonConverter()); - options.Converters.Add(new PlatformEnumToStringJsonConverter()); - return options; } } \ No newline at end of file diff --git a/MinecraftLaunch/Utilities/ZipUtil.cs b/MinecraftLaunch/Utilities/ZipUtil.cs index 4f33b2d..6d340ac 100644 --- a/MinecraftLaunch/Utilities/ZipUtil.cs +++ b/MinecraftLaunch/Utilities/ZipUtil.cs @@ -12,9 +12,9 @@ public static void ExtractNatives(string targetFolder, IEnumerable files DirectoryUtil.DeleteAllFiles(targetFolder); var extension = EnvironmentUtil.GetPlatformName() switch { - Platform.windows => ".dll", - Platform.linux => ".so", - Platform.osx => ".dylib", + "windows" => ".dll", + "linux" => ".so", + "osx" => ".dylib", _ => "." };