Skip to content

Commit

Permalink
修复多个Bug,更新版本号
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Jan 28, 2024
1 parent a5e5016 commit 799c915
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 75 deletions.
12 changes: 5 additions & 7 deletions MinecraftLaunch.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -29,4 +25,6 @@
//检测游戏退出
gameProcessWatcher.Exited += (sender, args) => {
Console.WriteLine("exit");
};
};

Console.ReadKey();
8 changes: 0 additions & 8 deletions MinecraftLaunch/Classes/Enums/Platform.cs

This file was deleted.

2 changes: 1 addition & 1 deletion MinecraftLaunch/Classes/Interfaces/IGameProcessWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MinecraftLaunch.Classes.Interfaces;

public class IGameProcessWatcher {
public interface IGameProcessWatcher {
public Process Process { get; }

public IEnumerable<string> Arguments { get; }
Expand Down
4 changes: 2 additions & 2 deletions MinecraftLaunch/Classes/Models/Game/LibraryJsonEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed record LibraryJsonEntry {
public DownloadsEntry Downloads { get; set; }

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

public sealed record RuleModel
Expand All @@ -21,7 +21,7 @@ public sealed record RuleModel
public string Action { get; set; }

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

[JsonSerializable(typeof(LibraryJsonEntry))]
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions MinecraftLaunch/Components/Fetcher/JavaFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public ImmutableArray<JavaEntry> Fetch() {

public async ValueTask<ImmutableArray<JavaEntry>> 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()
};
}
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 @@ -134,9 +134,9 @@ private string GetNativesDirectory() {
}

private static IEnumerable<string> 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";
Expand Down
8 changes: 3 additions & 5 deletions MinecraftLaunch/Components/Launcher/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public async ValueTask<IGameProcessWatcher> 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<string> arguments, string versionPath) {
return new Process {
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo {
FileName = LaunchConfig.JvmConfig.JavaPath.FullName,
Arguments = string.Join(' '.ToString(), arguments),
Expand All @@ -42,18 +43,15 @@ private Process CreateProcess(IEnumerable<string> 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();
}
}
18 changes: 9 additions & 9 deletions MinecraftLaunch/Components/Resolver/LibrariesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ private bool GetLibraryEnable(IEnumerable<RuleModel> 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;
}
Expand All @@ -149,13 +149,13 @@ private bool GetLibraryEnable(IEnumerable<RuleModel> 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;
}
Expand All @@ -164,9 +164,9 @@ private bool GetLibraryEnable(IEnumerable<RuleModel> rules) {
}

return EnvironmentUtil.GetPlatformName() switch {
Platform.windows => windows,
Platform.linux=> linux,
Platform.osx => osx,
"windows" => windows,
"linux" => linux,
"osx" => osx,
_ => false,
};
}
Expand Down
6 changes: 4 additions & 2 deletions MinecraftLaunch/Components/Watcher/GameProcessWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MinecraftLaunch.Components.Watcher;
/// <summary>
/// 游戏进程监视器
/// </summary>
public sealed class GameProcessWatcher : IGameProcessWatcher {
public class GameProcessWatcher : IGameProcessWatcher {
public Process Process { get; }

public IEnumerable<string> Arguments { get; }
Expand All @@ -19,9 +19,11 @@ public sealed class GameProcessWatcher : IGameProcessWatcher {
public GameProcessWatcher(Process process, IEnumerable<string> arguments) {
Process = process;
Arguments = arguments;
process.Exited += OnExited;
process.Exited += OnExited;
process.ErrorDataReceived += OnOutputDataReceived;
process.OutputDataReceived += OnOutputDataReceived;
process.Start();

process.BeginErrorReadLine();
process.BeginOutputReadLine();
}
Expand Down
4 changes: 2 additions & 2 deletions MinecraftLaunch/Extensions/JsonExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public static bool GetBool(this JsonNode node, string name) {
}

public static string GetString(this JsonNode node) {
return node.GetValue<string>();
return node?.GetValue<string>();
}

public static string GetString(this JsonNode node, string name) {
return node[name].GetValue<string>();
return node[name]?.GetValue<string>();
}

public static JsonArray GetEnumerable(this JsonNode node) {
Expand Down
2 changes: 1 addition & 1 deletion MinecraftLaunch/MinecraftLaunch.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>3.0.0-preview9</Version>
<Version>3.0.0-preview10</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
12 changes: 6 additions & 6 deletions MinecraftLaunch/Utilities/EnvironmentUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ public static bool IsLinux
public static bool IsWindow
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

public readonly static Dictionary<Func<bool>, Platform> PlatformDirectory = new() {
{ () => IsMac, Platform.osx },
{ () => IsLinux, Platform.linux },
{ () => IsWindow, Platform.windows }
public readonly static Dictionary<Func<bool>, 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 "";
}
}
2 changes: 0 additions & 2 deletions MinecraftLaunch/Utilities/JsonConverterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ private static JsonSerializerOptions Get() {
};

options.Converters.Add(new AccountJsonConverter());
options.Converters.Add(new PlatformEnumToStringJsonConverter());

return options;
}
}
6 changes: 3 additions & 3 deletions MinecraftLaunch/Utilities/ZipUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static void ExtractNatives(string targetFolder, IEnumerable<string> files
DirectoryUtil.DeleteAllFiles(targetFolder);

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

Expand Down

0 comments on commit 799c915

Please sign in to comment.