mirror of
https://github.com/2dust/v2rayN.git
synced 2025-07-02 21:12:09 +00:00
Code optimization
This commit is contained in:
parent
b013213745
commit
3fafc6de93
8 changed files with 29 additions and 55 deletions
|
@ -5,8 +5,8 @@
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Copyright>Copyright © 2019-2024 (GPLv3)</Copyright>
|
<Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
|
||||||
<FileVersion>1.2.0.0</FileVersion>
|
<FileVersion>1.3.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
|
@ -228,12 +228,6 @@ namespace ServiceLib.Common
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// byte 转成 有两位小数点的 方便阅读的数据 比如 2.50 MB
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="amount">bytes</param>
|
|
||||||
/// <param name="result">转换之后的数据</param>
|
|
||||||
/// <param name="unit">单位</param>
|
|
||||||
private static void ToHumanReadable(long amount, out double result, out string unit)
|
private static void ToHumanReadable(long amount, out double result, out string unit)
|
||||||
{
|
{
|
||||||
var factor = 1024u;
|
var factor = 1024u;
|
||||||
|
@ -411,7 +405,6 @@ namespace ServiceLib.Common
|
||||||
/// <param name="domain"></param>
|
/// <param name="domain"></param>
|
||||||
public static bool IsDomain(string? domain)
|
public static bool IsDomain(string? domain)
|
||||||
{
|
{
|
||||||
//如果为空
|
|
||||||
if (IsNullOrEmpty(domain))
|
if (IsNullOrEmpty(domain))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -564,10 +557,6 @@ namespace ServiceLib.Common
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取系统hosts
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static Dictionary<string, string> GetSystemHosts()
|
public static Dictionary<string, string> GetSystemHosts()
|
||||||
{
|
{
|
||||||
var systemHosts = new Dictionary<string, string>();
|
var systemHosts = new Dictionary<string, string>();
|
||||||
|
@ -634,10 +623,6 @@ namespace ServiceLib.Common
|
||||||
|
|
||||||
#region TempPath
|
#region TempPath
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取启动了应用程序的可执行文件的路径
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string GetPath(string fileName)
|
public static string GetPath(string fileName)
|
||||||
{
|
{
|
||||||
var startupPath = StartupPath();
|
var startupPath = StartupPath();
|
||||||
|
@ -648,10 +633,6 @@ namespace ServiceLib.Common
|
||||||
return Path.Combine(startupPath, fileName);
|
return Path.Combine(startupPath, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取启动了应用程序的可执行文件的路径及文件名
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string GetExePath()
|
public static string GetExePath()
|
||||||
{
|
{
|
||||||
return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;
|
return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;
|
||||||
|
|
|
@ -102,17 +102,17 @@
|
||||||
|
|
||||||
#region SqliteHelper
|
#region SqliteHelper
|
||||||
|
|
||||||
public async Task<List<SubItem>> SubItems()
|
public async Task<List<SubItem>?> SubItems()
|
||||||
{
|
{
|
||||||
return await SQLiteHelper.Instance.TableAsync<SubItem>().OrderBy(t => t.Sort).ToListAsync();
|
return await SQLiteHelper.Instance.TableAsync<SubItem>().OrderBy(t => t.Sort).ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SubItem> GetSubItem(string subid)
|
public async Task<SubItem?> GetSubItem(string subid)
|
||||||
{
|
{
|
||||||
return await SQLiteHelper.Instance.TableAsync<SubItem>().FirstOrDefaultAsync(t => t.Id == subid);
|
return await SQLiteHelper.Instance.TableAsync<SubItem>().FirstOrDefaultAsync(t => t.Id == subid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<ProfileItem>> ProfileItems(string subid)
|
public async Task<List<ProfileItem>?> ProfileItems(string subid)
|
||||||
{
|
{
|
||||||
if (Utils.IsNullOrEmpty(subid))
|
if (Utils.IsNullOrEmpty(subid))
|
||||||
{
|
{
|
||||||
|
@ -124,12 +124,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<string>> ProfileItemIndexes(string subid)
|
public async Task<List<string>?> ProfileItemIndexes(string subid)
|
||||||
{
|
{
|
||||||
return (await ProfileItems(subid)).Select(t => t.IndexId).ToList();
|
return (await ProfileItems(subid))?.Select(t => t.IndexId)?.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<ProfileItemModel>> ProfileItems(string subid, string filter)
|
public async Task<List<ProfileItemModel>?> ProfileItems(string subid, string filter)
|
||||||
{
|
{
|
||||||
var sql = @$"select a.*
|
var sql = @$"select a.*
|
||||||
,b.remarks subRemarks
|
,b.remarks subRemarks
|
||||||
|
@ -152,7 +152,7 @@
|
||||||
return await SQLiteHelper.Instance.QueryAsync<ProfileItemModel>(sql);
|
return await SQLiteHelper.Instance.QueryAsync<ProfileItemModel>(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<ProfileItemModel>> ProfileItemsEx(string subid, string filter)
|
public async Task<List<ProfileItemModel>?> ProfileItemsEx(string subid, string filter)
|
||||||
{
|
{
|
||||||
var lstModel = await ProfileItems(_config.SubIndexId, filter);
|
var lstModel = await ProfileItems(_config.SubIndexId, filter);
|
||||||
|
|
||||||
|
@ -209,22 +209,22 @@
|
||||||
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.Remarks == remarks);
|
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.Remarks == remarks);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<RoutingItem>> RoutingItems()
|
public async Task<List<RoutingItem>?> RoutingItems()
|
||||||
{
|
{
|
||||||
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().Where(it => it.Locked == false).OrderBy(t => t.Sort).ToListAsync();
|
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().Where(it => it.Locked == false).OrderBy(t => t.Sort).ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RoutingItem> GetRoutingItem(string id)
|
public async Task<RoutingItem?> GetRoutingItem(string id)
|
||||||
{
|
{
|
||||||
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.Locked == false && it.Id == id);
|
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.Locked == false && it.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<DNSItem>> DNSItems()
|
public async Task<List<DNSItem>?> DNSItems()
|
||||||
{
|
{
|
||||||
return await SQLiteHelper.Instance.TableAsync<DNSItem>().ToListAsync();
|
return await SQLiteHelper.Instance.TableAsync<DNSItem>().ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<DNSItem> GetDNSItem(ECoreType eCoreType)
|
public async Task<DNSItem?> GetDNSItem(ECoreType eCoreType)
|
||||||
{
|
{
|
||||||
return await SQLiteHelper.Instance.TableAsync<DNSItem>().FirstOrDefaultAsync(it => it.CoreType == eCoreType);
|
return await SQLiteHelper.Instance.TableAsync<DNSItem>().FirstOrDefaultAsync(it => it.CoreType == eCoreType);
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,18 +171,7 @@ namespace ServiceLib.Handler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="config"></param>
|
/// <param name="config"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<int> SaveConfig(Config config, bool reload = true)
|
public static async Task<int> SaveConfig(Config config)
|
||||||
{
|
|
||||||
await ToJsonFile(config);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 存储文件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="config"></param>
|
|
||||||
private static async Task ToJsonFile(Config config)
|
|
||||||
{
|
{
|
||||||
lock (_objLock)
|
lock (_objLock)
|
||||||
{
|
{
|
||||||
|
@ -193,7 +182,7 @@ namespace ServiceLib.Handler
|
||||||
var tempPath = $"{resPath}_temp";
|
var tempPath = $"{resPath}_temp";
|
||||||
if (JsonUtils.ToFile(config, tempPath) != 0)
|
if (JsonUtils.ToFile(config, tempPath) != 0)
|
||||||
{
|
{
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (File.Exists(resPath))
|
if (File.Exists(resPath))
|
||||||
|
@ -206,8 +195,11 @@ namespace ServiceLib.Handler
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logging.SaveLog("ToJsonFile", ex);
|
Logging.SaveLog("ToJsonFile", ex);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion ConfigHandler
|
#endregion ConfigHandler
|
||||||
|
@ -369,7 +361,7 @@ namespace ServiceLib.Handler
|
||||||
|
|
||||||
config.IndexId = indexId;
|
config.IndexId = indexId;
|
||||||
|
|
||||||
await ToJsonFile(config);
|
await SaveConfig(config);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1142,7 +1134,7 @@ namespace ServiceLib.Handler
|
||||||
await SQLiteHelper.Instance.InsertAllAsync(lstAdd);
|
await SQLiteHelper.Instance.InsertAllAsync(lstAdd);
|
||||||
}
|
}
|
||||||
|
|
||||||
await ToJsonFile(config);
|
await SaveConfig(config);
|
||||||
return countServers;
|
return countServers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1272,7 +1264,7 @@ namespace ServiceLib.Handler
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await ToJsonFile(config);
|
await SaveConfig(config);
|
||||||
return counter;
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1605,7 +1597,7 @@ namespace ServiceLib.Handler
|
||||||
config.RoutingBasicItem.RoutingIndexId = routingItem.Id;
|
config.RoutingBasicItem.RoutingIndexId = routingItem.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
await ToJsonFile(config);
|
await SaveConfig(config);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -566,8 +566,8 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
public async Task CloseCore()
|
public async Task CloseCore()
|
||||||
{
|
{
|
||||||
await ConfigHandler.SaveConfig(_config, false);
|
await ConfigHandler.SaveConfig(_config);
|
||||||
CoreHandler.Instance.CoreStop();
|
await CoreHandler.Instance.CoreStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task AutoHideStartup()
|
private async Task AutoHideStartup()
|
||||||
|
@ -588,7 +588,7 @@ namespace ServiceLib.ViewModels
|
||||||
await ConfigHandler.InitRouting(_config);
|
await ConfigHandler.InitRouting(_config);
|
||||||
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
|
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
|
||||||
|
|
||||||
await ConfigHandler.SaveConfig(_config, false);
|
await ConfigHandler.SaveConfig(_config);
|
||||||
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
|
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
|
||||||
await Reload();
|
await Reload();
|
||||||
}
|
}
|
||||||
|
|
|
@ -321,7 +321,7 @@ namespace ServiceLib.ViewModels
|
||||||
NoticeHandler.Instance.SendMessageEx($"{ResUI.TipChangeSystemProxy} - {_config.SystemProxyItem.SysProxyType.ToString()}");
|
NoticeHandler.Instance.SendMessageEx($"{ResUI.TipChangeSystemProxy} - {_config.SystemProxyItem.SysProxyType.ToString()}");
|
||||||
|
|
||||||
SystemProxySelected = (int)_config.SystemProxyItem.SysProxyType;
|
SystemProxySelected = (int)_config.SystemProxyItem.SysProxyType;
|
||||||
await ConfigHandler.SaveConfig(_config, false);
|
await ConfigHandler.SaveConfig(_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange)
|
public async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange)
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<ApplicationIcon>Assets\v2rayN.ico</ApplicationIcon>
|
<ApplicationIcon>Assets\v2rayN.ico</ApplicationIcon>
|
||||||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
||||||
|
<Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
|
||||||
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
|
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
|
@ -103,7 +103,7 @@ namespace v2rayN.Views
|
||||||
{
|
{
|
||||||
_config.GlobalHotkeys = _TextBoxKeyEventItem.Values.ToList();
|
_config.GlobalHotkeys = _TextBoxKeyEventItem.Values.ToList();
|
||||||
|
|
||||||
if ( ConfigHandler.SaveConfig(_config, false).Result == 0)
|
if ( ConfigHandler.SaveConfig(_config).Result == 0)
|
||||||
{
|
{
|
||||||
HotkeyHandler.Instance.ReLoad();
|
HotkeyHandler.Instance.ReLoad();
|
||||||
this.DialogResult = true;
|
this.DialogResult = true;
|
||||||
|
|
Loading…
Reference in a new issue