Просмотр исходного кода

Hotfix层增加事件机制,让客户端热更层可以更新事件

tanghai 8 лет назад
Родитель
Сommit
0018078e2d

+ 3 - 1
Unity/Assets/Scripts/Base/Event/EventIdType.cs

@@ -2,7 +2,7 @@
 {
 	public enum EventIdType
 	{
-		InitSceneStart,
+		InitSceneStart = 0,
 
 		BehaviorTreeRunTreeEvent,
 		BehaviorTreeOpenEditor,
@@ -24,5 +24,7 @@
 		
 		LoadingBegin,
 		LoadingFinish,
+
+		MaxModelEvent = 10000,
 	}
 }

+ 0 - 2
Unity/Assets/Scripts/Init.cs

@@ -84,8 +84,6 @@ namespace Model
 
 				// 进入热更新层
 				this.start.Run();
-
-				Game.EventSystem.Run(EventIdType.InitSceneStart);
 			}
 			catch (Exception e)
 			{

+ 7 - 0
Unity/Hotfix/Base/Event/EventIdType.cs

@@ -0,0 +1,7 @@
+namespace Hotfix
+{
+	public enum EventIdType
+	{
+		InitSceneStart = 10001,
+	}
+}

+ 107 - 0
Unity/Hotfix/Base/Object/EventSystem.cs

@@ -34,6 +34,8 @@ namespace Hotfix
 	{
 		private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
 
+		private readonly Dictionary<EventIdType, List<object>> allEvents = new Dictionary<EventIdType, List<object>>();
+
 		private Queue<Disposer> updates = new Queue<Disposer>();
 		private Queue<Disposer> updates2 = new Queue<Disposer>();
 
@@ -71,6 +73,23 @@ namespace Hotfix
 				this.disposerEvents[objectSystem.Type()] = objectSystem;
 			}
 
+			allEvents.Clear();
+			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);
+				}
+			}
+
 			this.Load();
 		}
 
@@ -324,5 +343,93 @@ namespace Hotfix
 
 			ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
 		}
+
+
+		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());
+				}
+			}
+		}
 	}
 }

+ 2 - 0
Unity/Hotfix/Init.cs

@@ -11,6 +11,8 @@ namespace Hotfix
 			{
 				Hotfix.Scene.ModelScene = Game.Scene;
 				Hotfix.Scene.AddComponent<UIComponent>();
+
+				Hotfix.EventSystem.Run(EventIdType.InitSceneStart);
 			}
 			catch (Exception e)
 			{

+ 1 - 0
Unity/Hotfix/Unity.Hotfix.csproj

@@ -49,6 +49,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Base\Event\EventIdType.cs" />
     <Compile Include="Base\Event\IEvent.cs" />
     <Compile Include="Base\Helper\ArrayHelper.cs" />
     <Compile Include="Base\Helper\AssetBundleHelper.cs" />

+ 4 - 7
Unity/Unity.csproj

@@ -12,15 +12,12 @@
     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
-    <TargetFrameworkProfile>
-    </TargetFrameworkProfile>
-    <CompilerResponseFile>
-    </CompilerResponseFile>
+    <TargetFrameworkProfile></TargetFrameworkProfile>
+    <CompilerResponseFile></CompilerResponseFile>
     <UnityProjectType>Game:1</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
     <UnityVersion>2017.1.1p4</UnityVersion>
-    <RootNamespace>
-    </RootNamespace>
+    <RootNamespace></RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -677,4 +674,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>