This commit is contained in:
HolographicHat
2022-07-16 01:41:11 +08:00
parent 4c3e9d8e50
commit 41863c32f7

View File

@@ -103,19 +103,17 @@ public static class Utils {
var conf = JsonNode.Parse(File.ReadAllText(GlobalVars.ConfigFileName))!; var conf = JsonNode.Parse(File.ReadAllText(GlobalVars.ConfigFileName))!;
var path = conf["location"]; var path = conf["location"];
if (path == null || !CheckGamePathValid(path.GetValue<string>())) { if (path == null || !CheckGamePathValid(path.GetValue<string>())) {
string? gameInstallPath = FindGamePathFromRegistry(); var gameInstallPath = FindGamePathFromRegistry();
if (!string.IsNullOrEmpty(gameInstallPath)) { if (!string.IsNullOrEmpty(gameInstallPath)) {
Console.WriteLine($"自动读取到游戏路径: {gameInstallPath}"); Console.WriteLine($"自动读取到游戏路径: {gameInstallPath}");
Console.WriteLine($"如果确认路径无误,请输入 Y 并回车;如果想要自行选择游戏路径请输入 N 并回车"); Console.WriteLine($"如果确认路径无误,请 Y ;若有误或需要自行选择,请按 N ");
string? s = Console.ReadLine(); var key = Console.ReadKey().Key;
if(string.Equals(s?.Trim().ToUpper(), "N")) { GlobalVars.GamePath = key == ConsoleKey.Y ? gameInstallPath : SelectGameExecutable();
GlobalVars.GamePath = SelectGameExecutable(); } else {
} else { GlobalVars.GamePath = SelectGameExecutable();
GlobalVars.GamePath = gameInstallPath;
}
conf["location"] = GlobalVars.GamePath;
File.WriteAllText(GlobalVars.ConfigFileName, conf.ToJsonString());
} }
conf["location"] = GlobalVars.GamePath;
File.WriteAllText(GlobalVars.ConfigFileName, conf.ToJsonString());
} else { } else {
GlobalVars.GamePath = path.GetValue<string>(); GlobalVars.GamePath = path.GetValue<string>();
} }
@@ -217,25 +215,22 @@ public static class Utils {
/// <returns></returns> /// <returns></returns>
private static string? FindGamePathFromRegistry() { private static string? FindGamePathFromRegistry() {
try { try {
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) { using var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\原神")) { using var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\原神");
if (key == null) { if (key == null) {
return null; return null;
} }
object? installLocation = key.GetValue("InstallPath"); var installLocation = key.GetValue("InstallPath")?.ToString();
if (installLocation != null && !string.IsNullOrEmpty(installLocation.ToString())) { if (!string.IsNullOrEmpty(installLocation)) {
string folder = Path.Combine(installLocation.ToString(), "Genshin Impact Game\\"); var folder = Path.Combine(installLocation, "Genshin Impact Game\\");
string exePath = Path.Combine(folder, "YuanShen.exe"); var exePath = Path.Combine(folder, "YuanShen.exe");
if (File.Exists(Path.Combine(folder, "UnityPlayer.dll")) if (File.Exists(Path.Combine(folder, "UnityPlayer.dll"))
&& File.Exists(Path.Combine(folder, "mhypbase.dll")) && File.Exists(Path.Combine(folder, "mhypbase.dll"))
&& File.Exists(exePath)) { && File.Exists(exePath)) {
return exePath; return exePath;
}
}
} }
} }
} } catch (Exception e) {
catch (Exception e) {
Logger.Warn(e.Message); Logger.Warn(e.Message);
} }
return null; return null;
@@ -284,6 +279,9 @@ public static class Utils {
public static Thread StartAndWaitResult(string exePath, Func<string, bool> onReceive) { public static Thread StartAndWaitResult(string exePath, Func<string, bool> onReceive) {
const string lib = "C:/ProgramData/yae.dll"; const string lib = "C:/ProgramData/yae.dll";
File.Copy(Path.GetFullPath(GlobalVars.LibName), lib, true); File.Copy(Path.GetFullPath(GlobalVars.LibName), lib, true);
AppDomain.CurrentDomain.ProcessExit += (_, _) => {
File.Delete(lib);
};
if (!Injector.CreateProcess(exePath, out var hProcess, out var hThread, out var pid)) { if (!Injector.CreateProcess(exePath, out var hProcess, out var hThread, out var pid)) {
Environment.Exit(new Win32Exception().PrintMsgAndReturnErrCode("ICreateProcess fail")); Environment.Exit(new Win32Exception().PrintMsgAndReturnErrCode("ICreateProcess fail"));
} }
@@ -302,9 +300,6 @@ public static class Utils {
Environment.Exit(114514); Environment.Exit(114514);
} }
}; };
AppDomain.CurrentDomain.ProcessExit += (_, _) => {
File.Delete(lib);
};
if (Native.ResumeThread(hThread) == 0xFFFFFFFF) { if (Native.ResumeThread(hThread) == 0xFFFFFFFF) {
var e = new Win32Exception(); var e = new Win32Exception();
if (!Native.TerminateProcess(hProcess, 0)) { if (!Native.TerminateProcess(hProcess, 0)) {