diff --git a/README.md b/README.md index 63fea3e..36af46e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Morpeh.Events [![Github license](https://img.shields.io/github/license/codewriter-packages/Morpeh.Events.svg?style=flat-square)](#) [![Unity 2020.1](https://img.shields.io/badge/Unity-2020.1+-2296F3.svg?style=flat-square)](#) ![GitHub package.json version](https://img.shields.io/github/package-json/v/codewriter-packages/Morpeh.Events?style=flat-square) +# Morpeh.Events [![Github license](https://img.shields.io/github/license/codewriter-packages/Morpeh.Events.svg?style=flat-square)](#) [![Unity 2020.1](https://img.shields.io/badge/Unity-2020.3+-2296F3.svg?style=flat-square)](#) ![GitHub package.json version](https://img.shields.io/github/package-json/v/codewriter-packages/Morpeh.Events?style=flat-square) _Events for Morpeh ECS_ ## How to use? diff --git a/Scellecs.Morpeh/Event.cs b/Scellecs.Morpeh/Event.cs index a0e814d..590d361 100644 --- a/Scellecs.Morpeh/Event.cs +++ b/Scellecs.Morpeh/Event.cs @@ -1,9 +1,19 @@ +#if !MORPEH_DEBUG +#define MORPEH_DEBUG_DISABLED +#endif + using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; using JetBrains.Annotations; using Scellecs.Morpeh.Collections; +using Unity.IL2CPP.CompilerServices; namespace Scellecs.Morpeh { + [Il2CppSetOption(Option.NullChecks, false)] + [Il2CppSetOption(Option.ArrayBoundsChecks, false)] + [Il2CppSetOption(Option.DivideByZeroChecks, false)] public class Event : IEventInternal where TData : struct, IEventData { private readonly EventRegistry _registry; @@ -59,11 +69,15 @@ public void Dispose() } } - public void OnFrameEnd() + void IEventInternal.OnFrameEnd() { if (IsPublished) { - Callback?.Invoke(BatchedChanges); + if (Callback != null) + { + TryCatchInvokeCallback(); + ForwardInvokeCallback(); + } IsPublished = false; BatchedChanges.Clear(); @@ -79,6 +93,28 @@ public void OnFrameEnd() _registry.DispatchedEvents.Add(this); } } + + [Conditional("MORPEH_DEBUG_DISABLED")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ForwardInvokeCallback() + { + Callback?.Invoke(BatchedChanges); + } + + [Conditional("MORPEH_DEBUG")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void TryCatchInvokeCallback() + { + try + { + Callback?.Invoke(BatchedChanges); + } + catch (Exception ex) + { + MLogger.LogError($"Can not invoke callback Event<{typeof(TData)}>"); + MLogger.LogException(ex); + } + } } public interface IEventInternal diff --git a/Scellecs.Morpeh/EventWorldExtensions.cs b/Scellecs.Morpeh/EventWorldExtensions.cs index 4b820cc..92051f0 100644 --- a/Scellecs.Morpeh/EventWorldExtensions.cs +++ b/Scellecs.Morpeh/EventWorldExtensions.cs @@ -7,30 +7,28 @@ public static class EventWorldExtensions { private static readonly IntHashMap Registries = new IntHashMap(); - internal static void CleanupEventRegistry(World world) + internal static void SetupEventRegistry(World world) { - var worldIdentifier = world.identifier; + var registry = new EventRegistry(); + + var eventSystemGroup = world.CreateSystemsGroup(); + eventSystemGroup.AddSystem(new ProcessEventsSystem(registry)); + world.AddPluginSystemsGroup(eventSystemGroup); + + Registries.Add(world.identifier, registry, out _); + } - Registries.Remove(worldIdentifier, out _); + internal static void CleanupEventRegistry(World world) + { + Registries.Remove(world.identifier, out _); } [PublicAPI] public static Event GetEvent(this World world) where TData : struct, IEventData { - var worldIdentifier = world.identifier; var eventIdentifier = EventIdentifier.identifier; - - if (!Registries.TryGetValue(worldIdentifier, out var registry)) - { - registry = new EventRegistry(); - - var eventSystemGroup = world.CreateSystemsGroup(); - eventSystemGroup.AddSystem(new ProcessEventsSystem(registry)); - world.AddSystemsGroup(9999, eventSystemGroup); - - Registries.Add(worldIdentifier, registry, out _); - } + var registry = Registries.GetValueByKey(world.identifier); if (!registry.RegisteredEvents.TryGetValue(eventIdentifier, out var registeredEvent)) { diff --git a/Scellecs.Morpeh/EventWorldPlugin.cs b/Scellecs.Morpeh/EventWorldPlugin.cs new file mode 100644 index 0000000..99b37fa --- /dev/null +++ b/Scellecs.Morpeh/EventWorldPlugin.cs @@ -0,0 +1,19 @@ +using UnityEngine.Scripting; + +namespace Scellecs.Morpeh +{ + [Preserve] + internal class EventWorldPlugin : IWorldPlugin + { + [Preserve] + public EventWorldPlugin() + { + } + + [Preserve] + public void Initialize(World world) + { + EventWorldExtensions.SetupEventRegistry(world); + } + } +} \ No newline at end of file diff --git a/Scellecs.Morpeh/EventWorldPlugin.cs.meta b/Scellecs.Morpeh/EventWorldPlugin.cs.meta new file mode 100644 index 0000000..5eb9f4e --- /dev/null +++ b/Scellecs.Morpeh/EventWorldPlugin.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3ea82d8fe0ff480baa2bd1f73c25ecc4 +timeCreated: 1671721331 \ No newline at end of file diff --git a/Scellecs.Morpeh/ProcessEventsSystem.cs b/Scellecs.Morpeh/ProcessEventsSystem.cs index 4c34685..d0ec879 100644 --- a/Scellecs.Morpeh/ProcessEventsSystem.cs +++ b/Scellecs.Morpeh/ProcessEventsSystem.cs @@ -1,7 +1,11 @@ using Scellecs.Morpeh.Collections; +using Unity.IL2CPP.CompilerServices; namespace Scellecs.Morpeh { + [Il2CppSetOption(Option.NullChecks, false)] + [Il2CppSetOption(Option.ArrayBoundsChecks, false)] + [Il2CppSetOption(Option.DivideByZeroChecks, false)] internal sealed class ProcessEventsSystem : ICleanupSystem { private readonly EventRegistry _registry; diff --git a/package.json b/package.json index 8cb022d..072b155 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Morpeh.Events", "description": "Events for Morpeh ECS", "version": "1.0.0", - "unity": "2020.1", + "unity": "2020.3", "license": "MIT", "author": "CodeWriter (https://github.com/orgs/codewriter-packages)", "dependencies": {}