Add AppConfig

This commit is contained in:
HolographicHat
2022-08-19 18:09:30 +08:00
parent b2111db4eb
commit 9b0c384d4b
2 changed files with 41 additions and 2 deletions

41
src/AppConfig.cs Normal file
View File

@@ -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<AppConfig>(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));
}
}

View File

@@ -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";
}