diff --git a/GFramework.Core.Abstractions/architecture/IArchitectureContextProvider.cs b/GFramework.Core.Abstractions/architecture/IArchitectureContextProvider.cs
new file mode 100644
index 0000000..220e497
--- /dev/null
+++ b/GFramework.Core.Abstractions/architecture/IArchitectureContextProvider.cs
@@ -0,0 +1,21 @@
+namespace GFramework.Core.Abstractions.architecture;
+
+///
+/// 架构上下文提供者接口,用于解耦上下文获取逻辑
+///
+public interface IArchitectureContextProvider
+{
+ ///
+ /// 获取当前的架构上下文
+ ///
+ /// 架构上下文实例
+ IArchitectureContext GetContext();
+
+ ///
+ /// 尝试获取指定类型的架构上下文
+ ///
+ /// 架构上下文类型
+ /// 输出的上下文实例
+ /// 如果成功获取则返回true,否则返回false
+ bool TryGetContext(out T? context) where T : class, IArchitectureContext;
+}
\ No newline at end of file
diff --git a/GFramework.Core/architecture/GameContextProvider.cs b/GFramework.Core/architecture/GameContextProvider.cs
new file mode 100644
index 0000000..140af30
--- /dev/null
+++ b/GFramework.Core/architecture/GameContextProvider.cs
@@ -0,0 +1,29 @@
+using GFramework.Core.Abstractions.architecture;
+
+namespace GFramework.Core.architecture;
+
+///
+/// 基于 GameContext 的默认上下文提供者
+///
+public sealed class GameContextProvider : IArchitectureContextProvider
+{
+ ///
+ /// 获取当前的架构上下文(返回第一个注册的架构上下文)
+ ///
+ /// 架构上下文实例
+ public IArchitectureContext GetContext()
+ {
+ return GameContext.GetFirstArchitectureContext();
+ }
+
+ ///
+ /// 尝试获取指定类型的架构上下文
+ ///
+ /// 架构上下文类型
+ /// 输出的上下文实例
+ /// 如果成功获取则返回true,否则返回false
+ public bool TryGetContext(out T? context) where T : class, IArchitectureContext
+ {
+ return GameContext.TryGet(out context);
+ }
+}
\ No newline at end of file
diff --git a/GFramework.Core/architecture/RegistryInitializationHookBase.cs b/GFramework.Core/architecture/RegistryInitializationHookBase.cs
new file mode 100644
index 0000000..99dc490
--- /dev/null
+++ b/GFramework.Core/architecture/RegistryInitializationHookBase.cs
@@ -0,0 +1,55 @@
+using GFramework.Core.Abstractions.architecture;
+using GFramework.Core.Abstractions.enums;
+using GFramework.Core.Abstractions.utility;
+
+namespace GFramework.Core.architecture;
+
+///
+/// 注册表初始化钩子抽象基类,简化注册表配置的初始化逻辑
+///
+/// 注册表类型
+/// 配置类型
+public abstract class RegistryInitializationHookBase : IArchitectureLifecycleHook
+ where TRegistry : class, IUtility
+{
+ private readonly IEnumerable _configs;
+ private readonly ArchitecturePhase _targetPhase;
+
+ ///
+ /// 初始化注册表初始化钩子
+ ///
+ /// 配置集合
+ /// 目标执行阶段,默认为 AfterSystemInit
+ protected RegistryInitializationHookBase(
+ IEnumerable configs,
+ ArchitecturePhase targetPhase = ArchitecturePhase.AfterSystemInit)
+ {
+ _configs = configs;
+ _targetPhase = targetPhase;
+ }
+
+ ///
+ /// 当架构进入指定阶段时触发的回调方法
+ ///
+ /// 当前的架构阶段
+ /// 相关的架构实例
+ public void OnPhase(ArchitecturePhase phase, IArchitecture architecture)
+ {
+ if (phase != _targetPhase) return;
+
+ var registry = architecture.Context.GetUtility();
+ if (registry == null) return;
+
+ foreach (var config in _configs)
+ {
+ RegisterConfig(registry, config);
+ }
+ }
+
+ ///
+ /// 注册单个配置项到注册表
+ ///
+ /// 注册表实例
+ /// 配置项
+ protected abstract void RegisterConfig(TRegistry registry, TConfig config);
+}
\ No newline at end of file
diff --git a/GFramework.Core/architecture/ScopedContextProvider.cs b/GFramework.Core/architecture/ScopedContextProvider.cs
new file mode 100644
index 0000000..da865dd
--- /dev/null
+++ b/GFramework.Core/architecture/ScopedContextProvider.cs
@@ -0,0 +1,44 @@
+using GFramework.Core.Abstractions.architecture;
+
+namespace GFramework.Core.architecture;
+
+///
+/// 作用域上下文提供者,用于多架构实例场景
+///
+public sealed class ScopedContextProvider : IArchitectureContextProvider
+{
+ private readonly IArchitectureContext _context;
+
+ ///
+ /// 初始化作用域上下文提供者
+ ///
+ /// 要绑定的架构上下文实例
+ public ScopedContextProvider(IArchitectureContext context)
+ {
+ _context = context;
+ }
+
+ ///
+ /// 获取当前的架构上下文
+ ///
+ /// 架构上下文实例
+ public IArchitectureContext GetContext() => _context;
+
+ ///
+ /// 尝试获取指定类型的架构上下文
+ ///
+ /// 架构上下文类型
+ /// 输出的上下文实例
+ /// 如果成功获取则返回true,否则返回false
+ public bool TryGetContext(out T? context) where T : class, IArchitectureContext
+ {
+ if (_context is T typedContext)
+ {
+ context = typedContext;
+ return true;
+ }
+
+ context = null;
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
index d45d436..00b61df 100644
--- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
+++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
@@ -121,7 +121,7 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
}
// =========================
- // Context 属性(无 global::,与测试一致)
+ // Context 属性(使用 IArchitectureContextProvider)
// =========================
///
/// 生成Context属性
@@ -130,9 +130,11 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
private static void GenerateContextProperty(StringBuilder sb)
{
sb.AppendLine(" private global::GFramework.Core.Abstractions.architecture.IArchitectureContext? _context;");
+ sb.AppendLine(
+ " private static global::GFramework.Core.Abstractions.architecture.IArchitectureContextProvider? _contextProvider;");
sb.AppendLine();
sb.AppendLine(" /// ");
- sb.AppendLine(" /// 自动获取的架构上下文(懒加载,默认使用第一个架构)");
+ sb.AppendLine(" /// 自动获取的架构上下文(懒加载,默认使用 GameContextProvider)");
sb.AppendLine(" /// ");
sb.AppendLine(" protected global::GFramework.Core.Abstractions.architecture.IArchitectureContext Context");
sb.AppendLine(" {");
@@ -141,13 +143,24 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(" if (_context == null)");
sb.AppendLine(" {");
sb.AppendLine(
- " _context = global::GFramework.Core.architecture.GameContext.GetFirstArchitectureContext();");
+ " _contextProvider ??= new global::GFramework.Core.architecture.GameContextProvider();");
+ sb.AppendLine(" _context = _contextProvider.GetContext();");
sb.AppendLine(" }");
sb.AppendLine();
sb.AppendLine(" return _context;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine();
+ sb.AppendLine(" /// ");
+ sb.AppendLine(" /// 配置上下文提供者(用于测试或多架构场景)");
+ sb.AppendLine(" /// ");
+ sb.AppendLine(" /// 上下文提供者实例");
+ sb.AppendLine(
+ " public static void SetContextProvider(global::GFramework.Core.Abstractions.architecture.IArchitectureContextProvider provider)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" _contextProvider = provider;");
+ sb.AppendLine(" }");
+ sb.AppendLine();
}