diff --git a/Server/Model/Component/EventComponent.cs b/Server/Model/Component/EventComponent.cs new file mode 100644 index 0000000000000000000000000000000000000000..bcecc1904892ce9c5c804436da3f3808a3208ac1 --- /dev/null +++ b/Server/Model/Component/EventComponent.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; + +namespace Model +{ + [ObjectEvent] + public class EventComponentEvent : ObjectEvent, IAwake, ILoad + { + public void Awake() + { + this.Get().Awake(); + } + + public void Load() + { + this.Get().Load(); + } + } + + public class EventComponent : Component + { + private Dictionary> allEvents; + + public void Awake() + { + this.Load(); + } + + public void Load() + { + this.allEvents = new Dictionary>(); + + Type[] types = DllHelper.GetMonoTypes(); + foreach (Type type in types) + { + object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); + + foreach (object attr in attrs) + { + EventAttribute aEventAttribute = (EventAttribute)attr; + object obj = Activator.CreateInstance(type); + if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type)) + { + this.allEvents.Add((EventIdType)aEventAttribute.Type, new List()); + } + this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj); + } + } + } + + public void Run(EventIdType type) + { + List iEvents; + if (!this.allEvents.TryGetValue(type, out iEvents)) + { + return; + } + foreach (object obj in iEvents) + { + try + { + IEvent iEvent = (IEvent)obj; + iEvent.Run(); + } + catch (Exception e) + { + Log.Error(e.ToString()); + } + } + } + + public void Run(EventIdType type, A a) + { + List iEvents; + if (!this.allEvents.TryGetValue(type, out iEvents)) + { + return; + } + + foreach (object obj in iEvents) + { + try + { + IEvent iEvent = (IEvent)obj; + iEvent.Run(a); + } + catch (Exception err) + { + Log.Error(err.ToString()); + } + } + } + + public void Run(EventIdType type, A a, B b) + { + List iEvents; + if (!this.allEvents.TryGetValue(type, out iEvents)) + { + return; + } + + foreach (object obj in iEvents) + { + try + { + IEvent iEvent = (IEvent)obj; + iEvent.Run(a, b); + } + catch (Exception err) + { + Log.Error(err.ToString()); + } + } + } + + public void Run(EventIdType type, A a, B b, C c) + { + List iEvents; + if (!this.allEvents.TryGetValue(type, out iEvents)) + { + return; + } + + foreach (object obj in iEvents) + { + try + { + IEvent iEvent = (IEvent)obj; + iEvent.Run(a, b, c); + } + catch (Exception err) + { + Log.Error(err.ToString()); + } + } + } + } +} \ No newline at end of file diff --git a/Server/Model/Server.Model.csproj b/Server/Model/Server.Model.csproj index 16964d9c52a22790fe9877556161b55240bba86e..e886810518791e1ebd1df1d710af885b48d79773 100644 --- a/Server/Model/Server.Model.csproj +++ b/Server/Model/Server.Model.csproj @@ -39,7 +39,6 @@ - @@ -64,5 +63,4 @@ -