Code optimization

pull/5940/head
2dust 2024-10-24 10:25:02 +08:00
parent ffb38129e2
commit f0bac4b4c8
8 changed files with 25 additions and 36 deletions

View File

@ -9,7 +9,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.28.2" /> <PackageReference Include="Google.Protobuf" Version="3.28.3" />
<PackageReference Include="Grpc.Net.Client" Version="2.66.0" /> <PackageReference Include="Grpc.Net.Client" Version="2.66.0" />
<PackageReference Include="Grpc.Tools" Version="2.67.0"> <PackageReference Include="Grpc.Tools" Version="2.67.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -495,7 +495,7 @@ namespace ServiceLib.Common
{ {
if (blFull) if (blFull)
{ {
return $"{Global.AppName} - V{GetVersionInfo()} - {File.GetLastWriteTime(GetExePath()):yyyy/MM/dd}"; return $"{Global.AppName} - V{GetVersionInfo()} - {RuntimeInformation.ProcessArchitecture} - {File.GetLastWriteTime(GetExePath()):yyyy/MM/dd}";
} }
else else
{ {

View File

@ -126,18 +126,7 @@
public async Task<List<string>> ProfileItemIndexes(string subid) public async Task<List<string>> ProfileItemIndexes(string subid)
{ {
if (Utils.IsNullOrEmpty(subid)) return (await ProfileItems(subid)).Select(t => t.IndexId).ToList();
{
return (await SQLiteHelper.Instance.TableAsync<ProfileItem>().ToListAsync())
.Select(t => t.IndexId)
.ToList();
}
else
{
return (await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.Subid == subid).ToListAsync())
.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)

View File

@ -7,7 +7,7 @@ namespace ServiceLib.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 async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config) public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config)

View File

@ -108,7 +108,7 @@ namespace ServiceLib.Handler
EnableAutoAdjustMainLvColWidth = true EnableAutoAdjustMainLvColWidth = true
}; };
config.UiItem.MainColumnItem ??= new(); config.UiItem.MainColumnItem ??= new();
if (Utils.IsNullOrEmpty(config.UiItem.CurrentLanguage)) if (Utils.IsNullOrEmpty(config.UiItem.CurrentLanguage))
{ {
if (Thread.CurrentThread.CurrentCulture.Name.Equals("zh-cn", StringComparison.CurrentCultureIgnoreCase)) if (Thread.CurrentThread.CurrentCulture.Name.Equals("zh-cn", StringComparison.CurrentCultureIgnoreCase))
@ -380,8 +380,8 @@ namespace ServiceLib.Handler
{ {
return 0; return 0;
} }
var count = await SQLiteHelper.Instance.TableAsync<ProfileItem>().CountAsync(t => t.IndexId == config.IndexId);
if (count > 0) if (await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(t => t.IndexId == config.IndexId) != null)
{ {
return 0; return 0;
} }
@ -390,7 +390,7 @@ namespace ServiceLib.Handler
return await SetDefaultServerIndex(config, lstProfile.FirstOrDefault(t => t.Port > 0)?.IndexId); return await SetDefaultServerIndex(config, lstProfile.FirstOrDefault(t => t.Port > 0)?.IndexId);
} }
var item = await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.Port > 0).FirstOrDefaultAsync(); var item = await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(t => t.Port > 0);
return await SetDefaultServerIndex(config, item.IndexId); return await SetDefaultServerIndex(config, item.IndexId);
} }

View File

