refactor(game): 拆分配置热重载启动流程

- 重构 GameConfigBootstrap 的热重载启动流程,提取状态准备、结果提交与失败回滚辅助方法

- 保持现有锁保护、异常路径与监听句柄释放语义不变,消除 StartHotReload 的 MA0051 warning
This commit is contained in:
gewuyou 2026-04-27 08:00:17 +08:00
parent 9deafac234
commit 9ce634ed1c

View File

@ -210,67 +210,16 @@ public sealed class GameConfigBootstrap : IDisposable
/// </exception>
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;
}
}
}