Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Jan 3, 2023
1 parent 1c667ac commit 2bed53a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
40 changes: 38 additions & 2 deletions Scellecs.Morpeh/Event.cs
Original file line number Diff line number Diff line change
@@ -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<TData> : IEventInternal where TData : struct, IEventData
{
private readonly EventRegistry _registry;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
28 changes: 13 additions & 15 deletions Scellecs.Morpeh/EventWorldExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@ public static class EventWorldExtensions
{
private static readonly IntHashMap<EventRegistry> Registries = new IntHashMap<EventRegistry>();

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<TData> GetEvent<TData>(this World world)
where TData : struct, IEventData
{
var worldIdentifier = world.identifier;
var eventIdentifier = EventIdentifier<TData>.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))
{
Expand Down
19 changes: 19 additions & 0 deletions Scellecs.Morpeh/EventWorldPlugin.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 3 additions & 0 deletions Scellecs.Morpeh/EventWorldPlugin.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Scellecs.Morpeh/ProcessEventsSystem.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
Expand Down

0 comments on commit 2bed53a

Please sign in to comment.