Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace GFramework.Core.Abstractions.architecture;

/// <summary>
/// 架构模块注册表 - 用于外部模块的自动注册
/// </summary>
public static class ArchitectureModuleRegistry
{
private static readonly List<Func<IServiceModule>> _factories = [];

/// <summary>
/// 注册模块工厂
/// </summary>
/// <param name="factory">模块工厂函数</param>
public static void Register(Func<IServiceModule> factory)
{
_factories.Add(factory);
}

/// <summary>
/// 创建所有已注册的模块实例
/// </summary>
/// <returns>模块实例集合</returns>
public static IEnumerable<IServiceModule> CreateModules()
{
return _factories.Select(f => f());
}

/// <summary>
/// 清空注册表(主要用于测试)
/// </summary>
public static void Clear()
{
_factories.Clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,4 @@ public sealed class ArchitectureProperties
/// 默认值为 false,表示不启用严格验证。
/// </summary>
public bool StrictPhaseValidation { get; set; }

/// <summary>
/// 启用 ECS(Entity Component System)功能的开关。
/// 当设置为 true 时,架构将启用 ECS 相关功能。
/// </summary>
public bool EnableEcs { get; set; }
}
4 changes: 3 additions & 1 deletion GFramework.Core.Tests/GFramework.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net10.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mediator.Abstractions" Version="3.0.1"/>
Expand Down
22 changes: 0 additions & 22 deletions GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using GFramework.Core.ioc;
using GFramework.Core.query;
using Mediator;
using NUnit.Framework;
using ICommand = GFramework.Core.Abstractions.command.ICommand;

namespace GFramework.Core.Tests.architecture;
Expand Down Expand Up @@ -269,27 +268,6 @@ public void ModuleManager_Should_Not_Be_Null()
{
Assert.That(_services!.ModuleManager, Is.Not.Null);
}

/// <summary>
/// 测试EnableEcs配置开关
/// </summary>
[Test]
public void EnableEcs_Should_Control_Ecs_Module_Registration()
{
var propertiesWithEcs = new ArchitectureProperties { EnableEcs = true };
var propertiesWithoutEcs = new ArchitectureProperties { EnableEcs = false };

var servicesWithEcs = new ArchitectureServices();
servicesWithEcs.ModuleManager.RegisterBuiltInModules(servicesWithEcs.Container, propertiesWithEcs);

var servicesWithoutEcs = new ArchitectureServices();
servicesWithoutEcs.ModuleManager.RegisterBuiltInModules(servicesWithoutEcs.Container, propertiesWithoutEcs);

var modulesWithEcs = servicesWithEcs.ModuleManager.GetModules();
var modulesWithoutEcs = servicesWithoutEcs.ModuleManager.GetModules();

Assert.That(modulesWithEcs.Count, Is.GreaterThan(modulesWithoutEcs.Count));
}
}

#region Test Classes
Expand Down
2 changes: 0 additions & 2 deletions GFramework.Core/GFramework.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.3"/>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0"/>
<PackageReference Include="Arch" Version="2.1.0"/>
<PackageReference Include="Arch.System" Version="1.1.0"/>
</ItemGroup>
</Project>
19 changes: 11 additions & 8 deletions GFramework.Core/services/ServiceModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using GFramework.Core.Abstractions.lifecycle;
using GFramework.Core.Abstractions.logging;
using GFramework.Core.Abstractions.properties;
using GFramework.Core.ecs;
using GFramework.Core.logging;
using GFramework.Core.services.modules;

Expand Down Expand Up @@ -44,11 +43,11 @@ public void RegisterModule(IServiceModule? module)

