From 8bc62abbe296042488051a6b48331bb23c91da5c Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Fri, 12 Jul 2024 08:07:15 +0500 Subject: [PATCH 01/11] good luck searching bugs caused by this --- .../Graphics/Rendering/Renderer.Render.cs | 4 +- .../Graphics/Rendering/Renderer.Window.cs | 6 +- .../Graphics/Rendering/Renderer.cs | 15 +- Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs | 8 +- Hypercube.Client/Runtimes/Runtime.cs | 13 +- .../Realisation/EventBus/EntitiesEventBus.cs | 15 +- .../Manager/EntitiesComponentManager.cs | 9 +- .../Realisation/Manager/EntitiesManager.cs | 4 +- .../Manager/EntitiesSystemManager.cs | 19 +-- Hypercube.Shared/EventBus/EventBus.cs | 128 +++++++++++++++--- .../Events/Broadcast/BroadcastRegistration.cs | 28 ++++ Hypercube.Shared/EventBus/Events/EventData.cs | 13 ++ .../EventBus/Events/EventHandler.cs | 4 + .../EventBus/Events/IEventArgs.cs | 15 ++ .../EventBus/Events/IEventSubscriber.cs | 5 + Hypercube.Shared/EventBus/Events/Unit.cs | 12 ++ Hypercube.Shared/EventBus/IEventBus.cs | 12 +- .../Scenes/Manager/SceneManager.cs | 4 +- .../EventBus/EventBusRefTests.cs | 97 +++++++++++++ .../EventBus/EventBusSubRaiseTests.cs | 63 +++++++++ 20 files changed, 408 insertions(+), 66 deletions(-) create mode 100644 Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs create mode 100644 Hypercube.Shared/EventBus/Events/EventData.cs create mode 100644 Hypercube.Shared/EventBus/Events/EventHandler.cs create mode 100644 Hypercube.Shared/EventBus/Events/IEventArgs.cs create mode 100644 Hypercube.Shared/EventBus/Events/IEventSubscriber.cs create mode 100644 Hypercube.Shared/EventBus/Events/Unit.cs create mode 100644 Hypercube.UnitTests/EventBus/EventBusRefTests.cs create mode 100644 Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs b/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs index dc5a1c6..3ded839 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.Render.cs @@ -65,7 +65,7 @@ private void OnLoad() _logger.EngineInfo("Loaded"); } - private void OnFrameUpdate(UpdateFrameEvent args) + private void OnFrameUpdate(ref UpdateFrameEvent args) { #if DEBUG _windowManager.WindowSetTitle(MainWindow, $"FPS: {_timing.Fps} | RealTime: {_timing.RealTime} | cPos: {_cameraManager.MainCamera?.Position ?? null} | cRot: {_cameraManager.MainCamera?.Rotation ?? null}"); @@ -74,7 +74,7 @@ private void OnFrameUpdate(UpdateFrameEvent args) _cameraManager.UpdateInput(_cameraManager.MainCamera, args.DeltaSeconds); } - private void OnFrameRender(RenderFrameEvent args) + private void OnFrameRender(ref RenderFrameEvent args) { BatchClear(); diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs b/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs index 0033816..7f5b065 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs @@ -39,11 +39,11 @@ public void DestroyWindow(WindowRegistration registration) public void CloseWindow(WindowRegistration registration) { - _eventBus.Invoke(new WindowClosedEvent(registration)); + _eventBus.RaiseEvent(new WindowClosedEvent(registration)); if (registration.Id == _mainWindowId) { - _eventBus.Invoke(new MainWindowClosedEvent(registration)); + _eventBus.RaiseEvent(new MainWindowClosedEvent(registration)); return; } @@ -84,6 +84,6 @@ private bool InitMainWindow(ContextInfo? context, WindowCreateSettings settings) public void OnFocusChanged(WindowRegistration window, bool focused) { - _eventBus.Invoke(new WindowFocusChangedEvent(window, focused)); + _eventBus.RaiseEvent(new WindowFocusChangedEvent(window, focused)); } } \ No newline at end of file diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.cs b/Hypercube.Client/Graphics/Rendering/Renderer.cs index a350fa7..c5cc42b 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.cs @@ -6,6 +6,7 @@ using Hypercube.Client.Graphics.Windows.Manager; using Hypercube.Shared.Dependency; using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.Logging; using Hypercube.Shared.Resources.Manager; using Hypercube.Shared.Runtimes.Event; @@ -17,7 +18,7 @@ namespace Hypercube.Client.Graphics.Rendering; -public sealed partial class Renderer : IRenderer, IPostInject +public sealed partial class Renderer : IRenderer, IPostInject, IEventSubscriber { [Dependency] private readonly IEventBus _eventBus = default!; [Dependency] private readonly ITextureManager _textureManager = default!; @@ -68,19 +69,19 @@ public sealed partial class Renderer : IRenderer, IPostInject public void PostInject() { - _eventBus.Subscribe(OnInitialization); - _eventBus.Subscribe(OnStartup); - _eventBus.Subscribe(OnFrameUpdate); - _eventBus.Subscribe(OnFrameRender); + _eventBus.SubscribeEvent(this, OnInitialization); + _eventBus.SubscribeEvent(this, OnStartup); + _eventBus.SubscribeEvent(this, OnFrameUpdate); + _eventBus.SubscribeEvent(this, OnFrameRender); } - private void OnInitialization(RuntimeInitializationEvent args) + private void OnInitialization(ref RuntimeInitializationEvent args) { _windowManager = CreateWindowManager(); _bindingsContext = new BindingsContext(_windowManager); } - private void OnStartup(RuntimeStartupEvent args) + private void OnStartup(ref RuntimeStartupEvent args) { _currentThread = Thread.CurrentThread; _logger.EngineInfo($"Working thread {_currentThread.Name}"); diff --git a/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs b/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs index 163a78e..074e716 100644 --- a/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs +++ b/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs @@ -20,10 +20,10 @@ public void Run() _timing.StartFrame(); var deltaTime = (float)_timing.RealFrameTime.TotalSeconds; - _eventBus.Invoke(new InputFrameEvent(deltaTime)); - _eventBus.Invoke(new TickFrameEvent(deltaTime)); - _eventBus.Invoke(new UpdateFrameEvent(deltaTime)); - _eventBus.Invoke(new RenderFrameEvent(deltaTime)); + _eventBus.RaiseEvent(new InputFrameEvent(deltaTime)); + _eventBus.RaiseEvent(new TickFrameEvent(deltaTime)); + _eventBus.RaiseEvent(new UpdateFrameEvent(deltaTime)); + _eventBus.RaiseEvent(new RenderFrameEvent(deltaTime)); } } diff --git a/Hypercube.Client/Runtimes/Runtime.cs b/Hypercube.Client/Runtimes/Runtime.cs index 1473661..f221cb0 100644 --- a/Hypercube.Client/Runtimes/Runtime.cs +++ b/Hypercube.Client/Runtimes/Runtime.cs @@ -4,12 +4,13 @@ using Hypercube.Client.Runtimes.Loop; using Hypercube.Shared.Dependency; using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.Logging; using Hypercube.Shared.Runtimes.Event; namespace Hypercube.Client.Runtimes; -public sealed partial class Runtime(DependenciesContainer dependenciesContainer) : IPostInject +public sealed partial class Runtime(DependenciesContainer dependenciesContainer) : IPostInject, IEventSubscriber { [Dependency] private readonly IEventBus _eventBus = default!; [Dependency] private readonly IRuntimeLoop _loop = default!; @@ -18,7 +19,7 @@ public sealed partial class Runtime(DependenciesContainer dependenciesContainer) public void PostInject() { - _eventBus.Subscribe(OnMainWindowClosed); + _eventBus.SubscribeEvent(this, OnMainWindowClosed); } /// @@ -43,24 +44,24 @@ private void Shutdown(string? reason = null) reason = reason is null ? "Shutting down" : $"Shutting down, reason: {reason}"; _logger.EngineInfo(reason); - _eventBus.Invoke(new RuntimeShutdownEvent(reason)); + _eventBus.RaiseEvent(new RuntimeShutdownEvent(reason)); _loop.Shutdown(); } private void RunLoop() { _logger.EngineInfo("Startup"); - _eventBus.Invoke(new RuntimeStartupEvent()); + _eventBus.RaiseEvent(new RuntimeStartupEvent()); _loop.Run(); } private void Initialize() { _logger.EngineInfo("Initialize"); - _eventBus.Invoke(new RuntimeInitializationEvent()); + _eventBus.RaiseEvent(new RuntimeInitializationEvent()); } - private void OnMainWindowClosed(MainWindowClosedEvent obj) + private void OnMainWindowClosed(ref MainWindowClosedEvent obj) { Shutdown("Main window closed"); } diff --git a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs index 7038b0b..1667fa7 100644 --- a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs +++ b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs @@ -1,18 +1,25 @@ -namespace Hypercube.Shared.Entities.Realisation.EventBus; +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.Shared.Entities.Realisation.EventBus; public sealed class EntitiesEventBus : IEntitiesEventBus { - public void Subscribe(Action callback) + public void SubscribeEvent(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull + { + throw new NotImplementedException(); + } + + public void RaiseEvent(object toRaise) { throw new NotImplementedException(); } - public void Unsubscribe(Action callback) + public void RaiseEvent(ref T toRaise) where T : notnull { throw new NotImplementedException(); } - public void Invoke(T signal) + public void RaiseEvent(T toRaise) where T : notnull { throw new NotImplementedException(); } diff --git a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs index 2ce8261..05c35d1 100644 --- a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs +++ b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs @@ -4,12 +4,13 @@ using Hypercube.Shared.Entities.Realisation.Components; using Hypercube.Shared.Entities.Realisation.Events; using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.Runtimes.Event; using Hypercube.Shared.Utilities.Helpers; namespace Hypercube.Shared.Entities.Realisation.Manager; -public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostInject +public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostInject, IEventSubscriber { private static readonly Type BaseComponentType = typeof(IComponent); @@ -20,10 +21,10 @@ public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostI public void PostInject() { - _eventBus.Subscribe(OnInitialized); + _eventBus.SubscribeEvent(this, OnInitialized); } - private void OnInitialized(RuntimeInitializationEvent args) + private void OnInitialized(ref RuntimeInitializationEvent args) { _components = ReflectionHelper.GetAllInstantiableSubclassOf(BaseComponentType); @@ -72,7 +73,7 @@ private object AddComponent(EntityUid entityUid, Type type) var instance = (IComponent)constructor.Invoke(Array.Empty()) ?? throw new NullReferenceException(); components.Add(entityUid, instance); - _eventBus.Invoke(new ComponentAdded(entityUid, instance)); + _eventBus.RaiseEvent(new ComponentAdded(entityUid, instance)); return instance; } diff --git a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs index fce2822..b0d98a7 100644 --- a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs +++ b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs @@ -31,7 +31,7 @@ public EntityUid Create(string name, SceneCoordinates coordinates) var transformComponent = _entitiesComponentManager.AddComponent(newEntity); _entities.Add(newEntity); - _eventBus.Invoke(new EntityAdded(newEntity)); + _eventBus.RaiseEvent(new EntityAdded(newEntity)); return newEntity; } @@ -39,6 +39,6 @@ public EntityUid Create(string name, SceneCoordinates coordinates) public void Delete(EntityUid entityUid) { _entities.Remove(entityUid); - _eventBus.Invoke(new EntityRemoved(entityUid)); + _eventBus.RaiseEvent(new EntityRemoved(entityUid)); } } \ No newline at end of file diff --git a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs index 4d48682..6a287aa 100644 --- a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs +++ b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs @@ -2,13 +2,14 @@ using Hypercube.Shared.Dependency; using Hypercube.Shared.Entities.Realisation.Systems; using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.Runtimes.Event; using Hypercube.Shared.Runtimes.Loop.Event; using Hypercube.Shared.Utilities.Helpers; namespace Hypercube.Shared.Entities.Realisation.Manager; -public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject +public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject, IEventSubscriber { [Dependency] private readonly IEventBus _eventBus = default!; @@ -19,14 +20,14 @@ public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject public void PostInject() { - _eventBus.Subscribe(OnInitialization); - _eventBus.Subscribe(OnStartup); + _eventBus.SubscribeEvent(this, OnInitialization); + _eventBus.SubscribeEvent(this, OnStartup); - _eventBus.Subscribe(OnFrameUpdate); - _eventBus.Subscribe(OnShutdown); + _eventBus.SubscribeEvent(this, OnFrameUpdate); + _eventBus.SubscribeEvent(this, OnShutdown); } - private void OnInitialization(RuntimeInitializationEvent args) + private void OnInitialization(ref RuntimeInitializationEvent args) { // Auto creating all systems var types = GetAllSystemTypes(); @@ -45,7 +46,7 @@ private void OnInitialization(RuntimeInitializationEvent args) _system = system.ToFrozenSet(); } - private void OnStartup(RuntimeStartupEvent args) + private void OnStartup(ref RuntimeStartupEvent args) { foreach (var instance in _system) { @@ -53,7 +54,7 @@ private void OnStartup(RuntimeStartupEvent args) } } - private void OnFrameUpdate(UpdateFrameEvent args) + private void OnFrameUpdate(ref UpdateFrameEvent args) { foreach (var instance in _system) { @@ -61,7 +62,7 @@ private void OnFrameUpdate(UpdateFrameEvent args) } } - private void OnShutdown(RuntimeShutdownEvent args) + private void OnShutdown(ref RuntimeShutdownEvent args) { foreach (var instance in _system) { diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index ffa22c2..87e43e4 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -1,38 +1,128 @@ -namespace Hypercube.Shared.EventBus; +using System.Runtime.CompilerServices; +using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Broadcast; +using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; + +namespace Hypercube.Shared.EventBus; public class EventBus : IEventBus { - private readonly Dictionary>> _callbacks = new(); + private readonly Dictionary _eventData = new(); + private readonly Dictionary> _inverseEventSubscriptions + = new(); + + + public void SubscribeEvent( + IEventSubscriber subscriber, + EventRefHandler refHandler) where T : notnull + { + SubscribeEventCommon(subscriber, ((ref Unit ev) => + { + ref var tev = ref Unsafe.As(ref ev); + refHandler(ref tev); + }), refHandler); + } - public void Subscribe(Action callback) + private void SubscribeEventCommon( + IEventSubscriber subscriber, + RefHandler refHandler, + object equality) where T : notnull { - GetListeners().Add(new WeakReference(callback)); + ArgumentNullException.ThrowIfNull(subscriber); + var evType = typeof(T); + + var subscription = new BroadcastRegistration(refHandler, equality); + + RegisterCommon(evType, out var subs); + if (!subs.BroadcastRegistrations.Contains(subscription)) + subs.BroadcastRegistrations.Add(subscription); + + Dictionary? inverseSubs; + if (!_inverseEventSubscriptions.TryGetValue(subscriber, out inverseSubs)) + { + inverseSubs = new Dictionary(); + _inverseEventSubscriptions[subscriber] = inverseSubs; + } + + if (!inverseSubs.TryAdd(evType, subscription)) + throw new InvalidOperationException(); + } - public void Unsubscribe(Action callback) + private void RegisterCommon(Type evType, out EventData data) { - GetListeners().Remove(new WeakReference(callback)); + if (!_eventData.TryGetValue(evType, out var found)) + { + var list = new List(); + data = new EventData(list); + _eventData[evType] = data; + return; + } + data = found; } - public void Invoke(T signal) + private void UnsubscribeEvent(IEventSubscriber subscriber) { - foreach (var reference in GetListeners()) + ArgumentNullException.ThrowIfNull(subscriber); + var eventType = typeof(T); + + if (_inverseEventSubscriptions.TryGetValue(subscriber, out var inverse) + && inverse.TryGetValue(eventType, out var tuple)) + UnsubscribeEvent(eventType, tuple, subscriber); + } + + private void UnsubscribeEvent(Type evType, BroadcastRegistration tuple, IEventSubscriber subscriber) + { + if (_eventData.TryGetValue(evType, out var subs) && + subs.BroadcastRegistrations.Contains(tuple)) { - if (!reference.TryGetTarget(out var @delegate)) - continue; - - if (@delegate is not Action action) - continue; - - action.Invoke(signal); + subs.BroadcastRegistrations.Remove(tuple); } + + if (_inverseEventSubscriptions.TryGetValue(subscriber, out var inverse) && inverse.ContainsKey(evType)) + inverse.Remove(evType); } - private HashSet> GetListeners() + public void RaiseEvent(object toRaise) { - if (_callbacks.TryGetValue(typeof(T), out var listeners)) - return listeners; + ArgumentNullException.ThrowIfNull(toRaise); - return _callbacks[typeof(T)] = new HashSet>(); + var evType = toRaise.GetType(); + ref var unitRef = ref ExtractUnitRef(ref toRaise, evType); + + ProcessEvent(ref unitRef, evType); + } + public void RaiseEvent(ref T toRaise) where T : notnull + { + ProcessEvent(ref Unsafe.As(ref toRaise), typeof(T)); + } + + public void RaiseEvent(T toRaise) where T : notnull + { + ProcessEvent(ref Unsafe.As(ref toRaise), typeof(T)); + } + + private void ProcessEvent(ref Unit unitRef, Type evType) + { + if (!_eventData!.TryGetValue(evType, out var data)) + return; + + ProcessEventCore(ref unitRef, data); + } + + private void ProcessEventCore(ref Unit unitRef, EventData data) + { + foreach (var handler in data.BroadcastRegistrations) + { + handler.Handler(ref unitRef); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ref Unit ExtractUnitRef(ref object obj, Type objType) + { + return ref objType.IsValueType + ? ref Unsafe.As(ref obj).Value + : ref Unsafe.As(ref obj); } } \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs b/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs new file mode 100644 index 0000000..0a27f59 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs @@ -0,0 +1,28 @@ +namespace Hypercube.Shared.EventBus.Events.Broadcast; + +public class BroadcastRegistration : IEquatable +{ + public RefHandler Handler { get; } + public object Equality { get; } + + public BroadcastRegistration(RefHandler refHandler, object equalityObj) + { + Handler = refHandler; + Equality = equalityObj; + } + + public bool Equals(BroadcastRegistration? other) + { + return other is not null && Equals(other.Equality, Equality); + } + + public override bool Equals(object? obj) + { + return obj is not null && obj is BroadcastRegistration registration && Equals(registration.Equality, Equality); + } + + public override int GetHashCode() + { + return Equality.GetHashCode(); + } +} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/EventData.cs b/Hypercube.Shared/EventBus/Events/EventData.cs new file mode 100644 index 0000000..aba4166 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/EventData.cs @@ -0,0 +1,13 @@ +using Hypercube.Shared.EventBus.Events.Broadcast; + +namespace Hypercube.Shared.EventBus.Events; + +public class EventData +{ + public List BroadcastRegistrations; + + public EventData(List broadcastRegistrations) + { + BroadcastRegistrations = broadcastRegistrations; + } +} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/EventHandler.cs b/Hypercube.Shared/EventBus/Events/EventHandler.cs new file mode 100644 index 0000000..0689142 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/EventHandler.cs @@ -0,0 +1,4 @@ +namespace Hypercube.Shared.EventBus.Events; + +public delegate void RefHandler(ref Unit ev); +public delegate void EventRefHandler(ref T ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/IEventArgs.cs b/Hypercube.Shared/EventBus/Events/IEventArgs.cs new file mode 100644 index 0000000..543384a --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/IEventArgs.cs @@ -0,0 +1,15 @@ +using System.Runtime.InteropServices; + +namespace Hypercube.Shared.EventBus.Events; + +public abstract class EventArgs +{ +} + +public abstract class CancellableEventArgs : EventArgs +{ + public bool Cancelled { get; private set; } + + public void Cancel() => Cancelled = true; + public void UnCancel() => Cancelled = false; +} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs b/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs new file mode 100644 index 0000000..40dd68e --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs @@ -0,0 +1,5 @@ +namespace Hypercube.Shared.EventBus.Events; + +public interface IEventSubscriber +{ +} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Unit.cs b/Hypercube.Shared/EventBus/Events/Unit.cs new file mode 100644 index 0000000..24fa990 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Unit.cs @@ -0,0 +1,12 @@ +using System.Runtime.InteropServices; + +namespace Hypercube.Shared.EventBus.Events; + +public readonly struct Unit +{ +} +[StructLayout(LayoutKind.Sequential)] +public sealed class UnitBox +{ + public Unit Value; +} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/IEventBus.cs b/Hypercube.Shared/EventBus/IEventBus.cs index c2e0b95..bdcd5d2 100644 --- a/Hypercube.Shared/EventBus/IEventBus.cs +++ b/Hypercube.Shared/EventBus/IEventBus.cs @@ -1,8 +1,12 @@ -namespace Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; +using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; + +namespace Hypercube.Shared.EventBus; public interface IEventBus { - void Subscribe(Action callback); - void Unsubscribe(Action callback); - void Invoke(T signal); + public void SubscribeEvent(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull; + void RaiseEvent(object toRaise); + void RaiseEvent(ref T toRaise) where T : notnull; + void RaiseEvent(T toRaise) where T : notnull; } \ No newline at end of file diff --git a/Hypercube.Shared/Scenes/Manager/SceneManager.cs b/Hypercube.Shared/Scenes/Manager/SceneManager.cs index 82fabcd..593af34 100644 --- a/Hypercube.Shared/Scenes/Manager/SceneManager.cs +++ b/Hypercube.Shared/Scenes/Manager/SceneManager.cs @@ -21,7 +21,7 @@ public SceneId Create() var scene = new Scene(id); _scenes.Add(id, scene); - _eventBus.Invoke(new SceneAdded(scene)); + _eventBus.RaiseEvent(new SceneAdded(scene)); return id; } @@ -30,7 +30,7 @@ public void Delete(SceneId sceneId) { var scene = _scenes[sceneId]; _scenes.Remove(sceneId); - _eventBus.Invoke(new SceneDeleted(scene)); + _eventBus.RaiseEvent(new SceneDeleted(scene)); } public bool HasScene(SceneId sceneId) diff --git a/Hypercube.UnitTests/EventBus/EventBusRefTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefTests.cs new file mode 100644 index 0000000..02d5652 --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusRefTests.cs @@ -0,0 +1,97 @@ +using Hypercube.Shared.EventBus.Events; +using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; + +namespace Hypercube.UnitTests.EventBus; + +public class EventBusRefTests : IEventSubscriber +{ + /* + * Class Ref Tests + */ + + [Test] + public void TestRef() + { + var evBus = new Shared.EventBus.EventBus(); + var s1 = new TestRefClassSubscriber1(evBus); + var s2 = new TestRefClassSubscriber2(evBus); + s1.Subscribe(); + s2.Subscribe(); + evBus.RaiseEvent(new TestEventClass()); + } + + private sealed class TestEventClass() : EventArgs + { + public int Counter { get; set; } = 0; + } + + private class TestRefClassSubscriber1(Shared.EventBus.EventBus bus) : IEventSubscriber + { + public void Subscribe() + { + bus.SubscribeEvent(this, RefFunc2); + } + + private void RefFunc2(ref TestEventClass args) + { + args.Counter++; + } + } + private class TestRefClassSubscriber2(Shared.EventBus.EventBus bus) : IEventSubscriber + { + public void Subscribe() + { + bus.SubscribeEvent(this, RefFunc1); + } + private void RefFunc1(ref TestEventClass args) + { + Assert.That(args.Counter == 1); + + Assert.Pass("Counter increased correctly"); + } + } + + /* + * Structs Ref Tests + */ + + [Test] + public void TestRefStruct() + { + var evBus = new Shared.EventBus.EventBus(); + var s1 = new TestRefStructSubscriber1(evBus); + var s2 = new TestRefStructSubscriber2(evBus); + s1.Subscribe(); + s2.Subscribe(); + evBus.RaiseEvent(new TestEventStruct()); + } + + + private class TestRefStructSubscriber1(Shared.EventBus.EventBus bus) : IEventSubscriber + { + public void Subscribe() + { + bus.SubscribeEvent(this, RefFunc2); + } + + private void RefFunc2(ref TestEventStruct args) + { + args.Counter++; + } + } + private class TestRefStructSubscriber2(Shared.EventBus.EventBus bus) : IEventSubscriber + { + public void Subscribe() + { + bus.SubscribeEvent(this, RefFunc1); + } + private void RefFunc1(ref TestEventStruct args) + { + Assert.That(args.Counter == 1); + + Assert.Pass("Counter increased correctly"); + } + } + + private record struct TestEventStruct(int Counter = 0); +} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs new file mode 100644 index 0000000..f1d3774 --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs @@ -0,0 +1,63 @@ +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.UnitTests.EventBus; + +public class EventBusSubRaiseTests +{ + [Test] + public void SubNRaiseEventTest() + { + var evBus = new Shared.EventBus.EventBus(); + var s1 = new TestEventSubscriber(evBus); + s1.Subscribe(); + evBus.RaiseEvent(new TestSubEventClass()); + evBus.RaiseEvent(new TestSubEventStruct()); + s1.AssertPassed(); + } + + private class TestEventSubscriber(Shared.EventBus.EventBus eventBus) : IEventSubscriber + { + private bool _classPassed; + private bool _structPassed; + + public void Subscribe() + { + eventBus.SubscribeEvent(this,OnClassSub); + eventBus.SubscribeEvent(this,OnStructSub); + } + + private void OnClassSub(ref TestSubEventClass args) + { + _classPassed = true; + } + + private void OnStructSub(ref TestSubEventStruct args) + { + _structPassed = true; + } + + public void AssertPassed() + { + if (_classPassed && _structPassed) + { + Assert.Pass("Both passed"); + return; + } + + if (_classPassed) + { + Assert.Fail("Struct failed"); + return; + } + + if (_structPassed) + Assert.Fail("Class failed"); + } + } + + private record struct TestSubEventStruct; + + private sealed class TestSubEventClass + { + } +} \ No newline at end of file From 9522ef1fbdf1d6fd681b3e9e073ce85737196caf Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Fri, 12 Jul 2024 08:09:56 +0500 Subject: [PATCH 02/11] remvoe todo --- Hypercube.Shared/EventBus/EventBus.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index f521889..fb5e84c 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -5,7 +5,6 @@ namespace Hypercube.Shared.EventBus; -// TODO: BURNING IN HELL SHIT!!!! Rewrite it public class EventBus : IEventBus { private readonly Dictionary _eventData = new(); From 86571825839ae771e99e054fec4e9749b47e7d87 Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Fri, 12 Jul 2024 08:16:42 +0500 Subject: [PATCH 03/11] add unsub tests, fix unsub method --- .../Realisation/EventBus/EntitiesEventBus.cs | 5 ++ Hypercube.Shared/EventBus/EventBus.cs | 2 +- Hypercube.Shared/EventBus/IEventBus.cs | 1 + .../EventBus/EventBusUnSubTests.cs | 72 +++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs diff --git a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs index 1667fa7..2e733d0 100644 --- a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs +++ b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs @@ -4,6 +4,11 @@ namespace Hypercube.Shared.Entities.Realisation.EventBus; public sealed class EntitiesEventBus : IEntitiesEventBus { + public void UnsubscribeEvent(IEventSubscriber subscriber) + { + throw new NotImplementedException(); + } + public void SubscribeEvent(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull { throw new NotImplementedException(); diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index fb5e84c..cf139b2 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -60,7 +60,7 @@ private void RegisterCommon(Type evType, out EventData data) data = found; } - private void UnsubscribeEvent(IEventSubscriber subscriber) + public void UnsubscribeEvent(IEventSubscriber subscriber) { ArgumentNullException.ThrowIfNull(subscriber); var eventType = typeof(T); diff --git a/Hypercube.Shared/EventBus/IEventBus.cs b/Hypercube.Shared/EventBus/IEventBus.cs index bdcd5d2..de2c30f 100644 --- a/Hypercube.Shared/EventBus/IEventBus.cs +++ b/Hypercube.Shared/EventBus/IEventBus.cs @@ -5,6 +5,7 @@ namespace Hypercube.Shared.EventBus; public interface IEventBus { + void UnsubscribeEvent(IEventSubscriber subscriber); public void SubscribeEvent(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull; void RaiseEvent(object toRaise); void RaiseEvent(ref T toRaise) where T : notnull; diff --git a/Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs new file mode 100644 index 0000000..7d5e179 --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs @@ -0,0 +1,72 @@ +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.UnitTests.EventBus; + +public class EventBusUnSubTests +{ + [Test] + public void TestUnsubscribing() + { + var evBus = new Shared.EventBus.EventBus(); + var s1 = new TestEventSubscriber(evBus); + s1.Subscribe(); + evBus.RaiseEvent(new TestUnsubEventClass()); + evBus.RaiseEvent(new TestUnsubEventStruct()); + s1.Unsubscribe(); + evBus.RaiseEvent(new TestUnsubEventClass()); + evBus.RaiseEvent(new TestUnsubEventStruct()); + s1.AssertPassed(); + } + + private class TestEventSubscriber(Shared.EventBus.EventBus eventBus) : IEventSubscriber + { + private bool _classPassed; + private bool _structPassed; + + public void Subscribe() + { + eventBus.SubscribeEvent(this,OnClassSub); + eventBus.SubscribeEvent(this,OnStructSub); + } + + public void Unsubscribe() + { + eventBus.UnsubscribeEvent(this); + eventBus.UnsubscribeEvent(this); + } + + private void OnClassSub(ref TestUnsubEventClass args) + { + _classPassed = !_classPassed; + } + + private void OnStructSub(ref TestUnsubEventStruct args) + { + _structPassed = !_structPassed; + } + + public void AssertPassed() + { + if (_classPassed && _structPassed) + { + Assert.Pass("Both passed"); + return; + } + + if (_classPassed) + { + Assert.Fail("Struct failed"); + return; + } + + if (_structPassed) + Assert.Fail("Class failed"); + } + } + + private record struct TestUnsubEventStruct; + + private sealed class TestUnsubEventClass + { + } +} \ No newline at end of file From 8d9b34a3b06825f835e86f37f1a56b301d7af3a8 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Fri, 12 Jul 2024 18:53:58 +1000 Subject: [PATCH 04/11] Clean up event bus tests --- .../EventBus/Events/IEventSubscriber.cs | 4 +- .../EventBus/EventBusRaiseTests.cs | 56 +++++++++++ .../EventBus/EventBusRefClassTests.cs | 58 +++++++++++ .../EventBus/EventBusRefStructTests.cs | 54 +++++++++++ .../EventBus/EventBusRefTests.cs | 97 ------------------- .../EventBus/EventBusSubRaiseTests.cs | 63 ------------ .../EventBus/EventBusUnSubTests.cs | 72 -------------- .../EventBus/EventBusUnsubscribeTests.cs | 67 +++++++++++++ 8 files changed, 236 insertions(+), 235 deletions(-) create mode 100644 Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs create mode 100644 Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs create mode 100644 Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs delete mode 100644 Hypercube.UnitTests/EventBus/EventBusRefTests.cs delete mode 100644 Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs delete mode 100644 Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs create mode 100644 Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs diff --git a/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs b/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs index 40dd68e..a3df382 100644 --- a/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs +++ b/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs @@ -1,5 +1,3 @@ namespace Hypercube.Shared.EventBus.Events; -public interface IEventSubscriber -{ -} \ No newline at end of file +public interface IEventSubscriber; \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs new file mode 100644 index 0000000..e452329 --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs @@ -0,0 +1,56 @@ +using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.UnitTests.EventBus; + +public static class EventBusRaiseTests +{ + [Test] + public static void Raising() + { + var eventBus = new Shared.EventBus.EventBus(); + + var subscriber = new TestEventSubscriber(eventBus); + subscriber.Subscribe(); + + eventBus.RaiseEvent(new TestSubEventClass()); + eventBus.RaiseEvent(new TestSubEventStruct()); + + subscriber.AssertPassed(); + } + + private sealed class TestEventSubscriber(IEventBus eventBus) : IEventSubscriber + { + private bool _classPassed; + private bool _structPassed; + + public void Subscribe() + { + eventBus.SubscribeEvent(this, OnClass); + eventBus.SubscribeEvent(this, OnStruct); + } + + private void OnClass(ref TestSubEventClass args) + { + _classPassed = true; + } + + private void OnStruct(ref TestSubEventStruct args) + { + _structPassed = true; + } + + public void AssertPassed() + { + Assert.Multiple(() => + { + Assert.That(_classPassed, Is.True); + Assert.That(_structPassed, Is.True); + }); + } + } + + private readonly record struct TestSubEventStruct; + + private sealed class TestSubEventClass; +} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs new file mode 100644 index 0000000..a9bfdf2 --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs @@ -0,0 +1,58 @@ +using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; +using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; + +namespace Hypercube.UnitTests.EventBus; + +public sealed class EventBusRefClassTests +{ + [Test] + public static void RefClass() + { + var eventBus = new Shared.EventBus.EventBus(); + + var subscriber1 = new TestRefClassSubscriber1(eventBus); + var subscriber2 = new TestRefClassSubscriber2(eventBus); + + subscriber1.Subscribe(); + subscriber2.Subscribe(); + + var args = new TestEventClass(); + eventBus.RaiseEvent(args); + + Assert.That(args.Counter, Is.EqualTo(2)); + Assert.Pass("All subscribers handled correctly"); + } + + private sealed class TestEventClass : EventArgs + { + public int Counter { get; set; } + } + + private sealed class TestRefClassSubscriber1(IEventBus bus) : IEventSubscriber + { + public void Subscribe() + { + bus.SubscribeEvent(this, RefMethod2); + } + + private void RefMethod2(ref TestEventClass args) + { + args.Counter++; + } + } + + private sealed class TestRefClassSubscriber2(IEventBus bus) : IEventSubscriber + { + public void Subscribe() + { + bus.SubscribeEvent(this, RefMethod1); + } + + private void RefMethod1(ref TestEventClass args) + { + Assert.That(args.Counter, Is.EqualTo(1)); + args.Counter++; + } + } +} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs new file mode 100644 index 0000000..c90688e --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs @@ -0,0 +1,54 @@ +using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.UnitTests.EventBus; + +public static class EventBusRefStructTests +{ + [Test] + public static void RefStruct() + { + var eventBus = new Shared.EventBus.EventBus(); + + var subscriber1 = new TestRefStructSubscriber1(eventBus); + var subscriber2 = new TestRefStructSubscriber2(eventBus); + + subscriber1.Subscribe(); + subscriber2.Subscribe(); + + var args = new TestEventStruct(); + eventBus.RaiseEvent(ref args); + + Assert.That(args.Counter, Is.EqualTo(2)); + Assert.Pass("All subscribers handled correctly"); + } + + private sealed class TestRefStructSubscriber1(IEventBus eventBus) : IEventSubscriber + { + public void Subscribe() + { + eventBus.SubscribeEvent(this, RefMethod); + } + + private void RefMethod(ref TestEventStruct args) + { + args.Counter++; + } + } + + private sealed class TestRefStructSubscriber2(IEventBus eventBus) : IEventSubscriber + { + public void Subscribe() + { + eventBus.SubscribeEvent(this, RefMethod); + } + + private void RefMethod(ref TestEventStruct args) + { + Assert.That(args.Counter, Is.EqualTo(1)); + args.Counter++; + } + } + + private record struct TestEventStruct(int Counter = 0); +} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusRefTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefTests.cs deleted file mode 100644 index 02d5652..0000000 --- a/Hypercube.UnitTests/EventBus/EventBusRefTests.cs +++ /dev/null @@ -1,97 +0,0 @@ -using Hypercube.Shared.EventBus.Events; -using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; - -namespace Hypercube.UnitTests.EventBus; - -public class EventBusRefTests : IEventSubscriber -{ - /* - * Class Ref Tests - */ - - [Test] - public void TestRef() - { - var evBus = new Shared.EventBus.EventBus(); - var s1 = new TestRefClassSubscriber1(evBus); - var s2 = new TestRefClassSubscriber2(evBus); - s1.Subscribe(); - s2.Subscribe(); - evBus.RaiseEvent(new TestEventClass()); - } - - private sealed class TestEventClass() : EventArgs - { - public int Counter { get; set; } = 0; - } - - private class TestRefClassSubscriber1(Shared.EventBus.EventBus bus) : IEventSubscriber - { - public void Subscribe() - { - bus.SubscribeEvent(this, RefFunc2); - } - - private void RefFunc2(ref TestEventClass args) - { - args.Counter++; - } - } - private class TestRefClassSubscriber2(Shared.EventBus.EventBus bus) : IEventSubscriber - { - public void Subscribe() - { - bus.SubscribeEvent(this, RefFunc1); - } - private void RefFunc1(ref TestEventClass args) - { - Assert.That(args.Counter == 1); - - Assert.Pass("Counter increased correctly"); - } - } - - /* - * Structs Ref Tests - */ - - [Test] - public void TestRefStruct() - { - var evBus = new Shared.EventBus.EventBus(); - var s1 = new TestRefStructSubscriber1(evBus); - var s2 = new TestRefStructSubscriber2(evBus); - s1.Subscribe(); - s2.Subscribe(); - evBus.RaiseEvent(new TestEventStruct()); - } - - - private class TestRefStructSubscriber1(Shared.EventBus.EventBus bus) : IEventSubscriber - { - public void Subscribe() - { - bus.SubscribeEvent(this, RefFunc2); - } - - private void RefFunc2(ref TestEventStruct args) - { - args.Counter++; - } - } - private class TestRefStructSubscriber2(Shared.EventBus.EventBus bus) : IEventSubscriber - { - public void Subscribe() - { - bus.SubscribeEvent(this, RefFunc1); - } - private void RefFunc1(ref TestEventStruct args) - { - Assert.That(args.Counter == 1); - - Assert.Pass("Counter increased correctly"); - } - } - - private record struct TestEventStruct(int Counter = 0); -} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs deleted file mode 100644 index f1d3774..0000000 --- a/Hypercube.UnitTests/EventBus/EventBusSubRaiseTests.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Hypercube.Shared.EventBus.Events; - -namespace Hypercube.UnitTests.EventBus; - -public class EventBusSubRaiseTests -{ - [Test] - public void SubNRaiseEventTest() - { - var evBus = new Shared.EventBus.EventBus(); - var s1 = new TestEventSubscriber(evBus); - s1.Subscribe(); - evBus.RaiseEvent(new TestSubEventClass()); - evBus.RaiseEvent(new TestSubEventStruct()); - s1.AssertPassed(); - } - - private class TestEventSubscriber(Shared.EventBus.EventBus eventBus) : IEventSubscriber - { - private bool _classPassed; - private bool _structPassed; - - public void Subscribe() - { - eventBus.SubscribeEvent(this,OnClassSub); - eventBus.SubscribeEvent(this,OnStructSub); - } - - private void OnClassSub(ref TestSubEventClass args) - { - _classPassed = true; - } - - private void OnStructSub(ref TestSubEventStruct args) - { - _structPassed = true; - } - - public void AssertPassed() - { - if (_classPassed && _structPassed) - { - Assert.Pass("Both passed"); - return; - } - - if (_classPassed) - { - Assert.Fail("Struct failed"); - return; - } - - if (_structPassed) - Assert.Fail("Class failed"); - } - } - - private record struct TestSubEventStruct; - - private sealed class TestSubEventClass - { - } -} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs deleted file mode 100644 index 7d5e179..0000000 --- a/Hypercube.UnitTests/EventBus/EventBusUnSubTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -using Hypercube.Shared.EventBus.Events; - -namespace Hypercube.UnitTests.EventBus; - -public class EventBusUnSubTests -{ - [Test] - public void TestUnsubscribing() - { - var evBus = new Shared.EventBus.EventBus(); - var s1 = new TestEventSubscriber(evBus); - s1.Subscribe(); - evBus.RaiseEvent(new TestUnsubEventClass()); - evBus.RaiseEvent(new TestUnsubEventStruct()); - s1.Unsubscribe(); - evBus.RaiseEvent(new TestUnsubEventClass()); - evBus.RaiseEvent(new TestUnsubEventStruct()); - s1.AssertPassed(); - } - - private class TestEventSubscriber(Shared.EventBus.EventBus eventBus) : IEventSubscriber - { - private bool _classPassed; - private bool _structPassed; - - public void Subscribe() - { - eventBus.SubscribeEvent(this,OnClassSub); - eventBus.SubscribeEvent(this,OnStructSub); - } - - public void Unsubscribe() - { - eventBus.UnsubscribeEvent(this); - eventBus.UnsubscribeEvent(this); - } - - private void OnClassSub(ref TestUnsubEventClass args) - { - _classPassed = !_classPassed; - } - - private void OnStructSub(ref TestUnsubEventStruct args) - { - _structPassed = !_structPassed; - } - - public void AssertPassed() - { - if (_classPassed && _structPassed) - { - Assert.Pass("Both passed"); - return; - } - - if (_classPassed) - { - Assert.Fail("Struct failed"); - return; - } - - if (_structPassed) - Assert.Fail("Class failed"); - } - } - - private record struct TestUnsubEventStruct; - - private sealed class TestUnsubEventClass - { - } -} \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs new file mode 100644 index 0000000..7c2e7b4 --- /dev/null +++ b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs @@ -0,0 +1,67 @@ +using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.UnitTests.EventBus; + +public class EventBusUnsubscribeTests +{ + [Test] + public void Unsubscribing() + { + var eventBus = new Shared.EventBus.EventBus(); + var subscriber = new TestEventSubscriber(eventBus); + + subscriber.Subscribe(); + + eventBus.RaiseEvent(new TestUnsubEventClass()); + eventBus.RaiseEvent(new TestUnsubEventStruct()); + + subscriber.Unsubscribe(); + + eventBus.RaiseEvent(new TestUnsubEventClass()); + eventBus.RaiseEvent(new TestUnsubEventStruct()); + + subscriber.AssertPassed(); + } + + private class TestEventSubscriber(IEventBus eventBus) : IEventSubscriber + { + private bool _classPassed; + private bool _structPassed; + + public void Subscribe() + { + eventBus.SubscribeEvent(this, OnClass); + eventBus.SubscribeEvent(this, OnStruct); + } + + public void Unsubscribe() + { + eventBus.UnsubscribeEvent(this); + eventBus.UnsubscribeEvent(this); + } + + private void OnClass(ref TestUnsubEventClass args) + { + _classPassed = !_classPassed; + } + + private void OnStruct(ref TestUnsubEventStruct args) + { + _structPassed = !_structPassed; + } + + public void AssertPassed() + { + Assert.Multiple(() => + { + Assert.That(_classPassed, Is.True); + Assert.That(_structPassed, Is.True); + }); + } + } + + private record struct TestUnsubEventStruct; + + private sealed class TestUnsubEventClass; +} \ No newline at end of file From 294d38dcac3e69b523cc54299f5f76f49ad741b8 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:56:54 +1000 Subject: [PATCH 05/11] Clean up event bus --- .../Graphics/Rendering/Renderer.Window.cs | 6 +- .../Graphics/Rendering/Renderer.cs | 8 +-- Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs | 8 +-- Hypercube.Client/Runtimes/Runtime.cs | 8 +-- .../Realisation/EventBus/EntitiesEventBus.cs | 10 +-- .../Manager/EntitiesComponentManager.cs | 4 +- .../Realisation/Manager/EntitiesManager.cs | 4 +- .../Manager/EntitiesSystemManager.cs | 8 +-- Hypercube.Shared/EventBus/EventBus.cs | 70 +++++++++---------- .../Events/Broadcast/BroadcastRegistration.cs | 4 +- .../EventBus/Events/IEventArgs.cs | 19 +++-- Hypercube.Shared/EventBus/Events/Unit.cs | 5 +- Hypercube.Shared/EventBus/IEventBus.cs | 15 ++-- .../Scenes/Manager/SceneManager.cs | 4 +- .../EventBus/EventBusRaiseTests.cs | 8 +-- .../EventBus/EventBusRefClassTests.cs | 6 +- .../EventBus/EventBusRefStructTests.cs | 6 +- .../EventBus/EventBusUnsubscribeTests.cs | 16 ++--- 18 files changed, 105 insertions(+), 104 deletions(-) diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs b/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs index 7f5b065..2f15b1d 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.Window.cs @@ -39,11 +39,11 @@ public void DestroyWindow(WindowRegistration registration) public void CloseWindow(WindowRegistration registration) { - _eventBus.RaiseEvent(new WindowClosedEvent(registration)); + _eventBus.Raise(new WindowClosedEvent(registration)); if (registration.Id == _mainWindowId) { - _eventBus.RaiseEvent(new MainWindowClosedEvent(registration)); + _eventBus.Raise(new MainWindowClosedEvent(registration)); return; } @@ -84,6 +84,6 @@ private bool InitMainWindow(ContextInfo? context, WindowCreateSettings settings) public void OnFocusChanged(WindowRegistration window, bool focused) { - _eventBus.RaiseEvent(new WindowFocusChangedEvent(window, focused)); + _eventBus.Raise(new WindowFocusChangedEvent(window, focused)); } } \ No newline at end of file diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.cs b/Hypercube.Client/Graphics/Rendering/Renderer.cs index c5cc42b..bf775e4 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.cs @@ -69,10 +69,10 @@ public sealed partial class Renderer : IRenderer, IPostInject, IEventSubscriber public void PostInject() { - _eventBus.SubscribeEvent(this, OnInitialization); - _eventBus.SubscribeEvent(this, OnStartup); - _eventBus.SubscribeEvent(this, OnFrameUpdate); - _eventBus.SubscribeEvent(this, OnFrameRender); + _eventBus.Subscribe(this, OnInitialization); + _eventBus.Subscribe(this, OnStartup); + _eventBus.Subscribe(this, OnFrameUpdate); + _eventBus.Subscribe(this, OnFrameRender); } private void OnInitialization(ref RuntimeInitializationEvent args) diff --git a/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs b/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs index 074e716..1caf37d 100644 --- a/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs +++ b/Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs @@ -20,10 +20,10 @@ public void Run() _timing.StartFrame(); var deltaTime = (float)_timing.RealFrameTime.TotalSeconds; - _eventBus.RaiseEvent(new InputFrameEvent(deltaTime)); - _eventBus.RaiseEvent(new TickFrameEvent(deltaTime)); - _eventBus.RaiseEvent(new UpdateFrameEvent(deltaTime)); - _eventBus.RaiseEvent(new RenderFrameEvent(deltaTime)); + _eventBus.Raise(new InputFrameEvent(deltaTime)); + _eventBus.Raise(new TickFrameEvent(deltaTime)); + _eventBus.Raise(new UpdateFrameEvent(deltaTime)); + _eventBus.Raise(new RenderFrameEvent(deltaTime)); } } diff --git a/Hypercube.Client/Runtimes/Runtime.cs b/Hypercube.Client/Runtimes/Runtime.cs index f221cb0..aa5e832 100644 --- a/Hypercube.Client/Runtimes/Runtime.cs +++ b/Hypercube.Client/Runtimes/Runtime.cs @@ -19,7 +19,7 @@ public sealed partial class Runtime(DependenciesContainer dependenciesContainer) public void PostInject() { - _eventBus.SubscribeEvent(this, OnMainWindowClosed); + _eventBus.Subscribe(this, OnMainWindowClosed); } /// @@ -44,21 +44,21 @@ private void Shutdown(string? reason = null) reason = reason is null ? "Shutting down" : $"Shutting down, reason: {reason}"; _logger.EngineInfo(reason); - _eventBus.RaiseEvent(new RuntimeShutdownEvent(reason)); + _eventBus.Raise(new RuntimeShutdownEvent(reason)); _loop.Shutdown(); } private void RunLoop() { _logger.EngineInfo("Startup"); - _eventBus.RaiseEvent(new RuntimeStartupEvent()); + _eventBus.Raise(new RuntimeStartupEvent()); _loop.Run(); } private void Initialize() { _logger.EngineInfo("Initialize"); - _eventBus.RaiseEvent(new RuntimeInitializationEvent()); + _eventBus.Raise(new RuntimeInitializationEvent()); } private void OnMainWindowClosed(ref MainWindowClosedEvent obj) diff --git a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs index 2e733d0..c549397 100644 --- a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs +++ b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs @@ -4,27 +4,27 @@ namespace Hypercube.Shared.Entities.Realisation.EventBus; public sealed class EntitiesEventBus : IEntitiesEventBus { - public void UnsubscribeEvent(IEventSubscriber subscriber) + public void Unsubscribe(IEventSubscriber subscriber) { throw new NotImplementedException(); } - public void SubscribeEvent(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull + public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull { throw new NotImplementedException(); } - public void RaiseEvent(object toRaise) + public void Raise(object receiver) { throw new NotImplementedException(); } - public void RaiseEvent(ref T toRaise) where T : notnull + public void Raise(ref T receiver) where T : notnull { throw new NotImplementedException(); } - public void RaiseEvent(T toRaise) where T : notnull + public void Raise(T receiver) where T : notnull { throw new NotImplementedException(); } diff --git a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs index 05c35d1..9a0f049 100644 --- a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs +++ b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesComponentManager.cs @@ -21,7 +21,7 @@ public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostI public void PostInject() { - _eventBus.SubscribeEvent(this, OnInitialized); + _eventBus.Subscribe(this, OnInitialized); } private void OnInitialized(ref RuntimeInitializationEvent args) @@ -73,7 +73,7 @@ private object AddComponent(EntityUid entityUid, Type type) var instance = (IComponent)constructor.Invoke(Array.Empty()) ?? throw new NullReferenceException(); components.Add(entityUid, instance); - _eventBus.RaiseEvent(new ComponentAdded(entityUid, instance)); + _eventBus.Raise(new ComponentAdded(entityUid, instance)); return instance; } diff --git a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs index b0d98a7..4350701 100644 --- a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs +++ b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesManager.cs @@ -31,7 +31,7 @@ public EntityUid Create(string name, SceneCoordinates coordinates) var transformComponent = _entitiesComponentManager.AddComponent(newEntity); _entities.Add(newEntity); - _eventBus.RaiseEvent(new EntityAdded(newEntity)); + _eventBus.Raise(new EntityAdded(newEntity)); return newEntity; } @@ -39,6 +39,6 @@ public EntityUid Create(string name, SceneCoordinates coordinates) public void Delete(EntityUid entityUid) { _entities.Remove(entityUid); - _eventBus.RaiseEvent(new EntityRemoved(entityUid)); + _eventBus.Raise(new EntityRemoved(entityUid)); } } \ No newline at end of file diff --git a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs index 6a287aa..2ff5357 100644 --- a/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs +++ b/Hypercube.Shared/Entities/Realisation/Manager/EntitiesSystemManager.cs @@ -20,11 +20,11 @@ public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject, IEvent public void PostInject() { - _eventBus.SubscribeEvent(this, OnInitialization); - _eventBus.SubscribeEvent(this, OnStartup); + _eventBus.Subscribe(this, OnInitialization); + _eventBus.Subscribe(this, OnStartup); - _eventBus.SubscribeEvent(this, OnFrameUpdate); - _eventBus.SubscribeEvent(this, OnShutdown); + _eventBus.Subscribe(this, OnFrameUpdate); + _eventBus.Subscribe(this, OnShutdown); } private void OnInitialization(ref RuntimeInitializationEvent args) diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index cf139b2..1e0d10e 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -1,41 +1,36 @@ using System.Runtime.CompilerServices; using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.EventBus.Events.Broadcast; -using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; namespace Hypercube.Shared.EventBus; -public class EventBus : IEventBus +public sealed class EventBus : IEventBus { private readonly Dictionary _eventData = new(); - private readonly Dictionary> _inverseEventSubscriptions - = new(); - - - public void SubscribeEvent( - IEventSubscriber subscriber, - EventRefHandler refHandler) where T : notnull + private readonly Dictionary> _inverseEventSubscriptions = new(); + + public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : IEventArgs { - SubscribeEventCommon(subscriber, ((ref Unit ev) => + SubscribeEventCommon(subscriber, (ref Unit ev) => { ref var tev = ref Unsafe.As(ref ev); refHandler(ref tev); - }), refHandler); + }, refHandler); } - private void SubscribeEventCommon( - IEventSubscriber subscriber, - RefHandler refHandler, - object equality) where T : notnull + private void SubscribeEventCommon(IEventSubscriber subscriber, RefHandler refHandler, object equality) + where T : IEventArgs { ArgumentNullException.ThrowIfNull(subscriber); - var evType = typeof(T); + var eventType = typeof(T); var subscription = new BroadcastRegistration(refHandler, equality); - RegisterCommon(evType, out var subs); - if (!subs.BroadcastRegistrations.Contains(subscription)) - subs.BroadcastRegistrations.Add(subscription); + var subscriptions = GetEventData(eventType); + if (subscriptions.BroadcastRegistrations.Contains(subscription)) + throw new InvalidOperationException(); + + subscriptions.BroadcastRegistrations.Add(subscription); Dictionary? inverseSubs; if (!_inverseEventSubscriptions.TryGetValue(subscriber, out inverseSubs)) @@ -44,23 +39,22 @@ private void SubscribeEventCommon( _inverseEventSubscriptions[subscriber] = inverseSubs; } - if (!inverseSubs.TryAdd(evType, subscription)) + if (!inverseSubs.TryAdd(eventType, subscription)) throw new InvalidOperationException(); } - private void RegisterCommon(Type evType, out EventData data) + private EventData GetEventData(Type eventType) { - if (!_eventData.TryGetValue(evType, out var found)) - { - var list = new List(); - data = new EventData(list); - _eventData[evType] = data; - return; - } - data = found; + if (_eventData.TryGetValue(eventType, out var found)) + return found; + + var list = new List(); + var data = new EventData(list); + + return _eventData[eventType] = data; } - public void UnsubscribeEvent(IEventSubscriber subscriber) + public void Unsubscribe(IEventSubscriber subscriber) where T : IEventArgs { ArgumentNullException.ThrowIfNull(subscriber); var eventType = typeof(T); @@ -82,23 +76,23 @@ private void UnsubscribeEvent(Type evType, BroadcastRegistration tuple, IEventSu inverse.Remove(evType); } - public void RaiseEvent(object toRaise) + public void Raise(object receiver) { - ArgumentNullException.ThrowIfNull(toRaise); + ArgumentNullException.ThrowIfNull(receiver); - var evType = toRaise.GetType(); - ref var unitRef = ref ExtractUnitRef(ref toRaise, evType); + var evType = receiver.GetType(); + ref var unitRef = ref ExtractUnitRef(ref receiver, evType); ProcessEvent(ref unitRef, evType); } - public void RaiseEvent(ref T toRaise) where T : notnull + public void Raise(ref T receiver) where T : IEventArgs { - ProcessEvent(ref Unsafe.As(ref toRaise), typeof(T)); + ProcessEvent(ref Unsafe.As(ref receiver), typeof(T)); } - public void RaiseEvent(T toRaise) where T : notnull + public void Raise(T receiver) where T : IEventArgs { - ProcessEvent(ref Unsafe.As(ref toRaise), typeof(T)); + ProcessEvent(ref Unsafe.As(ref receiver), typeof(T)); } private void ProcessEvent(ref Unit unitRef, Type evType) diff --git a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs b/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs index 0a27f59..c3b9979 100644 --- a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs +++ b/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs @@ -1,6 +1,6 @@ namespace Hypercube.Shared.EventBus.Events.Broadcast; -public class BroadcastRegistration : IEquatable +public sealed class BroadcastRegistration : IEquatable { public RefHandler Handler { get; } public object Equality { get; } @@ -18,7 +18,7 @@ public bool Equals(BroadcastRegistration? other) public override bool Equals(object? obj) { - return obj is not null && obj is BroadcastRegistration registration && Equals(registration.Equality, Equality); + return obj is BroadcastRegistration registration && Equals(registration); } public override int GetHashCode() diff --git a/Hypercube.Shared/EventBus/Events/IEventArgs.cs b/Hypercube.Shared/EventBus/Events/IEventArgs.cs index 543384a..09ee291 100644 --- a/Hypercube.Shared/EventBus/Events/IEventArgs.cs +++ b/Hypercube.Shared/EventBus/Events/IEventArgs.cs @@ -1,15 +1,20 @@ -using System.Runtime.InteropServices; +namespace Hypercube.Shared.EventBus.Events; -namespace Hypercube.Shared.EventBus.Events; +public interface IEventArgs; -public abstract class EventArgs -{ -} +public abstract class EventArgs : IEventArgs; public abstract class CancellableEventArgs : EventArgs { public bool Cancelled { get; private set; } - public void Cancel() => Cancelled = true; - public void UnCancel() => Cancelled = false; + public void Cancel() + { + Cancelled = true; + } + + public void UnCancel() + { + Cancelled = false; + } } \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Unit.cs b/Hypercube.Shared/EventBus/Events/Unit.cs index 24fa990..147a168 100644 --- a/Hypercube.Shared/EventBus/Events/Unit.cs +++ b/Hypercube.Shared/EventBus/Events/Unit.cs @@ -2,9 +2,8 @@ namespace Hypercube.Shared.EventBus.Events; -public readonly struct Unit -{ -} +public readonly struct Unit; + [StructLayout(LayoutKind.Sequential)] public sealed class UnitBox { diff --git a/Hypercube.Shared/EventBus/IEventBus.cs b/Hypercube.Shared/EventBus/IEventBus.cs index de2c30f..d7adfce 100644 --- a/Hypercube.Shared/EventBus/IEventBus.cs +++ b/Hypercube.Shared/EventBus/IEventBus.cs @@ -1,13 +1,16 @@ using Hypercube.Shared.EventBus.Events; -using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; namespace Hypercube.Shared.EventBus; +/// +/// EventBus providing interaction between loosely coupled +/// components according to the principle of "event publisher -> event subscriber". +/// public interface IEventBus { - void UnsubscribeEvent(IEventSubscriber subscriber); - public void SubscribeEvent(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull; - void RaiseEvent(object toRaise); - void RaiseEvent(ref T toRaise) where T : notnull; - void RaiseEvent(T toRaise) where T : notnull; + void Raise(ref T receiver) where T : IEventArgs; + void Raise(T receiver) where T : IEventArgs; + void Raise(object receiver); + void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : IEventArgs; + void Unsubscribe(IEventSubscriber subscriber) where T : IEventArgs; } \ No newline at end of file diff --git a/Hypercube.Shared/Scenes/Manager/SceneManager.cs b/Hypercube.Shared/Scenes/Manager/SceneManager.cs index 593af34..1be5252 100644 --- a/Hypercube.Shared/Scenes/Manager/SceneManager.cs +++ b/Hypercube.Shared/Scenes/Manager/SceneManager.cs @@ -21,7 +21,7 @@ public SceneId Create() var scene = new Scene(id); _scenes.Add(id, scene); - _eventBus.RaiseEvent(new SceneAdded(scene)); + _eventBus.Raise(new SceneAdded(scene)); return id; } @@ -30,7 +30,7 @@ public void Delete(SceneId sceneId) { var scene = _scenes[sceneId]; _scenes.Remove(sceneId); - _eventBus.RaiseEvent(new SceneDeleted(scene)); + _eventBus.Raise(new SceneDeleted(scene)); } public bool HasScene(SceneId sceneId) diff --git a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs index e452329..aa940ce 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs @@ -13,8 +13,8 @@ public static void Raising() var subscriber = new TestEventSubscriber(eventBus); subscriber.Subscribe(); - eventBus.RaiseEvent(new TestSubEventClass()); - eventBus.RaiseEvent(new TestSubEventStruct()); + eventBus.Raise(new TestSubEventClass()); + eventBus.Raise(new TestSubEventStruct()); subscriber.AssertPassed(); } @@ -26,8 +26,8 @@ private sealed class TestEventSubscriber(IEventBus eventBus) : IEventSubscriber public void Subscribe() { - eventBus.SubscribeEvent(this, OnClass); - eventBus.SubscribeEvent(this, OnStruct); + eventBus.Subscribe(this, OnClass); + eventBus.Subscribe(this, OnStruct); } private void OnClass(ref TestSubEventClass args) diff --git a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs index a9bfdf2..871320c 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs @@ -18,7 +18,7 @@ public static void RefClass() subscriber2.Subscribe(); var args = new TestEventClass(); - eventBus.RaiseEvent(args); + eventBus.Raise(args); Assert.That(args.Counter, Is.EqualTo(2)); Assert.Pass("All subscribers handled correctly"); @@ -33,7 +33,7 @@ private sealed class TestRefClassSubscriber1(IEventBus bus) : IEventSubscriber { public void Subscribe() { - bus.SubscribeEvent(this, RefMethod2); + bus.Subscribe(this, RefMethod2); } private void RefMethod2(ref TestEventClass args) @@ -46,7 +46,7 @@ private sealed class TestRefClassSubscriber2(IEventBus bus) : IEventSubscriber { public void Subscribe() { - bus.SubscribeEvent(this, RefMethod1); + bus.Subscribe(this, RefMethod1); } private void RefMethod1(ref TestEventClass args) diff --git a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs index c90688e..c3ceb30 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs @@ -17,7 +17,7 @@ public static void RefStruct() subscriber2.Subscribe(); var args = new TestEventStruct(); - eventBus.RaiseEvent(ref args); + eventBus.Raise(ref args); Assert.That(args.Counter, Is.EqualTo(2)); Assert.Pass("All subscribers handled correctly"); @@ -27,7 +27,7 @@ private sealed class TestRefStructSubscriber1(IEventBus eventBus) : IEventSubscr { public void Subscribe() { - eventBus.SubscribeEvent(this, RefMethod); + eventBus.Subscribe(this, RefMethod); } private void RefMethod(ref TestEventStruct args) @@ -40,7 +40,7 @@ private sealed class TestRefStructSubscriber2(IEventBus eventBus) : IEventSubscr { public void Subscribe() { - eventBus.SubscribeEvent(this, RefMethod); + eventBus.Subscribe(this, RefMethod); } private void RefMethod(ref TestEventStruct args) diff --git a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs index 7c2e7b4..27bcbf6 100644 --- a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs @@ -13,13 +13,13 @@ public void Unsubscribing() subscriber.Subscribe(); - eventBus.RaiseEvent(new TestUnsubEventClass()); - eventBus.RaiseEvent(new TestUnsubEventStruct()); + eventBus.Raise(new TestUnsubEventClass()); + eventBus.Raise(new TestUnsubEventStruct()); subscriber.Unsubscribe(); - eventBus.RaiseEvent(new TestUnsubEventClass()); - eventBus.RaiseEvent(new TestUnsubEventStruct()); + eventBus.Raise(new TestUnsubEventClass()); + eventBus.Raise(new TestUnsubEventStruct()); subscriber.AssertPassed(); } @@ -31,14 +31,14 @@ private class TestEventSubscriber(IEventBus eventBus) : IEventSubscriber public void Subscribe() { - eventBus.SubscribeEvent(this, OnClass); - eventBus.SubscribeEvent(this, OnStruct); + eventBus.Subscribe(this, OnClass); + eventBus.Subscribe(this, OnStruct); } public void Unsubscribe() { - eventBus.UnsubscribeEvent(this); - eventBus.UnsubscribeEvent(this); + eventBus.Unsubscribe(this); + eventBus.Unsubscribe(this); } private void OnClass(ref TestUnsubEventClass args) From 50ea7ae27a567a594cc55d12c381dd48122315ec Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Fri, 12 Jul 2024 20:00:00 +1000 Subject: [PATCH 06/11] Fixed error IEventArgs implementation --- Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs | 3 ++- .../Entities/Realisation/EventBus/EntitiesEventBus.cs | 8 ++++---- .../Runtimes/Event/RuntimeInitializationEvent.cs | 6 ++++-- Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs | 6 ++++-- Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs | 6 ++++-- Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs | 6 ++++-- Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs | 6 ++++-- Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs | 4 ++-- Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs | 2 +- Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs | 4 ++-- 10 files changed, 31 insertions(+), 20 deletions(-) diff --git a/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs b/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs index d24029c..1372c1e 100644 --- a/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs +++ b/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs @@ -1,8 +1,9 @@ using Hypercube.Client.Graphics.Windows; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Client.Graphics.Event; -public readonly struct MainWindowClosedEvent(WindowRegistration registration) +public readonly struct MainWindowClosedEvent(WindowRegistration registration) : IEventArgs { public readonly WindowRegistration Registration = registration; } \ No newline at end of file diff --git a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs index c549397..4b21b4f 100644 --- a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs +++ b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs @@ -4,12 +4,12 @@ namespace Hypercube.Shared.Entities.Realisation.EventBus; public sealed class EntitiesEventBus : IEntitiesEventBus { - public void Unsubscribe(IEventSubscriber subscriber) + public void Unsubscribe(IEventSubscriber subscriber) where T : IEventArgs { throw new NotImplementedException(); } - public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : notnull + public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : IEventArgs { throw new NotImplementedException(); } @@ -19,12 +19,12 @@ public void Raise(object receiver) throw new NotImplementedException(); } - public void Raise(ref T receiver) where T : notnull + public void Raise(ref T receiver) where T : IEventArgs { throw new NotImplementedException(); } - public void Raise(T receiver) where T : notnull + public void Raise(T receiver) where T : IEventArgs { throw new NotImplementedException(); } diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs index a0691f0..bffad53 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Runtimes.Event; +using Hypercube.Shared.EventBus.Events; -public readonly record struct RuntimeInitializationEvent; \ No newline at end of file +namespace Hypercube.Shared.Runtimes.Event; + +public readonly record struct RuntimeInitializationEvent : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs index e3c97bf..e89ee29 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Runtimes.Event; +using Hypercube.Shared.EventBus.Events; -public readonly record struct RuntimeShutdownEvent(string Reason); \ No newline at end of file +namespace Hypercube.Shared.Runtimes.Event; + +public readonly record struct RuntimeShutdownEvent(string Reason) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs index 3985d95..7f3b673 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Runtimes.Event; +using Hypercube.Shared.EventBus.Events; -public readonly record struct RuntimeStartupEvent; \ No newline at end of file +namespace Hypercube.Shared.Runtimes.Event; + +public readonly record struct RuntimeStartupEvent : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs index cf76f7d..05d25ed 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs @@ -1,6 +1,8 @@ -namespace Hypercube.Shared.Runtimes.Loop.Event; +using Hypercube.Shared.EventBus.Events; -public readonly struct RenderFrameEvent(float deltaSeconds) +namespace Hypercube.Shared.Runtimes.Loop.Event; + +public readonly struct RenderFrameEvent(float deltaSeconds) : IEventArgs { public readonly float DeltaSeconds = deltaSeconds; } \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs index 1251058..4c948c8 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs @@ -1,6 +1,8 @@ -namespace Hypercube.Shared.Runtimes.Loop.Event; +using Hypercube.Shared.EventBus.Events; -public readonly struct UpdateFrameEvent(float deltaSeconds) +namespace Hypercube.Shared.Runtimes.Loop.Event; + +public readonly struct UpdateFrameEvent(float deltaSeconds) : IEventArgs { public readonly float DeltaSeconds = deltaSeconds; } \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs index aa940ce..3af5229 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs @@ -50,7 +50,7 @@ public void AssertPassed() } } - private readonly record struct TestSubEventStruct; + private readonly record struct TestSubEventStruct : IEventArgs; - private sealed class TestSubEventClass; + private sealed class TestSubEventClass : IEventArgs; } \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs index c3ceb30..f54d7cf 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs @@ -50,5 +50,5 @@ private void RefMethod(ref TestEventStruct args) } } - private record struct TestEventStruct(int Counter = 0); + private record struct TestEventStruct(int Counter = 0) : IEventArgs; } \ No newline at end of file diff --git a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs index 27bcbf6..6d63474 100644 --- a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs @@ -61,7 +61,7 @@ public void AssertPassed() } } - private record struct TestUnsubEventStruct; + private record struct TestUnsubEventStruct : IEventArgs; - private sealed class TestUnsubEventClass; + private sealed class TestUnsubEventClass : IEventArgs; } \ No newline at end of file From 03c90ce9f7ec4ea4dead12f838c7de2d28e8253f Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sat, 13 Jul 2024 00:03:26 +1000 Subject: [PATCH 07/11] Clean up event bus --- .../Graphics/Event/MainWindowClosedEvent.cs | 2 +- .../Graphics/Event/WindowClosed.cs | 3 +- .../Graphics/Event/WindowFocusChangedEvent.cs | 3 +- Hypercube.Client/Runtimes/Runtime.cs | 4 +- .../Realisation/EventBus/EntitiesEventBus.cs | 4 +- .../Realisation/Events/ComponentAdded.cs | 3 +- .../Realisation/Events/EntityAdded.cs | 6 +- .../Realisation/Events/EntityRemoved.cs | 6 +- Hypercube.Shared/EventBus/EventBus.cs | 143 ++++++++++-------- .../Events/Broadcast/BroadcastRegistration.cs | 4 +- Hypercube.Shared/EventBus/Events/EventData.cs | 13 -- .../EventBus/Events/EventHandler.cs | 4 - .../EventBus/Events/EventRegistration.cs | 31 ++++ .../CancellableEventArgs.cs} | 6 +- .../EventBus/Events/Events/EventArgs.cs | 3 + .../EventBus/Events/Events/IEventArgs.cs | 3 + .../Exceptions/UnregisteredEventException.cs | 4 + .../Events/Handlers/EventRefHandler.cs | 3 + .../EventBus/Events/Handlers/RefHandler.cs | 3 + Hypercube.Shared/EventBus/IEventBus.cs | 4 +- .../Event/RuntimeInitializationEvent.cs | 2 +- .../Runtimes/Event/RuntimeShutdownEvent.cs | 2 +- .../Runtimes/Event/RuntimeStartupEvent.cs | 2 +- .../Runtimes/Loop/Event/InputFrameEvent.cs | 6 +- .../Runtimes/Loop/Event/RenderFrameEvent.cs | 2 +- .../Runtimes/Loop/Event/TickFrameEvent.cs | 6 +- .../Runtimes/Loop/Event/UpdateFrameEvent.cs | 2 +- Hypercube.Shared/Scenes/Events/SceneAdded.cs | 6 +- .../Scenes/Events/SceneDeleted.cs | 6 +- .../Utilities/Helpers/PathHelpers.cs | 4 +- .../EventBus/EventBusRaiseTests.cs | 1 + .../EventBus/EventBusRefClassTests.cs | 2 +- .../EventBus/EventBusRefStructTests.cs | 1 + .../EventBus/EventBusUnsubscribeTests.cs | 1 + 34 files changed, 180 insertions(+), 115 deletions(-) delete mode 100644 Hypercube.Shared/EventBus/Events/EventData.cs delete mode 100644 Hypercube.Shared/EventBus/Events/EventHandler.cs create mode 100644 Hypercube.Shared/EventBus/Events/EventRegistration.cs rename Hypercube.Shared/EventBus/Events/{IEventArgs.cs => Events/CancellableEventArgs.cs} (65%) create mode 100644 Hypercube.Shared/EventBus/Events/Events/EventArgs.cs create mode 100644 Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs create mode 100644 Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs create mode 100644 Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs create mode 100644 Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs diff --git a/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs b/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs index 1372c1e..477c7b0 100644 --- a/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs +++ b/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs @@ -1,5 +1,5 @@ using Hypercube.Client.Graphics.Windows; -using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Client.Graphics.Event; diff --git a/Hypercube.Client/Graphics/Event/WindowClosed.cs b/Hypercube.Client/Graphics/Event/WindowClosed.cs index f7d2b31..744b64c 100644 --- a/Hypercube.Client/Graphics/Event/WindowClosed.cs +++ b/Hypercube.Client/Graphics/Event/WindowClosed.cs @@ -1,8 +1,9 @@ using Hypercube.Client.Graphics.Windows; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Client.Graphics.Event; -public readonly struct WindowClosedEvent(WindowRegistration registration) +public readonly struct WindowClosedEvent(WindowRegistration registration) : IEventArgs { public readonly WindowRegistration Registration = registration; } \ No newline at end of file diff --git a/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs b/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs index 3628687..3311ecf 100644 --- a/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs +++ b/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs @@ -1,5 +1,6 @@ using Hypercube.Client.Graphics.Windows; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Client.Graphics.Event; -public readonly record struct WindowFocusChangedEvent(WindowRegistration Registration, bool Focused); \ No newline at end of file +public readonly record struct WindowFocusChangedEvent(WindowRegistration Registration, bool Focused) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Client/Runtimes/Runtime.cs b/Hypercube.Client/Runtimes/Runtime.cs index aa5e832..7627118 100644 --- a/Hypercube.Client/Runtimes/Runtime.cs +++ b/Hypercube.Client/Runtimes/Runtime.cs @@ -1,6 +1,4 @@ -using Hypercube.Client.Graphics; -using Hypercube.Client.Graphics.Event; -using Hypercube.Client.Graphics.Rendering; +using Hypercube.Client.Graphics.Event; using Hypercube.Client.Runtimes.Loop; using Hypercube.Shared.Dependency; using Hypercube.Shared.EventBus; diff --git a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs index 4b21b4f..a8754da 100644 --- a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs +++ b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs @@ -1,4 +1,6 @@ using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events.Handlers; namespace Hypercube.Shared.Entities.Realisation.EventBus; @@ -14,7 +16,7 @@ public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHand throw new NotImplementedException(); } - public void Raise(object receiver) + public void Raise(IEventArgs receiver) { throw new NotImplementedException(); } diff --git a/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs b/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs index 1919f2e..6677226 100644 --- a/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs +++ b/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs @@ -1,5 +1,6 @@ using Hypercube.Shared.Entities.Realisation.Components; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Shared.Entities.Realisation.Events; -public readonly record struct ComponentAdded(EntityUid EntityUid, IComponent Component); \ No newline at end of file +public readonly record struct ComponentAdded(EntityUid EntityUid, IComponent Component) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs b/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs index b4bfa6a..01666e7 100644 --- a/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs +++ b/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Entities.Realisation.Events; +using Hypercube.Shared.EventBus.Events.Events; -public readonly record struct EntityAdded(EntityUid EntityUid); \ No newline at end of file +namespace Hypercube.Shared.Entities.Realisation.Events; + +public readonly record struct EntityAdded(EntityUid EntityUid) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs b/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs index edaa062..da7a3af 100644 --- a/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs +++ b/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Entities.Realisation.Events; +using Hypercube.Shared.EventBus.Events.Events; -public readonly record struct EntityRemoved(EntityUid EntityUid); \ No newline at end of file +namespace Hypercube.Shared.Entities.Realisation.Events; + +public readonly record struct EntityRemoved(EntityUid EntityUid) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index 1e0d10e..ad84ac1 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -1,14 +1,35 @@ using System.Runtime.CompilerServices; using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.EventBus.Events.Broadcast; +using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events.Exceptions; +using Hypercube.Shared.EventBus.Events.Handlers; namespace Hypercube.Shared.EventBus; public sealed class EventBus : IEventBus { - private readonly Dictionary _eventData = new(); + private readonly Dictionary _eventRegistration = new(); private readonly Dictionary> _inverseEventSubscriptions = new(); + public void Raise(ref T receiver) where T : IEventArgs + { + ProcessEvent(ref Unsafe.As(ref receiver), typeof(T)); + } + + public void Raise(T receiver) where T : IEventArgs + { + ProcessEvent(ref Unsafe.As(ref receiver), typeof(T)); + } + + public void Raise(IEventArgs eventArgs) + { + var eventType = eventArgs.GetType(); + ref var unitRef = ref ExtractUnitRef(ref eventArgs, eventType); + + ProcessEvent(ref unitRef, eventType); + } + public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : IEventArgs { SubscribeEventCommon(subscriber, (ref Unit ev) => @@ -18,6 +39,8 @@ public void Subscribe(IEventSubscriber subscriber, EventRefHandler refHand }, refHandler); } + /// Throws when subscriber is null + /// private void SubscribeEventCommon(IEventSubscriber subscriber, RefHandler refHandler, object equality) where T : IEventArgs { @@ -26,96 +49,92 @@ private void SubscribeEventCommon(IEventSubscriber subscriber, RefHandler ref var eventType = typeof(T); var subscription = new BroadcastRegistration(refHandler, equality); - var subscriptions = GetEventData(eventType); - if (subscriptions.BroadcastRegistrations.Contains(subscription)) - throw new InvalidOperationException(); - - subscriptions.BroadcastRegistrations.Add(subscription); - - Dictionary? inverseSubs; - if (!_inverseEventSubscriptions.TryGetValue(subscriber, out inverseSubs)) - { - inverseSubs = new Dictionary(); - _inverseEventSubscriptions[subscriber] = inverseSubs; - } + var subscriptions = GetEventRegistration(eventType); + subscriptions.Add(subscription); - if (!inverseSubs.TryAdd(eventType, subscription)) - throw new InvalidOperationException(); - } - - private EventData GetEventData(Type eventType) - { - if (_eventData.TryGetValue(eventType, out var found)) - return found; + var inverseSubscriptions = GetEventInverseSubscription(subscriber); + if (inverseSubscriptions.TryAdd(eventType, subscription)) + return; - var list = new List(); - var data = new EventData(list); - - return _eventData[eventType] = data; + throw new InvalidOperationException(); } + /// Throws when subscriber is null + /// public void Unsubscribe(IEventSubscriber subscriber) where T : IEventArgs { ArgumentNullException.ThrowIfNull(subscriber); + var eventType = typeof(T); - if (_inverseEventSubscriptions.TryGetValue(subscriber, out var inverse) - && inverse.TryGetValue(eventType, out var tuple)) - UnsubscribeEvent(eventType, tuple, subscriber); - } - - private void UnsubscribeEvent(Type evType, BroadcastRegistration tuple, IEventSubscriber subscriber) - { - if (_eventData.TryGetValue(evType, out var subs) && - subs.BroadcastRegistrations.Contains(tuple)) - { - subs.BroadcastRegistrations.Remove(tuple); - } + var inverseSubscriptions = GetEventInverseSubscription(subscriber); + if (!inverseSubscriptions.TryGetValue(eventType, out var registration)) + throw new InvalidOperationException(); - if (_inverseEventSubscriptions.TryGetValue(subscriber, out var inverse) && inverse.ContainsKey(evType)) - inverse.Remove(evType); + Unsubscribe(eventType, registration, subscriber); } - public void Raise(object receiver) + /// + /// + private void Unsubscribe(Type eventType, BroadcastRegistration registration, IEventSubscriber subscriber) { - ArgumentNullException.ThrowIfNull(receiver); - - var evType = receiver.GetType(); - ref var unitRef = ref ExtractUnitRef(ref receiver, evType); + var eventRegistration = GetEventRegistration(eventType, false); + eventRegistration.Remove(registration); - ProcessEvent(ref unitRef, evType); - } - public void Raise(ref T receiver) where T : IEventArgs - { - ProcessEvent(ref Unsafe.As(ref receiver), typeof(T)); - } - - public void Raise(T receiver) where T : IEventArgs - { - ProcessEvent(ref Unsafe.As(ref receiver), typeof(T)); + var inverseSubscriptions = GetEventInverseSubscription(subscriber, false); + inverseSubscriptions.Remove(eventType); } - private void ProcessEvent(ref Unit unitRef, Type evType) + private void ProcessEvent(ref Unit unitRef, Type eventType) { - if (!_eventData!.TryGetValue(evType, out var data)) + if (!_eventRegistration.TryGetValue(eventType, out var registration)) return; - ProcessEventCore(ref unitRef, data); + ProcessEventCore(ref unitRef, registration); } - private void ProcessEventCore(ref Unit unitRef, EventData data) + private void ProcessEventCore(ref Unit unitRef, EventRegistration registration) { - foreach (var handler in data.BroadcastRegistrations) + foreach (var handler in registration.BroadcastRegistrations) { handler.Handler(ref unitRef); } } + /// Type of event whose registration we want to receive. + /// Allows you to control the automatic registration of an event if it does not exist. + /// If autoRegistration is false, it will throw an exception if registration is not found. + private EventRegistration GetEventRegistration(Type eventType, bool autoRegistration = true) + { + if (_eventRegistration.TryGetValue(eventType, out var found)) + return found; + + if (!autoRegistration) + throw new UnregisteredEventException(eventType); + + return _eventRegistration[eventType] = new EventRegistration(); + } + + /// + private Dictionary GetEventInverseSubscription(IEventSubscriber subscriber, bool creating = true) + { + if (_inverseEventSubscriptions.TryGetValue(subscriber, out var subscriptions)) + return subscriptions; + + if (!creating) + throw new InvalidOperationException(); + + return _inverseEventSubscriptions[subscriber] = new Dictionary(); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ref Unit ExtractUnitRef(ref object obj, Type objType) + private static ref Unit ExtractUnitRef(ref IEventArgs eventArgs, Type objType) { + //return ref Unsafe.As(ref eventArgs); + + // Why not only unit? return ref objType.IsValueType - ? ref Unsafe.As(ref obj).Value - : ref Unsafe.As(ref obj); + ? ref Unsafe.As(ref eventArgs).Value + : ref Unsafe.As(ref eventArgs); } } \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs b/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs index c3b9979..09a8e1a 100644 --- a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs +++ b/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs @@ -1,4 +1,6 @@ -namespace Hypercube.Shared.EventBus.Events.Broadcast; +using Hypercube.Shared.EventBus.Events.Handlers; + +namespace Hypercube.Shared.EventBus.Events.Broadcast; public sealed class BroadcastRegistration : IEquatable { diff --git a/Hypercube.Shared/EventBus/Events/EventData.cs b/Hypercube.Shared/EventBus/Events/EventData.cs deleted file mode 100644 index aba4166..0000000 --- a/Hypercube.Shared/EventBus/Events/EventData.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Hypercube.Shared.EventBus.Events.Broadcast; - -namespace Hypercube.Shared.EventBus.Events; - -public class EventData -{ - public List BroadcastRegistrations; - - public EventData(List broadcastRegistrations) - { - BroadcastRegistrations = broadcastRegistrations; - } -} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/EventHandler.cs b/Hypercube.Shared/EventBus/Events/EventHandler.cs deleted file mode 100644 index 0689142..0000000 --- a/Hypercube.Shared/EventBus/Events/EventHandler.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Hypercube.Shared.EventBus.Events; - -public delegate void RefHandler(ref Unit ev); -public delegate void EventRefHandler(ref T ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/EventRegistration.cs b/Hypercube.Shared/EventBus/Events/EventRegistration.cs new file mode 100644 index 0000000..35d1a53 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/EventRegistration.cs @@ -0,0 +1,31 @@ +using Hypercube.Shared.EventBus.Events.Broadcast; + +namespace Hypercube.Shared.EventBus.Events; + +/// +/// Saves information about a specific event. +/// +public readonly struct EventRegistration() +{ + private readonly HashSet _broadcastRegistrations = new(); + + public IReadOnlySet BroadcastRegistrations => _broadcastRegistrations; + + /// + public void Add(BroadcastRegistration registration) + { + if (_broadcastRegistrations.Contains(registration)) + throw new InvalidOperationException(); + + _broadcastRegistrations.Add(registration); + } + + /// + public void Remove(BroadcastRegistration registration) + { + if (!_broadcastRegistrations.Contains(registration)) + throw new InvalidOperationException(); + + _broadcastRegistrations.Remove(registration); + } +} \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/IEventArgs.cs b/Hypercube.Shared/EventBus/Events/Events/CancellableEventArgs.cs similarity index 65% rename from Hypercube.Shared/EventBus/Events/IEventArgs.cs rename to Hypercube.Shared/EventBus/Events/Events/CancellableEventArgs.cs index 09ee291..a4d72cf 100644 --- a/Hypercube.Shared/EventBus/Events/IEventArgs.cs +++ b/Hypercube.Shared/EventBus/Events/Events/CancellableEventArgs.cs @@ -1,8 +1,4 @@ -namespace Hypercube.Shared.EventBus.Events; - -public interface IEventArgs; - -public abstract class EventArgs : IEventArgs; +namespace Hypercube.Shared.EventBus.Events.Events; public abstract class CancellableEventArgs : EventArgs { diff --git a/Hypercube.Shared/EventBus/Events/Events/EventArgs.cs b/Hypercube.Shared/EventBus/Events/Events/EventArgs.cs new file mode 100644 index 0000000..2f13048 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Events/EventArgs.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Events.Events; + +public abstract class EventArgs : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs b/Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs new file mode 100644 index 0000000..dae40dd --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Events.Events; + +public interface IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs b/Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs new file mode 100644 index 0000000..8280ee4 --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs @@ -0,0 +1,4 @@ +namespace Hypercube.Shared.EventBus.Events.Exceptions; + +public sealed class UnregisteredEventException(Type registrationType) : + Exception($"Attempted to resolve unregistered event {registrationType.FullName}."); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs b/Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs new file mode 100644 index 0000000..5ec59ce --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Events.Handlers; + +public delegate void EventRefHandler(ref T ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs b/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs new file mode 100644 index 0000000..c890a3e --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Events.Handlers; + +public delegate void RefHandler(ref Unit ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/IEventBus.cs b/Hypercube.Shared/EventBus/IEventBus.cs index d7adfce..5bfffc8 100644 --- a/Hypercube.Shared/EventBus/IEventBus.cs +++ b/Hypercube.Shared/EventBus/IEventBus.cs @@ -1,4 +1,6 @@ using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events.Handlers; namespace Hypercube.Shared.EventBus; @@ -10,7 +12,7 @@ public interface IEventBus { void Raise(ref T receiver) where T : IEventArgs; void Raise(T receiver) where T : IEventArgs; - void Raise(object receiver); + void Raise(IEventArgs eventArgs); void Subscribe(IEventSubscriber subscriber, EventRefHandler refHandler) where T : IEventArgs; void Unsubscribe(IEventSubscriber subscriber) where T : IEventArgs; } \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs index bffad53..3a909e7 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Shared.Runtimes.Event; diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs index e89ee29..d7ce56a 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Shared.Runtimes.Event; diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs index 7f3b673..79672bf 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Shared.Runtimes.Event; diff --git a/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs index b58cc1f..a31ff27 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs @@ -1,6 +1,8 @@ -namespace Hypercube.Shared.Runtimes.Loop.Event; +using Hypercube.Shared.EventBus.Events.Events; -public readonly struct InputFrameEvent(float deltaSeconds) +namespace Hypercube.Shared.Runtimes.Loop.Event; + +public readonly struct InputFrameEvent(float deltaSeconds) : IEventArgs { public readonly float DeltaSeconds = deltaSeconds; } \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs index 05d25ed..6dade5f 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Shared.Runtimes.Loop.Event; diff --git a/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs index 9a5e387..1f05fe4 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs @@ -1,6 +1,8 @@ -namespace Hypercube.Shared.Runtimes.Loop.Event; +using Hypercube.Shared.EventBus.Events.Events; -public readonly struct TickFrameEvent(float deltaSeconds) +namespace Hypercube.Shared.Runtimes.Loop.Event; + +public readonly struct TickFrameEvent(float deltaSeconds) : IEventArgs { public readonly float DeltaSeconds = deltaSeconds; } \ No newline at end of file diff --git a/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs index 4c948c8..01a76c8 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.Shared.Runtimes.Loop.Event; diff --git a/Hypercube.Shared/Scenes/Events/SceneAdded.cs b/Hypercube.Shared/Scenes/Events/SceneAdded.cs index c77641e..ad8fd9d 100644 --- a/Hypercube.Shared/Scenes/Events/SceneAdded.cs +++ b/Hypercube.Shared/Scenes/Events/SceneAdded.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Scenes.Events; +using Hypercube.Shared.EventBus.Events.Events; -public readonly record struct SceneAdded(Scene Scene); \ No newline at end of file +namespace Hypercube.Shared.Scenes.Events; + +public readonly record struct SceneAdded(Scene Scene) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Scenes/Events/SceneDeleted.cs b/Hypercube.Shared/Scenes/Events/SceneDeleted.cs index 746e073..b9bc551 100644 --- a/Hypercube.Shared/Scenes/Events/SceneDeleted.cs +++ b/Hypercube.Shared/Scenes/Events/SceneDeleted.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.Scenes.Events; +using Hypercube.Shared.EventBus.Events.Events; -public readonly record struct SceneDeleted(Scene Scene); \ No newline at end of file +namespace Hypercube.Shared.Scenes.Events; + +public readonly record struct SceneDeleted(Scene Scene) : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/Utilities/Helpers/PathHelpers.cs b/Hypercube.Shared/Utilities/Helpers/PathHelpers.cs index 3067606..8817bcb 100644 --- a/Hypercube.Shared/Utilities/Helpers/PathHelpers.cs +++ b/Hypercube.Shared/Utilities/Helpers/PathHelpers.cs @@ -1,6 +1,4 @@ -using System.Reflection; - -namespace Hypercube.Shared.Utilities.Helpers; +namespace Hypercube.Shared.Utilities.Helpers; public static class PathHelpers { diff --git a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs index 3af5229..8f02788 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs @@ -1,5 +1,6 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.UnitTests.EventBus; diff --git a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs index 871320c..bc67e3f 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs @@ -1,6 +1,6 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; -using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; +using EventArgs = Hypercube.Shared.EventBus.Events.Events.EventArgs; namespace Hypercube.UnitTests.EventBus; diff --git a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs index f54d7cf..44039cb 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs @@ -1,5 +1,6 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.UnitTests.EventBus; diff --git a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs index 6d63474..2d88a6a 100644 --- a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs @@ -1,5 +1,6 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.UnitTests.EventBus; From ca9e6faded613d7bcbac18e5bc78d50d38f9cec8 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sat, 13 Jul 2024 08:39:18 +1000 Subject: [PATCH 08/11] Moved EventBus namespace --- Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs | 2 +- Hypercube.Client/Graphics/Event/WindowClosed.cs | 2 +- Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs | 2 +- .../Entities/Realisation/EventBus/EntitiesEventBus.cs | 6 +++--- .../Entities/Realisation/Events/ComponentAdded.cs | 2 +- .../Entities/Realisation/Events/EntityAdded.cs | 2 +- .../Entities/Realisation/Events/EntityRemoved.cs | 2 +- .../{Events => }/Broadcast/BroadcastRegistration.cs | 4 ++-- Hypercube.Shared/EventBus/EventBus.cs | 7 +++---- .../EventBus/{Events => }/EventRegistration.cs | 4 ++-- .../EventBus/Events/{Events => }/CancellableEventArgs.cs | 2 +- Hypercube.Shared/EventBus/Events/EventArgs.cs | 3 +++ Hypercube.Shared/EventBus/Events/Events/EventArgs.cs | 3 --- Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs | 3 --- .../EventBus/Events/Handlers/EventRefHandler.cs | 3 --- Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs | 3 --- .../EventBus/Events/{IEventSubscriber.cs => IEventArgs.cs} | 2 +- .../{Events => }/Exceptions/UnregisteredEventException.cs | 2 +- Hypercube.Shared/EventBus/Handlers/EventRefHandler.cs | 3 +++ Hypercube.Shared/EventBus/Handlers/RefHandler.cs | 3 +++ Hypercube.Shared/EventBus/IEventBus.cs | 3 +-- Hypercube.Shared/EventBus/IEventSubscriber.cs | 3 +++ Hypercube.Shared/EventBus/{Events => }/Unit.cs | 2 +- .../Runtimes/Event/RuntimeInitializationEvent.cs | 2 +- Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs | 2 +- Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs | 2 +- Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs | 2 +- Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs | 2 +- Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs | 2 +- Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs | 2 +- Hypercube.Shared/Scenes/Events/SceneAdded.cs | 2 +- Hypercube.Shared/Scenes/Events/SceneDeleted.cs | 2 +- Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs | 1 - Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs | 4 ++-- Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs | 1 - Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs | 1 - 36 files changed, 44 insertions(+), 49 deletions(-) rename Hypercube.Shared/EventBus/{Events => }/Broadcast/BroadcastRegistration.cs (86%) rename Hypercube.Shared/EventBus/{Events => }/EventRegistration.cs (90%) rename Hypercube.Shared/EventBus/Events/{Events => }/CancellableEventArgs.cs (81%) create mode 100644 Hypercube.Shared/EventBus/Events/EventArgs.cs delete mode 100644 Hypercube.Shared/EventBus/Events/Events/EventArgs.cs delete mode 100644 Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs delete mode 100644 Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs delete mode 100644 Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs rename Hypercube.Shared/EventBus/Events/{IEventSubscriber.cs => IEventArgs.cs} (58%) rename Hypercube.Shared/EventBus/{Events => }/Exceptions/UnregisteredEventException.cs (73%) create mode 100644 Hypercube.Shared/EventBus/Handlers/EventRefHandler.cs create mode 100644 Hypercube.Shared/EventBus/Handlers/RefHandler.cs create mode 100644 Hypercube.Shared/EventBus/IEventSubscriber.cs rename Hypercube.Shared/EventBus/{Events => }/Unit.cs (78%) diff --git a/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs b/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs index 477c7b0..1372c1e 100644 --- a/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs +++ b/Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs @@ -1,5 +1,5 @@ using Hypercube.Client.Graphics.Windows; -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Client.Graphics.Event; diff --git a/Hypercube.Client/Graphics/Event/WindowClosed.cs b/Hypercube.Client/Graphics/Event/WindowClosed.cs index 744b64c..dccaab7 100644 --- a/Hypercube.Client/Graphics/Event/WindowClosed.cs +++ b/Hypercube.Client/Graphics/Event/WindowClosed.cs @@ -1,5 +1,5 @@ using Hypercube.Client.Graphics.Windows; -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Client.Graphics.Event; diff --git a/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs b/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs index 3311ecf..6fb3e33 100644 --- a/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs +++ b/Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs @@ -1,5 +1,5 @@ using Hypercube.Client.Graphics.Windows; -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Client.Graphics.Event; diff --git a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs index a8754da..72552d2 100644 --- a/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs +++ b/Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs @@ -1,6 +1,6 @@ -using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Events; -using Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.EventBus; +using Hypercube.Shared.EventBus.Events; +using Hypercube.Shared.EventBus.Handlers; namespace Hypercube.Shared.Entities.Realisation.EventBus; diff --git a/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs b/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs index 6677226..8cb3204 100644 --- a/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs +++ b/Hypercube.Shared/Entities/Realisation/Events/ComponentAdded.cs @@ -1,5 +1,5 @@ using Hypercube.Shared.Entities.Realisation.Components; -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Entities.Realisation.Events; diff --git a/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs b/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs index 01666e7..26b9ccb 100644 --- a/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs +++ b/Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Entities.Realisation.Events; diff --git a/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs b/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs index da7a3af..5f48b66 100644 --- a/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs +++ b/Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Entities.Realisation.Events; diff --git a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs b/Hypercube.Shared/EventBus/Broadcast/BroadcastRegistration.cs similarity index 86% rename from Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs rename to Hypercube.Shared/EventBus/Broadcast/BroadcastRegistration.cs index 09a8e1a..4e32d84 100644 --- a/Hypercube.Shared/EventBus/Events/Broadcast/BroadcastRegistration.cs +++ b/Hypercube.Shared/EventBus/Broadcast/BroadcastRegistration.cs @@ -1,6 +1,6 @@ -using Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.EventBus.Handlers; -namespace Hypercube.Shared.EventBus.Events.Broadcast; +namespace Hypercube.Shared.EventBus.Broadcast; public sealed class BroadcastRegistration : IEquatable { diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index ad84ac1..a4755c6 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -1,9 +1,8 @@ using System.Runtime.CompilerServices; +using Hypercube.Shared.EventBus.Broadcast; using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Broadcast; -using Hypercube.Shared.EventBus.Events.Events; -using Hypercube.Shared.EventBus.Events.Exceptions; -using Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.EventBus.Exceptions; +using Hypercube.Shared.EventBus.Handlers; namespace Hypercube.Shared.EventBus; diff --git a/Hypercube.Shared/EventBus/Events/EventRegistration.cs b/Hypercube.Shared/EventBus/EventRegistration.cs similarity index 90% rename from Hypercube.Shared/EventBus/Events/EventRegistration.cs rename to Hypercube.Shared/EventBus/EventRegistration.cs index 35d1a53..89edfa9 100644 --- a/Hypercube.Shared/EventBus/Events/EventRegistration.cs +++ b/Hypercube.Shared/EventBus/EventRegistration.cs @@ -1,6 +1,6 @@ -using Hypercube.Shared.EventBus.Events.Broadcast; +using Hypercube.Shared.EventBus.Broadcast; -namespace Hypercube.Shared.EventBus.Events; +namespace Hypercube.Shared.EventBus; /// /// Saves information about a specific event. diff --git a/Hypercube.Shared/EventBus/Events/Events/CancellableEventArgs.cs b/Hypercube.Shared/EventBus/Events/CancellableEventArgs.cs similarity index 81% rename from Hypercube.Shared/EventBus/Events/Events/CancellableEventArgs.cs rename to Hypercube.Shared/EventBus/Events/CancellableEventArgs.cs index a4d72cf..0b7457e 100644 --- a/Hypercube.Shared/EventBus/Events/Events/CancellableEventArgs.cs +++ b/Hypercube.Shared/EventBus/Events/CancellableEventArgs.cs @@ -1,4 +1,4 @@ -namespace Hypercube.Shared.EventBus.Events.Events; +namespace Hypercube.Shared.EventBus.Events; public abstract class CancellableEventArgs : EventArgs { diff --git a/Hypercube.Shared/EventBus/Events/EventArgs.cs b/Hypercube.Shared/EventBus/Events/EventArgs.cs new file mode 100644 index 0000000..ca430bc --- /dev/null +++ b/Hypercube.Shared/EventBus/Events/EventArgs.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Events; + +public abstract class EventArgs : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Events/EventArgs.cs b/Hypercube.Shared/EventBus/Events/Events/EventArgs.cs deleted file mode 100644 index 2f13048..0000000 --- a/Hypercube.Shared/EventBus/Events/Events/EventArgs.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Hypercube.Shared.EventBus.Events.Events; - -public abstract class EventArgs : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs b/Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs deleted file mode 100644 index dae40dd..0000000 --- a/Hypercube.Shared/EventBus/Events/Events/IEventArgs.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Hypercube.Shared.EventBus.Events.Events; - -public interface IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs b/Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs deleted file mode 100644 index 5ec59ce..0000000 --- a/Hypercube.Shared/EventBus/Events/Handlers/EventRefHandler.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Hypercube.Shared.EventBus.Events.Handlers; - -public delegate void EventRefHandler(ref T ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs b/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs deleted file mode 100644 index c890a3e..0000000 --- a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Hypercube.Shared.EventBus.Events.Handlers; - -public delegate void RefHandler(ref Unit ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs b/Hypercube.Shared/EventBus/Events/IEventArgs.cs similarity index 58% rename from Hypercube.Shared/EventBus/Events/IEventSubscriber.cs rename to Hypercube.Shared/EventBus/Events/IEventArgs.cs index a3df382..8ed8b7e 100644 --- a/Hypercube.Shared/EventBus/Events/IEventSubscriber.cs +++ b/Hypercube.Shared/EventBus/Events/IEventArgs.cs @@ -1,3 +1,3 @@ namespace Hypercube.Shared.EventBus.Events; -public interface IEventSubscriber; \ No newline at end of file +public interface IEventArgs; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs b/Hypercube.Shared/EventBus/Exceptions/UnregisteredEventException.cs similarity index 73% rename from Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs rename to Hypercube.Shared/EventBus/Exceptions/UnregisteredEventException.cs index 8280ee4..1c4dc87 100644 --- a/Hypercube.Shared/EventBus/Events/Exceptions/UnregisteredEventException.cs +++ b/Hypercube.Shared/EventBus/Exceptions/UnregisteredEventException.cs @@ -1,4 +1,4 @@ -namespace Hypercube.Shared.EventBus.Events.Exceptions; +namespace Hypercube.Shared.EventBus.Exceptions; public sealed class UnregisteredEventException(Type registrationType) : Exception($"Attempted to resolve unregistered event {registrationType.FullName}."); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Handlers/EventRefHandler.cs b/Hypercube.Shared/EventBus/Handlers/EventRefHandler.cs new file mode 100644 index 0000000..8c26b80 --- /dev/null +++ b/Hypercube.Shared/EventBus/Handlers/EventRefHandler.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Handlers; + +public delegate void EventRefHandler(ref T ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Handlers/RefHandler.cs b/Hypercube.Shared/EventBus/Handlers/RefHandler.cs new file mode 100644 index 0000000..22c87fd --- /dev/null +++ b/Hypercube.Shared/EventBus/Handlers/RefHandler.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus.Handlers; + +public delegate void RefHandler(ref Unit ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/IEventBus.cs b/Hypercube.Shared/EventBus/IEventBus.cs index 5bfffc8..99b970c 100644 --- a/Hypercube.Shared/EventBus/IEventBus.cs +++ b/Hypercube.Shared/EventBus/IEventBus.cs @@ -1,6 +1,5 @@ using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Events; -using Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.EventBus.Handlers; namespace Hypercube.Shared.EventBus; diff --git a/Hypercube.Shared/EventBus/IEventSubscriber.cs b/Hypercube.Shared/EventBus/IEventSubscriber.cs new file mode 100644 index 0000000..edfb654 --- /dev/null +++ b/Hypercube.Shared/EventBus/IEventSubscriber.cs @@ -0,0 +1,3 @@ +namespace Hypercube.Shared.EventBus; + +public interface IEventSubscriber; \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Unit.cs b/Hypercube.Shared/EventBus/Unit.cs similarity index 78% rename from Hypercube.Shared/EventBus/Events/Unit.cs rename to Hypercube.Shared/EventBus/Unit.cs index 147a168..bad563a 100644 --- a/Hypercube.Shared/EventBus/Events/Unit.cs +++ b/Hypercube.Shared/EventBus/Unit.cs @@ -1,6 +1,6 @@ using System.Runtime.InteropServices; -namespace Hypercube.Shared.EventBus.Events; +namespace Hypercube.Shared.EventBus; public readonly struct Unit; diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs index 3a909e7..bffad53 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeInitializationEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Event; diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs index d7ce56a..e89ee29 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeShutdownEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Event; diff --git a/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs b/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs index 79672bf..7f3b673 100644 --- a/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs +++ b/Hypercube.Shared/Runtimes/Event/RuntimeStartupEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Event; diff --git a/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs index a31ff27..4b67281 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/InputFrameEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Loop.Event; diff --git a/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs index 6dade5f..05d25ed 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/RenderFrameEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Loop.Event; diff --git a/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs index 1f05fe4..b74eaf0 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/TickFrameEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Loop.Event; diff --git a/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs b/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs index 01a76c8..4c948c8 100644 --- a/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs +++ b/Hypercube.Shared/Runtimes/Loop/Event/UpdateFrameEvent.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Runtimes.Loop.Event; diff --git a/Hypercube.Shared/Scenes/Events/SceneAdded.cs b/Hypercube.Shared/Scenes/Events/SceneAdded.cs index ad8fd9d..3cbf68c 100644 --- a/Hypercube.Shared/Scenes/Events/SceneAdded.cs +++ b/Hypercube.Shared/Scenes/Events/SceneAdded.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Scenes.Events; diff --git a/Hypercube.Shared/Scenes/Events/SceneDeleted.cs b/Hypercube.Shared/Scenes/Events/SceneDeleted.cs index b9bc551..5d92020 100644 --- a/Hypercube.Shared/Scenes/Events/SceneDeleted.cs +++ b/Hypercube.Shared/Scenes/Events/SceneDeleted.cs @@ -1,4 +1,4 @@ -using Hypercube.Shared.EventBus.Events.Events; +using Hypercube.Shared.EventBus.Events; namespace Hypercube.Shared.Scenes.Events; diff --git a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs index 8f02788..3af5229 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRaiseTests.cs @@ -1,6 +1,5 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.UnitTests.EventBus; diff --git a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs index bc67e3f..babd170 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefClassTests.cs @@ -1,6 +1,6 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; -using EventArgs = Hypercube.Shared.EventBus.Events.Events.EventArgs; +using EventArgs = Hypercube.Shared.EventBus.Events.EventArgs; namespace Hypercube.UnitTests.EventBus; @@ -24,7 +24,7 @@ public static void RefClass() Assert.Pass("All subscribers handled correctly"); } - private sealed class TestEventClass : EventArgs + private sealed class TestEventClass : Shared.EventBus.Events.EventArgs { public int Counter { get; set; } } diff --git a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs index 44039cb..f54d7cf 100644 --- a/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusRefStructTests.cs @@ -1,6 +1,5 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.UnitTests.EventBus; diff --git a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs index 2d88a6a..6d63474 100644 --- a/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs +++ b/Hypercube.UnitTests/EventBus/EventBusUnsubscribeTests.cs @@ -1,6 +1,5 @@ using Hypercube.Shared.EventBus; using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Events; namespace Hypercube.UnitTests.EventBus; From 79e6d6a7aad618ed1a1e62cdc3f6eabbc9aa0dae Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Sat, 13 Jul 2024 03:47:10 +0500 Subject: [PATCH 09/11] shuffle files around --- .../Manager/GlfwWindowManager.Callbacks.cs | 8 ++++++++ Hypercube.Shared/EventBus/EventBus.cs | 15 +++------------ .../EventBus/Events/Handlers/RefHandler.cs | 5 ++++- Hypercube.Shared/EventBus/Events/Unit.cs | 11 ----------- Hypercube.Shared/Utilities/UnitHelper.cs | 13 +++++++++++++ Hypercube.Shared/Utilities/Units/Unit.cs | 8 ++++++++ Hypercube.Shared/Utilities/Units/UnitBox.cs | 15 +++++++++++++++ 7 files changed, 51 insertions(+), 24 deletions(-) delete mode 100644 Hypercube.Shared/EventBus/Events/Unit.cs create mode 100644 Hypercube.Shared/Utilities/UnitHelper.cs create mode 100644 Hypercube.Shared/Utilities/Units/Unit.cs create mode 100644 Hypercube.Shared/Utilities/Units/UnitBox.cs diff --git a/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Callbacks.cs b/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Callbacks.cs index 22e20f1..a11c77b 100644 --- a/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Callbacks.cs +++ b/Hypercube.Client/Graphics/Windows/Manager/GlfwWindowManager.Callbacks.cs @@ -94,4 +94,12 @@ private void OnWindowFocusChanged(Window* window, bool focused) _renderer.OnFocusChanged(registration, focused); } + private void OnWindowFocused(Window* window, bool focused) + { + if (!TryGetWindow(window, out var registration)) + return; + + _renderer.OnFocusChanged(registration, focused); + } + } \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index ad84ac1..28b11cf 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -4,6 +4,8 @@ using Hypercube.Shared.EventBus.Events.Events; using Hypercube.Shared.EventBus.Events.Exceptions; using Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.Utilities; +using Hypercube.Shared.Utilities.Units; namespace Hypercube.Shared.EventBus; @@ -25,7 +27,7 @@ public void Raise(T receiver) where T : IEventArgs public void Raise(IEventArgs eventArgs) { var eventType = eventArgs.GetType(); - ref var unitRef = ref ExtractUnitRef(ref eventArgs, eventType); + ref var unitRef = ref UnitHelper.ExtractUnitRef(ref eventArgs, eventType); ProcessEvent(ref unitRef, eventType); } @@ -126,15 +128,4 @@ private Dictionary GetEventInverseSubscription(IEve return _inverseEventSubscriptions[subscriber] = new Dictionary(); } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ref Unit ExtractUnitRef(ref IEventArgs eventArgs, Type objType) - { - //return ref Unsafe.As(ref eventArgs); - - // Why not only unit? - return ref objType.IsValueType - ? ref Unsafe.As(ref eventArgs).Value - : ref Unsafe.As(ref eventArgs); - } } \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs b/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs index c890a3e..7f7722c 100644 --- a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs +++ b/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs @@ -1,3 +1,6 @@ -namespace Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.Utilities; +using Hypercube.Shared.Utilities.Units; + +namespace Hypercube.Shared.EventBus.Events.Handlers; public delegate void RefHandler(ref Unit ev); \ No newline at end of file diff --git a/Hypercube.Shared/EventBus/Events/Unit.cs b/Hypercube.Shared/EventBus/Events/Unit.cs deleted file mode 100644 index 147a168..0000000 --- a/Hypercube.Shared/EventBus/Events/Unit.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Runtime.InteropServices; - -namespace Hypercube.Shared.EventBus.Events; - -public readonly struct Unit; - -[StructLayout(LayoutKind.Sequential)] -public sealed class UnitBox -{ - public Unit Value; -} \ No newline at end of file diff --git a/Hypercube.Shared/Utilities/UnitHelper.cs b/Hypercube.Shared/Utilities/UnitHelper.cs new file mode 100644 index 0000000..44926be --- /dev/null +++ b/Hypercube.Shared/Utilities/UnitHelper.cs @@ -0,0 +1,13 @@ +using System.Runtime.CompilerServices; +using Hypercube.Shared.Utilities.Units; + +namespace Hypercube.Shared.Utilities; + +public static class UnitHelper +{ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref Unit ExtractUnitRef(ref T eventArgs, Type objType) + { + return ref Unsafe.As(ref eventArgs); + } +} \ No newline at end of file diff --git a/Hypercube.Shared/Utilities/Units/Unit.cs b/Hypercube.Shared/Utilities/Units/Unit.cs new file mode 100644 index 0000000..090528e --- /dev/null +++ b/Hypercube.Shared/Utilities/Units/Unit.cs @@ -0,0 +1,8 @@ +using System.Runtime.CompilerServices; + +namespace Hypercube.Shared.Utilities.Units; + +/// +/// Whenever you see this struct it some other object, it should be resolved using Unsafe.As() +/// +public readonly struct Unit; \ No newline at end of file diff --git a/Hypercube.Shared/Utilities/Units/UnitBox.cs b/Hypercube.Shared/Utilities/Units/UnitBox.cs new file mode 100644 index 0000000..1b64f2f --- /dev/null +++ b/Hypercube.Shared/Utilities/Units/UnitBox.cs @@ -0,0 +1,15 @@ +using System.Runtime.InteropServices; + +namespace Hypercube.Shared.Utilities.Units; + +/// +/// Whenever you see this it is some other object, it should be resolved using Unsafe.As() +/// +/// +/// Should be used whenever we want to pass value by ref +/// > +[StructLayout(LayoutKind.Sequential)] +public sealed class UnitBox +{ + public Unit Value; +} \ No newline at end of file From ac3d91be4d2be18e8d03b8f6b681cabb0069ff54 Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Sat, 13 Jul 2024 03:49:19 +0500 Subject: [PATCH 10/11] resolve conflicts --- Hypercube.Shared/EventBus/EventBus.cs | 7 +++---- Hypercube.Shared/EventBus/Handlers/RefHandler.cs | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Hypercube.Shared/EventBus/EventBus.cs b/Hypercube.Shared/EventBus/EventBus.cs index 28b11cf..00cfba7 100644 --- a/Hypercube.Shared/EventBus/EventBus.cs +++ b/Hypercube.Shared/EventBus/EventBus.cs @@ -1,9 +1,8 @@ using System.Runtime.CompilerServices; +using Hypercube.Shared.EventBus.Broadcast; using Hypercube.Shared.EventBus.Events; -using Hypercube.Shared.EventBus.Events.Broadcast; -using Hypercube.Shared.EventBus.Events.Events; -using Hypercube.Shared.EventBus.Events.Exceptions; -using Hypercube.Shared.EventBus.Events.Handlers; +using Hypercube.Shared.EventBus.Exceptions; +using Hypercube.Shared.EventBus.Handlers; using Hypercube.Shared.Utilities; using Hypercube.Shared.Utilities.Units; diff --git a/Hypercube.Shared/EventBus/Handlers/RefHandler.cs b/Hypercube.Shared/EventBus/Handlers/RefHandler.cs index 22c87fd..7559924 100644 --- a/Hypercube.Shared/EventBus/Handlers/RefHandler.cs +++ b/Hypercube.Shared/EventBus/Handlers/RefHandler.cs @@ -1,3 +1,5 @@ -namespace Hypercube.Shared.EventBus.Handlers; +using Hypercube.Shared.Utilities.Units; + +namespace Hypercube.Shared.EventBus.Handlers; public delegate void RefHandler(ref Unit ev); \ No newline at end of file From 130e2872cca7a211cfea85b9f7f0a2957c2fa2f9 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sat, 13 Jul 2024 08:53:36 +1000 Subject: [PATCH 11/11] Fixed merge shit --- Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs diff --git a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs b/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs deleted file mode 100644 index 7f7722c..0000000 --- a/Hypercube.Shared/EventBus/Events/Handlers/RefHandler.cs +++ /dev/null @@ -1,6 +0,0 @@ -using Hypercube.Shared.Utilities; -using Hypercube.Shared.Utilities.Units; - -namespace Hypercube.Shared.EventBus.Events.Handlers; - -public delegate void RefHandler(ref Unit ev); \ No newline at end of file