From 1f7eb2d48a80237488350f203d424d3dec63feb7 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:23:15 +0800 Subject: [PATCH] Optimizing reload core --- v2rayN/v2rayN/Global.cs | 1 - v2rayN/v2rayN/Handler/ConfigHandler.cs | 15 ---- v2rayN/v2rayN/Handler/CoreHandler.cs | 69 +++++++++---------- .../v2rayN/ViewModels/MainWindowViewModel.cs | 9 ++- 4 files changed, 39 insertions(+), 55 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 0638ae16..93f07339 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -124,7 +124,6 @@ #region global variable - public static bool reloadCore { get; set; } public static int statePort { get; set; } public static Job processJob { get; set; } public static bool ShowInTaskbar { get; set; } diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 6eecaa3b..8d3776de 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -195,16 +195,6 @@ namespace v2rayN.Handler config.guiItem.statisticsFreshRate = 1; } - if (config == null) - { - Global.reloadCore = false; - } - else - { - Global.reloadCore = true; - - } - LazyConfig.Instance.SetConfig(config); return 0; } @@ -215,7 +205,6 @@ namespace v2rayN.Handler /// public static int SaveConfig(ref Config config, bool reload = true) { - Global.reloadCore = reload; ToJsonFile(config); @@ -446,7 +435,6 @@ namespace v2rayN.Handler } config.indexId = indexId; - Global.reloadCore = true; ToJsonFile(config); @@ -856,7 +844,6 @@ namespace v2rayN.Handler } else if (profileItem.indexId == config.indexId) { - Global.reloadCore = true; } if (SqliteHelper.Instance.Replace(profileItem) > 0) @@ -1461,8 +1448,6 @@ namespace v2rayN.Handler config.routingBasicItem.routingIndexId = routingItem.id; } - Global.reloadCore = true; - ToJsonFile(config); return 0; diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 4dc2ca6a..23715782 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -27,46 +27,43 @@ namespace v2rayN.Handler public void LoadCore(Config config) { - if (Global.reloadCore) + var node = ConfigHandler.GetDefaultServer(ref config); + if (node == null) { - var node = ConfigHandler.GetDefaultServer(ref config); - if (node == null) - { - ShowMsg(false, ResUI.CheckServerSettings); - return; - } + ShowMsg(false, ResUI.CheckServerSettings); + return; + } - if (SetCore(config, node) != 0) - { - ShowMsg(false, ResUI.CheckServerSettings); - return; - } - string fileName = Utils.GetConfigPath(_coreCConfigRes); - if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0) - { - ShowMsg(false, msg); - } - else - { - ShowMsg(false, msg); - ShowMsg(true, $"{node.GetSummary()}"); - CoreStop(); - CoreStart(node); - } + if (SetCore(config, node) != 0) + { + ShowMsg(false, ResUI.CheckServerSettings); + return; + } + string fileName = Utils.GetConfigPath(_coreCConfigRes); + if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0) + { + ShowMsg(false, msg); + } + else + { + ShowMsg(false, msg); + ShowMsg(true, $"{node.GetSummary()}"); + CoreStop(); + CoreStart(node); + } - //start a socks service - if (_process != null && !_process.HasExited && node.configType == EConfigType.Custom && node.preSocksPort > 0) + //start a socks service + if (_process != null && !_process.HasExited && node.configType == EConfigType.Custom && node.preSocksPort > 0) + { + var itemSocks = new ProfileItem() { - var itemSocks = new ProfileItem() - { - configType = EConfigType.Socks, - address = Global.Loopback, - port = node.preSocksPort - }; - if (CoreConfigHandler.GenerateClientConfig(itemSocks, null, out string msg2, out string configStr) == 0) - { - _processId = CoreStartViaString(configStr); - } + configType = EConfigType.Socks, + address = Global.Loopback, + port = node.preSocksPort + }; + if (CoreConfigHandler.GenerateClientConfig(itemSocks, null, out string msg2, out string configStr) == 0) + { + _processId = CoreStartViaString(configStr); } } } diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index dc345dac..5f1cadf9 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -540,8 +540,12 @@ namespace v2rayN.ViewModels _noticeHandler?.SendMessage(msg); if (success) { + var indexIdOld = _config.indexId; RefreshServers(); - Reload(); + if (indexIdOld != _config.indexId) + { + Reload(); + } if (_config.uiItem.enableAutoAdjustMainLvColWidth) { _updateView("AdjustMainLvColWidth"); @@ -740,6 +744,7 @@ namespace v2rayN.ViewModels subRemarks = t.subRemarks, isActive = t.indexId == _config.indexId, sort = t33 == null ? 0 : t33.sort, + delay = t33 == null ? 0 : t33.delay, delayVal = t33?.delay != 0 ? $"{t33?.delay} {Global.DelayUnit}" : string.Empty, speedVal = t33?.speed != 0 ? $"{t33?.speed} {Global.SpeedUnit}" : string.Empty, todayDown = t22 == null ? "" : Utils.HumanFy(t22.todayDown), @@ -1367,7 +1372,6 @@ namespace v2rayN.ViewModels public void Reload() { - Global.reloadCore = true; _ = LoadV2ray(); } @@ -1387,7 +1391,6 @@ namespace v2rayN.ViewModels { _coreHandler.LoadCore(_config); - Global.reloadCore = false; //ConfigHandler.SaveConfig(ref _config, false); ChangeSystemProxyStatus(_config.sysProxyType, false);