This commit is contained in:
HolographicHat
2022-08-19 18:09:49 +08:00
parent 9b0c384d4b
commit 2f1a5ad99e
2 changed files with 20 additions and 33 deletions

View File

@@ -6,6 +6,7 @@ using static YaeAchievement.Utils;
InstallExitHook(); InstallExitHook();
CheckVcRuntime(); CheckVcRuntime();
CheckIsTempDir();
CheckSelfIsRunning(); CheckSelfIsRunning();
TryDisableQuickEdit(); TryDisableQuickEdit();
InstallExceptionHook(); InstallExceptionHook();
@@ -16,7 +17,7 @@ Console.WriteLine($"YaeAchievement - 原神成就导出工具 ({GlobalVars.AppVe
Console.WriteLine("https://github.com/HolographicHat/YaeAchievement"); Console.WriteLine("https://github.com/HolographicHat/YaeAchievement");
Console.WriteLine("----------------------------------------------------"); Console.WriteLine("----------------------------------------------------");
LoadConfig(); AppConfig.Load();
CheckUpdate(); CheckUpdate();
AppCenter.Init(); AppCenter.Init();
new EventLog("AppInit") { new EventLog("AppInit") {
@@ -31,7 +32,7 @@ if (historyCache.LastWriteTime.AddMinutes(10) > DateTime.UtcNow) {
Console.WriteLine("要重新获取数据,手动删除 cache\\d1a8ef40a67a5929.miko 后重新启动 YaeAchievement"); Console.WriteLine("要重新获取数据,手动删除 cache\\d1a8ef40a67a5929.miko 后重新启动 YaeAchievement");
Export.Choose(AchievementAllDataNotify.Parser.ParseFrom(historyCache.Read().Content)); Export.Choose(AchievementAllDataNotify.Parser.ParseFrom(historyCache.Read().Content));
} else { } else {
StartAndWaitResult(GlobalVars.GamePath, str => { StartAndWaitResult(AppConfig.GamePath, str => {
GlobalVars.UnexpectedExit = false; GlobalVars.UnexpectedExit = false;
var data = Convert.FromBase64String(str); var data = Convert.FromBase64String(str);
var list = AchievementAllDataNotify.Parser.ParseFrom(data); var list = AchievementAllDataNotify.Parser.ParseFrom(data);

View File

@@ -4,7 +4,6 @@ using System.IO.Pipes;
using System.Net; using System.Net;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using Microsoft.Win32; using Microsoft.Win32;
using YaeAchievement.AppCenterSDK; using YaeAchievement.AppCenterSDK;
using YaeAchievement.Win32; using YaeAchievement.Win32;
@@ -65,26 +64,6 @@ public static class Utils {
} }
} }
public static void LoadConfig() {
var conf = JsonNode.Parse(File.ReadAllText(GlobalVars.ConfigFileName))!;
var path = conf["location"];
if (path == null || !CheckGamePathValid(path.GetValue<string>())) {
var gameInstallPath = FindGamePathFromRegistry();
if (!string.IsNullOrEmpty(gameInstallPath)) {
Console.WriteLine($"自动读取到游戏路径: {gameInstallPath}");
Console.WriteLine($"如果确认路径无误,请按 Y ;若有误或需要自行选择,请按 N ");
var key = Console.ReadKey().Key;
GlobalVars.GamePath = key == ConsoleKey.Y ? gameInstallPath : SelectGameExecutable();
} else {
GlobalVars.GamePath = SelectGameExecutable();
}
conf["location"] = GlobalVars.GamePath;
File.WriteAllText(GlobalVars.ConfigFileName, conf.ToJsonString());
} else {
GlobalVars.GamePath = path.GetValue<string>();
}
}
public static void CheckUpdate() { public static void CheckUpdate() {
var info = UpdateInfo.Parser.ParseFrom(GetBucketFileAsByteArray("schicksal/version"))!; var info = UpdateInfo.Parser.ParseFrom(GetBucketFileAsByteArray("schicksal/version"))!;
if (GlobalVars.AppVersionCode != info.VersionCode) { if (GlobalVars.AppVersionCode != info.VersionCode) {
@@ -120,6 +99,13 @@ public static class Utils {
Process.LeaveDebugMode(); Process.LeaveDebugMode();
} }
public static void CheckIsTempDir() {
if (GlobalVars.AppPath.Contains(Path.GetTempPath())) {
Console.WriteLine("请将程序完整解压后再运行");
Environment.Exit(303);
}
}
public static bool ShellOpen(string path) { public static bool ShellOpen(string path) {
return new Process { return new Process {
StartInfo = { StartInfo = {
@@ -129,12 +115,13 @@ public static class Utils {
}.Start(); }.Start();
} }
private static bool CheckGamePathValid(string path) { public static bool CheckGamePathValid(string? path) {
if (path == null) return false;
var dir = Path.GetDirectoryName(path)!; var dir = Path.GetDirectoryName(path)!;
return !GlobalVars.CheckGamePath || File.Exists($"{dir}/UnityPlayer.dll"); return !GlobalVars.CheckGamePath || File.Exists(Path.Combine(dir, "UnityPlayer.dll"));
} }
private static string SelectGameExecutable() { public static string SelectGameExecutable() {
var fnPtr = Marshal.AllocHGlobal(32768); var fnPtr = Marshal.AllocHGlobal(32768);
Native.RtlZeroMemory(fnPtr, 32768); Native.RtlZeroMemory(fnPtr, 32768);
var ofn = new OpenFileName { var ofn = new OpenFileName {
@@ -270,19 +257,18 @@ public static class Utils {
/// 从注册表中寻找安装路径 暂时只支持国服 /// 从注册表中寻找安装路径 暂时只支持国服
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private static string? FindGamePathFromRegistry() { public static string? FindGamePathFromRegistry() {
try { try {
using var root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); using var root = Registry.LocalMachine;
using var key = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\原神"); using var sub = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\原神");
if (key == null) { if (sub == null) {
return null; return null;
} }
var installLocation = key.GetValue("InstallPath")?.ToString(); var installLocation = sub.GetValue("InstallPath")?.ToString();
if (!string.IsNullOrEmpty(installLocation)) { if (!string.IsNullOrEmpty(installLocation)) {
var folder = Path.Combine(installLocation, "Genshin Impact Game\\"); var folder = Path.Combine(installLocation, "Genshin Impact Game\\");
var 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(exePath)) {
&& File.Exists(exePath)) {
return exePath; return exePath;
} }
} }