Optimized code

pull/5566/head
2dust 2024-08-18 15:04:56 +08:00
parent a432852b78
commit bbe7c7b884
6 changed files with 32 additions and 50 deletions

View File

@ -8,19 +8,9 @@ namespace v2rayN.Handler
private static readonly Lazy<ClashApiHandler> instance = new(() => new()); private static readonly Lazy<ClashApiHandler> instance = new(() => new());
public static ClashApiHandler Instance => instance.Value; public static ClashApiHandler Instance => instance.Value;
private Dictionary<String, ProxiesItem> _proxies; private Dictionary<String, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; } public Dictionary<string, object> ProfileContent { get; set; }
public void SetProxies(Dictionary<String, ProxiesItem> proxies)
{
_proxies = proxies;
}
public Dictionary<String, ProxiesItem> GetProxies()
{
return _proxies;
}
public void GetClashProxies(Config config, Action<ClashProxies, ClashProviders> update) public void GetClashProxies(Config config, Action<ClashProxies, ClashProviders> update)
{ {
Task.Run(() => GetClashProxiesAsync(config, update)); Task.Run(() => GetClashProxiesAsync(config, update));
@ -40,10 +30,11 @@ namespace v2rayN.Handler
if (clashProxies != null || clashProviders != null) if (clashProxies != null || clashProviders != null)
{ {
_proxies = clashProxies?.proxies;
update(clashProxies, clashProviders); update(clashProxies, clashProviders);
return; return;
} }
Thread.Sleep(5000); Task.Delay(5000).Wait();
} }
update(null, null); update(null, null);
} }
@ -56,19 +47,18 @@ namespace v2rayN.Handler
{ {
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
if (GetProxies() != null) if (_proxies != null)
{ {
break; break;
} }
Thread.Sleep(5000); Task.Delay(5000).Wait();
} }
var proxies = GetProxies(); if (_proxies == null)
if (proxies == null)
{ {
return; return;
} }
lstProxy = new List<ClashProxyModel>(); lstProxy = new List<ClashProxyModel>();
foreach (KeyValuePair<string, ProxiesItem> kv in proxies) foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
{ {
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower())) if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
{ {
@ -106,7 +96,7 @@ namespace v2rayN.Handler
} }
Task.WaitAll(tasks.ToArray()); Task.WaitAll(tasks.ToArray());
Thread.Sleep(1000); Task.Delay(1000).Wait();
update(null, ""); update(null, "");
}); });
} }
@ -148,8 +138,7 @@ namespace v2rayN.Handler
{ {
Task.Run(async () => Task.Run(async () =>
{ {
var proxies = GetProxies(); if (_proxies == null)
if (proxies == null)
{ {
return; return;
} }

View File

@ -46,12 +46,6 @@ namespace v2rayN.Handler
ShowMsg(false, msg); ShowMsg(false, msg);
ShowMsg(true, $"{node.GetSummary()}"); ShowMsg(true, $"{node.GetSummary()}");
CoreStop(); CoreStop();
//if (_config.tunModeItem.enableTun)
//{
// Thread.Sleep(1000);
// WindowsUtils.RemoveTunDevice();
//}
CoreStart(node); CoreStart(node);
//In tun mode, do a delay check and restart the core //In tun mode, do a delay check and restart the core

View File

@ -96,7 +96,7 @@ namespace v2rayN.ViewModels
GetClashConnections(); GetClashConnections();
lastTime = dtNow; lastTime = dtNow;
} }
Thread.Sleep(1000); Task.Delay(1000).Wait();
} }
}); });
} }

View File