/// <summary>
/// 注册内置服务模块,并根据优先级排序后完成服务注册。
/// 内置模块包括事件总线、命令执行器、查询执行器等核心模块
/// 并根据配置决定是否启用ECS模块
/// 内置模块包括事件总线、命令执行器、查询执行器等核心模块
/// 同时注册通过 ModuleInitializer 自动注册的外部模块
/// </summary>
/// <param name="container">IoC容器实例,用于模块服务注册。</param>
/// <param name="properties">架构属性配置,用于判断是否启用ECS模块。</param>
/// <param name="properties">架构属性配置。</param>
public void RegisterBuiltInModules(IIocContainer container, ArchitectureProperties properties)
Comment thread
GeWuYou marked this conversation as resolved.
Outdated
{
if (_builtInModulesRegistered)
Expand All @@ -57,29 +56,33 @@ public void RegisterBuiltInModules(IIocContainer container, ArchitectureProperti
return;
}

// 注册内置模块
RegisterModule(new EventBusModule());
RegisterModule(new CommandExecutorModule());
RegisterModule(new QueryExecutorModule());
RegisterModule(new AsyncQueryExecutorModule());

if (properties.EnableEcs)
// 注册外部模块(通过 ModuleInitializer 自动注册)
foreach (var module in ArchitectureModuleRegistry.CreateModules())
{
RegisterModule(new ArchEcsModule(enabled: true));
_logger.Info("ECS module enabled via configuration");
RegisterModule(module);
_logger.Info($"External module registered: {module.ModuleName}");
}

// 按优先级排序
var sortedModules = _modules.OrderBy(m => m.Priority).ToList();
_modules.Clear();
_modules.AddRange(sortedModules);

// 注册服务
foreach (var module in _modules.Where(module => module.IsEnabled))
{
_logger.Debug($"Registering services for module: {module.ModuleName}");
module.Register(container);
}

_builtInModulesRegistered = true;
_logger.Info($"Registered {_modules.Count} built-in service modules");
_logger.Info($"Registered {_modules.Count} service modules");
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions GFramework.Ecs.Arch.Abstractions/ArchOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace GFramework.Ecs.Arch.Abstractions;

/// <summary>
/// Arch ECS 配置选项
/// </summary>
public sealed class ArchOptions
{
/// <summary>
/// World 初始容量
/// </summary>
public int WorldCapacity { get; set; } = 1000;

/// <summary>
/// 是否启用统计信息
/// </summary>
public bool EnableStatistics { get; set; }

/// <summary>
/// 模块优先级
/// </summary>
public int Priority { get; set; } = 50;
}
18 changes: 18 additions & 0 deletions GFramework.Ecs.Arch.Abstractions/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project>
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.264">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.71">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>GeWuYou.$(AssemblyName)</PackageId>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\GFramework.Core.Abstractions\GFramework.Core.Abstractions.csproj" PrivateAssets="all"/>
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions GFramework.Ecs.Arch.Abstractions/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using System;
global using System.Collections.Generic;
global using System.Threading.Tasks;
15 changes: 15 additions & 0 deletions GFramework.Ecs.Arch.Abstractions/IArchEcsModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GFramework.Core.Abstractions.architecture;

namespace GFramework.Ecs.Arch.Abstractions;

/// <summary>
/// Arch ECS 模块接口 - 定义 ECS 模块的核心契约
/// </summary>
public interface IArchEcsModule : IServiceModule
{
/// <summary>
/// 更新所有 ECS 系统
/// </summary>
/// <param name="deltaTime">帧间隔时间</param>
void Update(float deltaTime);
}
16 changes: 16 additions & 0 deletions GFramework.Ecs.Arch.Abstractions/IArchSystemAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using GFramework.Core.Abstractions.system;

namespace GFramework.Ecs.Arch.Abstractions;

/// <summary>
/// Arch 系统适配器接口 - 桥接 Arch.System.ISystem&lt;T&gt; 到框架上下文
/// </summary>
/// <typeparam name="T">系统数据类型(通常是 float 表示 deltaTime)</typeparam>
public interface IArchSystemAdapter<T> : ISystem
{
/// <summary>
/// 更新系统
/// </summary>
/// <param name="t">系统数据参数(通常是 deltaTime)</param>
void Update(in T t);
}
25 changes: 25 additions & 0 deletions GFramework.Ecs.Arch.Tests/GFramework.Ecs.Arch.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0"/>
<PackageReference Include="Moq" Version="4.20.72"/>
<PackageReference Include="NUnit" Version="4.5.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="6.1.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GFramework.Ecs.Arch\GFramework.Ecs.Arch.csproj"/>
<ProjectReference Include="..\GFramework.Core\GFramework.Core.csproj"/>
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions GFramework.Ecs.Arch.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using System;
global using System.Collections.Generic;
global using System.Threading.Tasks;
global using NUnit.Framework;
global using GFramework.Ecs.Arch;
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using Arch.Core;
using GFramework.Core.Abstractions.rule;
using GFramework.Core.architecture;
using GFramework.Core.ecs;
using GFramework.Core.ecs.components;
using GFramework.Core.ecs.systems;
using GFramework.Core.ioc;
using NUnit.Framework;
using GFramework.Ecs.Arch.components;
using GFramework.Ecs.Arch.systems;

namespace GFramework.Core.Tests.ecs;
namespace GFramework.Ecs.Arch.Tests.ecs;

/// <summary>
/// ECS 高级功能测试类 - 使用 Arch 原生 API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using Arch.Core;
using GFramework.Core.Abstractions.rule;
using GFramework.Core.architecture;
using GFramework.Core.ecs;
using GFramework.Core.ecs.components;
using GFramework.Core.ecs.systems;
using GFramework.Core.ioc;
using NUnit.Framework;
using GFramework.Ecs.Arch.components;
using GFramework.Ecs.Arch.systems;

namespace GFramework.Core.Tests.ecs;
namespace GFramework.Ecs.Arch.Tests.ecs;

/// <summary>
/// ECS 基础功能测试类 - 使用 Arch 原生 API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using Arch.Core;
using GFramework.Core.Abstractions.rule;
using GFramework.Core.architecture;
using GFramework.Core.ecs;
using GFramework.Core.ecs.components;
using GFramework.Core.ecs.systems;
using GFramework.Core.ioc;
using NUnit.Framework;
using GFramework.Ecs.Arch.components;
using GFramework.Ecs.Arch.systems;

namespace GFramework.Core.Tests.ecs;
namespace GFramework.Ecs.Arch.Tests.ecs;

/// <summary>
/// ECS 集成测试类 - 使用 Arch 原生 API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Arch.Core;
using GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.ioc;

namespace GFramework.Core.ecs;
namespace GFramework.Ecs.Arch;

/// <summary>
/// Arch ECS 模块 - 核心适配器,桥接 Arch 到框架生命周期
/// </summary>
public sealed class ArchEcsModule : IServiceModule
public sealed class ArchEcsModule : IArchEcsModule
{
private readonly List<ArchSystemAdapter<float>> _systems = [];
private IIocContainer? _container;
Expand Down
Loading
Loading