From 3f42156b20d82b1842d1eb6b8fac68f790102b85 Mon Sep 17 00:00:00 2001 From: HolographicHat Date: Mon, 4 Aug 2025 16:19:33 +0800 Subject: [PATCH] ReadAtLeast --- YaeAchievement/src/AppConfig.cs | 12 ++---------- YaeAchievement/src/Utils.cs | 14 +++++++++----- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/YaeAchievement/src/AppConfig.cs b/YaeAchievement/src/AppConfig.cs index c3d254b..d6e4548 100644 --- a/YaeAchievement/src/AppConfig.cs +++ b/YaeAchievement/src/AppConfig.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using Spectre.Console; @@ -23,12 +22,7 @@ public static partial class AppConfig { } else { GamePath = ReadGamePathFromProcess(); } - Span buffer = stackalloc byte[0x10000]; - using var stream = File.OpenRead(GamePath); - if (stream.Read(buffer) == buffer.Length) { - var hash = Convert.ToHexString(MD5.HashData(buffer)); - CacheFile.Write("genshin_impact_game_path_v2", Encoding.UTF8.GetBytes($"{GamePath}\u1145{hash}")); - } + CacheFile.Write("genshin_impact_game_path_v2", Encoding.UTF8.GetBytes($"{GamePath}\u1145{Utils.GetGameHash(GamePath)}")); SentrySdk.AddBreadcrumb(GamePath.EndsWith("YuanShen.exe") ? "CN" : "OS", "GamePath"); return; static bool TryReadGamePathFromCache([NotNullWhen(true)] out string? path) { @@ -38,9 +32,7 @@ public static partial class AppConfig { return false; } var cacheData = cacheFile.Content.ToStringUtf8().Split("\u1145"); - Span buffer = stackalloc byte[0x10000]; - using var stream = File.OpenRead(cacheData[0]); - if (stream.Read(buffer) != buffer.Length || Convert.ToHexString(MD5.HashData(buffer)) != cacheData[1]) { + if (Utils.GetGameHash(cacheData[0]) != uint.Parse(cacheData[1])) { return false; } path = cacheData[0]; diff --git a/YaeAchievement/src/Utils.cs b/YaeAchievement/src/Utils.cs index 532447c..449722a 100644 --- a/YaeAchievement/src/Utils.cs +++ b/YaeAchievement/src/Utils.cs @@ -259,11 +259,15 @@ public static class Utils { AnsiConsole.WriteLine(App.GameLoading, _proc.Id); } - private static uint GetGameHash(string exePath) { - Span buffer = stackalloc byte[0x10000]; - using var stream = File.OpenRead(exePath); - _ = stream.Read(buffer); - return Crc32.Compute(buffer); + public static uint GetGameHash(string exePath) { + try { + Span buffer = stackalloc byte[0x10000]; + using var stream = File.OpenRead(exePath); + _ = stream.ReadAtLeast(buffer, 0x10000, false); + return Crc32.Compute(buffer); + } catch (IOException) { + return 0xFFFFFFFF; + } } internal static unsafe void SetQuickEditMode(bool enable) {