@ -42,7 +42,7 @@ namespace ServiceLib.Handler
else else
{ {
ShowMsg(true, $"{node.GetSummary()}"); ShowMsg(true, $"{node.GetSummary()}");
CoreStop(); await CoreStop();
await CoreStart(node); await CoreStart(node);
//In tun mode, do a delay check and restart the core //In tun mode, do a delay check and restart the core
@ -74,19 +74,19 @@ namespace ServiceLib.Handler
ShowMsg(false, result.Msg); ShowMsg(false, result.Msg);
if (result.Success) if (result.Success)
{ {
pid = CoreStartSpeedtest(configPath, coreType); pid = await CoreStartSpeedtest(configPath, coreType);
} }
return pid; return pid;
} }
public void CoreStop() public async Task CoreStop()
{ {
try try
{ {
bool hasProc = false; bool hasProc = false;
if (_process != null) if (_process != null)
{ {
KillProcess(_process); await KillProcess(_process);
_process.Dispose(); _process.Dispose();
_process = null; _process = null;
hasProc = true; hasProc = true;
@ -94,7 +94,7 @@ namespace ServiceLib.Handler
if (_processPre != null) if (_processPre != null)
{ {
KillProcess(_processPre); await KillProcess(_processPre);
_processPre.Dispose(); _processPre.Dispose();
_processPre = null; _processPre = null;
hasProc = true; hasProc = true;
@ -117,7 +117,7 @@ namespace ServiceLib.Handler
string? path = p.MainModule?.FileName; string? path = p.MainModule?.FileName;
if (path == Utils.GetExeName(Utils.GetBinPath(vName, it.CoreType.ToString()))) if (path == Utils.GetExeName(Utils.GetBinPath(vName, it.CoreType.ToString())))
{ {
KillProcess(p); await KillProcess(p);
} }
} }
} }
@ -130,12 +130,12 @@ namespace ServiceLib.Handler
} }
} }
public void CoreStopPid(int pid) public async Task CoreStopPid(int pid)
{ {
try try
{ {
var _p = Process.GetProcessById(pid); var _p = Process.GetProcessById(pid);
KillProcess(_p); await KillProcess(_p);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -186,7 +186,7 @@ namespace ServiceLib.Handler
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog; var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog;
var proc = RunProcess(node, coreInfo, "", displayLog); var proc = await RunProcess(node, coreInfo, "", displayLog);
if (proc is null) if (proc is null)
{ {
return; return;
@ -228,7 +228,7 @@ namespace ServiceLib.Handler
if (result.Success) if (result.Success)
{ {
var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType); var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
var proc2 = RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true); var proc2 = await RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true);
if (proc2 is not null) if (proc2 is not null)
{ {
_processPre = proc2; _processPre = proc2;
@ -238,7 +238,7 @@ namespace ServiceLib.Handler
} }
} }
private int CoreStartSpeedtest(string configPath, ECoreType coreType) private async Task<int> CoreStartSpeedtest(string configPath, ECoreType coreType)
{ {
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
@ -246,7 +246,7 @@ namespace ServiceLib.Handler
try try
{ {
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
var proc = RunProcess(new(), coreInfo, $" -c {Global.CoreSpeedtestConfigFileName}", true); var proc = await RunProcess(new(), coreInfo, $" -c {Global.CoreSpeedtestConfigFileName}", true);
if (proc is null) if (proc is null)
{ {
return -1; return -1;
@ -272,7 +272,7 @@ namespace ServiceLib.Handler
#region Process #region Process
private Process? RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, bool displayLog) private async Task<Process?> RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, bool displayLog)
{ {
try try
{ {
@ -351,7 +351,7 @@ namespace ServiceLib.Handler
} }
} }
private void KillProcess(Process? proc) private async Task KillProcess(Process? proc)
{ {
if (proc is null) if (proc is null)
{ {

View File

@ -11,8 +11,8 @@ namespace ServiceLib.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>();

View File

@ -15,7 +15,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="5.1.0" /> <PackageReference Include="MaterialDesignThemes" Version="5.1.0" />
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.3" /> <PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.4" />
<PackageReference Include="TaskScheduler" Version="2.11.0" /> <PackageReference Include="TaskScheduler" Version="2.11.0" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" /> <PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="ReactiveUI.WPF" Version="20.1.63" /> <PackageReference Include="ReactiveUI.WPF" Version="20.1.63" />