From bcbfb9603aef981d8348663c3bb1924146abb56a Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 27 Nov 2025 06:21:06 +0000 Subject: [PATCH] fix: prevent duplicate GameScope creation when other mods interfere with GameStart patch Add protection mechanism to prevent IoC container from being created multiple times in the same game session. This fixes an issue where other mods' prefix patches that return false and manually call the original method would cause the postfix to be executed twice, leading to duplicate scope creation. - Add _isGameScopeActive flag to track if a game scope is currently active - Check flag in CreateGameScope() to prevent duplicate creation - Reset flag in DisposeCurrentScope() when cleaning up --- .../DependencyInjection/ModServiceConfigurator.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Application/DependencyInjection/ModServiceConfigurator.cs b/src/Application/DependencyInjection/ModServiceConfigurator.cs index 1b73a72..191cd0f 100644 --- a/src/Application/DependencyInjection/ModServiceConfigurator.cs +++ b/src/Application/DependencyInjection/ModServiceConfigurator.cs @@ -18,6 +18,7 @@ namespace MDIP.Application.DependencyInjection; public static class ModServiceConfigurator { private static readonly HashSet _staticInjectionTargets = []; + private static bool _isGameScopeActive; public static SimpleServiceProvider Provider { get; private set; } public static IServiceScope CurrentScope { get; private set; } @@ -58,8 +59,14 @@ public static void Build() public static void CreateGameScope() { + // Prevent duplicate scope creation in the same game session + // This can happen if another mod's prefix patch returns false and manually calls the original method + if (_isGameScopeActive) + return; + DisposeCurrentScope(); CurrentScope = Provider.CreateScope(); + _isGameScopeActive = true; Provider.RefreshSingletonPropertyInjections(); RefreshStaticInjections(); } @@ -68,6 +75,7 @@ public static void DisposeCurrentScope() { CurrentScope?.Dispose(); CurrentScope = null; + _isGameScopeActive = false; if (Provider == null) return;