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 ffb3812 commit f0bac4b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 36 deletions.
2 changes: 1 addition & 1 deletion v2rayN/ProtosLib/ProtosLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</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.Tools" Version="2.67.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public static string GetVersion(bool blFull = true)
{
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
{
Expand Down
13 changes: 1 addition & 12 deletions v2rayN/ServiceLib/Handler/AppHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,7 @@ public async Task<List<ProfileItem>> ProfileItems(string subid)

public async Task<List<string>> ProfileItemIndexes(string subid)
{
if (Utils.IsNullOrEmpty(subid))
{
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();
}
return (await ProfileItems(subid)).Select(t => t.IndexId).ToList();
}

public async Task<List<ProfileItemModel>> ProfileItems(string subid, string filter)
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Handler/ClashApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public sealed class ClashApiHandler
private static readonly Lazy<ClashApiHandler> instance = new(() => new());
public static ClashApiHandler Instance => instance.Value;

private Dictionary<String, ProxiesItem>? _proxies;
private Dictionary<string, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; }

public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config)
Expand Down
8 changes: 4 additions & 4 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class ConfigHandler
EnableAutoAdjustMainLvColWidth = true
};
config.UiItem.MainColumnItem ??= new();

if (Utils.IsNullOrEmpty(config.UiItem.CurrentLanguage))
{
if (Thread.CurrentThread.CurrentCulture.Name.Equals("zh-cn", StringComparison.CurrentCultureIgnoreCase))
Expand Down Expand Up @@ -380,8 +380,8 @@ public static async Task<int> SetDefaultServer(Config config, List<ProfileItemMo
{
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;
}
Expand All @@ -390,7 +390,7 @@ public static async Task<int> SetDefaultServer(Config config, List<ProfileItemMo
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);
}

Expand Down
28 changes: 14 additions & 14 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task LoadCore(ProfileItem? node)
else
{
ShowMsg(true, $"{node.GetSummary()}");
CoreStop();
await CoreStop();
await CoreStart(node);

//In tun mode, do a delay check and restart the core
Expand Down Expand Up @@ -74,27 +74,27 @@ public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
ShowMsg(false, result.Msg);
if (result.Success)
{
pid = CoreStartSpeedtest(configPath, coreType);
pid = await CoreStartSpeedtest(configPath, coreType);
}
return pid;
}

public void CoreStop()
public async Task CoreStop()
{
try
{
bool hasProc = false;
if (_process != null)
{
KillProcess(_process);
await KillProcess(_process);
_process.Dispose();
_process = null;
hasProc = true;
}

if (_processPre != null)
{
KillProcess(_processPre);
await KillProcess(_processPre);
_processPre.Dispose();
_processPre = null;
hasProc = true;
Expand All @@ -117,7 +117,7 @@ public void CoreStop()
string? path = p.MainModule?.FileName;
if (path == Utils.GetExeName(Utils.GetBinPath(vName, it.CoreType.ToString())))
{
KillProcess(p);
await KillProcess(p);
}
}
}
Expand All @@ -130,12 +130,12 @@ public void CoreStop()
}
}

public void CoreStopPid(int pid)
public async Task CoreStopPid(int pid)
{
try
{
var _p = Process.GetProcessById(pid);
KillProcess(_p);
await KillProcess(_p);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -186,7 +186,7 @@ private async Task CoreStart(ProfileItem node)
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);

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)
{
return;
Expand Down Expand Up @@ -228,7 +228,7 @@ private async Task CoreStart(ProfileItem node)
if (result.Success)
{
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)
{
_processPre = proc2;
Expand All @@ -238,15 +238,15 @@ private async Task CoreStart(ProfileItem node)
}
}

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, configPath);
try
{
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)
{
return -1;
Expand All @@ -272,7 +272,7 @@ private void ShowMsg(bool notify, string msg)

#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
{
Expand Down Expand Up @@ -351,7 +351,7 @@ private void ShowMsg(bool notify, string msg)
}
}

private void KillProcess(Process? proc)
private async Task KillProcess(Process? proc)
{
if (proc is null)
{
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace ServiceLib.ViewModels
{
public class ClashProxiesViewModel : MyReactiveObject
{
private Dictionary<String, ProxiesItem>? _proxies;
private Dictionary<String, ProvidersItem>? _providers;
private Dictionary<string, ProxiesItem>? _proxies;
private Dictionary<string, ProvidersItem>? _providers;
private int _delayTimeout = 99999999;

private IObservableCollection<ClashProxyModel> _proxyGroups = new ObservableCollectionExtended<ClashProxyModel>();
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/v2rayN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<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="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="ReactiveUI.WPF" Version="20.1.63" />
Expand Down

0 comments on commit f0bac4b

Please sign in to comment.