Optimize AppManager

This commit is contained in:
2dust
2026-11-15 15:53:41 +08:00
parent 41231533b1
commit 4e40d5f5f7
4 changed files with 16 additions and 23 deletions
@@ -4,11 +4,8 @@ internal static class CoreConfigTestFactory
{ {
public static void BindAppManagerConfig(Config config) public static void BindAppManagerConfig(Config config)
{ {
var field = typeof(AppManager).GetField("_config", BindingFlags.Instance | BindingFlags.NonPublic); var field = typeof(ConfigState).GetField("_current", BindingFlags.Instance | BindingFlags.NonPublic);
field?.SetValue(AppManager.Instance, config); field?.SetValue(ConfigState.Instance, config);
var field2 = typeof(ConfigState).GetField("_current", BindingFlags.Instance | BindingFlags.NonPublic);
field2?.SetValue(ConfigState.Instance, config);
} }
public static Config CreateConfig(ECoreType vmessCoreType = ECoreType.Xray) public static Config CreateConfig(ECoreType vmessCoreType = ECoreType.Xray)
@@ -350,7 +350,7 @@ public static class ConfigDbHandler
profileItem.ConfigType = EConfigType.Custom; profileItem.ConfigType = EConfigType.Custom;
if (profileItem.Remarks.IsNullOrEmpty()) 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); await AddServerCommon(profileItem, true);
@@ -387,7 +387,7 @@ public static class ConfigDbHandler
profileItem.ConfigType = EConfigType.Outbound; profileItem.ConfigType = EConfigType.Outbound;
if (profileItem.Remarks.IsNullOrEmpty()) 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); await AddServerCommon(profileItem, toFile);
@@ -13,16 +13,16 @@ public static class ConfigTextHandler
#region Init #region Init
public static Config? Init() public static bool Init()
{ {
var config = LoadConfig(); var config = LoadConfig();
if (config == null) if (config == null)
{ {
return null; return false;
} }
Config = config; Config = config;
return Config; return true;
} }
/// <summary> /// <summary>
+9 -13
View File
@@ -5,7 +5,7 @@ public sealed class AppManager
#region Property #region Property
private static readonly Lazy<AppManager> _instance = new(() => new()); private static readonly Lazy<AppManager> _instance = new(() => new());
private Config _config; private static Config CurrentConfig => ConfigState.Instance.Current;
private int? _statePort; private int? _statePort;
private int? _statePort2; private int? _statePort2;
public static AppManager Instance => _instance.Value; public static AppManager Instance => _instance.Value;
@@ -25,7 +25,7 @@ public sealed class AppManager
get get
{ {
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2)); _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(); Logging.Setup();
_config = ConfigTextHandler.Init(); if (ConfigTextHandler.Init() != true)
if (_config == null)
{ {
return false; return false;
} }
Thread.CurrentThread.CurrentUICulture = new(_config.UiItem.CurrentLanguage); Thread.CurrentThread.CurrentUICulture = new(CurrentConfig.UiItem.CurrentLanguage);
//Under Win10 //Under Win10
if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10) if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10)
@@ -96,16 +95,13 @@ public sealed class AppManager
public bool InitComponents() public bool InitComponents()
{ {
Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}"); Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}");
Logging.LoggingEnabled(_config.GuiItem.EnableLog); Logging.LoggingEnabled(CurrentConfig.GuiItem.EnableLog);
//First determine the port value //First determine the port value
_ = StatePort; _ = StatePort;
_ = StatePort2; _ = StatePort2;
Task.Run(async () => Task.Run(async () => await ConfigDbHandler.MigrateProfileExtra()).Wait();
{
await ConfigDbHandler.MigrateProfileExtra();
}).Wait();
return true; return true;
} }
@@ -123,7 +119,7 @@ public sealed class AppManager
{ {
Logging.SaveLog("AppExitAsync Begin"); Logging.SaveLog("AppExitAsync Begin");
await SysProxyHandler.UpdateSysProxy(_config, true); await SysProxyHandler.UpdateSysProxy(CurrentConfig, true);
AppEvents.AppExitRequested.Publish(); AppEvents.AppExitRequested.Publish();
await Task.Delay(50); //Wait for AppExitRequested to be processed await Task.Delay(50); //Wait for AppExitRequested to be processed
@@ -162,7 +158,7 @@ public sealed class AppManager
public int GetLocalPort(EInboundProtocol protocol) 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; return localPort + (int)protocol;
} }
@@ -194,7 +190,7 @@ public sealed class AppManager
return (ECoreType)profileItem.CoreType; 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; return item?.CoreType ?? ECoreType.Xray;
} }