diff --git a/BililiveRecorder.Cli/Program.cs b/BililiveRecorder.Cli/Program.cs index ac8f6b1..395e16f 100644 --- a/BililiveRecorder.Cli/Program.cs +++ b/BililiveRecorder.Cli/Program.cs @@ -117,13 +117,18 @@ namespace BililiveRecorder.Cli path = Path.GetFullPath(path); + ConfigV3? config; + if (args.ConfigOverride is not null) { logger.Information("Using config from {ConfigOverride}", args.ConfigOverride); - path = args.ConfigOverride; + config = ConfigParser.LoadFromFile(args.ConfigOverride); + } + else + { + config = ConfigParser.LoadFromDirectory(path); } - var config = ConfigParser.LoadFrom(path); if (config is null) { logger.Error("Config Loading Failed"); diff --git a/BililiveRecorder.Core/Config/ConfigParser.cs b/BililiveRecorder.Core/Config/ConfigParser.cs index 3d9c5ae..31ba787 100644 --- a/BililiveRecorder.Core/Config/ConfigParser.cs +++ b/BililiveRecorder.Core/Config/ConfigParser.cs @@ -18,26 +18,31 @@ namespace BililiveRecorder.Core.Config NullValueHandling = NullValueHandling.Ignore }; - public static V3.ConfigV3? LoadFrom(string directory) + public static V3.ConfigV3? LoadFromDirectory(string directory) + { + if (!Directory.Exists(directory)) + { + logger.Warning("目标文件夹不存在"); + return null; + } + + var path = Path.Combine(directory, CONFIG_FILE_NAME); + + return LoadFromFile(path); + } + + public static V3.ConfigV3? LoadFromFile(string path) { try { - if (!Directory.Exists(directory)) + if (!File.Exists(path)) { - logger.Warning("目标文件夹不存在"); - return null; - } - - var filepath = Path.Combine(directory, CONFIG_FILE_NAME); - - if (!File.Exists(filepath)) - { - logger.Information("初始化默认设置,因为配置文件不存在 {Path}", filepath); + logger.Information("初始化默认设置,因为配置文件不存在 {Path}", path); return new V3.ConfigV3(); } - logger.Debug("Loading config from {Path}", filepath); - var json = File.ReadAllText(filepath, Encoding.UTF8); + logger.Debug("Loading config from {Path}", path); + var json = File.ReadAllText(path, Encoding.UTF8); return LoadJson(json); } diff --git a/BililiveRecorder.WPF/Pages/RootPage.xaml.cs b/BililiveRecorder.WPF/Pages/RootPage.xaml.cs index 09b3545..15143b5 100644 --- a/BililiveRecorder.WPF/Pages/RootPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/RootPage.xaml.cs @@ -240,7 +240,7 @@ You can uninstall me in system settings.", "安装成功 Installed", MessageBoxB catch (Exception) { } // 加载配置文件 - var config = ConfigParser.LoadFrom(path); + var config = ConfigParser.LoadFromDirectory(path); if (config is null) { error = WorkDirectorySelectorDialog.WorkDirectorySelectorDialogError.FailedToLoadConfig; diff --git a/test/BililiveRecorder.Core.UnitTests/Expectations/PublicApi.HasNoChangesAsync.verified.txt b/test/BililiveRecorder.Core.UnitTests/Expectations/PublicApi.HasNoChangesAsync.verified.txt index cadefdf..28cf704 100644 --- a/test/BililiveRecorder.Core.UnitTests/Expectations/PublicApi.HasNoChangesAsync.verified.txt +++ b/test/BililiveRecorder.Core.UnitTests/Expectations/PublicApi.HasNoChangesAsync.verified.txt @@ -30,10 +30,11 @@ namespace BililiveRecorder.Core.Config { public const string CONFIG_FILE_NAME = "config.json"; public ConfigParser() { } - public static BililiveRecorder.Core.Config.V3.ConfigV3? LoadFrom(string directory) { } + public static BililiveRecorder.Core.Config.V3.ConfigV3? LoadFromDirectory(string directory) { } + public static BililiveRecorder.Core.Config.V3.ConfigV3? LoadFromFile(string path) { } public static BililiveRecorder.Core.Config.V3.ConfigV3? LoadJson(string json) { } + public static bool Save(BililiveRecorder.Core.Config.V3.ConfigV3 config) { } public static string? SaveJson(BililiveRecorder.Core.Config.V3.ConfigV3 config) { } - public static bool SaveTo(string directory, BililiveRecorder.Core.Config.V3.ConfigV3 config) { } public static void WriteAllTextWithBackup(string path, string contents) { } } public enum CuttingMode @@ -61,6 +62,8 @@ namespace BililiveRecorder.Core.Config.V3 { public ConfigV3() { } [Newtonsoft.Json.JsonIgnore] + public string? ConfigPathOverride { get; set; } + [Newtonsoft.Json.JsonIgnore] public bool DisableConfigSave { get; set; } [Newtonsoft.Json.JsonProperty("global")] public BililiveRecorder.Core.Config.V3.GlobalConfig Global { get; set; }