@ -17,9 +17,9 @@ namespace v2rayN.ViewModels
{ {
public class ClashProxiesViewModel : MyReactiveObject public class ClashProxiesViewModel : MyReactiveObject
{ {
private Dictionary<String, ProxiesItem>? proxies; private Dictionary<String, ProxiesItem>? _proxies;
private Dictionary<String, ProvidersItem>? providers; private Dictionary<String, ProvidersItem>? _providers;
private int delayTimeout = 99999999; private int _delayTimeout = 99999999;
private IObservableCollection<ClashProxyModel> _proxyGroups = new ObservableCollectionExtended<ClashProxyModel>(); private IObservableCollection<ClashProxyModel> _proxyGroups = new ObservableCollectionExtended<ClashProxyModel>();
private IObservableCollection<ClashProxyModel> _proxyDetails = new ObservableCollectionExtended<ClashProxyModel>(); private IObservableCollection<ClashProxyModel> _proxyDetails = new ObservableCollectionExtended<ClashProxyModel>();
@ -175,11 +175,10 @@ namespace v2rayN.ViewModels
ClashApiHandler.Instance.GetClashProxies(_config, (it, it2) => ClashApiHandler.Instance.GetClashProxies(_config, (it, it2) =>
{ {
//UpdateHandler(false, "Refresh Clash Proxies"); //UpdateHandler(false, "Refresh Clash Proxies");
proxies = it?.proxies; _proxies = it?.proxies;
providers = it2?.providers; _providers = it2?.providers;
ClashApiHandler.Instance.SetProxies(proxies); if (_proxies == null)
if (proxies == null)
{ {
return; return;
} }
@ -200,11 +199,11 @@ namespace v2rayN.ViewModels
{ {
foreach (var it in proxyGroups) foreach (var it in proxyGroups)
{ {
if (string.IsNullOrEmpty(it.name) || !proxies.ContainsKey(it.name)) if (string.IsNullOrEmpty(it.name) || !_proxies.ContainsKey(it.name))
{ {
continue; continue;
} }
var item = proxies[it.name]; var item = _proxies[it.name];
if (!Global.allowSelectType.Contains(item.type.ToLower())) if (!Global.allowSelectType.Contains(item.type.ToLower()))
{ {
continue; continue;
@ -219,7 +218,7 @@ namespace v2rayN.ViewModels
} }
//from api //from api
foreach (KeyValuePair<string, ProxiesItem> kv in proxies) foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
{ {
if (!Global.allowSelectType.Contains(kv.Value.type.ToLower())) if (!Global.allowSelectType.Contains(kv.Value.type.ToLower()))
{ {
@ -267,12 +266,12 @@ namespace v2rayN.ViewModels
{ {
return; return;
} }
if (proxies == null) if (_proxies == null)
{ {
return; return;
} }
proxies.TryGetValue(name, out ProxiesItem proxy); _proxies.TryGetValue(name, out ProxiesItem proxy);
if (proxy == null || proxy.all == null) if (proxy == null || proxy.all == null)
{ {
return; return;
@ -298,7 +297,7 @@ namespace v2rayN.ViewModels
isActive = isActive, isActive = isActive,
name = item, name = item,
type = proxy2.type, type = proxy2.type,
delay = delay <= 0 ? delayTimeout : delay, delay = delay <= 0 ? _delayTimeout : delay,
delayName = delay <= 0 ? string.Empty : $"{delay}ms", delayName = delay <= 0 ? string.Empty : $"{delay}ms",
}); });
} }
@ -321,17 +320,17 @@ namespace v2rayN.ViewModels
private ProxiesItem? TryGetProxy(string name) private ProxiesItem? TryGetProxy(string name)
{ {
if (proxies is null) if (_proxies is null)
return null; return null;
proxies.TryGetValue(name, out ProxiesItem proxy2); _proxies.TryGetValue(name, out ProxiesItem proxy2);
if (proxy2 != null) if (proxy2 != null)
{ {
return proxy2; return proxy2;
} }
//from providers //from providers
if (providers != null) if (_providers != null)
{ {
foreach (KeyValuePair<string, ProvidersItem> kv in providers) foreach (KeyValuePair<string, ProvidersItem> kv in _providers)
{ {
if (Global.proxyVehicleType.Contains(kv.Value.vehicleType.ToLower())) if (Global.proxyVehicleType.Contains(kv.Value.vehicleType.ToLower()))
{ {
@ -424,12 +423,12 @@ namespace v2rayN.ViewModels
} }
else if (dicResult != null && dicResult.ContainsKey("message")) else if (dicResult != null && dicResult.ContainsKey("message"))
{ {
detail.delay = delayTimeout; detail.delay = _delayTimeout;
detail.delayName = $"{dicResult["message"]}"; detail.delayName = $"{dicResult["message"]}";
} }
else else
{ {
detail.delay = delayTimeout; detail.delay = _delayTimeout;
detail.delayName = String.Empty; detail.delayName = String.Empty;
} }
_proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail)); _proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail));
@ -459,7 +458,7 @@ namespace v2rayN.ViewModels
ProxiesDelayTest(); ProxiesDelayTest();
lastTime = dtNow; lastTime = dtNow;
} }
Thread.Sleep(1000); Task.Delay(1000).Wait();
} }
}); });
} }

View File

@ -828,8 +828,8 @@ namespace v2rayN.ViewModels
{ {
if (_config.tunModeItem.enableTun) if (_config.tunModeItem.enableTun)
{ {
Thread.Sleep(1000); Task.Delay(1000).Wait();
//WindowsUtils.RemoveTunDevice(); WindowsUtils.RemoveTunDevice();
} }
var node = ConfigHandler.GetDefaultServer(_config); var node = ConfigHandler.GetDefaultServer(_config);

View File

@ -17,7 +17,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Downloader" Version="3.1.2" /> <PackageReference Include="Downloader" Version="3.1.2" />
<PackageReference Include="MaterialDesignThemes" Version="5.1.0" /> <PackageReference Include="MaterialDesignThemes" Version="5.1.0" />
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.0" /> <PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.2" />
<PackageReference Include="QRCoder.Xaml" Version="1.6.0" /> <PackageReference Include="QRCoder.Xaml" Version="1.6.0" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" /> <PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="TaskScheduler" Version="2.11.0" /> <PackageReference Include="TaskScheduler" Version="2.11.0" />