Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 78 additions & 17 deletions BedrockLauncher.Core/BedrockCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@

if (processes.Length > 0)
{
return processes

Check warning on line 247 in BedrockLauncher.Core/BedrockCore.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference return.
.Where(p => p.StartTime > startTime)
.OrderBy(p => (p.StartTime - startTime).TotalMilliseconds)
.FirstOrDefault();
Expand All @@ -254,7 +254,7 @@
await Task.Delay(200);
}

return null;

Check warning on line 257 in BedrockLauncher.Core/BedrockCore.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference return.
}
private static DateTime GetStartTimeSafe(Process proc)
{
Expand All @@ -267,6 +267,7 @@
return DateTime.MinValue;
}
}

/// <summary>
/// Launch the Minecraft game process based on the specified launch options
/// </summary>
Expand All @@ -280,24 +281,24 @@
options.Progress?.Report(LaunchState.Launching);
string targetExe = "Minecraft.Windows.exe";
string fullPath = Path.Combine(options.GameFolder, targetExe);
string processName = Path.GetFileNameWithoutExtension(targetExe);
string processName = Path.GetFileNameWithoutExtension(targetExe);


var beforeSnapshot = Process.GetProcessesByName(processName)
.ToDictionary(p => p.Id, p => GetStartTimeSafe(p));


DateTime launchTime = DateTime.Now;
Process.Start(new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = fullPath,
UseShellExecute = true
FileName = "cmd.exe",
ArgumentList =
{ "/c", "start", fullPath, string.IsNullOrEmpty(options.LaunchArgs) ? "" : options.LaunchArgs },
UseShellExecute = false,
CreateNoWindow = true
});

Process minecraftProcess = null;

Check warning on line 299 in BedrockLauncher.Core/BedrockCore.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.


for (int i = 0; i < 10; i++)
{
var currentProcesses = Process.GetProcessesByName(processName);
Expand All @@ -310,7 +311,7 @@

DateTime startTime = GetStartTimeSafe(proc);
if (startTime == DateTime.MinValue)
continue;
continue;

if (startTime >= launchTime.AddMilliseconds(-200))
{
Expand All @@ -323,8 +324,9 @@
break;

Thread.Sleep(500);

}

process = minecraftProcess;

options.Progress?.Report(LaunchState.Launched);
Expand All @@ -345,10 +347,12 @@
MinecraftGameTypeVersion.Release => "Microsoft.MinecraftUWP",
MinecraftGameTypeVersion.Preview => "Microsoft.MinecraftWindowsBeta"
};

if (!File.Exists(manifest))
{
throw new IOException("File doesn't exist");
}

PackageManager packageManager = new PackageManager();
bool twice_launch = false;
foreach (var package in packageManager.FindPackagesForUser(string.Empty))
Expand All @@ -361,6 +365,7 @@
}
}
}

bool is_installed = UwpRegister.UwpRegister.IsPackageInstalled(packageName);
var config = new DeploymentOptionsConfig();
options.Progress?.Report(LaunchState.Registering);
Expand All @@ -370,6 +375,7 @@
? DeploymentOptions.DevelopmentMode | DeploymentOptions.ForceUpdateFromAnyVersion
: DeploymentOptions.DevelopmentMode;
config.ProgressCallback = options.RegisterProgress;

if (!twice_launch && is_installed || !is_installed)
{
var appxAsync = await UwpRegister.UwpRegister.RegisterAppxAsync(config);
Expand All @@ -385,7 +391,7 @@
var appDiagnosticInfos = AppDiagnosticInfo.RequestInfoForPackageAsync(packageFamily).AsTask().Result;
if (appDiagnosticInfos.Count != 0)
{
await appDiagnosticInfos[0].LaunchAsync();
await appDiagnosticInfos[0].LaunchAsync();
}
}
else
Expand All @@ -394,23 +400,78 @@
{
TargetApplicationPackageFamilyName = packageFamily
};
if (options?.LaunchArgs == string.Empty)
{
await Launcher.LaunchUriAsync(new Uri(options.LaunchArgs), options_st);
}
else

// 构建启动 URI
string uriString = "minecraft://launch";

if (!string.IsNullOrEmpty(options?.LaunchArgs))
{
await Launcher.LaunchUriAsync(new Uri("minecraft://launch"), options_st);
string queryString = string.Empty;
string launchArgs = options.LaunchArgs;

if (launchArgs.StartsWith("minecraft://", StringComparison.OrdinalIgnoreCase))
{
int questionMarkIndex = launchArgs.IndexOf('?');
if (questionMarkIndex >= 0 && questionMarkIndex < launchArgs.Length - 1)
{
queryString = launchArgs.Substring(questionMarkIndex + 1);
}
else
{
string path = launchArgs.Substring("minecraft://".Length).TrimStart('/');
if (!string.IsNullOrEmpty(path))
{
queryString = $"{path}=true";
}
}
}
else
{
// 如果传入的不是 minecraft:// 格式,直接作为参数
// 检查是否已经是 key=value 格式
if (launchArgs.Contains('=') && !launchArgs.Contains(' '))
{
queryString = launchArgs;
}
else if (launchArgs.Contains('=') && launchArgs.Contains(' '))
{
var pairs = launchArgs.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Where(arg => arg.Contains('='))
.Select(arg => arg.Trim())
.ToArray();
if (pairs.Length > 0)
{
queryString = string.Join("&", pairs);
}
else
{
queryString = $"args={Uri.EscapeDataString(launchArgs)}";
}
}
else
{
queryString = $"args={Uri.EscapeDataString(launchArgs)}";
}
}

if (!string.IsNullOrEmpty(queryString))
{
uriString = $"minecraft://launch?{queryString}";
}
}


await Launcher.LaunchUriAsync(new Uri(uriString), options_st);
}

Process[] processes = Process.GetProcessesByName("Minecraft.Windows");
Process[] process_oldVersion = Process.GetProcessesByName("Minecraft.Win10.DX11");
processes = processes.Concat(process_oldVersion).ToArray();
process = processes.OrderBy(p => p.StartTime).Last();
}

return process;

Check warning on line 472 in BedrockLauncher.Core/BedrockCore.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference return.
}

/// <summary>
/// Remove Uwp Minecraft Game
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions BedrockLauncher.Core/BedrockLauncher.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<Version>2.0.4.3-dev</Version>
<Version>2.0.4.4-dev</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<Version>2.0.4.3</Version>
<Version>2.0.4.4</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions TestProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ static void Main(string[] args)
bedrockCore.InitAsync().Wait();
var launchOptions = new LaunchOptions()
{
GameFolder = Path.GetFullPath(@"E:\Bedrock\bedrock_versions\1.21.11401"),
MinecraftBuildType = MinecraftBuildTypeVersion.UWP,
GameFolder = "D:\\BedrockBoot\\bedrock_versions\\1.21.11401",
GameType = MinecraftGameTypeVersion.Release,
LaunchArgs = null
MinecraftBuildType = MinecraftBuildTypeVersion.UWP,
LaunchArgs = "minecraft://creator/?Editor=true"
};
var process = bedrockCore.LaunchGameAsync(launchOptions).Result;
Console.WriteLine(process.Id);
Expand Down
Loading