Skip to content

Commit 1bbd3d7

Browse files
committed
rework and add GetReflectionEvent
1 parent 1d36967 commit 1bbd3d7

5 files changed

+41
-48
lines changed

Scellecs.Morpeh/Event.cs

+8-13
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ namespace Scellecs.Morpeh
1414
[Il2CppSetOption(Option.NullChecks, false)]
1515
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
1616
[Il2CppSetOption(Option.DivideByZeroChecks, false)]
17-
public class Event<TData> : IEventInternal where TData : struct, IEventData
17+
public class Event<TData> : EventBase where TData : struct, IEventData
1818
{
19-
private readonly EventRegistry _registry;
20-
2119
[PublicAPI] public FastList<TData> BatchedChanges { get; } = new FastList<TData>();
2220
[PublicAPI] public FastList<TData> ScheduledChanges { get; } = new FastList<TData>();
2321

@@ -26,19 +24,14 @@ public class Event<TData> : IEventInternal where TData : struct, IEventData
2624

2725
internal event Action<FastList<TData>> Callback;
2826

29-
internal Event(EventRegistry registry)
30-
{
31-
_registry = registry;
32-
}
33-
3427
[PublicAPI]
3528
public void NextFrame(TData data)
3629
{
3730
ScheduledChanges.Add(data);
3831

3932
if (!IsPublished && !IsScheduled)
4033
{
41-
_registry.DispatchedEvents.Add(this);
34+
registry.DispatchedEvents.Add(this);
4235
}
4336

4437
IsScheduled = true;
@@ -69,7 +62,7 @@ public void Dispose()
6962
}
7063
}
7164

72-
void IEventInternal.OnFrameEnd()
65+
internal sealed override void OnFrameEnd()
7366
{
7467
if (IsPublished)
7568
{
@@ -90,7 +83,7 @@ void IEventInternal.OnFrameEnd()
9083
BatchedChanges.AddListRange(ScheduledChanges);
9184
ScheduledChanges.Clear();
9285

93-
_registry.DispatchedEvents.Add(this);
86+
registry.DispatchedEvents.Add(this);
9487
}
9588
}
9689

@@ -117,8 +110,10 @@ private void TryCatchInvokeCallback()
117110
}
118111
}
119112

120-
public interface IEventInternal
113+
public abstract class EventBase
121114
{
122-
void OnFrameEnd();
115+
internal EventRegistry registry;
116+
117+
internal abstract void OnFrameEnd();
123118
}
124119
}

Scellecs.Morpeh/EventIdentifier.cs

-24
This file was deleted.

Scellecs.Morpeh/EventIdentifier.cs.meta

-3
This file was deleted.

Scellecs.Morpeh/EventRegistry.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
13
using Scellecs.Morpeh.Collections;
24

35
namespace Scellecs.Morpeh
46
{
57
internal class EventRegistry
68
{
7-
internal readonly IntHashMap<IEventInternal> RegisteredEvents = new IntHashMap<IEventInternal>();
9+
internal readonly Dictionary<Type, EventBase> RegisteredEvents = new Dictionary<Type, EventBase>();
810

9-
internal FastList<IEventInternal> DispatchedEvents = new FastList<IEventInternal>();
10-
internal FastList<IEventInternal> ExecutingEvents = new FastList<IEventInternal>();
11+
internal FastList<EventBase> DispatchedEvents = new FastList<EventBase>();
12+
internal FastList<EventBase> ExecutingEvents = new FastList<EventBase>();
1113
}
1214
}

Scellecs.Morpeh/EventWorldExtensions.cs

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using JetBrains.Annotations;
23
using Scellecs.Morpeh.Collections;
34

@@ -27,17 +28,39 @@ internal static void CleanupEventRegistry(World world)
2728
public static Event<TData> GetEvent<TData>(this World world)
2829
where TData : struct, IEventData
2930
{
30-
var eventIdentifier = EventIdentifier<TData>.identifier;
31+
var type = typeof(TData);
3132
var registry = Registries.GetValueByKey(world.identifier);
3233

33-
if (!registry.RegisteredEvents.TryGetValue(eventIdentifier, out var registeredEvent))
34+
if (registry.RegisteredEvents.TryGetValue(type, out var registeredEvent))
3435
{
35-
registeredEvent = new Event<TData>(registry);
36-
37-
registry.RegisteredEvents.Add(eventIdentifier, registeredEvent, out _);
36+
return (Event<TData>) registeredEvent;
3837
}
3938

39+
registeredEvent = new Event<TData>();
40+
registeredEvent.registry = registry;
41+
42+
registry.RegisteredEvents.Add(type, registeredEvent);
43+
4044
return (Event<TData>) registeredEvent;
4145
}
46+
47+
[PublicAPI]
48+
public static EventBase GetReflectionEvent(this World world, Type type)
49+
{
50+
var registry = Registries.GetValueByKey(world.identifier);
51+
52+
if (registry.RegisteredEvents.TryGetValue(type, out var registeredEvent))
53+
{
54+
return registeredEvent;
55+
}
56+
57+
var constructedType = typeof(Event<>).MakeGenericType(type);
58+
registeredEvent = (EventBase) Activator.CreateInstance(constructedType, true);
59+
registeredEvent.registry = registry;
60+
61+
registry.RegisteredEvents.Add(type, registeredEvent);
62+
63+
return registeredEvent;
64+
}
4265
}
4366
}

0 commit comments

Comments
 (0)