From 17748c6c5951f095d221a4b4daaac3201c189540 Mon Sep 17 00:00:00 2001 From: Andrew Macdonald Date: Sun, 19 Feb 2017 21:47:48 -0500 Subject: [PATCH] Seems to be working. Needs some more love though. --- EgoEvents.cs | 94 +++++++++++++++++++++++++++++++------------- Util/ComponentIDs.cs | 2 +- 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/EgoEvents.cs b/EgoEvents.cs index 4b4aaed..b418607 100644 --- a/EgoEvents.cs +++ b/EgoEvents.cs @@ -25,34 +25,69 @@ public static Dictionary invokeLookup public static void Start() { - _firstEvents.Add( typeof( AddedGameObject ) ); - _lastEvents.Add( typeof( DestroyedGameObject ) ); + foreach( var assembly in AppDomain.CurrentDomain.GetAssemblies() ) + { + foreach( var type in assembly.GetTypes() ) + { + if( type.IsSubclassOf( typeof( EgoEvent ) ) && !type.IsAbstract && !type.IsGenericType ) + { + MakeEventInvoke( type ); + } + } + } + + _firstEvents.Add( typeof( AddedGameObject ) ); + _unorderedEvents.Remove( typeof( AddedGameObject ) ); + _lastEvents.Add( typeof( DestroyedGameObject ) ); + _unorderedEvents.Remove( typeof( DestroyedGameObject ) ); + ComponentIDs.componentTypes.ForEach( componentType => { MakeComponentEventInvoke( componentType, typeof( AddedComponent<> ), ref _firstEvents ); MakeComponentEventInvoke( componentType, typeof( DestroyedComponent<> ), ref _lastEvents ); } ); - } - static void MakeComponentEventInvoke( Type eventType, Type genericComponentEventType, ref List eventList ) - { - var componentEventType = genericComponentEventType.MakeGenericType( eventType ); - var fullEventType = typeof( EgoEvents<> ).MakeGenericType( componentEventType ); - if( fullEventType.IsAbstract ) { return; } - fullEventType.TypeInitializer.Invoke( null ); - eventList.Add( componentEventType ); + _unorderedEvents.ExceptWith( _firstEvents ); + _unorderedEvents.ExceptWith( _userOrderedFirstEvents ); + _unorderedEvents.ExceptWith( _userOrderedLastEvents ); + _unorderedEvents.ExceptWith( _lastEvents ); } - public static void Invoke() - { - _firstEvents.ForEach( t => _invokeLookup[t]() ); - _userOrderedFirstEvents.ForEach( t => _invokeLookup[t]() ); - var unordered = new HashSet( _unorderedEvents ); - foreach( var t in unordered ) { _invokeLookup[t](); } - _userOrderedLastEvents.ForEach( t => _invokeLookup[t]() ); - _lastEvents.ForEach( t => _invokeLookup[t]() ); - } + public static void AddToFirstEvents() where E : EgoEvent + { + var e = typeof( E ); + _userOrderedFirstEvents.Add( e ); + } + + public static void AddToLastEvents() where E : EgoEvent + { + var e = typeof( E ); + _userOrderedLastEvents.Add( e ); + } + + static void MakeEventInvoke( Type eventType ) + { + var fullEventType = typeof( EgoEvents<> ).MakeGenericType( eventType ); + fullEventType.GetMethod( "Init", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic ).Invoke( null, null ); + } + + static void MakeComponentEventInvoke( Type eventType, Type genericComponentEventType, ref List eventList ) + { + var componentEventType = genericComponentEventType.MakeGenericType( eventType ); + var fullEventType = typeof( EgoEvents<> ).MakeGenericType( componentEventType ); + fullEventType.GetMethod("Init", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic ).Invoke( null, null ); + eventList.Add( componentEventType ); + } + + public static void Invoke() + { + _firstEvents.ForEach( t => _invokeLookup[ t ]() ); + _userOrderedFirstEvents.ForEach( t => _invokeLookup[ t ]() ); + foreach( var t in _unorderedEvents ) { _invokeLookup[ t ](); } + _userOrderedLastEvents.ForEach( t => _invokeLookup[ t ]() ); + _lastEvents.ForEach( t => _invokeLookup[ t ]() ); + } } public static class EgoEvents @@ -61,15 +96,18 @@ public static class EgoEvents static List _events = new List(); static List> _handlers = new List>(); - static EgoEvents() - { - var e = typeof( E ); - if( !EgoEvents.unorderedEvents.Contains( e ) ) - { - EgoEvents.unorderedEvents.Add( e ); - } - EgoEvents.invokeLookup[e] = Invoke; - } + static EgoEvents() + { + Init(); + } + + static void Init() + { + var e = typeof( E ); + + EgoEvents.invokeLookup[ e ] = Invoke; + EgoEvents.unorderedEvents.Add( e ); + } static void Invoke() { diff --git a/Util/ComponentIDs.cs b/Util/ComponentIDs.cs index b12d71e..83e3f7e 100644 --- a/Util/ComponentIDs.cs +++ b/Util/ComponentIDs.cs @@ -18,7 +18,7 @@ static ComponentIDs() var types = assembly.GetTypes(); foreach( var type in types ) { - if( type.IsSubclassOf( typeof( Component ) ) ) + if( type.IsSubclassOf( typeof( Component ) ) && !type.IsAbstract ) { componentTypes.Add( type ); }