using System; using System.Collections.Generic; using Encompass.Exceptions; namespace Encompass { internal class MessageStore { private readonly Dictionary _stores = new Dictionary(512); private void RegisterMessageType() where TMessage : struct { _stores.Add(typeof(TMessage), new TypedMessageStore()); } private TypedMessageStore Lookup() where TMessage : struct { if (!_stores.ContainsKey(typeof(TMessage))) { RegisterMessageType(); } return _stores[typeof(TMessage)] as TypedMessageStore; } public void AddMessage(in TMessage message) where TMessage : struct { Lookup().Add(message); } public void AddMessage(in TMessage message, double time) where TMessage : struct { Lookup().Add(message, time); } public void AddMessageIgnoringTimeDilation(in TMessage message, double time) where TMessage : struct { Lookup().AddIgnoringTimeDilation(message, time); } public ref readonly TMessage First() where TMessage : struct { if (!Any()) { throw new NoMessageOfTypeException("No Message of type {0} exists", typeof(TMessage).Name); } return ref Lookup().First(); } public ReadOnlySpan All() where TMessage : struct { return Lookup().All(); } public bool Any() where TMessage : struct { return Lookup().Any(); } public IEnumerable WithEntity(int entityID) where TMessage : struct, IHasEntity { return Lookup().WithEntity(entityID); } public ref readonly TMessage FirstWithEntity(int entityID) where TMessage : struct { return ref Lookup().FirstWithEntity(entityID); } public bool SomeWithEntity(int entityID) where TMessage : struct, IHasEntity { return Lookup().SomeWithEntity(entityID); } public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta) { foreach (var store in _stores.Values) { store.ProcessDelayedMessages(dilatedDelta, realtimeDelta); } } public void ClearAll() { foreach (var store in _stores.Values) { store.Clear(); } } } }