Эх сурвалжийг харах

1.修复ResourcesComponent加载顺序问题,上次漏提交了
2.为了全热更,Opcode改成ushort类型

tanghai 8 жил өмнө
parent
commit
a3a8d272d0
24 өөрчлөгдсөн 193 нэмэгдсэн , 149 устгасан
  1. 1 1
      Client-Server.sln.DotSettings.user
  2. 1 1
      Server/Hotfix/Module/Network/InnerMessageDispatcher.cs
  3. 1 1
      Server/Hotfix/Module/Network/OuterMessageDispatcher.cs
  4. 2 2
      Server/Model/Base/Message/MessageAttribute.cs
  5. 4 4
      Server/Model/Component/OpcodeTypeComponent.cs
  6. 1 0
      Server/Model/Server.Model.csproj
  7. 2 2
      Unity/Assets/Editor/Proto2CsEditor/Proto2CSEditor.cs
  8. 10 5
      Unity/Assets/Scripts/Base/Message/AMessage.cs
  9. 1 1
      Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs
  10. 1 1
      Unity/Assets/Scripts/Base/Message/IMessageDispatcher.cs
  11. 2 2
      Unity/Assets/Scripts/Base/Message/MessageAttribute.cs
  12. 2 2
      Unity/Assets/Scripts/Base/Message/MessageInfo.cs
  13. 11 0
      Unity/Assets/Scripts/Base/Message/Opcode.cs
  14. 12 0
      Unity/Assets/Scripts/Base/Message/Opcode.cs.meta
  15. 1 1
      Unity/Assets/Scripts/Component/ClientFrameComponent.cs
  16. 8 8
      Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
  17. 3 3
      Unity/Assets/Scripts/Component/OpcodeTypeComponent.cs
  18. 57 43
      Unity/Assets/Scripts/Component/ResourcesComponent.cs
  19. 57 57
      Unity/Assets/Scripts/Entity/Message/Opcode.cs
  20. 5 8
      Unity/Assets/Scripts/Entity/Session.cs
  21. 1 1
      Unity/Assets/Scripts/Handler/Actor_CreateUnitsHandler.cs
  22. 1 1
      Unity/Assets/Scripts/Handler/Actor_TestHandler.cs
  23. 1 1
      Unity/Assets/Scripts/Handler/Frame_ClickMapHandler.cs
  24. 8 4
      Unity/Unity.csproj

+ 1 - 1
Client-Server.sln.DotSettings.user

@@ -9,7 +9,7 @@
 	<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">VISIBLE_FILES</s:String>
 	<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Unity/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Unity"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;DISABLED&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;IMPLICIT_EXCEPT_SIMPLE_TYPES&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSMakeAutoPropertyGetOnly&gt;True&lt;/CSMakeAutoPropertyGetOnly&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="True" ArrangeBraces="True" ArrangeAttributes="True" ArrangeArgumentsStyle="True" ArrangeCodeBodyStyle="True" /&gt;&lt;/Profile&gt;</s:String>
 	<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">Unity</s:String>
-	<s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/RefactoringWizardWindow/Location/@EntryValue">247,-158</s:String>
+	<s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/RefactoringWizardWindow/Location/@EntryValue">-241,-80</s:String>
 	<s:Int64 x:Key="/Default/Environment/SearchAndNavigation/DefaultOccurrencesGroupingIndex/@EntryValue">0</s:Int64>
 	<s:String x:Key="/Default/Profiling/Configurations/=1/@EntryIndexedValue">&lt;data&gt;&lt;HostParameters type="LocalHostParameters" /&gt;&lt;Argument type="StandaloneArgument"&gt;&lt;Arguments IsNull="False"&gt;&lt;/Arguments&gt;&lt;FileName IsNull="False"&gt;&lt;/FileName&gt;&lt;WorkingDirectory IsNull="False"&gt;&lt;/WorkingDirectory&gt;&lt;Scope&gt;&lt;ProcessFilters /&gt;&lt;/Scope&gt;&lt;/Argument&gt;&lt;Info type="TimelineInfo" /&gt;&lt;HostOptions type="HostOptions"&gt;&lt;HostTempPath IsNull="False"&gt;&lt;/HostTempPath&gt;&lt;/HostOptions&gt;&lt;/data&gt;</s:String>
 	</wpf:ResourceDictionary>

