mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-17 18:01:26 +08:00
fix: 默认配置创建修复
This commit is contained in:
@@ -20,6 +20,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Unicode;
|
||||
using System.Threading;
|
||||
@@ -167,9 +168,14 @@ public static class ConfigFactory
|
||||
_logger.Warning("{File} save failed", _configBakFile);
|
||||
}
|
||||
|
||||
if (ParseJsonFile(ConfigurationHelper.ConfigFile) is JObject oldConfigJson && oldConfigJson["Configurations"] is JObject configurationsObj)
|
||||
if (ParseJsonFile(ConfigurationHelper.ConfigFile) is JsonObject oldConfigJson && oldConfigJson["Configurations"] is JsonObject configurationsObj)
|
||||
{
|
||||
var configNames = configurationsObj.Properties().Select(i => i.Name);
|
||||
if (oldConfigJson["Current"]?.GetValue<string>() is string oldCurrent && parsed.Current != oldCurrent)
|
||||
{
|
||||
_logger.Warning("Current configuration in old configuration is {OldCurrent}, but in new configuration is {NewCurrent}, switching to old current", oldCurrent, parsed.Current);
|
||||
parsed.Current = oldCurrent;
|
||||
}
|
||||
var configNames = configurationsObj.Select(i => i.Key);
|
||||
foreach (var name in parsed.Configurations.Select(i => i.Key).Except(configNames))
|
||||
{
|
||||
parsed.Configurations.Remove(name);
|
||||
@@ -177,6 +183,11 @@ public static class ConfigFactory
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.Configurations.All(i => i.Key != parsed.Current))
|
||||
{
|
||||
parsed.Configurations.Add(parsed.Current, new SpecificConfig());
|
||||
}
|
||||
|
||||
return parsed;
|
||||
|
||||
void SpecificConfigBind(string name, SpecificConfig config)
|
||||
@@ -219,7 +230,7 @@ public static class ConfigFactory
|
||||
}
|
||||
}
|
||||
|
||||
JObject? ParseJsonFile(string filePath)
|
||||
JsonObject? ParseJsonFile(string filePath)
|
||||
{
|
||||
if (File.Exists(filePath) is false)
|
||||
{
|
||||
@@ -229,7 +240,7 @@ public static class ConfigFactory
|
||||
var str = File.ReadAllText(filePath);
|
||||
try
|
||||
{
|
||||
var obj = (JObject?)Newtonsoft.Json.JsonConvert.DeserializeObject(str);
|
||||
var obj = JsonSerializer.Deserialize<JsonObject>(str);
|
||||
return obj ?? throw new Exception("Failed to parse json file");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -35,6 +35,7 @@ public class Root : INotifyPropertyChanged
|
||||
|
||||
public int ConfigVersion { get; set; } = 1;
|
||||
|
||||
[JsonInclude]
|
||||
public string Current { get; set; } = ConfigurationKeys.DefaultConfiguration;
|
||||
|
||||
[JsonInclude]
|
||||
@@ -50,18 +51,9 @@ public class Root : INotifyPropertyChanged
|
||||
public SpecificConfig CurrentConfig
|
||||
{
|
||||
get {
|
||||
if (Configurations.TryGetValue(Current, out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
var defaultConfig = new SpecificConfig();
|
||||
Configurations[Current] = defaultConfig;
|
||||
return defaultConfig;
|
||||
}
|
||||
Configurations.TryGetValue(Current, out var result);
|
||||
return result!;
|
||||
}
|
||||
|
||||
set => Configurations[Current] = value;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user