Quellcode durchsuchen

修复server编译的问题

tanghai vor 8 Jahren
Ursprung
Commit
4d424dfa70
2 geänderte Dateien mit 138 neuen und 2 gelöschten Zeilen
  1. 138 0
      Server/Model/Component/EventComponent.cs
  2. 0 2
      Server/Model/Server.Model.csproj

+ 138 - 0
Server/Model/Component/EventComponent.cs

@@ -0,0 +1,138 @@
+using System;
+using System.Collections.Generic;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+
+	public class EventComponent : Component
+	{
+		private Dictionary<EventIdType, List<object>> allEvents;
+
+		public void Awake()
+		{
+			this.Load();
+		}
+
+		public void Load()
+		{
+			this.allEvents = new Dictionary<EventIdType, List<object>>();
+
+			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<object>());
+					}
+					this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
+				}
+			}
+		}
+
+		public void Run(EventIdType type)
+		{
+			List<object> 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<A>(EventIdType type, A a)
+		{
+			List<object> iEvents;
+			if (!this.allEvents.TryGetValue(type, out iEvents))
+			{
+				return;
+			}
+
+			foreach (object obj in iEvents)
+			{
+				try
+				{
+					IEvent<A> iEvent = (IEvent<A>)obj;
+					iEvent.Run(a);
+				}
+				catch (Exception err)
+				{
+					Log.Error(err.ToString());
+				}
+			}
+		}
+
+		public void Run<A, B>(EventIdType type, A a, B b)
+		{
+			List<object> iEvents;
+			if (!this.allEvents.TryGetValue(type, out iEvents))
+			{
+				return;
+			}
+
+			foreach (object obj in iEvents)
+			{
+				try
+				{
+					IEvent<A, B> iEvent = (IEvent<A, B>)obj;
+					iEvent.Run(a, b);
+				}
+				catch (Exception err)
+				{
+					Log.Error(err.ToString());
+				}
+			}
+		}
+
+		public void Run<A, B, C>(EventIdType type, A a, B b, C c)
+		{
+			List<object> iEvents;
+			if (!this.allEvents.TryGetValue(type, out iEvents))
+			{
+				return;
+			}
+
+			foreach (object obj in iEvents)
+			{
+				try
+				{
+					IEvent<A, B, C> iEvent = (IEvent<A, B, C>)obj;
+					iEvent.Run(a, b, c);
+				}
+				catch (Exception err)
+				{
+					Log.Error(err.ToString());
+				}
+			}
+		}
+	}
+}

+ 0 - 2
Server/Model/Server.Model.csproj

@@ -39,7 +39,6 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ObjectEventAttribute.cs" Link="Base\Object\ObjectEventAttribute.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ObjectPool.cs" Link="Base\Object\ObjectPool.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Component\ConfigComponent.cs" Link="Component\ConfigComponent.cs" />
-    <Compile Include="..\..\Unity\Assets\Scripts\Component\EventComponent.cs" Link="Component\EventComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Component\NetworkComponent.cs" Link="Component\NetworkComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Component\TimerComponent.cs" Link="Component\TimerComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\Opcode.cs" Link="Entity\Message\Opcode.cs" />
@@ -64,5 +63,4 @@
     <ProjectReference Include="..\ThirdParty\MongodbDriver\DotNetCoreDriver\MongoDB.Driver.Core\MongoDB.Driver.Core.csproj" />
     <ProjectReference Include="..\ThirdParty\MongodbDriver\DotNetCoreDriver\MongoDB.Driver\MongoDB.Driver.csproj" />
   </ItemGroup>
-
 </Project>