From 4e40d5f5f731668edc40a4f76ead319d742f7286 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:53:41 +0800 Subject: [PATCH] Optimize AppManager --- .../CoreConfig/CoreConfigTestFactory.cs | 7 ++---- .../Handler/Data/ConfigDbHandler.cs | 4 ++-- .../Handler/Data/ConfigTextHandler.cs | 6 ++--- v2rayN/ServiceLib/Manager/AppManager.cs | 22 ++++++++----------- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/v2rayN/ServiceLib.Tests/CoreConfig/CoreConfigTestFactory.cs b/v2rayN/ServiceLib.Tests/CoreConfig/CoreConfigTestFactory.cs index 44401730..eba35f62 100644 --- a/v2rayN/ServiceLib.Tests/CoreConfig/CoreConfigTestFactory.cs +++ b/v2rayN/ServiceLib.Tests/CoreConfig/CoreConfigTestFactory.cs @@ -4,11 +4,8 @@ internal static class CoreConfigTestFactory { public static void BindAppManagerConfig(Config config) { - var field = typeof(AppManager).GetField("_config", BindingFlags.Instance | BindingFlags.NonPublic); - field?.SetValue(AppManager.Instance, config); - - var field2 = typeof(ConfigState).GetField("_current", BindingFlags.Instance | BindingFlags.NonPublic); - field2?.SetValue(ConfigState.Instance, config); + var field = typeof(ConfigState).GetField("_current", BindingFlags.Instance | BindingFlags.NonPublic); + field?.SetValue(ConfigState.Instance, config); } public static Config CreateConfig(ECoreType vmessCoreType = ECoreType.Xray) diff --git a/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs b/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs index 11317198..db099171 100644 --- a/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs +++ b/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs @@ -350,7 +350,7 @@ public static class ConfigDbHandler profileItem.ConfigType = EConfigType.Custom; if (profileItem.Remarks.IsNullOrEmpty()) { - profileItem.Remarks = $"import custom@{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}"; + profileItem.Remarks = $"import custom@{DateTime.Now:yyyy/MM/dd HH:mm:ss}"; } await AddServerCommon(profileItem, true); @@ -387,7 +387,7 @@ public static class ConfigDbHandler profileItem.ConfigType = EConfigType.Outbound; if (profileItem.Remarks.IsNullOrEmpty()) { - profileItem.Remarks = $"import custom outbound@{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}"; + profileItem.Remarks = $"import custom outbound@{DateTime.Now:yyyy/MM/dd HH:mm:ss}"; } await AddServerCommon(profileItem, toFile); diff --git a/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs b/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs index 80d03078..113f2e4e 100644 --- a/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs +++ b/v2rayN/ServiceLib/Handler/Data/ConfigTextHandler.cs @@ -13,16 +13,16 @@ public static class ConfigTextHandler #region Init - public static Config? Init() + public static bool Init() { var config = LoadConfig(); if (config == null) { - return null; + return false; } Config = config; - return Config; + return true; } /// diff --git a/v2rayN/ServiceLib/Manager/AppManager.cs b/v2rayN/ServiceLib/Manager/AppManager.cs index 8c99a85f..3bf59f20 100644 --- a/v2rayN/ServiceLib/Manager/AppManager.cs +++ b/v2rayN/ServiceLib/Manager/AppManager.cs @@ -5,7 +5,7 @@ public sealed class AppManager #region Property private static readonly Lazy _instance = new(() => new()); - private Config _config; + private static Config CurrentConfig => ConfigState.Instance.Current; private int? _statePort; private int? _statePort2; public static AppManager Instance => _instance.Value; @@ -25,7 +25,7 @@ public sealed class AppManager get { _statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2)); - return _statePort2.Value + (_config.TunModeItem.EnableTun ? 1 : 0); + return _statePort2.Value + (CurrentConfig.TunModeItem.EnableTun ? 1 : 0); } } @@ -67,12 +67,11 @@ public sealed class AppManager } Logging.Setup(); - _config = ConfigTextHandler.Init(); - if (_config == null) + if (ConfigTextHandler.Init() != true) { return false; } - Thread.CurrentThread.CurrentUICulture = new(_config.UiItem.CurrentLanguage); + Thread.CurrentThread.CurrentUICulture = new(CurrentConfig.UiItem.CurrentLanguage); //Under Win10 if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10) @@ -96,16 +95,13 @@ public sealed class AppManager public bool InitComponents() { Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}"); - Logging.LoggingEnabled(_config.GuiItem.EnableLog); + Logging.LoggingEnabled(CurrentConfig.GuiItem.EnableLog); //First determine the port value _ = StatePort; _ = StatePort2; - Task.Run(async () => - { - await ConfigDbHandler.MigrateProfileExtra(); - }).Wait(); + Task.Run(async () => await ConfigDbHandler.MigrateProfileExtra()).Wait(); return true; } @@ -123,7 +119,7 @@ public sealed class AppManager { Logging.SaveLog("AppExitAsync Begin"); - await SysProxyHandler.UpdateSysProxy(_config, true); + await SysProxyHandler.UpdateSysProxy(CurrentConfig, true); AppEvents.AppExitRequested.Publish(); await Task.Delay(50); //Wait for AppExitRequested to be processed @@ -162,7 +158,7 @@ public sealed class AppManager public int GetLocalPort(EInboundProtocol protocol) { - var localPort = _config.Inbound.FirstOrDefault(t => t.Protocol == nameof(EInboundProtocol.socks))?.LocalPort ?? 10808; + var localPort = CurrentConfig.Inbound.FirstOrDefault(t => t.Protocol == nameof(EInboundProtocol.socks))?.LocalPort ?? 10808; return localPort + (int)protocol; } @@ -194,7 +190,7 @@ public sealed class AppManager return (ECoreType)profileItem.CoreType; } - var item = _config.CoreTypeItem?.FirstOrDefault(it => it.ConfigType == eConfigType); + var item = CurrentConfig.CoreTypeItem?.FirstOrDefault(it => it.ConfigType == eConfigType); return item?.CoreType ?? ECoreType.Xray; }