/* * Copyright 2023 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Ragon.Client; public class RagonEventCache { private readonly Dictionary _eventsRegistryByType = new(); private readonly HashSet _codes = new(); private readonly HashSet _types = new(); private ushort _eventIdGenerator = 0; public ushort GetEventCode(TEvent _) where TEvent : IRagonEvent { var type = typeof(TEvent); var evntCode = _eventsRegistryByType[type]; return evntCode; } public void Register() where T : IRagonEvent, new() { var type = typeof(T); if (_types.Contains(type)) { RagonLog.Trace($"[Ragon] Event already registered: {type.Name}"); return; } RagonLog.Trace($"[Ragon] Registered Event: {type.Name} - {_eventIdGenerator}"); _eventsRegistryByType.Add(type, _eventIdGenerator); _codes.Add(_eventIdGenerator); _types.Add(type); _eventIdGenerator++; } public void Register(ushort evntCode) where T : IRagonEvent, new() { var type = typeof(T); if (_codes.Contains(evntCode) || _types.Contains(type)) { RagonLog.Warn($"[Ragon] Event already registered: {type.Name} - {evntCode}"); return; } RagonLog.Trace($"[Ragon] Registered Event: {type.Name} - {evntCode}"); _codes.Add(evntCode); _types.Add(type); _eventsRegistryByType.Add(type, evntCode); } public T Create() where T : IRagonEvent, new() { return new T(); } }