using System.Collections.ObjectModel; namespace Sand.Core; public sealed class ParticleLibrary : IParticleLibrary { private readonly IReadOnlyList _definitions; private readonly Dictionary _typeIds; public ParticleLibrary(IReadOnlyList definitions) { if (definitions.Count == 0) { throw new ArgumentException("At least one particle definition is required.", nameof(definitions)); } _definitions = new ReadOnlyCollection(definitions.ToArray()); _typeIds = new Dictionary(StringComparer.OrdinalIgnoreCase); for (var index = 0; index < definitions.Count; index++) { _typeIds[definitions[index].Id] = checked((ushort)(index + 1)); } } public IReadOnlyList Definitions => _definitions; public ushort GetTypeId(string id) { if (_typeIds.TryGetValue(id, out var typeId)) { return typeId; } throw new KeyNotFoundException($"Unknown particle id '{id}'."); } public bool TryGetTypeId(string id, out ushort typeId) => _typeIds.TryGetValue(id, out typeId); public ParticleDef GetDefinition(ushort typeId) { if (typeId == 0 || typeId > _definitions.Count) { throw new ArgumentOutOfRangeException(nameof(typeId), $"Unknown type id '{typeId}'."); } return _definitions[typeId - 1]; } }