保存配置文件时删除一些不必要的内容,增加唤醒功能

This commit is contained in:
snltty
2025-05-25 16:48:00 +08:00
parent 706f89bc16
commit e03cf8f518
6 changed files with 66 additions and 56 deletions

View File

@@ -7,7 +7,7 @@ using System.Text.Json.Serialization;
using linker.libs.timer;
using System.Text.Json;
using System.Text.Json.Nodes;
using linker.messenger.tunnel.stun.enums;
using System.Collections.Generic;
namespace linker.messenger.store.file
{
@@ -21,6 +21,7 @@ namespace linker.messenger.store.file
private string configPath = "./configs/";
private Dictionary<string, FileReadWrite> fsDic = new Dictionary<string, FileReadWrite>();
private List<string[]> saveJsonIgnorePaths = new List<string[]>();
public ConfigInfo Data { get; private set; } = new ConfigInfo();
@@ -41,45 +42,39 @@ namespace linker.messenger.store.file
private void Init()
{
/*
Type saveAttr = typeof(SaveJsonIgnore);
List<List<string>> saveJsonIgnores = FindPathsWithSaveJsonIgnore(Data);
List<List<string>> FindPathsWithSaveJsonIgnore(object obj)
{
var paths = new List<List<string>>();
FindPathsRecursive(obj, new List<string>(), paths);
return paths;
}
void FindPathsRecursive(object obj, List<string> currentPath, List<List<string>> resultPaths)
{
if (obj == null) return;
Type type = obj.GetType();
foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
currentPath.Add(property.Name);
currentPath = currentPath.ToArray().ToList();
if (Attribute.IsDefined(property, saveAttr))
{
resultPaths.Add(currentPath);
}
else if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && !property.PropertyType.IsArray && !typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType))
{
try
{
object value = property.GetValue(obj);
FindPathsRecursive(value, currentPath, resultPaths);
}
catch (Exception)
{
}
}
}
}
Console.WriteLine(saveJsonIgnores.ToJson());
*/
Type saveAttr = typeof(SaveJsonIgnore);
saveJsonIgnorePaths = FindPathsWithSaveJsonIgnore(Data);
List<string[]> FindPathsWithSaveJsonIgnore(object obj)
{
var paths = new List<string[]>();
FindPathsRecursive(obj, string.Empty, paths);
return paths;
}
void FindPathsRecursive(object obj, string currentPath, List<string[]> resultPaths)
{
if (obj == null) return;
Type type = obj.GetType();
foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
string propertyPath = string.IsNullOrEmpty(currentPath) ? property.Name : $"{currentPath}.{property.Name}";
if (Attribute.IsDefined(property, saveAttr))
{
resultPaths.Add([.. propertyPath.Split('.')]);
}
else if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && !property.PropertyType.IsArray && !typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType))
{
try
{
object value = property.GetValue(obj);
FindPathsRecursive(value, propertyPath, resultPaths);
}
catch (Exception)
{
}
}
}
}
if (Directory.Exists(Path.Combine(Helper.currentDirectory, configPath)) == false)
{
Directory.CreateDirectory(Path.Combine(Helper.currentDirectory, configPath));
@@ -156,8 +151,28 @@ namespace linker.messenger.store.file
LoggerHelper.Instance.Error($"{item.Value.Property.Name} save not found");
continue;
}
string text = item.Value.PropertyMethod.Serialize(item.Value.Property.GetValue(Data));
JsonNode jsonNode = JsonNode.Parse(item.Value.Property.GetValue(Data).ToJson());
try
{
foreach (string[] path in saveJsonIgnorePaths.Where(c => c.Length > 0 && c[0] == item.Value.Property.Name))
{
if (path.Length == 0) continue;
JsonNode root = jsonNode;
for (int i = 1; i < path.Length - 1; i++)
{
if (root.AsObject().TryGetPropertyValue(path[i], out root) == false || root == null) break;
if (root == null) break;
}
root?.AsObject().Remove(path[^1]);
}
}
catch (Exception)
{
}
string text = item.Value.PropertyMethod.Serialize(jsonNode);
if (json != null && json.RootElement.TryGetProperty(item.Value.Property.Name, out JsonElement import))
{
text = item.Value.PropertyMethod.Deserialize(text).ToJson();
@@ -183,6 +198,7 @@ namespace linker.messenger.store.file
}
finally
{
GC.Collect();
slim.Release();
}
@@ -297,18 +313,6 @@ namespace linker.messenger.store.file
public string Serialize(object obj)
{
/*
JsonNode jsonNode = JsonNode.Parse(obj.ToJson());
JsonObject jsonObj = jsonNode.AsObject();
jsonObj.Remove("Accesss");
jsonObj.Remove("Server");
jsonObj.Remove("Group");
if (jsonObj.TryGetPropertyValue("Relay", out JsonNode relay))
{
relay.AsObject().Remove("Server");
}
*/
#if DEBUG
return obj.ToJsonFormat();
#else
@@ -364,7 +368,6 @@ namespace linker.messenger.store.file
[AttributeUsage(AttributeTargets.Property)]
public sealed class SaveJsonIgnore : Attribute
{
}
}

View File

@@ -95,6 +95,7 @@ namespace linker.messenger.store.file
}
finally
{
GC.Collect();
slim.Release();
}
}

View File

@@ -28,6 +28,8 @@ namespace linker.messenger.store.file
/// 中继服务器列表
/// </summary>
public RelayServerInfo[] Servers { get; set; } = new RelayServerInfo[] { new RelayServerInfo { } };
[SaveJsonIgnore]
public RelayServerInfo Server => Servers[0];
}

View File

@@ -18,6 +18,8 @@ namespace linker.messenger.store.file
new SignInClientServerInfo{ Name="Linker", Host="linker.snltty.com:1802" }
#endif
};
[SaveJsonIgnore]
public SignInClientServerInfo Server => Servers[0];
public SignInClientServerInfo[] Servers
{
@@ -43,6 +45,8 @@ namespace linker.messenger.store.file
}
private SignInClientGroupInfo[] groups = new[] { new SignInClientGroupInfo { } };
[SaveJsonIgnore]
public SignInClientGroupInfo Group => Groups[0];
public SignInClientGroupInfo[] Groups
{

View File

@@ -136,7 +136,7 @@ namespace linker.messenger.tuntap
{
tuntapProxy.SetIP(item.MachineId, NetworkHelper.ToValue(item.IP));
}
foreach (var item in Infos.Values.Where(c => c.Available==false || c.Exists || c.IP.Equals(IPAddress.Any)))
foreach (var item in Infos.Values.Where(c => c.Available == false || c.Exists || c.IP.Equals(IPAddress.Any)))
{
tuntapProxy.RemoveIP(item.MachineId);
}
@@ -156,7 +156,7 @@ namespace linker.messenger.tuntap
foreach (var item in infos.Where(c => c.Available == true).OrderBy(c => c.IP, new IPAddressComparer()).OrderByDescending(c => c.Status))
{
item.Exists = hashSet.Contains(NetworkHelper.ToValue(item.IP));
item.Exists = item.IP.Equals(IPAddress.Any) == false && hashSet.Contains(NetworkHelper.ToValue(item.IP));
hashSet.Add(NetworkHelper.ToValue(item.IP));
}

View File

@@ -1,4 +1,4 @@
v1.8.1
2025-05-25 15:50:58
2025-05-25 16:47:59
1. 一些累计更新
2. 优化修复一些已知BUG