+ 1 - 1
Server/Hotfix/Module/Network/InnerMessageDispatcher.cs

@@ -5,7 +5,7 @@ namespace Hotfix
 {
 	public class InnerMessageDispatcher: IMessageDispatcher
 	{
-		public void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message)
+		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// 收到actor rpc request
 			if (message is ActorRpcRequest actorRpcRequest)

+ 1 - 1
Server/Hotfix/Module/Network/OuterMessageDispatcher.cs

@@ -5,7 +5,7 @@ namespace Hotfix
 {
 	public class OuterMessageDispatcher: IMessageDispatcher
 	{
-		public async void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message)
+		public async void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// gate session收到actor消息直接转发给actor自己去处理
 			if (message is AActorMessage)

+ 2 - 2
Server/Model/Base/Message/MessageAttribute.cs

@@ -4,9 +4,9 @@ namespace Model
 {
 	public class MessageAttribute: Attribute
 	{
-		public Opcode Opcode { get; }
+		public ushort Opcode { get; }
 
-		public MessageAttribute(Opcode opcode)
+		public MessageAttribute(ushort opcode)
 		{
 			this.Opcode = opcode;
 		}

+ 4 - 4
Server/Model/Component/OpcodeTypeComponent.cs

@@ -19,7 +19,7 @@ namespace Model
 	
 	public class OpcodeTypeComponent : Component
 	{
-		private Dictionary<Opcode, Type> opcodeType { get; set; }
+		private Dictionary<ushort, Type> opcodeType { get; set; }
 		private Dictionary<Type, MessageAttribute> messageOpcode { get; set; }
 
 		public void Awake()
@@ -29,7 +29,7 @@ namespace Model
 
 		public void Load()
 		{
-			this.opcodeType = new Dictionary<Opcode, Type>();
+			this.opcodeType = new Dictionary<ushort, Type>();
 			this.messageOpcode = new Dictionary<Type, MessageAttribute>();
 
 			Type[] types = DllHelper.GetMonoTypes();
@@ -47,7 +47,7 @@ namespace Model
 			}
 		}
 
-		public Opcode GetOpcode(Type type)
+		public ushort GetOpcode(Type type)
 		{
 			if (!this.messageOpcode.TryGetValue(type, out MessageAttribute messageAttribute))
 			{
@@ -56,7 +56,7 @@ namespace Model
 			return messageAttribute.Opcode;
 		}
 
-		public Type GetType(Opcode opcode)
+		public Type GetType(ushort opcode)
 		{
 			if (!this.opcodeType.TryGetValue(opcode, out Type messageType))
 			{

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

@@ -44,6 +44,7 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\IMessagePacker.cs" Link="Module\Network\IMessagePacker.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\MessageInfo.cs" Link="Base\Message\MessageInfo.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\MongoPacker.cs" Link="Module\Network\MongoPacker.cs" />
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\Opcode.cs" Link="Base\Message\Opcode.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\ProtobufPacker.cs" Link="Module\Network\ProtobufPacker.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\RpcException.cs" Link="Module\Network\RpcException.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\MultiMap.cs" Link="Base\MultiMap.cs" />

+ 2 - 2
Unity/Assets/Editor/Proto2CsEditor/Proto2CSEditor.cs

@@ -163,11 +163,11 @@ namespace MyEditor
 			StringBuilder sb = new StringBuilder();
 			sb.AppendLine("namespace Model");
 			sb.AppendLine("{");
-			sb.AppendLine("\tpublic enum Opcode: ushort");
+			sb.AppendLine("\tpublic static partial class Opcode");
 			sb.AppendLine("\t{");
 			foreach (OpcodeInfo info in msgOpcode)
 			{
-				sb.AppendLine($"\t\t{info.Name} = {info.Opcode},");
+				sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
 			}
 			sb.AppendLine("\t}");
 			sb.AppendLine("}");

+ 10 - 5
Unity/Assets/Scripts/Base/Message/AMessage.cs

@@ -5,9 +5,12 @@ using ProtoBuf;
 namespace Model
 {
 	[ProtoContract]
+	[ProtoInclude(Opcode.AActorMessage, typeof(AActorMessage))]
+	[ProtoInclude(Opcode.ARequest, typeof(ARequest))]
+	[ProtoInclude(Opcode.AResponse, typeof(AResponse))]
 	[BsonKnownTypes(typeof(AActorMessage))]
 	[BsonKnownTypes(typeof(ARequest))]
-	[BsonKnownTypes(typeof(AActorResponse))]
+	[BsonKnownTypes(typeof(AResponse))]
 	public abstract partial class AMessage
 	{
 		public override string ToString()
@@ -17,10 +20,11 @@ namespace Model
 	}
 
 	[ProtoContract]
+	[ProtoInclude(Opcode.AActorRequest, typeof(AActorRequest))]
 	[BsonKnownTypes(typeof(AActorRequest))]
 	public abstract partial class ARequest : AMessage
 	{
-		[ProtoMember(90)]
+		[ProtoMember(1)]
 		[BsonIgnoreIfDefault]
 		public uint RpcId;
 	}
@@ -29,17 +33,18 @@ namespace Model
 	/// 服务端回的RPC消息需要继承这个抽象类
 	/// </summary>
 	[ProtoContract]
+	[ProtoInclude(Opcode.AActorResponse, typeof(AActorResponse))]
 	[BsonKnownTypes(typeof(AActorResponse))]
 	[BsonKnownTypes(typeof(ErrorResponse))]
 	public abstract partial class AResponse : AMessage
 	{
-		[ProtoMember(90)]
+		[ProtoMember(1)]
 		public uint RpcId;
 
-		[ProtoMember(91)]
+		[ProtoMember(2)]
 		public int Error = 0;
 
-		[ProtoMember(92)]
+		[ProtoMember(3)]
 		public string Message = "";
 	}
 

+ 1 - 1
Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs

@@ -4,7 +4,7 @@ namespace Model
 {
 	public class ClientDispatcher: IMessageDispatcher
 	{
-		public void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message)
+		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// 如果是帧同步消息,交给ClientFrameComponent处理
 			FrameMessage frameMessage = message as FrameMessage;

+ 1 - 1
Unity/Assets/Scripts/Base/Message/IMessageDispatcher.cs

@@ -2,6 +2,6 @@
 {
 	public interface IMessageDispatcher
 	{
-		void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message);
+		void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message);
 	}
 }

+ 2 - 2
Unity/Assets/Scripts/Base/Message/MessageAttribute.cs

@@ -4,9 +4,9 @@ namespace Model
 {
 	public class MessageAttribute: Attribute
 	{
-		public Opcode Opcode { get; }
+		public ushort Opcode { get; }
 
-		public MessageAttribute(Opcode opcode)
+		public MessageAttribute(ushort opcode)
 		{
 			this.Opcode = opcode;
 		}

+ 2 - 2
Unity/Assets/Scripts/Base/Message/MessageInfo.cs

@@ -2,10 +2,10 @@
 {
 	public struct MessageInfo
 	{
-		public Opcode Opcode { get; set; }
+		public ushort Opcode { get; set; }
 		public AMessage Message { get; set; }
 
-		public MessageInfo(Opcode opcode, AMessage message)
+		public MessageInfo(ushort opcode, AMessage message)
 		{
 			this.Opcode = opcode;
 			this.Message = message;

+ 11 - 0
Unity/Assets/Scripts/Base/Message/Opcode.cs

@@ -0,0 +1,11 @@
+namespace Model
+{
+	public static partial class Opcode
+	{
+		public const ushort ARequest = 1;
+		public const ushort AResponse = 2;
+		public const ushort AActorMessage = 3;
+		public const ushort AActorRequest = 4;
+		public const ushort AActorResponse = 5;
+	}
+}

+ 12 - 0
Unity/Assets/Scripts/Base/Message/Opcode.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 129811e752b0782479b48ee6a12e1f2b
+timeCreated: 1517571639
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 1
Unity/Assets/Scripts/Component/ClientFrameComponent.cs

@@ -79,7 +79,7 @@ namespace Model
             for (int i = 0; i < sessionFrameMessage.FrameMessage.Messages.Count; ++i)
             {
 	            AFrameMessage message = sessionFrameMessage.FrameMessage.Messages[i];
-                Opcode opcode = Game.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
+	            ushort opcode = Game.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
                 Game.Scene.GetComponent<MessageDispatherComponent>().Handle(sessionFrameMessage.Session, new MessageInfo() { Opcode= opcode, Message = message });
             }
         }

+ 8 - 8
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -72,7 +72,7 @@ namespace Model
 	/// </summary>
 	public class MessageDispatherComponent : Component
 	{
-		private Dictionary<Opcode, List<IMessageMethod>> handlers;
+		private Dictionary<ushort, List<IMessageMethod>> handlers;
 
 
 		public void Awake()
@@ -82,7 +82,7 @@ namespace Model
 
 		public void Load()
 		{
-			handlers = new Dictionary<Opcode, List<IMessageMethod>>();
+			handlers = new Dictionary<ushort, List<IMessageMethod>>();
 
 			Type[] types = DllHelper.GetMonoTypes();
 
@@ -95,11 +95,11 @@ namespace Model
 				}
 				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
 				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
-				if (!this.handlers.ContainsKey((Opcode)messageHandlerAttribute.Opcode))
+				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
 				{
-					this.handlers.Add((Opcode)messageHandlerAttribute.Opcode, new List<IMessageMethod>());
+					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
 				}
-				this.handlers[(Opcode)messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
+				this.handlers[messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
 			}
 
 			// hotfix dll
@@ -118,11 +118,11 @@ namespace Model
 				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
 				IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler);
 #endif
-				if (!this.handlers.ContainsKey((Opcode)messageHandlerAttribute.Opcode))
+				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
 				{
-					this.handlers.Add((Opcode)messageHandlerAttribute.Opcode, new List<IMessageMethod>());
+					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
 				}
-				this.handlers[(Opcode)messageHandlerAttribute.Opcode].Add(iMessageMethod);
+				this.handlers[messageHandlerAttribute.Opcode].Add(iMessageMethod);
 			}
 		}
 

+ 3 - 3
Unity/Assets/Scripts/Component/OpcodeTypeComponent.cs

@@ -13,7 +13,7 @@ namespace Model
 
 	public class OpcodeTypeComponent : Component
 	{
-		private readonly DoubleMap<Opcode, Type> opcodeTypes = new DoubleMap<Opcode, Type>();
+		private readonly DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
 
 		public void Awake()
 		{
@@ -36,12 +36,12 @@ namespace Model
 			}
 		}
 
-		public Opcode GetOpcode(Type type)
+		public ushort GetOpcode(Type type)
 		{
 			return this.opcodeTypes.GetKeyByValue(type);
 		}
 
-		public Type GetType(Opcode opcode)
+		public Type GetType(ushort opcode)
 		{
 			return this.opcodeTypes.GetValueByKey(opcode);
 		}

+ 57 - 43
Unity/Assets/Scripts/Component/ResourcesComponent.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Text;
 using System.Threading.Tasks;
 using UnityEngine;
 #if UNITY_EDITOR
@@ -10,7 +11,7 @@ using UnityEditor;
 
 namespace Model
 {
-	public class ABInfo: Disposer
+	public class ABInfo : Disposer
 	{
 		private int refCount;
 		public string Name { get; }
@@ -23,7 +24,7 @@ namespace Model
 			}
 			set
 			{
-				Log.Debug($"{this.Name} refcount: {value}");
+				//Log.Debug($"{this.Name} refcount: {value}");
 				this.refCount = value;
 			}
 		}
@@ -35,7 +36,7 @@ namespace Model
 			this.Name = name;
 			this.AssetBundle = ab;
 			this.RefCount = 1;
-			Log.Debug($"load assetbundle: {this.Name}");
+			//Log.Debug($"load assetbundle: {this.Name}");
 		}
 
 		public override void Dispose()
@@ -44,11 +45,11 @@ namespace Model
 			{
 				return;
 			}
-			
+
 			base.Dispose();
-			
-			Log.Debug($"desdroy assetbundle: {this.Name}");
-			
+
+			//Log.Debug($"desdroy assetbundle: {this.Name}");
+
 			this.AssetBundle?.Unload(true);
 		}
 	}
@@ -60,10 +61,29 @@ namespace Model
 		private readonly Dictionary<string, UnityEngine.Object> resourceCache = new Dictionary<string, UnityEngine.Object>();
 
 		private readonly Dictionary<string, ABInfo> bundles = new Dictionary<string, ABInfo>();
-		
+
 		// lru缓存队列
 		private readonly QueueDictionary<string, ABInfo> cacheDictionary = new QueueDictionary<string, ABInfo>();
 
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+
+			base.Dispose();
+
+			foreach (var abInfo in this.bundles)
+			{
+				abInfo.Value?.AssetBundle?.Unload(true);
+			}
+
+			this.bundles.Clear();
+			this.cacheDictionary.Clear();
+			this.resourceCache.Clear();
+		}
+
 		public K GetAsset<K>(string bundleName, string prefab) where K : class
 		{
 			string path = $"{bundleName}/{prefab}".ToLower();
@@ -73,15 +93,18 @@ namespace Model
 			{
 				throw new Exception($"not found asset: {path}");
 			}
-			
-			return resource as K;
+
+			K k = resource as K;
+			if (k == null)
+			{
+				throw new Exception($"asset type error, type: {k.GetType().Name}, path: {path}");
+			}
+			return k;
 		}
 
 		public void UnloadBundle(string assetBundleName)
 		{
 			assetBundleName = assetBundleName.ToLower();
-			
-			this.UnloadOneBundle(assetBundleName);
 
 			string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
 
@@ -90,12 +113,14 @@ namespace Model
 			{
 				this.UnloadOneBundle(dependency);
 			}
+
+			this.UnloadOneBundle(assetBundleName);
 		}
 
 		private void UnloadOneBundle(string assetBundleName)
 		{
 			assetBundleName = assetBundleName.ToLower();
-			
+
 			//Log.Debug($"unload bundle {assetBundleName}");
 			ABInfo abInfo;
 			if (!this.bundles.TryGetValue(assetBundleName, out abInfo))
@@ -108,10 +133,10 @@ namespace Model
 			{
 				return;
 			}
-			
-			
+
+
 			this.bundles.Remove(assetBundleName);
-			
+
 			// 缓存10个包
 			this.cacheDictionary.Enqueue(assetBundleName, abInfo);
 			if (this.cacheDictionary.Count > 10)
@@ -120,9 +145,9 @@ namespace Model
 				this.cacheDictionary.Dequeue();
 				abInfo.Dispose();
 			}
-			Log.Debug($"cache count: {this.cacheDictionary.Count}");
+			//Log.Debug($"cache count: {this.cacheDictionary.Count}");
 		}
-		
+
 		/// <summary>
 		/// 同步加载assetbundle
 		/// </summary>
@@ -131,8 +156,6 @@ namespace Model
 		public void LoadBundle(string assetBundleName)
 		{
 			assetBundleName = assetBundleName.ToLower();
-			this.LoadOneBundle(assetBundleName);
-
 			string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
 
 			Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
@@ -144,8 +167,9 @@ namespace Model
 				}
 				this.LoadOneBundle(dependency);
 			}
+			this.LoadOneBundle(assetBundleName);
 		}
-		
+
 		public void LoadOneBundle(string assetBundleName)
 		{
 			ABInfo abInfo;
@@ -164,7 +188,7 @@ namespace Model
 				this.cacheDictionary.Remove(assetBundleName);
 				return;
 			}
-			
+
 
 			if (!Define.IsAsync)
 			{
@@ -177,7 +201,7 @@ namespace Model
 					UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
 					this.resourceCache[path] = resource;
 				}
-				
+
 				this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
 				return;
 #endif
@@ -195,7 +219,7 @@ namespace Model
 					this.resourceCache[path] = asset;
 				}
 			}
-			
+
 			this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
 		}
 
