This commit is contained in:
HolographicHat
2023-04-02 18:23:58 +08:00
parent a72007ffa6
commit 58dcd5b228
7 changed files with 69 additions and 29 deletions

9
res/App.Designer.cs generated
View File

@@ -246,6 +246,15 @@ namespace YaeAchievement.res {
}
}
/// <summary>
/// Looks up a localized string similar to No write permission on {0}..
/// </summary>
internal static string NoWritePermission {
get {
return ResourceManager.GetString("NoWritePermission", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Press any key to exit..
/// </summary>

View File

@@ -126,4 +126,7 @@ Input a number (0-5): </value>
<data name="GenshinHashError" xml:space="preserve">
<value>Please update genshin and retry.</value>
</data>
<data name="NoWritePermission" xml:space="preserve">
<value>No write permission on {0}.</value>
</data>
</root>

View File

@@ -117,4 +117,7 @@
<data name="GenshinHashError" xml:space="preserve">
<value>请将原神更新至最新版本后重试</value>
</data>
<data name="NoWritePermission" xml:space="preserve">
<value>无法写入文件,请更换软件所在目录后重试</value>
</data>
</root>

View File

@@ -85,8 +85,9 @@ public static class Export {
// ReSharper disable once InconsistentNaming
private static void ToUIAFJson(AchievementAllDataNotify data) {
var path = Path.GetFullPath($"uiaf-{DateTime.Now:yyyyMMddHHmmss}.json");
File.WriteAllText(path, JsonSerializer.Serialize(ExportToUIAFApp(data)));
Console.WriteLine(App.ExportToFileSuccess, path);
if (TryWriteToFile(path, JsonSerializer.Serialize(ExportToUIAFApp(data)))) {
Console.WriteLine(App.ExportToFileSuccess, path);
}
}
private static void ToPaimon(AchievementAllDataNotify data) {
@@ -105,8 +106,9 @@ public static class Export {
["achievement"] = output.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value)
};
var path = Path.GetFullPath($"export-{DateTime.Now:yyyyMMddHHmmss}-paimon.json");
File.WriteAllText(path, JsonSerializer.Serialize(final));
Console.WriteLine(App.ExportToFileSuccess, path);
if (TryWriteToFile(path, JsonSerializer.Serialize(final))) {
Console.WriteLine(App.ExportToFileSuccess, path);
}
}
private static void ToSeelie(AchievementAllDataNotify data) {
@@ -120,8 +122,9 @@ public static class Export {
["achievements"] = output.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value)
};
var path = Path.GetFullPath($"export-{DateTime.Now:yyyyMMddHHmmss}-seelie.json");
File.WriteAllText(path, JsonSerializer.Serialize(final));
Console.WriteLine(App.ExportToFileSuccess, path);
if (TryWriteToFile(path, JsonSerializer.Serialize(final))) {
Console.WriteLine(App.ExportToFileSuccess, path);
}
}
// ReSharper disable once InconsistentNaming
@@ -151,9 +154,10 @@ public static class Export {
return item.JoinToString(",");
}));
var path = Path.GetFullPath($"achievement-{DateTime.Now:yyyyMMddHHmmss}.csv");
File.WriteAllText(path, $"\uFEFF{string.Join("\n", output)}");
Console.WriteLine(App.ExportToFileSuccess, path);
Process.Start("explorer.exe", $"{Path.GetDirectoryName(path)}");
if (TryWriteToFile(path, $"\uFEFF{string.Join("\n", output)}")) {
Console.WriteLine(App.ExportToFileSuccess, path);
Process.Start("explorer.exe", $"{Path.GetDirectoryName(path)}");
}
}
private static void ToXunkong(AchievementAllDataNotify data) {
@@ -169,10 +173,12 @@ public static class Export {
private static void ToRawJson(AchievementAllDataNotify data) {
var path = Path.GetFullPath($"export-{DateTime.Now:yyyyMMddHHmmss}-raw.json");
File.WriteAllText(path, JsonSerializer.Serialize(data, new JsonSerializerOptions {
var text = JsonSerializer.Serialize(data, new JsonSerializerOptions {
WriteIndented = true
}));
Console.WriteLine(App.ExportToFileSuccess, path);
});
if (TryWriteToFile(path, text)) {
Console.WriteLine(App.ExportToFileSuccess, path);
}
}
// ReSharper disable once InconsistentNaming
@@ -228,4 +234,14 @@ public static class Export {
AppCenter.TrackCrash(ex, false);
return ex.NativeErrorCode;
}
private static bool TryWriteToFile(string path, string contents) {
try {
File.WriteAllText(path, contents);
return true;
} catch (UnauthorizedAccessException) {
Console.WriteLine(App.NoWritePermission, path);
return false;
}
}
}

View File

@@ -7,6 +7,7 @@ using Windows.Win32.System.Threading;
namespace YaeAchievement;
public static class Injector {
public static unsafe bool CreateProcess(string path, out HANDLE hProc, out HANDLE hThread, out uint pid) {
Span<char> cmdLines = stackalloc char[1]; // "\0"
var si = new STARTUPINFOW {

View File

@@ -9,8 +9,8 @@ TryDisableQuickEdit();
InstallExitHook();
InstallExceptionHook();
await CheckVcRuntime();
CheckSelfIsRunning();
await CheckVcRuntime();
CheckGenshinIsRunning();
Console.WriteLine(@"----------------------------------------------------");

View File

@@ -50,7 +50,7 @@ public static class Utils {
cacheFile.Write(responseBytes, etag);
}
return responseBytes;
} catch (Exception e) {
} catch (Exception e) when(e is SocketException or TaskCanceledException) {
Console.WriteLine(App.NetworkError, e.Message);
Environment.Exit(-1);
return null!;
@@ -73,8 +73,8 @@ public static class Utils {
if (Native.OpenClipboard(HWND.Null))
{
Native.EmptyClipboard();
HANDLE hGlobal = (HANDLE)Marshal.AllocHGlobal((text.Length + 1) * 2);
IntPtr hPtr = (IntPtr)Native.GlobalLock(hGlobal);
var hGlobal = (HANDLE) Marshal.AllocHGlobal((text.Length + 1) * 2);
var hPtr = (nint) Native.GlobalLock(hGlobal);
Marshal.Copy(text.ToCharArray(), 0, hPtr, text.Length);
Native.GlobalUnlock(hPtr);
Native.SetClipboardData(13, hGlobal);
@@ -121,15 +121,17 @@ public static class Utils {
}
public static void CheckSelfIsRunning() {
Process.EnterDebugMode();
var cur = Process.GetCurrentProcess();
foreach (var process in Process.GetProcesses().Where(process => process.Id != cur.Id)) {
if (process.ProcessName == cur.ProcessName) {
Console.WriteLine(App.AnotherInstance);
Environment.Exit(302);
try {
Process.EnterDebugMode();
var cur = Process.GetCurrentProcess();
foreach (var process in Process.GetProcesses().Where(process => process.Id != cur.Id)) {
if (process.ProcessName == cur.ProcessName) {
Console.WriteLine(App.AnotherInstance);
Environment.Exit(302);
}
}
}
Process.LeaveDebugMode();
Process.LeaveDebugMode();
} catch (Win32Exception) {}
}
// ReSharper disable once UnusedMethodReturnValue.Global
@@ -162,7 +164,7 @@ public static class Utils {
foreach (var process in Process.GetProcesses()) {
if (process.ProcessName is "GenshinImpact" or "YuanShen"
&& !process.HasExited
&& process.MainWindowHandle != IntPtr.Zero
&& process.MainWindowHandle != nint.Zero
) {
Console.WriteLine(App.GenshinIsRunning, process.Id);
Environment.Exit(301);
@@ -212,8 +214,9 @@ public static class Utils {
#if DEBUG
return true;
#else
if (!File.Exists(path)) return false;
var hash = File.ReadAllBytes(path).MD5Hash();
return File.Exists(path) && (hash == _updateInfo.CurrentCNGameHash || hash == _updateInfo.CurrentOSGameHash);
return hash == _updateInfo.CurrentCNGameHash || hash == _updateInfo.CurrentOSGameHash;
#endif
}
@@ -282,8 +285,6 @@ public static class Utils {
th.Start();
return th;
}
#pragma warning disable CA1416
public static async Task CheckVcRuntime() {
using var root = Registry.LocalMachine;
@@ -301,7 +302,14 @@ public static class Utils {
if (!installed) {
Console.WriteLine(App.VcRuntimeDownload);
var pkgPath = Path.Combine(GlobalVars.DataPath, "vc_redist.x64.exe");
var bytes = await CHttpClient.GetByteArrayAsync("https://aka.ms/vs/17/release/vc_redist.x64.exe");
byte[] bytes;
try {
bytes = await CHttpClient.GetByteArrayAsync("https://aka.ms/vs/17/release/vc_redist.x64.exe");
} catch (Exception e) when(e is SocketException or TaskCanceledException) {
Console.WriteLine(App.NetworkError, e.Message);
Environment.Exit(-1);
return;
}
await File.WriteAllBytesAsync(pkgPath, bytes);
Console.WriteLine(App.VcRuntimeInstalling);
using var process = new Process {