JD2022-TU1/main/tools/framework/JD.Remoting/PlatformUtilities.cs

138 lines
No EOL
5.8 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using JD.Core;
namespace JD.Remoting
{
public static class PlatformUtilities
{
public static string GetSdkPath(PlatformId platform)
{
switch (platform)
{
case PlatformId.DURANGO:
return Environment.ExpandEnvironmentVariables("%DurangoXDK%");
default:
throw new NotImplementedException();
}
}
public static bool SelectConsole(PlatformId platform, string ip)
{
switch (platform)
{
case PlatformId.DURANGO:
var workingDirectory = Path.Combine(GetSdkPath(platform), "bin");
var xbconnectProcessInfo = new ProcessStartInfo();
xbconnectProcessInfo.FileName = Path.Combine(workingDirectory, "xbconnect.exe");
xbconnectProcessInfo.Arguments = ip;
xbconnectProcessInfo.CreateNoWindow = true;
xbconnectProcessInfo.UseShellExecute = false;
xbconnectProcessInfo.RedirectStandardOutput = true;
xbconnectProcessInfo.RedirectStandardError = true;
var xbConnectProcess = Process.Start(xbconnectProcessInfo);
xbConnectProcess?.WaitForExit();
return xbConnectProcess.StandardOutput.ReadToEnd().Contains($"Default console set to \"{ip}\" (devkit)");
default:
throw new NotImplementedException();
}
}
public enum ConnectionType
{
Debug,
Game
}
public static string GetConsoleIp(PlatformId platform, ConnectionType connectionType)
{
switch (platform)
{
case PlatformId.PC:
return "127.0.0.1";
case PlatformId.DURANGO:
// > xbconnect
// Connections at 10.31.4.205, client build 6.2.11274.0:
// HOST: 6.2.11274.0 (Durango Host OS) @10.31.4.205
// SYSTEM: 6.2.11274.0 (Durango System OS) @10.31.5.183
// TITLE: 6.2.11274.0 (Durango Title OS) @10.31.5.183
var workingDirectory = Path.Combine(GetSdkPath(platform), "bin");
var xbconnectProcessInfo = new ProcessStartInfo();
xbconnectProcessInfo.FileName = Path.Combine(workingDirectory, "xbconnect.exe");
xbconnectProcessInfo.Arguments = "";
xbconnectProcessInfo.CreateNoWindow = true;
xbconnectProcessInfo.UseShellExecute = false;
xbconnectProcessInfo.RedirectStandardOutput = true;
xbconnectProcessInfo.RedirectStandardError = true;
var xbConnectProcess = Process.Start(xbconnectProcessInfo);
xbConnectProcess.WaitForExit();
var log = xbConnectProcess.StandardOutput.ReadToEnd();
switch (connectionType)
{
case ConnectionType.Debug:
return Regex.Match(log, "HOST.*@(.*)\n").Groups[1].ToString().Trim('\r');
case ConnectionType.Game:
return Regex.Match(log, "SYSTEM.*@(.*)\n").Groups[1].ToString().Trim('\r');
default:
throw new ArgumentOutOfRangeException("connectionType");
}
default:
throw new NotImplementedException();
}
}
public static bool ProcessIsRunning(PlatformId platform, string processIdentifier)
{
switch (platform)
{
case PlatformId.DURANGO:
// xbapp query JustDance2016_0.0.0.0_x64__z1zgv8ff8e49g
// Package execution state: 1 (running)
var xbappProcessInfo = new ProcessStartInfo();
xbappProcessInfo.FileName = Path.Combine(GetSdkPath(platform), "bin", "xbapp.exe");
xbappProcessInfo.Arguments = $"query {processIdentifier}";
xbappProcessInfo.CreateNoWindow = true;
xbappProcessInfo.UseShellExecute = false;
xbappProcessInfo.RedirectStandardOutput = true;
xbappProcessInfo.RedirectStandardError = true;
var xbAppProcess = Process.Start(xbappProcessInfo);
xbAppProcess.WaitForExit();
var log = xbAppProcess.StandardOutput.ReadToEnd();
return log.Contains("running") || log.Contains("constrained");
default:
throw new NotImplementedException();
}
}
public class DurangoPackageInfo
{
public string PackageGameName { get; set; }
public string PackageVersion { get; set; }
public string PackageAssembly { get; set; }
public string PackageHash { get; set; }
}
public static DurangoPackageInfo ParseDurangoPackageFullName(string packageFullName)
{
var match = Regex.Match(packageFullName, @"(.*)_(\d+\.\d+\.\d+\.\d+)_(.*)__(.*)");
if (!match.Success)
throw new Exception($"Cannot retrieve package hash from {packageFullName}");
var packageInfo = new DurangoPackageInfo
{
PackageGameName = match.Groups[1].ToString(),
PackageVersion = match.Groups[2].ToString(),
PackageAssembly = match.Groups[3].ToString(),
PackageHash = match.Groups[4].ToString()
};
// x64, neutral
return packageInfo;
}
}
}