@@ -207,8 +231,6 @@ namespace Model
 		public async Task LoadBundleAsync(string assetBundleName)
 		{
 			assetBundleName = assetBundleName.ToLower();
-			await this.LoadOneBundleAsync(assetBundleName);
-
 			string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
 
 			//Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
@@ -220,6 +242,7 @@ namespace Model
 				}
 				await this.LoadOneBundleAsync(dependency);
 			}
+			await this.LoadOneBundleAsync(assetBundleName);
 		}
 
 		public async Task LoadOneBundleAsync(string assetBundleName)
@@ -240,7 +263,7 @@ namespace Model
 				this.cacheDictionary.Remove(assetBundleName);
 				return;
 			}
-			
+
 
 			if (!Define.IsAsync)
 			{
@@ -253,7 +276,7 @@ namespace Model
 					UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
 					this.resourceCache[path] = resource;
 				}
-				
+
 				this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
 				return;
 #endif
@@ -279,27 +302,18 @@ namespace Model
 					this.resourceCache[path] = asset;
 				}
 			}
-			
+
 			this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
 		}
 
-		public override void Dispose()
+		public string DebugString()
 		{
-			if (this.Id == 0)
+			StringBuilder sb = new StringBuilder();
+			foreach (ABInfo abInfo in this.bundles.Values)
 			{
-				return;
+				sb.Append($"{abInfo.Name}:{abInfo.RefCount}\n");
 			}
-
-			base.Dispose();
-
-			foreach (var abInfo in this.bundles)
-			{
-				abInfo.Value?.AssetBundle?.Unload(true);
-			}
-			
-			this.bundles.Clear();
-			this.cacheDictionary.Clear();
-			this.resourceCache.Clear();
+			return sb.ToString();
 		}
 	}
 }

+ 57 - 57
Unity/Assets/Scripts/Entity/Message/Opcode.cs

@@ -1,62 +1,62 @@
 namespace Model
 {
-	public enum Opcode: ushort
+	public static partial class Opcode
 	{
-		C2R_Login = 101,
-		R2C_Login = 102,
-		C2G_LoginGate = 103,
-		G2C_LoginGate = 104,
-		Actor_Test = 105,
-		Actor_TestRequest = 106,
-		Actor_TestResponse = 107,
-		Actor_TransferRequest = 108,
-		Actor_TransferResponse = 109,
-		C2G_EnterMap = 110,
-		G2C_EnterMap = 111,
-		UnitInfo = 112,
-		Actor_CreateUnits = 113,
-		FrameMessageInfo = 114,
-		FrameMessage = 115,
-		Frame_ClickMap = 116,
-		C2M_Reload = 117,
-		M2C_Reload = 118,
-		C2R_Ping = 119,
-		R2C_Ping = 120,
-		ActorRequest = 1001,
-		ActorResponse = 1002,
-		ActorRpcRequest = 1003,
-		ActorRpcResponse = 1004,
-		M2M_TrasferUnitRequest = 1005,
-		M2M_TrasferUnitResponse = 1006,
-		M2A_Reload = 1007,
-		A2M_Reload = 1008,
-		G2G_LockRequest = 1009,
-		G2G_LockResponse = 1010,
-		G2G_LockReleaseRequest = 1011,
-		G2G_LockReleaseResponse = 1012,
-		DBSaveRequest = 1013,
-		DBSaveBatchResponse = 1014,
-		DBSaveBatchRequest = 1015,
-		DBSaveResponse = 1016,
-		DBQueryRequest = 1017,
-		DBQueryResponse = 1018,
-		DBQueryBatchRequest = 1019,
-		DBQueryBatchResponse = 1020,
-		DBQueryJsonRequest = 1021,
-		DBQueryJsonResponse = 1022,
-		ObjectAddRequest = 1023,
-		ObjectAddResponse = 1024,
-		ObjectRemoveRequest = 1025,
-		ObjectRemoveResponse = 1026,
-		ObjectLockRequest = 1027,
-		ObjectLockResponse = 1028,
-		ObjectUnLockRequest = 1029,
-		ObjectUnLockResponse = 1030,
-		ObjectGetRequest = 1031,
-		ObjectGetResponse = 1032,
-		R2G_GetLoginKey = 1033,
-		G2R_GetLoginKey = 1034,
-		G2M_CreateUnit = 1035,
-		M2G_CreateUnit = 1036,
+		 public const ushort C2R_Login = 101;
+		 public const ushort R2C_Login = 102;
+		 public const ushort C2G_LoginGate = 103;
+		 public const ushort G2C_LoginGate = 104;
+		 public const ushort Actor_Test = 105;
+		 public const ushort Actor_TestRequest = 106;
+		 public const ushort Actor_TestResponse = 107;
+		 public const ushort Actor_TransferRequest = 108;
+		 public const ushort Actor_TransferResponse = 109;
+		 public const ushort C2G_EnterMap = 110;
+		 public const ushort G2C_EnterMap = 111;
+		 public const ushort UnitInfo = 112;
+		 public const ushort Actor_CreateUnits = 113;
+		 public const ushort FrameMessageInfo = 114;
+		 public const ushort FrameMessage = 115;
+		 public const ushort Frame_ClickMap = 116;
+		 public const ushort C2M_Reload = 117;
+		 public const ushort M2C_Reload = 118;
+		 public const ushort C2R_Ping = 119;
+		 public const ushort R2C_Ping = 120;
+		 public const ushort ActorRequest = 1001;
+		 public const ushort ActorResponse = 1002;
+		 public const ushort ActorRpcRequest = 1003;
+		 public const ushort ActorRpcResponse = 1004;
+		 public const ushort M2M_TrasferUnitRequest = 1005;
+		 public const ushort M2M_TrasferUnitResponse = 1006;
+		 public const ushort M2A_Reload = 1007;
+		 public const ushort A2M_Reload = 1008;
+		 public const ushort G2G_LockRequest = 1009;
+		 public const ushort G2G_LockResponse = 1010;
+		 public const ushort G2G_LockReleaseRequest = 1011;
+		 public const ushort G2G_LockReleaseResponse = 1012;
+		 public const ushort DBSaveRequest = 1013;
+		 public const ushort DBSaveBatchResponse = 1014;
+		 public const ushort DBSaveBatchRequest = 1015;
+		 public const ushort DBSaveResponse = 1016;
+		 public const ushort DBQueryRequest = 1017;
+		 public const ushort DBQueryResponse = 1018;
+		 public const ushort DBQueryBatchRequest = 1019;
+		 public const ushort DBQueryBatchResponse = 1020;
+		 public const ushort DBQueryJsonRequest = 1021;
+		 public const ushort DBQueryJsonResponse = 1022;
+		 public const ushort ObjectAddRequest = 1023;
+		 public const ushort ObjectAddResponse = 1024;
+		 public const ushort ObjectRemoveRequest = 1025;
+		 public const ushort ObjectRemoveResponse = 1026;
+		 public const ushort ObjectLockRequest = 1027;
+		 public const ushort ObjectLockResponse = 1028;
+		 public const ushort ObjectUnLockRequest = 1029;
+		 public const ushort ObjectUnLockResponse = 1030;
+		 public const ushort ObjectGetRequest = 1031;
+		 public const ushort ObjectGetResponse = 1032;
+		 public const ushort R2G_GetLoginKey = 1033;
+		 public const ushort G2R_GetLoginKey = 1034;
+		 public const ushort G2M_CreateUnit = 1035;
+		 public const ushort M2G_CreateUnit = 1036;
 	}
 }

+ 5 - 8
Unity/Assets/Scripts/Entity/Session.cs

@@ -125,12 +125,10 @@ namespace Model
 		private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset, int count)
 		{
 			object message;
-			Opcode op;
 
 			try
 			{
-				op = (Opcode)opcode;
-				Type messageType = this.network.Parent.GetComponent<OpcodeTypeComponent>().GetType(op);
+				Type messageType = this.network.Parent.GetComponent<OpcodeTypeComponent>().GetType(opcode);
 				message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, count - offset);
 			}
 			catch (Exception e)
@@ -157,7 +155,7 @@ namespace Model
 				return;
 			}
 
-			this.network.MessageDispatcher.Dispatch(this, op, offset, messageBytes, (AMessage)message);
+			this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, (AMessage)message);
 		}
 
 		/// <summary>
@@ -247,8 +245,7 @@ namespace Model
 		private void SendMessage(object message)
 		{
 			//Log.Debug($"send: {MongoHelper.ToJson(message)}");
-			Opcode opcode = this.network.Parent.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
-			ushort op = (ushort)opcode;
+			ushort opcode = this.network.Parent.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
 			byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
 
 #if SERVER
@@ -256,12 +253,12 @@ namespace Model
 			if (this.network.AppType == AppType.AllServer)
 			{
 				Session session = this.network.Parent.GetComponent<NetInnerComponent>().Get(this.RemoteAddress);
-				session.RunDecompressedBytes(op, messageBytes, 0, messageBytes.Length);
+				session.RunDecompressedBytes(opcode, messageBytes, 0, messageBytes.Length);
 				return;
 			}
 #endif
 
-			byte[] opcodeBytes = BitConverter.GetBytes(op);
+			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
 			
 			this.byteses[0] = opcodeBytes;
 			this.byteses[1] = messageBytes;

+ 1 - 1
Unity/Assets/Scripts/Handler/Actor_CreateUnitsHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	[MessageHandler((int)Opcode.Actor_CreateUnits)]
+	[MessageHandler(Opcode.Actor_CreateUnits)]
 	public class Actor_CreateUnitsHandler : AMHandler<Actor_CreateUnits>
 	{
 		protected override void Run(Session session, Actor_CreateUnits message)

+ 1 - 1
Unity/Assets/Scripts/Handler/Actor_TestHandler.cs

@@ -1,6 +1,6 @@
 namespace Model
 {
-	[MessageHandler((int)Opcode.Actor_Test)]
+	[MessageHandler(Opcode.Actor_Test)]
 	public class Actor_TestHandler : AMHandler<Actor_Test>
 	{
 		protected override void Run(Session session, Actor_Test message)

+ 1 - 1
Unity/Assets/Scripts/Handler/Frame_ClickMapHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	[MessageHandler((int)Opcode.Frame_ClickMap)]
+	[MessageHandler(Opcode.Frame_ClickMap)]
 	public class Frame_ClickMapHandler : AMHandler<Frame_ClickMap>
 	{
 		protected override void Run(Session session, Frame_ClickMap message)

+ 8 - 4
Unity/Unity.csproj

@@ -12,12 +12,15 @@
     <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>
@@ -222,6 +225,7 @@
     <Compile Include="Assets\Scripts\Base\Message\MessageInfo.cs" />
     <Compile Include="Assets\Scripts\Base\Message\MongoPacker.cs" />
     <Compile Include="Assets\Scripts\Base\Message\NumericType.cs" />
+    <Compile Include="Assets\Scripts\Base\Message\Opcode.cs" />
     <Compile Include="Assets\Scripts\Base\Message\OpcodeHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Message\ProtobufPacker.cs" />
     <Compile Include="Assets\Scripts\Base\Message\RpcException.cs" />
@@ -673,4 +677,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>