add;自动清理过期配置
避免老版本配置保留
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
using Hardcodet.Wpf.TaskbarNotification;
|
using Hardcodet.Wpf.TaskbarNotification;
|
||||||
using Ink_Canvas.Helpers;
|
using Ink_Canvas.Helpers;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using OSVersionExtension;
|
using OSVersionExtension;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
@@ -31,6 +33,11 @@ namespace Ink_Canvas
|
|||||||
string text = File.ReadAllText(App.RootPath + settingsFileName);
|
string text = File.ReadAllText(App.RootPath + settingsFileName);
|
||||||
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
|
|
||||||
|
if (Settings != null)
|
||||||
|
{
|
||||||
|
CleanupObsoleteSettings(text);
|
||||||
|
}
|
||||||
|
|
||||||
// 验证设置是否成功加载
|
// 验证设置是否成功加载
|
||||||
if (Settings == null)
|
if (Settings == null)
|
||||||
{
|
{
|
||||||
@@ -42,6 +49,8 @@ namespace Ink_Canvas
|
|||||||
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
if (Settings != null)
|
if (Settings != null)
|
||||||
{
|
{
|
||||||
|
// 清理过期配置项
|
||||||
|
CleanupObsoleteSettings(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +76,8 @@ namespace Ink_Canvas
|
|||||||
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
if (Settings != null)
|
if (Settings != null)
|
||||||
{
|
{
|
||||||
|
// 清理过期配置项
|
||||||
|
CleanupObsoleteSettings(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception restoreEx)
|
catch (Exception restoreEx)
|
||||||
@@ -95,6 +106,8 @@ namespace Ink_Canvas
|
|||||||
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
if (Settings != null)
|
if (Settings != null)
|
||||||
{
|
{
|
||||||
|
// 清理过期配置项
|
||||||
|
CleanupObsoleteSettings(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception restoreEx)
|
catch (Exception restoreEx)
|
||||||
@@ -1150,5 +1163,102 @@ namespace Ink_Canvas
|
|||||||
LogHelper.WriteLogToFile($"加载墨迹渐隐设置时出错: {ex.Message}", LogHelper.LogType.Error);
|
LogHelper.WriteLogToFile($"加载墨迹渐隐设置时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <param name="userConfigJson">用户配置的JSON字符串</param>
|
||||||
|
private void CleanupObsoleteSettings(string userConfigJson)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 创建默认配置对象
|
||||||
|
Settings defaultSettings = new Settings();
|
||||||
|
|
||||||
|
// 将默认配置和用户配置都序列化为JObject
|
||||||
|
JObject defaultConfigObj = JObject.FromObject(defaultSettings);
|
||||||
|
JObject userConfigObj = JObject.Parse(userConfigJson);
|
||||||
|
|
||||||
|
// 记录是否有清理操作
|
||||||
|
bool hasChanges = false;
|
||||||
|
|
||||||
|
// 递归比较并删除用户配置中多余的键
|
||||||
|
RemoveObsoleteProperties(userConfigObj, defaultConfigObj, ref hasChanges);
|
||||||
|
|
||||||
|
// 如果有清理操作,重新反序列化并保存
|
||||||
|
if (hasChanges)
|
||||||
|
{
|
||||||
|
string cleanedJson = userConfigObj.ToString(Formatting.Indented);
|
||||||
|
Settings = JsonConvert.DeserializeObject<Settings>(cleanedJson);
|
||||||
|
SaveSettingsToFile();
|
||||||
|
LogHelper.WriteLogToFile("已清理过期配置项", LogHelper.LogType.Event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"清理过期配置时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <param name="userObj">用户配置的JObject</param>
|
||||||
|
/// <param name="defaultObj">默认配置的JObject</param>
|
||||||
|
/// <param name="hasChanges">是否有变更的引用标志</param>
|
||||||
|
private void RemoveObsoleteProperties(JObject userObj, JObject defaultObj, ref bool hasChanges)
|
||||||
|
{
|
||||||
|
if (userObj == null || defaultObj == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// 获取需要删除的键列表(避免在遍历时修改集合)
|
||||||
|
List<string> keysToRemove = new List<string>();
|
||||||
|
|
||||||
|
foreach (var property in userObj.Properties())
|
||||||
|
{
|
||||||
|
string propertyName = property.Name;
|
||||||
|
|
||||||
|
// 如果默认配置中不存在该属性,标记为删除
|
||||||
|
if (!defaultObj.ContainsKey(propertyName))
|
||||||
|
{
|
||||||
|
keysToRemove.Add(propertyName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果两个属性都是对象类型,递归比较
|
||||||
|
JToken userValue = property.Value;
|
||||||
|
JToken defaultValue = defaultObj[propertyName];
|
||||||
|
|
||||||
|
if (userValue != null && defaultValue != null)
|
||||||
|
{
|
||||||
|
if (userValue.Type == JTokenType.Object && defaultValue.Type == JTokenType.Object)
|
||||||
|
{
|
||||||
|
RemoveObsoleteProperties(userValue as JObject, defaultValue as JObject, ref hasChanges);
|
||||||
|
}
|
||||||
|
// 处理数组中的对象(如自定义图标列表等)
|
||||||
|
else if (userValue.Type == JTokenType.Array && defaultValue.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
JArray userArray = userValue as JArray;
|
||||||
|
JArray defaultArray = defaultValue as JArray;
|
||||||
|
|
||||||
|
if (userArray != null && defaultArray != null && userArray.Count > 0 && defaultArray.Count > 0)
|
||||||
|
{
|
||||||
|
// 如果数组元素是对象,比较第一个元素的属性结构
|
||||||
|
if (userArray[0].Type == JTokenType.Object && defaultArray[0].Type == JTokenType.Object)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < userArray.Count; i++)
|
||||||
|
{
|
||||||
|
if (userArray[i] is JObject userItemObj && defaultArray[0] is JObject defaultItemObj)
|
||||||
|
{
|
||||||
|
RemoveObsoleteProperties(userItemObj, defaultItemObj, ref hasChanges);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除标记的键
|
||||||
|
foreach (string key in keysToRemove)
|
||||||
|
{
|
||||||
|
userObj.Remove(key);
|
||||||
|
hasChanges = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user