AI-optimized code

This commit is contained in:
2dust 2025-03-06 16:36:39 +08:00
parent 7e348c196e
commit 07a3bdc618
4 changed files with 70 additions and 76 deletions

View file

@ -1668,7 +1668,7 @@ namespace ServiceLib.Handler
public static async Task<int> 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<RoutingTemplate>(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<DNSItem>(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;

View file

@ -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;

View file

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

View file

@ -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<Dictionary<string, object>>(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,7 +421,7 @@ namespace ServiceLib.ViewModels
public async Task DelayTestTask()
{
Task.Run(async () =>
_ = Task.Run(async () =>
{
var numOfExecuted = 1;
while (true)