diff --git a/src/AppConfig.cs b/src/AppConfig.cs new file mode 100644 index 0000000..c797177 --- /dev/null +++ b/src/AppConfig.cs @@ -0,0 +1,41 @@ +using Newtonsoft.Json; + +namespace YaeAchievement; + +public class AppConfig { + + [JsonProperty(PropertyName = "location")] + public string? Location { get; set; } + + private static AppConfig? _instance; + + private static readonly string FileName = Path.Combine(GlobalVars.AppPath, "conf.json"); + + public static string GamePath => _instance!.Location!; + + internal static void Load() { + if (File.Exists(FileName)) { + var text = File.ReadAllText(FileName); + _instance = JsonConvert.DeserializeObject(text)!; + } + if (_instance?.Location == null || !Utils.CheckGamePathValid(_instance.Location)) { + var gameInstallPath = Utils.FindGamePathFromRegistry(); + if (!string.IsNullOrEmpty(gameInstallPath)) { + Console.WriteLine($"自动读取到游戏路径: {gameInstallPath}"); + Console.WriteLine($"如果确认路径无误,请按 Y ;若有误或需要自行选择,请按 N "); + var key = Console.ReadKey().Key; + gameInstallPath = key == ConsoleKey.Y ? gameInstallPath : Utils.SelectGameExecutable(); + } else { + gameInstallPath = Utils.SelectGameExecutable(); + } + _instance = new AppConfig { + Location = gameInstallPath + }; + Save(); + } + } + + public static void Save() { + File.WriteAllText(FileName, JsonConvert.SerializeObject(_instance!, Formatting.Indented)); + } +} diff --git a/src/GlobalVars.cs b/src/GlobalVars.cs index ebcf4ff..1210e0e 100644 --- a/src/GlobalVars.cs +++ b/src/GlobalVars.cs @@ -12,7 +12,6 @@ public static class GlobalVars { public static bool DebugProxy = false; public static bool CheckGamePath = true; public static bool UnexpectedExit = true; - public static string GamePath = null!; public static Version AppVersion = Assembly.GetEntryAssembly()!.GetName().Version!; public static readonly string AppPath = AppDomain.CurrentDomain.BaseDirectory; @@ -21,6 +20,5 @@ public static class GlobalVars { public const string LibName = "YaeLib.dll"; public const string PipeName = "YaeAchievementPipe"; public const string BucketHost = "https://cn-cd-1259389942.file.myqcloud.com"; - public const string ConfigFileName = "YaeAchievement.runtimeconfig.json"; }