mirror of
https://github.com/2dust/v2rayN.git
synced 2025-07-02 04:52:09 +00:00
Optimize save config files
This commit is contained in:
parent
dbd4f55981
commit
83ad83b135
4 changed files with 35 additions and 70 deletions
|
@ -70,8 +70,9 @@ namespace ServiceLib.Common
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj"></param>
|
/// <param name="obj"></param>
|
||||||
/// <param name="indented"></param>
|
/// <param name="indented"></param>
|
||||||
|
/// <param name="nullValue"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Serialize(object? obj, bool indented = true)
|
public static string Serialize(object? obj, bool indented = true, bool nullValue = false)
|
||||||
{
|
{
|
||||||
var result = string.Empty;
|
var result = string.Empty;
|
||||||
try
|
try
|
||||||
|
@ -82,8 +83,8 @@ namespace ServiceLib.Common
|
||||||
}
|
}
|
||||||
var options = new JsonSerializerOptions
|
var options = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
WriteIndented = indented ? true : false,
|
WriteIndented = indented,
|
||||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
|
||||||
};
|
};
|
||||||
result = JsonSerializer.Serialize(obj, options);
|
result = JsonSerializer.Serialize(obj, options);
|
||||||
}
|
}
|
||||||
|
@ -100,38 +101,5 @@ namespace ServiceLib.Common
|
||||||
/// <param name="obj"></param>
|
/// <param name="obj"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj);
|
public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Save as json file
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="obj"></param>
|
|
||||||
/// <param name="filePath"></param>
|
|
||||||
/// <param name="nullValue"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static int ToFile(object? obj, string? filePath, bool nullValue = true)
|
|
||||||
{
|
|
||||||
if (filePath is null)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using var file = File.Create(filePath);
|
|
||||||
|
|
||||||
var options = new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
WriteIndented = true,
|
|
||||||
DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
|
|
||||||
};
|
|
||||||
|
|
||||||
JsonSerializer.Serialize(file, obj, options);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logging.SaveLog(ex.Message, ex);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,7 +9,6 @@ namespace ServiceLib.Handler
|
||||||
public class ConfigHandler
|
public class ConfigHandler
|
||||||
{
|
{
|
||||||
private static readonly string _configRes = Global.ConfigFileName;
|
private static readonly string _configRes = Global.ConfigFileName;
|
||||||
private static readonly object _objLock = new();
|
|
||||||
|
|
||||||
#region ConfigHandler
|
#region ConfigHandler
|
||||||
|
|
||||||
|
@ -67,7 +66,6 @@ namespace ServiceLib.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
config.RoutingBasicItem ??= new();
|
config.RoutingBasicItem ??= new();
|
||||||
|
|
||||||
if (Utils.IsNullOrEmpty(config.RoutingBasicItem.DomainStrategy))
|
if (Utils.IsNullOrEmpty(config.RoutingBasicItem.DomainStrategy))
|
||||||
{
|
{
|
||||||
config.RoutingBasicItem.DomainStrategy = Global.DomainStrategies.First();//"IPIfNonMatch";
|
config.RoutingBasicItem.DomainStrategy = Global.DomainStrategies.First();//"IPIfNonMatch";
|
||||||
|
@ -120,15 +118,6 @@ namespace ServiceLib.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
config.ConstItem ??= new ConstItem();
|
config.ConstItem ??= new ConstItem();
|
||||||
if (Utils.IsNotEmpty(config.ConstItem.DefIEProxyExceptions))
|
|
||||||
{
|
|
||||||
config.SystemProxyItem.SystemProxyExceptions = $"{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
|
|
||||||
config.ConstItem.DefIEProxyExceptions = string.Empty;
|
|
||||||
}
|
|
||||||
if (config.SystemProxyItem.SystemProxyExceptions.IsNullOrEmpty())
|
|
||||||
{
|
|
||||||
config.SystemProxyItem.SystemProxyExceptions = Utils.IsWindows() ? Global.SystemProxyExceptionsWindows : Global.SystemProxyExceptionsLinux;
|
|
||||||
}
|
|
||||||
|
|
||||||
config.SpeedTestItem ??= new();
|
config.SpeedTestItem ??= new();
|
||||||
if (config.SpeedTestItem.SpeedTestTimeout < 10)
|
if (config.SpeedTestItem.SpeedTestTimeout < 10)
|
||||||
|
@ -167,6 +156,16 @@ namespace ServiceLib.Handler
|
||||||
config.WebDavItem ??= new();
|
config.WebDavItem ??= new();
|
||||||
config.CheckUpdateItem ??= new();
|
config.CheckUpdateItem ??= new();
|
||||||
|
|
||||||
|
if (Utils.IsNotEmpty(config.ConstItem.DefIEProxyExceptions))
|
||||||
|
{
|
||||||
|
config.SystemProxyItem.SystemProxyExceptions = $"{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
|
||||||
|
config.ConstItem.DefIEProxyExceptions = string.Empty;
|
||||||
|
}
|
||||||
|
if (config.SystemProxyItem.SystemProxyExceptions.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
config.SystemProxyItem.SystemProxyExceptions = Utils.IsWindows() ? Global.SystemProxyExceptionsWindows : Global.SystemProxyExceptionsLinux;
|
||||||
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,30 +176,26 @@ namespace ServiceLib.Handler
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<int> SaveConfig(Config config)
|
public static async Task<int> SaveConfig(Config config)
|
||||||
{
|
{
|
||||||
lock (_objLock)
|
try
|
||||||
{
|
{
|
||||||
try
|
//save temp file
|
||||||
{
|
var resPath = Utils.GetConfigPath(_configRes);
|
||||||
//save temp file
|
var tempPath = $"{resPath}_temp";
|
||||||
var resPath = Utils.GetConfigPath(_configRes);
|
|
||||||
var tempPath = $"{resPath}_temp";
|
|
||||||
if (JsonUtils.ToFile(config, tempPath) != 0)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (File.Exists(resPath))
|
var content = JsonUtils.Serialize(config, true, true);
|
||||||
{
|
if (content.IsNullOrEmpty())
|
||||||
File.Delete(resPath);
|
|
||||||
}
|
|
||||||
//rename
|
|
||||||
File.Move(tempPath, resPath);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
Logging.SaveLog("ToJsonFile", ex);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
await File.WriteAllTextAsync(tempPath, content);
|
||||||
|
|
||||||
|
//rename
|
||||||
|
File.Move(tempPath, resPath, true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logging.SaveLog("ToJsonFile", ex);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -409,7 +409,9 @@ namespace ServiceLib.Services.CoreConfig
|
||||||
{
|
{
|
||||||
await GenInbounds(singboxConfig);
|
await GenInbounds(singboxConfig);
|
||||||
await GenExperimental(singboxConfig);
|
await GenExperimental(singboxConfig);
|
||||||
JsonUtils.ToFile(singboxConfig, fileName, false);
|
|
||||||
|
var content = JsonUtils.Serialize(singboxConfig, true);
|
||||||
|
await File.WriteAllTextAsync(fileName, content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -282,16 +282,16 @@ namespace ServiceLib.ViewModels
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Logging.SaveLog("MyAppExit Begin");
|
Logging.SaveLog("MyAppExitAsync Begin");
|
||||||
await SysProxyHandler.UpdateSysProxy(_config, true);
|
|
||||||
|
|
||||||
await ConfigHandler.SaveConfig(_config);
|
await ConfigHandler.SaveConfig(_config);
|
||||||
|
await SysProxyHandler.UpdateSysProxy(_config, true);
|
||||||
await ProfileExHandler.Instance.SaveTo();
|
await ProfileExHandler.Instance.SaveTo();
|
||||||
await StatisticsHandler.Instance.SaveTo();
|
await StatisticsHandler.Instance.SaveTo();
|
||||||
StatisticsHandler.Instance.Close();
|
StatisticsHandler.Instance.Close();
|
||||||
await CoreHandler.Instance.CoreStop();
|
await CoreHandler.Instance.CoreStop();
|
||||||
|
|
||||||
Logging.SaveLog("MyAppExit End");
|
Logging.SaveLog("MyAppExitAsync End");
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
finally
|
finally
|
||||||
|
|
Loading…
Reference in a new issue