diff --git a/BedrockLauncher.Core/BedrockCore.cs b/BedrockLauncher.Core/BedrockCore.cs index d4505e8..902294e 100644 --- a/BedrockLauncher.Core/BedrockCore.cs +++ b/BedrockLauncher.Core/BedrockCore.cs @@ -252,7 +252,6 @@ private static async Task WaitForProcessAsync(string processName, DateT if (processes.Length > 0) { - return processes .Where(p => p.StartTime > startTime) .OrderBy(p => (p.StartTime - startTime).TotalMilliseconds) @@ -265,6 +264,17 @@ private static async Task WaitForProcessAsync(string processName, DateT return null; } + private static DateTime GetStartTimeSafe(Process proc) + { + try + { + return proc.StartTime; + } + catch + { + return DateTime.MinValue; + } + } /// /// Launch the Minecraft game process based on the specified launch options /// @@ -276,15 +286,55 @@ public async Task LaunchGameAsync(LaunchOptions options) if (options.MinecraftBuildType == MinecraftBuildTypeVersion.GDK) { options.Progress?.Report(LaunchState.Launching); - DateTime startTimestamp = DateTime.Now; + string targetExe = "Minecraft.Windows.exe"; + string fullPath = Path.Combine(options.GameFolder, targetExe); + string processName = Path.GetFileNameWithoutExtension(targetExe); + + + var beforeSnapshot = Process.GetProcessesByName(processName) + .ToDictionary(p => p.Id, p => GetStartTimeSafe(p)); - Process.Start(new ProcessStartInfo() + + DateTime launchTime = DateTime.Now; + Process.Start(new ProcessStartInfo { FileName = "explorer.exe", - Arguments = Path.Combine(options.GameFolder, "Minecraft.Windows.exe") + Arguments = fullPath, + UseShellExecute = true }); - process = await WaitForProcessAsync("Minecraft.Windows", startTimestamp, TimeSpan.FromSeconds(10)); + Process minecraftProcess = null; + + + for (int i = 0; i < 10; i++) + { + var currentProcesses = Process.GetProcessesByName(processName); + foreach (var proc in currentProcesses) + { + + if (beforeSnapshot.ContainsKey(proc.Id)) + continue; + + + DateTime startTime = GetStartTimeSafe(proc); + if (startTime == DateTime.MinValue) + continue; + + if (startTime >= launchTime.AddMilliseconds(-200)) + { + minecraftProcess = proc; + break; + } + } + + if (minecraftProcess != null) + break; + + Thread.Sleep(500); + + } + process = minecraftProcess; + options.Progress?.Report(LaunchState.Launched); } diff --git a/BedrockLauncher.Core/BedrockLauncher.Core.csproj b/BedrockLauncher.Core/BedrockLauncher.Core.csproj index 4d0dc9a..c188aa9 100644 --- a/BedrockLauncher.Core/BedrockLauncher.Core.csproj +++ b/BedrockLauncher.Core/BedrockLauncher.Core.csproj @@ -20,11 +20,11 @@ true - 2.0.4-dev + 2.0.4.1-dev - 2.0.4 + 2.0.4.1 diff --git a/CoreTest/LaunchTest.cs b/CoreTest/LaunchTest.cs index 62f1b4a..f398291 100644 --- a/CoreTest/LaunchTest.cs +++ b/CoreTest/LaunchTest.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using BedrockLauncher.Core; using BedrockLauncher.Core.CoreOption; using BedrockLauncher.Core.DependsComplete; @@ -15,13 +16,14 @@ public void Test() bedrockCore.InitAsync().Wait(); var launchOptions = new LaunchOptions() { - GameFolder = Path.GetFullPath(""), + GameFolder = Path.GetFullPath(@"E:\test2\plugins\Glacie\bedrock_versions\1.26.2"), MinecraftBuildType = MinecraftBuildTypeVersion.GDK, GameType = MinecraftGameTypeVersion.Release, LaunchArgs = null }; var process = bedrockCore.LaunchGameAsync(launchOptions).Result; - // ExeLauncher.LaunchWithLowPrivilege(@"E:\GlacieCrack\plugins\Glacie\bedrock_versions\1234\Minecraft.Windows.exe", ""); + Debug.WriteLine(process.Id); + // ExeLauncher.LaunchWithLowPrivilege(@"E:\GlacieCrack\plugins\Glacie\bedrock_versions\1234\Minecraft.Windows.exe", ""); } [TestMethod] public void MsiTest() diff --git a/CoreTest/VersionTest.cs b/CoreTest/VersionTest.cs index 9be5655..a378aa5 100644 --- a/CoreTest/VersionTest.cs +++ b/CoreTest/VersionTest.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using BedrockLauncher.Core.Utils; +using BedrockLauncher.Core.VersionJsons; namespace CoreTest { @@ -18,7 +19,7 @@ public void Test() for (long i = 0; i < 1000; i++) { - Console.WriteLine(VersionHelper.GetNextVersion(new Version("1.21.10006.0"))); + Console.WriteLine(VersionsHelper.GetNextVersion(new Version("1.21.11236.0"))); } } }