diff --git a/Capability/GameFrameX.Capability.Hosting.Core/CapabilityDependencyChecker.cs b/Capability/GameFrameX.Capability.Hosting.Core/CapabilityDependencyChecker.cs index 46cd12c6..ad3e6db3 100644 --- a/Capability/GameFrameX.Capability.Hosting.Core/CapabilityDependencyChecker.cs +++ b/Capability/GameFrameX.Capability.Hosting.Core/CapabilityDependencyChecker.cs @@ -106,9 +106,40 @@ private static void ApplyHostModulePhaseEdges(IReadOnlyList e return; } - var beforePhaseIds = new List(); - var afterPhaseIds = new List(); - var hostModuleIds = new HashSet(StringComparer.Ordinal); + var phases = CollectHostModulePhases(hostModules, edges); + if (phases.HostModuleIds.Count == 0) + { + return; + } + + for (var i = 0; i < enabledModules.Count; i++) + { + var moduleId = enabledModules[i].Manifest.Id; + if (phases.HostModuleIds.Contains(moduleId)) + { + // After 相位宿主模块依赖能力模块与 Before 相位宿主模块;Before 相位宿主模块之间不注入合成边(由自身声明与 Id 序决定)。 + if (phases.AfterPhaseIds.Contains(moduleId)) + { + AddAfterPhaseHostModuleDependencies(moduleId, enabledModules, phases.AfterPhaseIds, edges); + } + + continue; + } + + // 能力模块依赖全部 Before 相位宿主模块。 + AddBeforePhaseHostModuleEdges(moduleId, phases.BeforePhaseIds, edges); + } + } + + /// + /// 收集进入启用集的宿主模块相位分类(未进入本次装配模块表的宿主模块不参与合成边)。 + /// + /// 宿主模块表(含相位)。 + /// 定序边累积器(启用集成员判定用)。 + /// 相位分类结果。 + private static HostModulePhases CollectHostModulePhases(IReadOnlyList hostModules, Dictionary> edges) + { + var phases = new HostModulePhases(); for (var i = 0; i < hostModules.Count; i++) { var hostModuleId = hostModules[i].Module.Manifest.Id; @@ -118,54 +149,68 @@ private static void ApplyHostModulePhaseEdges(IReadOnlyList e continue; } - hostModuleIds.Add(hostModuleId); + phases.HostModuleIds.Add(hostModuleId); if (hostModules[i].Phase == CapabilityHostModulePhase.BeforeCapabilities) { - beforePhaseIds.Add(hostModuleId); + phases.BeforePhaseIds.Add(hostModuleId); } else { - afterPhaseIds.Add(hostModuleId); + phases.AfterPhaseIds.Add(hostModuleId); } } - if (beforePhaseIds.Count == 0 && afterPhaseIds.Count == 0) - { - return; - } + return phases; + } - for (var i = 0; i < enabledModules.Count; i++) + /// + /// 注入 After 相位宿主模块的合成定序边:依赖全部能力模块与全部 Before 相位宿主模块(不含其他 After 相位宿主模块)。 + /// + /// After 相位宿主模块 Id。 + /// 启用模块。 + /// After 相位宿主模块 Id 集。 + /// 定序边累积器。 + private static void AddAfterPhaseHostModuleDependencies(string moduleId, IReadOnlyList enabledModules, List afterPhaseIds, Dictionary> edges) + { + var dependentIds = edges[moduleId]; + for (var j = 0; j < enabledModules.Count; j++) { - var moduleId = enabledModules[i].Manifest.Id; - if (hostModuleIds.Contains(moduleId)) + var otherId = enabledModules[j].Manifest.Id; + if (!string.Equals(otherId, moduleId, StringComparison.Ordinal) + && !afterPhaseIds.Contains(otherId)) { - // After 相位宿主模块依赖能力模块与 Before 相位宿主模块;Before 相位宿主模块之间不注入合成边(由自身声明与 Id 序决定)。 - if (afterPhaseIds.Contains(moduleId)) - { - var dependentIds = edges[moduleId]; - for (var j = 0; j < enabledModules.Count; j++) - { - var otherId = enabledModules[j].Manifest.Id; - if (!string.Equals(otherId, moduleId, StringComparison.Ordinal) - && !afterPhaseIds.Contains(otherId)) - { - dependentIds.Add(otherId); - } - } - } - - continue; + dependentIds.Add(otherId); } + } + } - // 能力模块依赖全部 Before 相位宿主模块。 - var moduleEdges = edges[moduleId]; - for (var j = 0; j < beforePhaseIds.Count; j++) - { - moduleEdges.Add(beforePhaseIds[j]); - } + /// + /// 注入能力模块对全部 Before 相位宿主模块的合成定序边。 + /// + /// 能力模块 Id。 + /// Before 相位宿主模块 Id 列表。 + /// 定序边累积器。 + private static void AddBeforePhaseHostModuleEdges(string moduleId, List beforePhaseIds, Dictionary> edges) + { + var moduleEdges = edges[moduleId]; + for (var j = 0; j < beforePhaseIds.Count; j++) + { + moduleEdges.Add(beforePhaseIds[j]); } } + /// + /// 宿主模块相位分类结果(均已进入启用集)。 + /// + private sealed class HostModulePhases + { + public List BeforePhaseIds { get; } = new List(); + + public List AfterPhaseIds { get; } = new List(); + + public HashSet HostModuleIds { get; } = new HashSet(StringComparer.Ordinal); + } + /// /// 检查单个模块的必选 / 可选依赖(必选缺失记问题;可选缺失记数据;在场依赖记定序边与来源策略)。