diff --git a/GFramework.Core.Abstractions/model/IModel.cs b/GFramework.Core.Abstractions/model/IModel.cs index 3ae34d8..a456627 100644 --- a/GFramework.Core.Abstractions/model/IModel.cs +++ b/GFramework.Core.Abstractions/model/IModel.cs @@ -1,9 +1,11 @@ -namespace GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.rule; + +namespace GFramework.Core.Abstractions.model; /// /// 模型接口,定义了模型的基本行为和功能 /// -public interface IModel +public interface IModel : IContextAware { /// /// 初始化模型 diff --git a/GFramework.Core.Tests/GFramework.Core.Tests.csproj b/GFramework.Core.Tests/GFramework.Core.Tests.csproj new file mode 100644 index 0000000..672904d --- /dev/null +++ b/GFramework.Core.Tests/GFramework.Core.Tests.csproj @@ -0,0 +1,18 @@ + + + + enable + enable + net8.0;net9.0;net10.0 + + + + + + + + + + + + diff --git a/GFramework.Core.Tests/architecture/TestArchitecture.cs b/GFramework.Core.Tests/architecture/TestArchitecture.cs new file mode 100644 index 0000000..a0a56f3 --- /dev/null +++ b/GFramework.Core.Tests/architecture/TestArchitecture.cs @@ -0,0 +1,18 @@ +using GFramework.Core.architecture; +using GFramework.Core.Tests.model; +using GFramework.Core.Tests.system; + +namespace GFramework.Core.Tests.architecture; + +public sealed class TestArchitecture : Architecture +{ + public bool InitCalled { get; private set; } + + protected override void Init() + { + InitCalled = true; + + RegisterModel(new TestModel()); + RegisterSystem(new TestSystem()); + } +} \ No newline at end of file diff --git a/GFramework.Core.Tests/model/TestModel.cs b/GFramework.Core.Tests/model/TestModel.cs new file mode 100644 index 0000000..2cb560b --- /dev/null +++ b/GFramework.Core.Tests/model/TestModel.cs @@ -0,0 +1,35 @@ +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.model; + +namespace GFramework.Core.Tests.model; + +/// +/// 测试模型类,用于框架测试目的 +/// +public sealed class TestModel : IModel +{ + private IArchitectureContext _context = null!; + + /// + /// 获取模型是否已初始化的状态 + /// + public bool Inited { get; private set; } + + /// + /// 初始化模型 + /// + public void Init() + { + Inited = true; + } + + public void SetContext(IArchitectureContext context) + { + _context = context; + } + + public IArchitectureContext GetContext() + { + return _context; + } +} \ No newline at end of file diff --git a/GFramework.Core.Tests/system/TestSystem.cs b/GFramework.Core.Tests/system/TestSystem.cs new file mode 100644 index 0000000..a0a0e88 --- /dev/null +++ b/GFramework.Core.Tests/system/TestSystem.cs @@ -0,0 +1,59 @@ +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.system; + +namespace GFramework.Core.Tests.system; + +/// +/// 测试系统类,实现了ISystem接口 +/// +public sealed class TestSystem : ISystem +{ + /// + /// 架构上下文对象 + /// + private IArchitectureContext _context = null!; + + /// + /// 获取系统是否已初始化的状态 + /// + public bool Inited { get; private set; } + + /// + /// 获取系统是否已销毁的状态 + /// + public bool Destroyed { get; private set; } + + /// + /// 设置架构上下文 + /// + /// 架构上下文对象 + public void SetContext(IArchitectureContext context) + { + _context = context; + } + + /// + /// 获取架构上下文 + /// + /// 架构上下文对象 + public IArchitectureContext GetContext() + { + return _context; + } + + /// + /// 初始化系统 + /// + public void Init() + { + Inited = true; + } + + /// + /// 销毁系统 + /// + public void Destroy() + { + Destroyed = true; + } +} \ No newline at end of file diff --git a/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs b/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs new file mode 100644 index 0000000..c1e3cc1 --- /dev/null +++ b/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs @@ -0,0 +1,47 @@ +using System.Reflection; +using GFramework.Core.Abstractions.enums; +using GFramework.Core.architecture; +using GFramework.Core.Tests.architecture; +using GFramework.Core.Tests.model; +using GFramework.Core.Tests.system; +using NUnit.Framework; + +namespace GFramework.Core.Tests.tests; + +[TestFixture] +public class ArchitectureInitializationTests +{ + [Test] + public void Architecture_Should_Initialize_All_Components_Correctly() + { + // Arrange + var architecture = new TestArchitecture(); + + // Act + architecture.Initialize(); + + // Assert - Init() 被调用 + Assert.That(architecture.InitCalled, Is.True, "Architecture.Init() should be called"); + + // Assert - Runtime 已创建 + Assert.That(architecture.Runtime, Is.Not.Null, "ArchitectureRuntime should be created"); + + // Assert - Phase 已进入 Ready + var phaseProperty = typeof(Architecture) + .GetProperty("CurrentPhase", BindingFlags.Instance | BindingFlags.NonPublic); + + var phase = (ArchitecturePhase)phaseProperty!.GetValue(architecture)!; + Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready), "Architecture should be in Ready phase"); + + // Assert - Model 初始化 + var context = architecture.Context; + var model = context.GetModel(); + Assert.That(model, Is.Not.Null); + Assert.That(model.Inited, Is.True, "Model should be initialized"); + + // Assert - System 初始化 + var system = context.GetSystem(); + Assert.That(system, Is.Not.Null); + Assert.That(system.Inited, Is.True, "System should be initialized"); + } +} \ No newline at end of file diff --git a/GFramework.Core/ioc/IocContainer.cs b/GFramework.Core/ioc/IocContainer.cs index 739c6ba..6d48fcc 100644 --- a/GFramework.Core/ioc/IocContainer.cs +++ b/GFramework.Core/ioc/IocContainer.cs @@ -52,7 +52,7 @@ public class IocContainer : ContextAwareBase, IIocContainer #region Register - public void Init() + protected override void OnContextReady() { _logger = Context.LoggerFactory.GetLogger(nameof(IocContainer)); } diff --git a/GFramework.Core/model/AbstractModel.cs b/GFramework.Core/model/AbstractModel.cs index 13d2973..8f81fe4 100644 --- a/GFramework.Core/model/AbstractModel.cs +++ b/GFramework.Core/model/AbstractModel.cs @@ -11,7 +11,7 @@ public abstract class AbstractModel : IModel /// /// 模型所属的架构实例 /// - protected IArchitecture Architecture; + protected IArchitectureContext _context { get; private set; } /// /// 初始化模型,调用抽象方法OnInit执行具体初始化逻辑 @@ -21,24 +21,17 @@ public abstract class AbstractModel : IModel OnInit(); } - /// - /// 获取模型所属的架构实例 - /// - /// 返回当前模型关联的架构对象 - public IArchitecture GetArchitecture() + public void SetContext(IArchitectureContext context) { - return Architecture; + _context = context; } - /// - /// 设置模型所属的架构实例 - /// - /// 要关联到此模型的架构实例 - public void SetArchitecture(IArchitecture architecture) + public IArchitectureContext GetContext() { - Architecture = architecture; + return _context; } + /// /// 抽象初始化方法,由子类实现具体的初始化逻辑 /// diff --git a/GFramework.csproj b/GFramework.csproj index f737276..961552b 100644 --- a/GFramework.csproj +++ b/GFramework.csproj @@ -46,6 +46,7 @@ + @@ -75,6 +76,7 @@ + @@ -90,9 +92,13 @@ + + + + diff --git a/GFramework.sln b/GFramework.sln index 54e8433..3eba55b 100644 --- a/GFramework.sln +++ b/GFramework.sln @@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Core.Abstraction EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Game.Abstractions", "GFramework.Game.Abstractions\GFramework.Game.Abstractions.csproj", "{E20DBA4C-CEB9-4184-B614-5A99A9AE4472}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Core.Tests", "GFramework.Core.Tests\GFramework.Core.Tests.csproj", "{759BCD95-A9D9-4D8F-9255-A9F1B661DF74}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -78,5 +80,9 @@ Global {E20DBA4C-CEB9-4184-B614-5A99A9AE4472}.Debug|Any CPU.Build.0 = Debug|Any CPU {E20DBA4C-CEB9-4184-B614-5A99A9AE4472}.Release|Any CPU.ActiveCfg = Release|Any CPU {E20DBA4C-CEB9-4184-B614-5A99A9AE4472}.Release|Any CPU.Build.0 = Release|Any CPU + {759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Debug|Any CPU.Build.0 = Debug|Any CPU + {759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Release|Any CPU.ActiveCfg = Release|Any CPU + {759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal