Procházet zdrojové kódy

1.修复资源下载进度错误
2.移动服务端部分方法到hotfix层
3.版本提示使用Unity2017.1.3p2,并且打印下载地址

tanghai před 7 roky
rodič
revize
a2bc374400

+ 12 - 7
Server/Hotfix/Module/Actor/ActorComponentSystem.cs

@@ -11,10 +11,8 @@ namespace ETHotfix
 		public override void Awake(ActorComponent self)
 		{
 			self.ActorType = ActorType.Common;
-			self.Queue = new Queue<ActorMessageInfo>();
+			self.Queue.Clear();
 			Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Entity);
-
-			self.HandleAsync();
 		}
 	}
 
@@ -24,13 +22,20 @@ namespace ETHotfix
 		public override void Awake(ActorComponent self, string actorType)
 		{
 			self.ActorType = actorType;
-			self.Queue = new Queue<ActorMessageInfo>();
+			self.Queue.Clear();
 			Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Entity);
+		}
+	}
 
+	[ObjectSystem]
+	public class ActorComponentStartSystem : StartSystem<ActorComponent>
+	{
+		public override void Start(ActorComponent self)
+		{
 			self.HandleAsync();
 		}
 	}
-	
+
 	[ObjectSystem]
 	public class ActorComponentDestroySystem : DestroySystem<ActorComponent>
 	{
@@ -82,6 +87,7 @@ namespace ETHotfix
 
 		public static async void HandleAsync(this ActorComponent self)
 		{
+			ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent<ActorMessageDispatherComponent>();
 			while (true)
 			{
 				if (self.IsDisposed)
@@ -98,8 +104,7 @@ namespace ETHotfix
 					}
 
 					// 根据这个actor的类型分发给相应的ActorHandler处理
-					await Game.Scene.GetComponent<ActorMessageDispatherComponent>().ActorTypeHandle(
-							self.ActorType, info.Session, (Entity)self.Parent, info.Message);
+					await actorMessageDispatherComponent.ActorTypeHandle(self.ActorType, (Entity)self.Parent, info);
 				}
 				catch (Exception e)
 				{

+ 3 - 3
Server/Hotfix/Module/Actor/ActorMessageDispatherComponentSystem.cs

@@ -96,10 +96,10 @@ namespace ETHotfix
 		}
 
 		/// <summary>
-		/// 根据actor的类型分发给不同的ActorHandler处理
+		/// 一个actor收到的所有消息先由其指定的ActorTypeHandle处理
 		/// </summary>
 		public static async Task ActorTypeHandle(
-				this ActorMessageDispatherComponent self, string actorType, Session session, Entity entity, IActorMessage actorMessage)
+				this ActorMessageDispatherComponent self, string actorType, Entity entity, ActorMessageInfo actorMessageInfo)
 		{
 			IActorTypeHandler iActorTypeHandler;
 			if (!self.ActorTypeHandlers.TryGetValue(actorType, out iActorTypeHandler))
@@ -107,7 +107,7 @@ namespace ETHotfix
 				throw new Exception($"not found actortype handler: {actorType}");
 			}
 
-			await iActorTypeHandler.Handle(session, entity, actorMessage);
+			await iActorTypeHandler.Handle(actorMessageInfo.Session, entity, actorMessageInfo.Message);
 		}
 
 		/// <summary>

+ 100 - 0
Server/Hotfix/Module/Message/MessageDispatherComponentSystem.cs

@@ -0,0 +1,100 @@
+using System;
+using System.Collections.Generic;
+
+namespace ETModel
+{
+	[ObjectSystem]
+	public class MessageDispatherComponentAwakeSystem : AwakeSystem<MessageDispatherComponent>
+	{
+		public override void Awake(MessageDispatherComponent self)
+		{
+			self.Load();
+		}
+	}
+
+	[ObjectSystem]
+	public class MessageDispatherComponentLoadSystem : LoadSystem<MessageDispatherComponent>
+	{
+		public override void Load(MessageDispatherComponent self)
+		{
+			self.Load();
+		}
+	}
+
+	/// <summary>
+	/// 消息分发组件
+	/// </summary>
+	public static class MessageDispatherComponentEx
+	{
+		public static void Load(this MessageDispatherComponent self)
+		{
+			self.Handlers.Clear();
+
+			AppType appType = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.AppType;
+
+			Type[] types = DllHelper.GetMonoTypes();
+
+			foreach (Type type in types)
+			{
+				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+
+				MessageHandlerAttribute messageHandlerAttribute = attrs[0] as MessageHandlerAttribute;
+				if (!messageHandlerAttribute.Type.Is(appType))
+				{
+					continue;
+				}
+
+				IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
+				if (iMHandler == null)
+				{
+					Log.Error($"message handle {type.Name} 需要继承 IMHandler");
+					continue;
+				}
+
+				Type messageType = iMHandler.GetMessageType();
+				ushort opcode = Game.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(messageType);
+				if (opcode == 0)
+				{
+					Log.Error($"消息opcode为0: {messageType.Name}");
+					continue;
+				}
+				self.RegisterHandler(opcode, iMHandler);
+			}
+		}
+
+		public static void RegisterHandler(this MessageDispatherComponent self, ushort opcode, IMHandler handler)
+		{
+			if (!self.Handlers.ContainsKey(opcode))
+			{
+				self.Handlers.Add(opcode, new List<IMHandler>());
+			}
+			self.Handlers[opcode].Add(handler);
+		}
+
+		public static void Handle(this MessageDispatherComponent self, Session session, MessageInfo messageInfo)
+		{
+			List<IMHandler> actions;
+			if (!self.Handlers.TryGetValue(messageInfo.Opcode, out actions))
+			{
+				Log.Error($"消息没有处理: {messageInfo.Opcode} {JsonHelper.ToJson(messageInfo.Message)}");
+				return;
+			}
+			
+			foreach (IMHandler ev in actions)
+			{
+				try
+				{
+					ev.Handle(session, messageInfo.Message);
+				}
+				catch (Exception e)
+				{
+					Log.Error(e);
+				}
+			}
+		}
+	}
+}

+ 1 - 1
Server/Model/Module/Actor/ActorComponent.cs

@@ -17,7 +17,7 @@ namespace ETModel
 		public string ActorType;
 
 		// 队列处理消息
-		public Queue<ActorMessageInfo> Queue;
+		public Queue<ActorMessageInfo> Queue = new Queue<ActorMessageInfo>();
 
 		public TaskCompletionSource<ActorMessageInfo> Tcs;
 

+ 2 - 2
Server/Model/Module/DB/DBTaskQueue.cs

@@ -30,13 +30,13 @@ namespace ETModel
 				try
 				{
 					await task.Run();
-
-					task.Dispose();
 				}
 				catch (Exception e)
 				{
 					Log.Error(e);
 				}
+
+				task.Dispose();
 			}
 		}
 	}

+ 2 - 103
Server/Model/Module/Message/MessageDispatherComponent.cs

@@ -1,113 +1,12 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 
 namespace ETModel
 {
-	[ObjectSystem]
-	public class MessageDispatherComponentAwakeSystem : AwakeSystem<MessageDispatherComponent>
-	{
-		public override void Awake(MessageDispatherComponent self)
-		{
-			self.AppType = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.AppType;
-			self.Load();
-		}
-	}
-
-	[ObjectSystem]
-	public class MessageDispatherComponentLoadSystem : LoadSystem<MessageDispatherComponent>
-	{
-		public override void Load(MessageDispatherComponent self)
-		{
-			self.Load();
-		}
-	}
-
 	/// <summary>
 	/// 消息分发组件
 	/// </summary>
 	public class MessageDispatherComponent : Component
 	{
-		public AppType AppType;
-
-		private readonly Dictionary<ushort, List<IMHandler>> handlers = new Dictionary<ushort, List<IMHandler>>();
-		
-		public void Load()
-		{
-			this.handlers.Clear();
-
-			Type[] types = DllHelper.GetMonoTypes();
-
-			foreach (Type type in types)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
-				if (attrs.Length == 0)
-				{
-					continue;
-				}
-
-				MessageHandlerAttribute messageHandlerAttribute = attrs[0] as MessageHandlerAttribute;
-				if (!messageHandlerAttribute.Type.Is(this.AppType))
-				{
-					continue;
-				}
-
-				IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
-				if (iMHandler == null)
-				{
-					Log.Error($"message handle {type.Name} 需要继承 IMHandler");
-					continue;
-				}
-
-				Type messageType = iMHandler.GetMessageType();
-				ushort opcode = this.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(messageType);
-				if (opcode == 0)
-				{
-					Log.Error($"消息opcode为0: {messageType.Name}");
-					continue;
-				}
-				this.RegisterHandler(opcode, iMHandler);
-			}
-		}
-
-		public void RegisterHandler(ushort opcode, IMHandler handler)
-		{
-			if (!this.handlers.ContainsKey(opcode))
-			{
-				this.handlers.Add(opcode, new List<IMHandler>());
-			}
-			this.handlers[opcode].Add(handler);
-		}
-
-		public void Handle(Session session, MessageInfo messageInfo)
-		{
-			List<IMHandler> actions;
-			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
-			{
-				Log.Error($"消息没有处理: {messageInfo.Opcode} {JsonHelper.ToJson(messageInfo.Message)}");
-				return;
-			}
-			
-			foreach (IMHandler ev in actions)
-			{
-				try
-				{
-					ev.Handle(session, messageInfo.Message);
-				}
-				catch (Exception e)
-				{
-					Log.Error(e);
-				}
-			}
-		}
-
-		public override void Dispose()
-		{
-			if (this.IsDisposed)
-			{
-				return;
-			}
-
-			base.Dispose();
-		}
+		public readonly Dictionary<ushort, List<IMHandler>> Handlers = new Dictionary<ushort, List<IMHandler>>();
 	}
 }

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

@@ -12,9 +12,9 @@ namespace ETModel
 		{
 			try
 			{
-				if (Application.unityVersion != "2017.1.0p5")
+				if (Application.unityVersion != "2017.1.3p2")
 				{
-					Log.Warning($"当前版本:{Application.unityVersion}, 最好使用运行指南推荐版本!");
+					Log.Warning($"请使用Unity2017.1.3p2, 下载地址:\n https://beta.unity3d.com/download/744dab055778/UnityDownloadAssistant-2017.1.3p2.exe?_ga=2.42497696.443074145.1521714954-1119432033.1499739574");
 				}
 
 				SynchronizationContext.SetSynchronizationContext(this.contex);

+ 96 - 98
Unity/Assets/Scripts/Module/AssetsBundle/BundleDownloaderComponent.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
-using System.Linq;
 using System.Threading.Tasks;
 
 namespace ETModel
@@ -16,11 +15,11 @@ namespace ETModel
 			self.downloadingBundle = "";
 		}
 	}
-	
+
 	/// <summary>
 	/// 用来对比web端的资源,比较md5,对比下载资源
 	/// </summary>
-	public class BundleDownloaderComponent: Component
+	public class BundleDownloaderComponent : Component
 	{
 		public VersionConfig VersionConfig { get; private set; }
 
@@ -32,134 +31,139 @@ namespace ETModel
 
 		public string downloadingBundle;
 
-		public UnityWebRequestAsync downloadingRequest;
+		public UnityWebRequestAsync webRequest;
 
 		public TaskCompletionSource<bool> Tcs;
 
 		public async Task StartAsync()
 		{
-			using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
+			using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>())
 			{
 				string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
-				Log.Debug(versionUrl);
-				await request.DownloadAsync(versionUrl);
-				this.VersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
+				//Log.Debug(versionUrl);
+				await webRequestAsync.DownloadAsync(versionUrl);
+				this.VersionConfig = JsonHelper.FromJson<VersionConfig>(webRequestAsync.Request.downloadHandler.text);
+				//Log.Debug(JsonHelper.ToJson(this.VersionConfig));
 			}
-			
-			Log.Debug("WebVersion:\n" + JsonHelper.ToJson(this.VersionConfig));
 
+
+			VersionConfig localVersionConfig;
 			// 对比本地的Version.txt
 			string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");
-			if (!File.Exists(versionPath))
+			if (File.Exists(versionPath))
+			{
+				localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
+			}
+			else
 			{
-				foreach (FileVersionInfo versionInfo in this.VersionConfig.FileInfoDict.Values)
+				versionPath = Path.Combine(PathHelper.AppResPath, "Version.txt");
+				using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
 				{
-					if(versionInfo.File == "Version.txt")
-					{
-						continue;
-					}
-					this.bundles.Enqueue(versionInfo.File);
-					this.TotalSize += versionInfo.Size;
+					await request.DownloadAsync(versionPath);
+					localVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
 				}
 			}
-			else
+
+
+			// 先删除服务器端没有的ab
+			foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values)
 			{
+				if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File))
+				{
+					continue;
+				}
+				string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File);
+				File.Delete(abPath);
+			}
 
-				VersionConfig localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
-				Log.Debug("LocalVersion:\n" + JsonHelper.ToJson(localVersionConfig));
-				// 先删除服务器端没有的ab
-				foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values)
+			// 再下载
+			foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values)
+			{
+				FileVersionInfo localVersionInfo;
+				if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo))
 				{
-					if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File))
+					if (fileVersionInfo.MD5 == localVersionInfo.MD5)
 					{
 						continue;
 					}
-					string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File);
-					File.Delete(abPath);
 				}
 
-				// 再下载
-				foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values)
+				if (fileVersionInfo.File == "Version.txt")
 				{
-					FileVersionInfo localVersionInfo;
-					if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo))
-					{
-						if (fileVersionInfo.MD5 == localVersionInfo.MD5)
-						{
-							continue;
-						}
-					}
-
-					if(fileVersionInfo.File == "Version.txt")
-					{
-						continue;
-					}
-					
-					this.bundles.Enqueue(fileVersionInfo.File);
-					this.TotalSize += fileVersionInfo.Size;
+					continue;
 				}
+
+				this.bundles.Enqueue(fileVersionInfo.File);
+				this.TotalSize += fileVersionInfo.Size;
 			}
-			
+
 			if (this.bundles.Count == 0)
 			{
 				return;
 			}
 
-			Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}");
+			//Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}");
 			await this.WaitAsync();
 		}
 
 		private async void UpdateAsync()
 		{
-			while (true)
+			try
 			{
-				if (this.bundles.Count == 0)
-				{
-					break;
-				}
-				
-				this.downloadingBundle = this.bundles.Dequeue();
-				
 				while (true)
 				{
-					try
+					if (this.bundles.Count == 0)
 					{
-						using (this.downloadingRequest = ComponentFactory.Create<UnityWebRequestAsync>())
-						{
-							await this.downloadingRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
-							byte[] data = this.downloadingRequest.Request.downloadHandler.data;
+						break;
+					}
 
-							string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
-							if (!Directory.Exists(Path.GetDirectoryName(path)))
-							{
-								Directory.CreateDirectory(Path.GetDirectoryName(path));
-							}
-							using (FileStream fs = new FileStream(path, FileMode.Create))
+					this.downloadingBundle = this.bundles.Dequeue();
+
+					while (true)
+					{
+						try
+						{
+							using (this.webRequest = ComponentFactory.Create<UnityWebRequestAsync>())
 							{
-								fs.Write(data, 0, data.Length);
+								await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
+								byte[] data = this.webRequest.Request.downloadHandler.data;
+
+								string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
+								if (!Directory.Exists(Path.GetDirectoryName(path)))
+								{
+									Directory.CreateDirectory(Path.GetDirectoryName(path));
+								}
+								using (FileStream fs = new FileStream(path, FileMode.Create))
+								{
+									fs.Write(data, 0, data.Length);
+								}
 							}
 						}
+						catch (Exception e)
+						{
+							Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
+							continue;
+						}
+
+						break;
 					}
-					catch(Exception e)
-					{
-						Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
-						continue;
-					}
+					this.downloadedBundles.Add(this.downloadingBundle);
+					this.downloadingBundle = "";
+					this.webRequest = null;
+				}
 
-					break;
+				using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
+				using (StreamWriter sw = new StreamWriter(fs))
+				{
+					sw.Write(JsonHelper.ToJson(this.VersionConfig));
 				}
-				this.downloadedBundles.Add(this.downloadingBundle);
-				this.downloadingBundle = "";
-				this.downloadingRequest = null;
-			}
 
-			using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
-			using (StreamWriter sw = new StreamWriter(fs))
+				this.Tcs?.SetResult(true);
+			}
+			catch (Exception e)
 			{
-				sw.Write(JsonHelper.ToJson(this.VersionConfig));
+				Log.Error(e);
 			}
-
-			this.Tcs?.SetResult(true);
 		}
 
 		public int Progress
@@ -170,17 +174,23 @@ namespace ETModel
 				{
 					return 0;
 				}
+
+				if (this.TotalSize == 0)
+				{
+					return 0;
+				}
+
 				long alreadyDownloadBytes = 0;
 				foreach (string downloadedBundle in this.downloadedBundles)
 				{
 					long size = this.VersionConfig.FileInfoDict[downloadedBundle].Size;
 					alreadyDownloadBytes += size;
 				}
-				if (this.downloadingRequest != null)
+				if (this.webRequest != null)
 				{
-					alreadyDownloadBytes += (long)this.downloadingRequest.Request.downloadedBytes;
+					alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
 				}
-				return (int)(alreadyDownloadBytes * 100f / this.VersionConfig.TotalSize);
+				return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
 			}
 		}
 
@@ -190,24 +200,12 @@ namespace ETModel
 			{
 				return Task.FromResult(true);
 			}
-			
+
 			this.Tcs = new TaskCompletionSource<bool>();
 
 			UpdateAsync();
-			
-			return this.Tcs.Task;
-		}
 
-		public override void Dispose()
-		{
-			if (this.IsDisposed)
-			{
-				return;
-			}
-
-			base.Dispose();
-
-			this.Entity?.RemoveComponent<BundleDownloaderComponent>();
+			return this.Tcs.Task;
 		}
 	}
 }