From 9ce634ed1c776c18e92ea12e21cc2aab7071e7ca Mon Sep 17 00:00:00 2001 From: gewuyou <95328647+GeWuYou@users.noreply.github.com> Date: Mon, 27 Apr 2026 08:00:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(game):=20=E6=8B=86=E5=88=86=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=83=AD=E9=87=8D=E8=BD=BD=E5=90=AF=E5=8A=A8=E6=B5=81?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构 GameConfigBootstrap 的热重载启动流程,提取状态准备、结果提交与失败回滚辅助方法 - 保持现有锁保护、异常路径与监听句柄释放语义不变,消除 StartHotReload 的 MA0051 warning --- GFramework.Game/Config/GameConfigBootstrap.cs | 123 ++++++++++-------- 1 file changed, 69 insertions(+), 54 deletions(-) diff --git a/GFramework.Game/Config/GameConfigBootstrap.cs b/GFramework.Game/Config/GameConfigBootstrap.cs index 9ffe34ec..9842d23b 100644 --- a/GFramework.Game/Config/GameConfigBootstrap.cs +++ b/GFramework.Game/Config/GameConfigBootstrap.cs @@ -210,67 +210,16 @@ public sealed class GameConfigBootstrap : IDisposable /// public void StartHotReload(YamlConfigHotReloadOptions? options = null) { - YamlConfigLoader loader; - lock (_stateGate) - { - ThrowIfDisposedCore(); - - loader = _loader ?? throw new InvalidOperationException( - "Hot reload can only be started after the initial config load succeeds."); - - if (_isStartingHotReload || _hotReload != null) - { - throw new InvalidOperationException("Hot reload is already enabled."); - } - - _isStartingHotReload = true; - _stopHotReloadAfterStart = false; - } - + var loader = BeginHotReloadStart(); IUnRegister? hotReload = null; try { hotReload = loader.EnableHotReload(Registry, options); - - var shouldStop = false; - lock (_stateGate) - { - try - { - ThrowIfDisposedCore(); - - // Stop/Dispose may arrive while the watcher is being created. In that - // case, release the new handle immediately instead of publishing it. - if (_stopHotReloadAfterStart) - { - shouldStop = true; - _stopHotReloadAfterStart = false; - } - else - { - _hotReload = hotReload; - hotReload = null; - } - } - finally - { - _isStartingHotReload = false; - } - } - - if (shouldStop) - { - hotReload?.UnRegister(); - } + hotReload = CompleteHotReloadStart(hotReload); } catch { - lock (_stateGate) - { - _isStartingHotReload = false; - _stopHotReloadAfterStart = false; - } - + ResetHotReloadStartAfterFailure(); hotReload?.UnRegister(); throw; } @@ -332,4 +281,70 @@ public sealed class GameConfigBootstrap : IDisposable throw new ObjectDisposedException(nameof(GameConfigBootstrap)); } } + + private YamlConfigLoader BeginHotReloadStart() + { + lock (_stateGate) + { + ThrowIfDisposedCore(); + + var loader = _loader ?? throw new InvalidOperationException( + "Hot reload can only be started after the initial config load succeeds."); + + if (_isStartingHotReload || _hotReload != null) + { + throw new InvalidOperationException("Hot reload is already enabled."); + } + + _isStartingHotReload = true; + _stopHotReloadAfterStart = false; + return loader; + } + } + + private IUnRegister? CompleteHotReloadStart(IUnRegister? hotReload) + { + var shouldStop = false; + lock (_stateGate) + { + try + { + ThrowIfDisposedCore(); + + // Stop/Dispose may arrive while the watcher is being created. In that + // case, release the new handle immediately instead of publishing it. + if (_stopHotReloadAfterStart) + { + shouldStop = true; + _stopHotReloadAfterStart = false; + } + else + { + _hotReload = hotReload; + hotReload = null; + } + } + finally + { + _isStartingHotReload = false; + } + } + + if (shouldStop) + { + hotReload?.UnRegister(); + return null; + } + + return hotReload; + } + + private void ResetHotReloadStartAfterFailure() + { + lock (_stateGate) + { + _isStartingHotReload = false; + _stopHotReloadAfterStart = false; + } + } }