Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
Seems to be working. Needs some more love though.
Browse files Browse the repository at this point in the history
  • Loading branch information
andoowhy committed Feb 20, 2017
1 parent cc2dc08 commit 17748c6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
94 changes: 66 additions & 28 deletions EgoEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,69 @@ public static Dictionary<Type, Action> 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<Type> 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<Type>( _unorderedEvents );
foreach( var t in unordered ) { _invokeLookup[t](); }
_userOrderedLastEvents.ForEach( t => _invokeLookup[t]() );
_lastEvents.ForEach( t => _invokeLookup[t]() );
}
public static void AddToFirstEvents<E>() where E : EgoEvent
{
var e = typeof( E );
_userOrderedFirstEvents.Add( e );
}

public static void AddToLastEvents<E>() 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<Type> 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<E>
Expand All @@ -61,15 +96,18 @@ public static class EgoEvents<E>
static List<E> _events = new List<E>();
static List<Action<E>> _handlers = new List<Action<E>>();

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()
{
Expand Down
2 changes: 1 addition & 1 deletion Util/ComponentIDs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down

0 comments on commit 17748c6

Please sign in to comment.