This commit is contained in:
HolographicHat
2022-06-13 17:43:02 +08:00
parent e847d4c80e
commit bbe7c2cd03
6 changed files with 145 additions and 13 deletions

View File

@@ -1,11 +1,81 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using YaeAchievement.Win32;
using static YaeAchievement.Win32.OpenFileFlags;
namespace YaeAchievement;
public static class Utils {
public static void LoadConfig() {
var conf = JsonNode.Parse(File.ReadAllText(GlobalVars.ConfigFileName))!;
var path = conf["genshinPath"];
if (path == null || CheckGamePathValid(path.GetValue<string>())) {
GlobalVars.GamePath = SelectGameExecutable();
conf["genshinPath"] = GlobalVars.GamePath;
File.WriteAllText(GlobalVars.ConfigFileName, conf.ToJsonString());
} else {
GlobalVars.GamePath = path.GetValue<string>();
}
}
private static bool CheckGamePathValid(string path) {
var dir = Path.GetDirectoryName(path)!;
return File.Exists($"{dir}/UnityPlayer.dll") && File.Exists($"{dir}/mhypbase.dll");
}
private static string SelectGameExecutable() {
var fnPtr = Marshal.AllocHGlobal(32768);
var ofn = new OpenFileName {
file = fnPtr,
size = Marshal.SizeOf<OpenFileName>(),
owner = Native.GetConsoleWindow(),
flags = Explorer | NoNetworkButton | FileMustExist | NoChangeDir,
title = "选择主程序",
filter = "国服/国际服主程序 (YuanShen/GenshinImpact.exe)\0YuanShen.exe;GenshinImpact.exe\0",
maxFile = 32768
};
if(!Native.GetOpenFileName(ofn)) {
var err = Native.CommDlgExtendedError();
if (err != 0) {
throw new SystemException($"Dialog error: {err}");
}
Console.WriteLine("操作被取消");
Environment.Exit(0);
}
var path = Marshal.PtrToStringAuto(fnPtr)!;
Marshal.FreeHGlobal(fnPtr);
return path;
}
public static void CheckGenshinIsRunning() {
Process.EnterDebugMode();
foreach (var process in Process.GetProcesses()) {
if (process.ProcessName is "GenshinImpact" or "YuanShen") {
Console.WriteLine("原神正在运行,请关闭后重试");
Environment.Exit(301);
}
}
Process.LeaveDebugMode();
}
public static void InstallExitHook() {
AppDomain.CurrentDomain.ProcessExit += (_, _) => {
Console.WriteLine("按任意键退出");
Console.ReadKey();
};
}
public static void InstallExceptionHook() {
AppDomain.CurrentDomain.UnhandledException += (_, e) => {
Console.WriteLine(e.ExceptionObject.ToString());
Console.WriteLine("发生错误,请联系开发者以获取帮助");
Environment.Exit(-1);
};
}
// ReSharper disable once UnusedMethodReturnValue.Global
public static Thread StartAndWaitResult(string exePath, Func<string, bool> onReceive) {
@@ -31,7 +101,7 @@ public static class Utils {
proc.EnableRaisingEvents = true;
proc.Exited += (_, _) => {
if (GlobalVars.UnexpectedExit) {
Console.WriteLine($"Game process exit at {proc.ExitTime:HH:mm:ss}");
Console.WriteLine("游戏进程异常退出");
Environment.Exit(114514);
}
};