SharpLibEar/Manager.cs
author StephaneLenclud
Wed, 17 Aug 2016 13:41:26 +0200
changeset 236 6ba20e02d04f
parent 234 0c75dec19d39
child 237 1a1c2ae3a29c
permissions -rw-r--r--
Updating HarmonyHub to v0.3.0.
Adding untested Harmony command action.
     1 //
     2 using System;
     3 using System.Collections.Generic;
     4 using System.ComponentModel;
     5 using System.Linq;
     6 using System.Reflection;
     7 using System.Runtime.Serialization;
     8 using SharpLib.Utils;
     9 
    10 
    11 namespace SharpLib.Ear
    12 {
    13     /// <summary>
    14     /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. 
    15     /// Users can implement their own events and actions.
    16     /// </summary>
    17     [DataContract]
    18     [KnownType("DerivedTypes")]
    19     public class Manager
    20     {
    21         /// <summary>
    22         /// Our events instances.
    23         /// </summary>
    24         [DataMember]
    25         public List<Event> Events;
    26 
    27         /// <summary>
    28         /// Constructor
    29         /// </summary>
    30         public Manager()
    31         {
    32             Init();
    33         }
    34 
    35         /// <summary>
    36         /// Executes after internalization took place.
    37         /// </summary>
    38         public void Init()
    39         {
    40             if (Events == null)
    41             {
    42                 Events = new List<Event>();
    43             }
    44             
    45         }
    46 
    47         /// <summary>
    48         /// Trigger the given event.
    49         /// </summary>
    50         /// <param name="aEventType"></param>
    51         public void TriggerEvent<T>() where T: class
    52         {
    53             //Only trigger enabled events matching the desired type
    54             foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
    55             {
    56                 e.Trigger();
    57             }
    58         }
    59 
    60 
    61         /// <summary>
    62         /// Remove the specified action from the event it belongs too.
    63         /// </summary>
    64         /// <param name="aAction"></param>
    65         public void RemoveAction(Action aAction)
    66         {
    67             foreach (Event e in Events)
    68             {
    69                 if (e.Actions.Remove(aAction))
    70                 {
    71                     //We removed our action, we are done here.
    72                     return;
    73                 }
    74             }
    75         }
    76 
    77         /// <summary>
    78         /// Allow extending our data contract.
    79         /// See KnownType above.
    80         /// </summary>
    81         /// <returns></returns>
    82         private static IEnumerable<Type> DerivedTypes()
    83         {
    84             return SharpLib.Utils.Reflection.GetDerivedTypes<Manager>();
    85         }
    86     }
    87 }