ReadAtLeast

This commit is contained in:
HolographicHat
2025-08-04 16:19:33 +08:00
parent 45638b7327
commit 3f42156b20
2 changed files with 11 additions and 15 deletions

View File

@@ -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<byte> 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<byte> 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];

View File

@@ -259,11 +259,15 @@ public static class Utils {
AnsiConsole.WriteLine(App.GameLoading, _proc.Id);
}
private static uint GetGameHash(string exePath) {
Span<byte> 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<byte> 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) {