Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 24, 2024
1 parent b013213 commit 3fafc6d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 55 deletions.
4 changes: 2 additions & 2 deletions v2rayN/AmazTool/AmazTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Copyright>Copyright © 2019-2024 (GPLv3)</Copyright>
<FileVersion>1.2.0.0</FileVersion>
<Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
<FileVersion>1.3.0</FileVersion>
</PropertyGroup>

</Project>
21 changes: 1 addition & 20 deletions v2rayN/ServiceLib/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ public static string ToString(object? obj)
}
}

/// <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)
{
var factor = 1024u;
Expand Down Expand Up @@ -411,7 +405,6 @@ public static bool IsNotEmpty(string? text)
/// <param name="domain"></param>
public static bool IsDomain(string? domain)
{
//如果为空
if (IsNullOrEmpty(domain))
{
return false;
Expand Down Expand Up @@ -564,10 +557,6 @@ public static void ProcessStart(string? fileName, string arguments = "")
}
}

/// <summary>
/// 获取系统hosts
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> GetSystemHosts()
{
var systemHosts = new Dictionary<string, string>();
Expand Down Expand Up @@ -634,10 +623,6 @@ public static Dictionary<string, string> GetSystemHosts()

#region TempPath

/// <summary>
/// 获取启动了应用程序的可执行文件的路径
/// </summary>
/// <returns></returns>
public static string GetPath(string fileName)
{
var startupPath = StartupPath();
Expand All @@ -647,11 +632,7 @@ public static string GetPath(string fileName)
}
return Path.Combine(startupPath, fileName);
}

/// <summary>
/// 获取启动了应用程序的可执行文件的路径及文件名
/// </summary>
/// <returns></returns>

public static string GetExePath()
{
return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;
Expand Down
22 changes: 11 additions & 11 deletions v2rayN/ServiceLib/Handler/AppHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ public void AddProcess(IntPtr processHandle)

#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();
}

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);
}

public async Task<List<ProfileItem>> ProfileItems(string subid)
public async Task<List<ProfileItem>?> ProfileItems(string subid)
{
if (Utils.IsNullOrEmpty(subid))
{
Expand All @@ -124,12 +124,12 @@ public async Task<List<ProfileItem>> ProfileItems(string subid)
}
}

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.*
,b.remarks subRemarks
Expand All @@ -152,7 +152,7 @@ from ProfileItem a
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);

Expand Down Expand Up @@ -209,22 +209,22 @@ from t33 in t3b.DefaultIfEmpty()
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();
}

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);
}

public async Task<List<DNSItem>> DNSItems()
public async Task<List<DNSItem>?> DNSItems()
{
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);
}
Expand Down
26 changes: 9 additions & 17 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,7 @@ public class ConfigHandler
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
public static async Task<int> SaveConfig(Config config, bool reload = true)
{
await ToJsonFile(config);

return 0;
}

/// <summary>
/// 存储文件
/// </summary>
/// <param name="config"></param>
private static async Task ToJsonFile(Config config)
public static async Task<int> SaveConfig(Config config)
{
lock (_objLock)
{
Expand All @@ -193,7 +182,7 @@ private static async Task ToJsonFile(Config config)
var tempPath = $"{resPath}_temp";
if (JsonUtils.ToFile(config, tempPath) != 0)
{
return;
return -1;
}

if (File.Exists(resPath))
Expand All @@ -206,8 +195,11 @@ private static async Task ToJsonFile(Config config)
catch (Exception ex)
{
Logging.SaveLog("ToJsonFile", ex);
return -1;
}
}

return 0;
}

#endregion ConfigHandler
Expand Down Expand Up @@ -369,7 +361,7 @@ public static async Task<int> SetDefaultServerIndex(Config config, string? index

config.IndexId = indexId;

await ToJsonFile(config);
await SaveConfig(config);

return 0;
}
Expand Down Expand Up @@ -1142,7 +1134,7 @@ private static async Task<int> AddBatchServers(Config config, string strData, st
await SQLiteHelper.Instance.InsertAllAsync(lstAdd);
}

await ToJsonFile(config);
await SaveConfig(config);
return countServers;
}

Expand Down Expand Up @@ -1272,7 +1264,7 @@ private static async Task<int> AddBatchServers4SsSIP008(Config config, string st
counter++;
}
}
await ToJsonFile(config);
await SaveConfig(config);
return counter;
}

Expand Down Expand Up @@ -1605,7 +1597,7 @@ public static async Task<int> SetDefaultRouting(Config config, RoutingItem routi
config.RoutingBasicItem.RoutingIndexId = routingItem.Id;
}

await ToJsonFile(config);
await SaveConfig(config);

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ await Task.Run(async () =>

public async Task CloseCore()
{
await ConfigHandler.SaveConfig(_config, false);
CoreHandler.Instance.CoreStop();
await ConfigHandler.SaveConfig(_config);
await CoreHandler.Instance.CoreStop();
}

private async Task AutoHideStartup()
Expand All @@ -588,7 +588,7 @@ public async Task ApplyRegionalPreset(EPresetType type)
await ConfigHandler.InitRouting(_config);
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();

await ConfigHandler.SaveConfig(_config, false);
await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
await Reload();
}
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public async Task SetListenerType(ESysProxyType type)
NoticeHandler.Instance.SendMessageEx($"{ResUI.TipChangeSystemProxy} - {_config.SystemProxyItem.SysProxyType.ToString()}");

SystemProxySelected = (int)_config.SystemProxyItem.SysProxyType;
await ConfigHandler.SaveConfig(_config, false);
await ConfigHandler.SaveConfig(_config);
}

public async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange)
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationIcon>Assets\v2rayN.ico</ApplicationIcon>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void btnSave_Click(object sender, RoutedEventArgs e)
{
_config.GlobalHotkeys = _TextBoxKeyEventItem.Values.ToList();

if ( ConfigHandler.SaveConfig(_config, false).Result == 0)
if ( ConfigHandler.SaveConfig(_config).Result == 0)
{
HotkeyHandler.Instance.ReLoad();
this.DialogResult = true;
Expand Down

0 comments on commit 3fafc6d

Please sign in to comment.