using System; using System.Collections.Generic; namespace ShrinkModFramework { public sealed class ShrinkModRegistryManager { private readonly Dictionary _registries = new(); public IReadOnlyShrinkModRegistry GetOrCreateRegistry(string name) => GetOrCreateMutableRegistry(name); internal ShrinkModRegistry GetOrCreateMutableRegistry(string name) { var compositeKey = BuildCompositeKey(typeof(T), name); if (_registries.TryGetValue(compositeKey, out var existing)) return (ShrinkModRegistry)existing; var registry = new ShrinkModRegistry(name); _registries.Add(compositeKey, registry); return registry; } public bool TryGetRegistry(string name, out IReadOnlyShrinkModRegistry registry) { var compositeKey = BuildCompositeKey(typeof(T), name); if (_registries.TryGetValue(compositeKey, out var existing)) { registry = (ShrinkModRegistry)existing; return true; } registry = null; return false; } internal void Clear() => _registries.Clear(); internal void RemoveOwnedEntries(string ownerModId) { foreach (var registry in _registries.Values) { if (registry is IShrinkOwnedRegistry ownedRegistry) ownedRegistry.RemoveOwnedEntries(ownerModId); } } private static string BuildCompositeKey(Type type, string name) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("注册表名称不能为空。", nameof(name)); return $"{type.AssemblyQualifiedName}::{name.Trim()}"; } } }