From 07a3bdc6188da56097dbb6333d4a56c2838ea943 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 6 Mar 2025 16:36:39 +0800 Subject: [PATCH] AI-optimized code --- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 16 ++--- .../Handler/SysProxy/ProxySettingWindows.cs | 2 +- .../ViewModels/ClashConnectionsViewModel.cs | 57 ++++++++------- .../ViewModels/ClashProxiesViewModel.cs | 71 ++++++++----------- 4 files changed, 70 insertions(+), 76 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 5f2c0166..714e5da1 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -1668,7 +1668,7 @@ namespace ServiceLib.Handler public static async Task InitRouting(Config config, bool blImportAdvancedRules = false) { - if (string.IsNullOrEmpty(config.ConstItem.RouteRulesTemplateSourceUrl)) + if (config.ConstItem.RouteRulesTemplateSourceUrl.IsNullOrEmpty()) { await InitBuiltinRouting(config, blImportAdvancedRules); } @@ -1684,7 +1684,7 @@ namespace ServiceLib.Handler { var downloadHandle = new DownloadService(); var templateContent = await downloadHandle.TryDownloadString(config.ConstItem.RouteRulesTemplateSourceUrl, true, ""); - if (string.IsNullOrEmpty(templateContent)) + if (templateContent.IsNullOrEmpty()) return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback var template = JsonUtils.Deserialize(templateContent); @@ -1701,14 +1701,14 @@ namespace ServiceLib.Handler { var item = template.RoutingItems[i]; - if (string.IsNullOrEmpty(item.Url) && string.IsNullOrEmpty(item.RuleSet)) + if (item.Url.IsNullOrEmpty() && item.RuleSet.IsNullOrEmpty()) continue; - var ruleSetsString = !string.IsNullOrEmpty(item.RuleSet) + var ruleSetsString = !item.RuleSet.IsNullOrEmpty() ? item.RuleSet : await downloadHandle.TryDownloadString(item.Url, true, ""); - if (string.IsNullOrEmpty(ruleSetsString)) + if (ruleSetsString.IsNullOrEmpty()) continue; item.Remarks = $"{template.Version}-{item.Remarks}"; @@ -1841,17 +1841,17 @@ namespace ServiceLib.Handler var downloadHandle = new DownloadService(); var templateContent = await downloadHandle.TryDownloadString(url, true, ""); - if (string.IsNullOrEmpty(templateContent)) + if (templateContent.IsNullOrEmpty()) return currentItem; var template = JsonUtils.Deserialize(templateContent); if (template == null) return currentItem; - if (!string.IsNullOrEmpty(template.NormalDNS)) + if (!template.NormalDNS.IsNullOrEmpty()) template.NormalDNS = await downloadHandle.TryDownloadString(template.NormalDNS, true, ""); - if (!string.IsNullOrEmpty(template.TunDNS)) + if (!template.TunDNS.IsNullOrEmpty()) template.TunDNS = await downloadHandle.TryDownloadString(template.TunDNS, true, ""); template.Id = currentItem.Id; diff --git a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs index 582d8702..614bd656 100644 --- a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs @@ -87,7 +87,7 @@ namespace ServiceLib.Handler.SysProxy } else if (type is 2 or 4) // named proxy or autoproxy script URL { - optionCount = string.IsNullOrEmpty(exceptions) ? 2 : 3; + optionCount = exceptions.IsNullOrEmpty() ? 2 : 3; } var m_Int = (int)PerConnFlags.PROXY_TYPE_DIRECT; diff --git a/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs index 0f390d47..03b21524 100644 --- a/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs @@ -53,32 +53,7 @@ namespace ServiceLib.ViewModels private async Task Init() { - _ = Task.Run(async () => - { - var numOfExecuted = 1; - while (true) - { - await Task.Delay(1000 * 5); - numOfExecuted++; - if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box))) - { - continue; - } - - if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0) - { - continue; - } - - if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0) - { - continue; - } - await GetClashConnections(); - } - }); - - await Task.CompletedTask; + _ = DelayTestTask(); } private async Task GetClashConnections() @@ -146,5 +121,35 @@ namespace ServiceLib.ViewModels await ClashApiHandler.Instance.ClashConnectionClose(id); await GetClashConnections(); } + + public async Task DelayTestTask() + { + _ = Task.Run(async () => + { + var numOfExecuted = 1; + while (true) + { + await Task.Delay(1000 * 5); + numOfExecuted++; + if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box))) + { + continue; + } + + if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0) + { + continue; + } + + if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0) + { + continue; + } + await GetClashConnections(); + } + }); + + await Task.CompletedTask; + } } } diff --git a/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs index e44220d1..0a5bc8dd 100644 --- a/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs @@ -96,7 +96,6 @@ namespace ServiceLib.ViewModels private async Task Init() { _ = DelayTestTask(); - await ProxiesReload(); } private async Task DoRulemodeSelected(bool c) @@ -135,22 +134,12 @@ namespace ServiceLib.ViewModels RefreshProxyDetails(c); } - private void UpdateHandler(bool notify, string msg) - { - NoticeHandler.Instance.SendMessageEx(msg); - } - public async Task ProxiesReload() { await GetClashProxies(true); await ProxiesDelayTest(); } - public async Task ProxiesDelayTest() - { - await ProxiesDelayTest(true); - } - #region proxy function private async Task SetRuleMode(ERuleMode mode) @@ -218,7 +207,7 @@ namespace ServiceLib.ViewModels { continue; } - var item = _proxyGroups.Where(t => t.Name == kv.Key).FirstOrDefault(); + var item = _proxyGroups.FirstOrDefault(t => t.Name == kv.Key); if (item != null && item.Name.IsNotEmpty()) { continue; @@ -369,7 +358,7 @@ namespace ServiceLib.ViewModels await ClashApiHandler.Instance.ClashSetActiveProxy(name, nameNode); selectedProxy.now = nameNode; - var group = _proxyGroups.Where(it => it.Name == SelectedGroup.Name).FirstOrDefault(); + var group = _proxyGroups.FirstOrDefault(it => it.Name == SelectedGroup.Name); if (group != null) { group.Now = nameNode; @@ -381,7 +370,7 @@ namespace ServiceLib.ViewModels NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess); } - private async Task ProxiesDelayTest(bool blAll) + private async Task ProxiesDelayTest(bool blAll = true) { ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) => { @@ -403,19 +392,19 @@ namespace ServiceLib.ViewModels public void ProxiesDelayTestResult(SpeedTestResult result) { //UpdateHandler(false, $"{item.name}={result}"); - var detail = _proxyDetails.Where(it => it.Name == result.IndexId).FirstOrDefault(); + var detail = _proxyDetails.FirstOrDefault(it => it.Name == result.IndexId); if (detail != null) { var dicResult = JsonUtils.Deserialize>(result.Delay); - if (dicResult != null && dicResult.ContainsKey("delay")) + if (dicResult != null && dicResult.TryGetValue("delay", out var value)) { - detail.Delay = Convert.ToInt32(dicResult["delay"].ToString()); + detail.Delay = Convert.ToInt32(value.ToString()); detail.DelayName = $"{detail.Delay}ms"; } - else if (dicResult != null && dicResult.ContainsKey("message")) + else if (dicResult != null && dicResult.TryGetValue("message", out var value1)) { detail.Delay = _delayTimeout; - detail.DelayName = $"{dicResult["message"]}"; + detail.DelayName = $"{value1}"; } else { @@ -432,28 +421,28 @@ namespace ServiceLib.ViewModels public async Task DelayTestTask() { - Task.Run(async () => - { - var numOfExecuted = 1; - while (true) - { - await Task.Delay(1000 * 60); - numOfExecuted++; - if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box))) - { - continue; - } - if (_config.ClashUIItem.ProxiesAutoDelayTestInterval <= 0) - { - continue; - } - if (numOfExecuted % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0) - { - continue; - } - await ProxiesDelayTest(); - } - }); + _ = Task.Run(async () => + { + var numOfExecuted = 1; + while (true) + { + await Task.Delay(1000 * 60); + numOfExecuted++; + if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box))) + { + continue; + } + if (_config.ClashUIItem.ProxiesAutoDelayTestInterval <= 0) + { + continue; + } + if (numOfExecuted % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0) + { + continue; + } + await ProxiesDelayTest(); + } + }); await Task.CompletedTask; }