From baf52720bb780d9a4c2ba413eb0cceb96d128f17 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sun, 7 Jan 2024 21:38:53 +0100 Subject: [PATCH] Fix to prevent config corruption --- app/AppConfig.cs | 44 +++++++++++++++++++++++++++++++++----------- app/AsusACPI.cs | 2 +- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/AppConfig.cs b/app/AppConfig.cs index 83602fd4..cc2cc6ec 100644 --- a/app/AppConfig.cs +++ b/app/AppConfig.cs @@ -1,5 +1,4 @@ using GHelper.Mode; -using System.Diagnostics; using System.Management; using System.Text.Json; @@ -13,6 +12,7 @@ public static class AppConfig private static string? _bios; private static Dictionary config = new Dictionary(); + private static System.Timers.Timer timer = new System.Timers.Timer(1000); static AppConfig() { @@ -52,8 +52,39 @@ public static class AppConfig Init(); } + timer.Elapsed += Timer_Elapsed; + } + private static void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) + { + + timer.Stop(); + string jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); + var backup = configFile + ".bak"; + + try + { + File.WriteAllText(backup, jsonString); + } + catch (Exception ex) + { + Logger.WriteLine(ex.ToString()); + return; + } + + Thread.Sleep(500); + + if (File.ReadAllText(backup).Contains("}")) + { + File.Copy(backup, configFile, true); + } + else + { + Logger.WriteLine("Error writing config"); + } + + } public static string GetModel() { @@ -157,16 +188,7 @@ public static class AppConfig private static void Write() { - string jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); - try - { - File.WriteAllText(configFile + ".bak", jsonString); - File.Copy(configFile + ".bak", configFile, true); - } - catch (Exception e) - { - Debug.Write(e.ToString()); - } + timer.Start(); } public static void Set(string name, int value) diff --git a/app/AsusACPI.cs b/app/AsusACPI.cs index 709c1167..fc6e20b4 100644 --- a/app/AsusACPI.cs +++ b/app/AsusACPI.cs @@ -556,7 +556,7 @@ public class AsusACPI public bool IsNVidiaGPU() { - return (!IsAllAmdPPT() && Program.acpi.DeviceGet(GPUEco) >= 0); + return (!IsAllAmdPPT() && Program.acpi.DeviceGet(GPUEco) >= 0 && !AppConfig.IsAlly()); } public void SetAPUMem(int memory = 4)