// #nullable enable namespace TestApp; /// /// 为当前规则类型补充自动生成的架构上下文访问实现。 /// partial class MyRule : global::GFramework.Core.Abstractions.Rule.IContextAware { private global::GFramework.Core.Abstractions.Architectures.IArchitectureContext? _context; private static global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider? _contextProvider; private static readonly object _contextSync = new(); /// /// 自动获取的架构上下文(懒加载,默认使用 GameContextProvider) /// protected global::GFramework.Core.Abstractions.Architectures.IArchitectureContext Context { get { var context = _context; if (context is not null) { return context; } // 在同一个同步域内协调懒加载与 provider 切换,避免读取到被并发重置的空提供者。 lock (_contextSync) { _contextProvider ??= new global::GFramework.Core.Architectures.GameContextProvider(); _context ??= _contextProvider.GetContext(); return _context; } } } /// /// 配置上下文提供者(用于测试或多架构场景) /// /// 上下文提供者实例 public static void SetContextProvider(global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider provider) { lock (_contextSync) { _contextProvider = provider; } } /// /// 重置上下文提供者为默认值(用于测试清理) /// public static void ResetContextProvider() { lock (_contextSync) { _contextProvider = null; } } void global::GFramework.Core.Abstractions.Rule.IContextAware.SetContext(global::GFramework.Core.Abstractions.Architectures.IArchitectureContext context) { _context = context; } global::GFramework.Core.Abstractions.Architectures.IArchitectureContext global::GFramework.Core.Abstractions.Rule.IContextAware.GetContext() { return Context; } }