// --------------------------------------------------------------------------------------------- // Copyright (c) 2021-2025, Jiaqi (0x7c13) Liu. All rights reserved. // See LICENSE file in the project root for license information. // --------------------------------------------------------------------------------------------- namespace Pal3.Game.Command { using System; using System.Collections.Generic; using System.Linq; using Core.Command; using Engine.Logging; public sealed class CommandExecutorRegistry : ICommandExecutorRegistry { public static ICommandExecutorRegistry Instance { get { return _instance ??= new CommandExecutorRegistry(); } } private static ICommandExecutorRegistry _instance; private readonly Dictionary> _executors = new (); private CommandExecutorRegistry() { } // Hide constructor, use Instance instead. /// public void Register(ICommandExecutor executor) where T : TCommand { if (_executors.ContainsKey(typeof(ICommandExecutor))) { if (!_executors[typeof(ICommandExecutor)].Contains(executor)) { _executors[typeof(ICommandExecutor)].Add(executor); } } else { _executors[typeof(ICommandExecutor)] = new HashSet { executor }; } } /// public void Register(object executor) { IList executorTypes = executor.GetType().GetInterfaces() .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandExecutor<>)).ToList(); if (executorTypes.Count == 0) { EngineLogger.LogError($"No ICommandExecutor interface found for type: {executor.GetType()}"); return; } foreach (Type executorType in executorTypes) { if (_executors.ContainsKey(executorType)) { if (!_executors[executorType].Contains(executor)) { _executors[executorType].Add(executor); } } else { _executors[executorType] = new HashSet { executor }; } } } /// public void UnRegister(ICommandExecutor executor) where T : TCommand { if (!_executors.ContainsKey(typeof(ICommandExecutor))) { return; } _executors[typeof(ICommandExecutor)].Remove(executor); if (_executors[typeof(ICommandExecutor)].Count == 0) { _executors.Remove(typeof(ICommandExecutor)); } } /// public void UnRegister(object executor) { IList executorTypes = executor.GetType().GetInterfaces() .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandExecutor<>)).ToList(); if (executorTypes.Count == 0) { EngineLogger.LogError($"No ICommandExecutor interface found for type: {executor.GetType()}"); return; } foreach (Type executorType in executorTypes) { if (!_executors.TryGetValue(executorType, out HashSet executors)) { continue; } executors.Remove(executor); if (_executors[executorType].Count == 0) { _executors.Remove(executorType); } } } /// public IEnumerable> GetRegisteredExecutors() where T : TCommand { Type handlerType = typeof(ICommandExecutor); if (!_executors.TryGetValue(handlerType, out HashSet executors)) yield break; foreach (ICommandExecutor executor in executors) { yield return executor; } } /// public IEnumerable GetRegisteredExecutors(Type type) { if (!_executors.TryGetValue(type, out HashSet executors)) yield break; foreach (object executor in executors) { yield return executor; } } } }