Răsfoiți Sursa

去掉Entity和Component的模板限制

tanghai 9 ani în urmă
părinte
comite
ef1520c31d
41 a modificat fișierele cu 303 adăugiri și 228 ștergeri
  1. 5 1
      Unity/Assets/Scripts/Base/Config/ACategory.cs
  2. 4 1
      Unity/Assets/Scripts/Base/Config/AConfig.cs
  3. 9 0
      Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs
  4. 3 3
      Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs.meta
  5. 0 0
      Unity/Assets/Scripts/Base/Config/ICategory.cs
  6. 0 0
      Unity/Assets/Scripts/Base/Config/ICategory.cs.meta
  7. 0 0
      Unity/Assets/Scripts/Base/Helper/FileHelper.cs
  8. 0 0
      Unity/Assets/Scripts/Base/Helper/FileHelper.cs.meta
  9. 5 1
      Unity/Assets/Scripts/Base/Network/AChannel.cs
  10. 0 11
      Unity/Assets/Scripts/Base/Network/TNet/IPoller.cs
  11. 1 4
      Unity/Assets/Scripts/Base/Network/TNet/TChannel.cs
  12. 1 1
      Unity/Assets/Scripts/Base/Network/TNet/TPoller.cs
  13. 1 1
      Unity/Assets/Scripts/Base/Network/TNet/TService.cs
  14. 2 2
      Unity/Assets/Scripts/Base/Network/TNet/TSocket.cs
  15. 1 5
      Unity/Assets/Scripts/Base/Network/UNet/UChannel.cs
  16. 0 1
      Unity/Assets/Scripts/Base/Network/UNet/UService.cs
  17. 9 12
      Unity/Assets/Scripts/Base/Object/Component.cs
  18. 30 40
      Unity/Assets/Scripts/Base/Object/Entity.cs
  19. 0 1
      Unity/Assets/Scripts/Base/Object/ObjectManager.cs
  20. 1 1
      Unity/Assets/Scripts/Component/Game.cs
  21. 10 10
      Unity/Assets/Scripts/Component/KVComponent.cs
  22. 12 0
      Unity/Assets/Scripts/Component/KVComponent.cs.meta
  23. 0 106
      Unity/Assets/Scripts/Component/LevelComponent.cs
  24. 5 6
      Unity/Assets/Scripts/Component/Scene.cs
  25. 102 0
      Unity/Assets/Scripts/Component/Scene/ChildrenComponent.cs
  26. 12 0
      Unity/Assets/Scripts/Component/Scene/ChildrenComponent.cs.meta
  27. 1 1
      Unity/Assets/Scripts/Component/Scene/EventComponent.cs
  28. 1 1
      Unity/Assets/Scripts/Component/Scene/GlobalConfigComponent.cs
  29. 1 1
      Unity/Assets/Scripts/Component/Scene/LogComponent.cs
  30. 44 6
      Unity/Assets/Scripts/Component/Scene/MessageComponent.cs
  31. 1 1
      Unity/Assets/Scripts/Component/Scene/NetworkComponent.cs
  32. 1 1
      Unity/Assets/Scripts/Component/Scene/TimeComponent.cs
  33. 1 1
      Unity/Assets/Scripts/Component/Scene/TimerComponent.cs
  34. 1 1
      Unity/Assets/Scripts/Component/Share.cs
  35. 16 0
      Unity/Assets/Scripts/Config/BuffProto.cs
  36. 12 0
      Unity/Assets/Scripts/Config/BuffProto.cs.meta
  37. 0 0
      Unity/Assets/Scripts/Message/MessageAttribute.cs
  38. 0 0
      Unity/Assets/Scripts/Message/MessageAttribute.cs.meta
  39. 1 0
      Unity/Controller/Controller.csproj
  40. 1 0
      Unity/Controller/Event/InitSceneStartEvent_InitGame.cs
  41. 9 8
      Unity/Unity.CSharp.csproj

+ 5 - 1
Unity/Assets/Scripts/Base/Config/ACategory.cs

@@ -5,6 +5,10 @@ using UnityEngine;
 
 namespace Base
 {
+	/// <summary>
+	/// 管理该所有的配置
+	/// </summary>
+	/// <typeparam name="T"></typeparam>
 	public abstract class ACategory<T>: ICategory where T : AConfig
 	{
 		protected Dictionary<long, T> dict;
@@ -34,7 +38,7 @@ namespace Base
 						continue;
 					}
 					T t = MongoHelper.FromJson<T>(str2);
-					this.dict.Add(t.id, t);
+					this.dict.Add(t.Id, t);
 				}
 				catch (Exception e)
 				{

+ 4 - 1
Unity/Assets/Scripts/Base/Config/AConfig.cs

@@ -3,10 +3,13 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Base
 {
+	/// <summary>
+	/// 每个Config的基类
+	/// </summary>
 	public abstract class AConfig: ISupportInitialize
 	{
 		[BsonId]
-		public long id { get; set; }
+		public long Id { get; set; }
 
 		public virtual void BeginInit()
 		{

+ 9 - 0
Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs

@@ -0,0 +1,9 @@
+using System;
+
+namespace Base
+{
+	[AttributeUsage(AttributeTargets.Class)]
+	public class ConfigAttribute: Attribute
+	{
+	}
+}

+ 3 - 3
Unity/Assets/Scripts/Base/Network/TNet/IPoller.cs.meta → Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs.meta

@@ -1,7 +1,7 @@
 fileFormatVersion: 2
-guid: 1d609f021d8833b4c8f9cc79d267a1a6
-timeCreated: 1429240758
-licenseType: Free
+guid: f3b8997482f68c0418b3982ed475def2
+timeCreated: 1475898829
+licenseType: Pro
 MonoImporter:
   serializedVersion: 2
   defaultReferences: []

+ 0 - 0
Unity/Assets/Scripts/Other/ICategory.cs → Unity/Assets/Scripts/Base/Config/ICategory.cs


+ 0 - 0
Unity/Assets/Scripts/Other/ICategory.cs.meta → Unity/Assets/Scripts/Base/Config/ICategory.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Base/FileHelper.cs → Unity/Assets/Scripts/Base/Helper/FileHelper.cs


+ 0 - 0
Unity/Assets/Scripts/Base/FileHelper.cs.meta → Unity/Assets/Scripts/Base/Helper/FileHelper.cs.meta


+ 5 - 1
Unity/Assets/Scripts/Base/Network/AChannel.cs

@@ -12,7 +12,7 @@ namespace Base
 		NoAllocate = 1 << 2
 	}
 
-	public abstract class AChannel: Entity<AChannel>
+	public abstract class AChannel: Entity
 	{
 		protected AService service;
 
@@ -42,7 +42,11 @@ namespace Base
 				return;
 			}
 
+			long id = this.Id;
+
 			base.Dispose();
+
+			this.service.Remove(id);
 		}
 	}
 }

+ 0 - 11
Unity/Assets/Scripts/Base/Network/TNet/IPoller.cs

@@ -1,11 +0,0 @@
-using System;
-
-namespace Base
-{
-	public interface IPoller
-	{
-		void Add(Action action);
-
-		void Update();
-	}
-}

+ 1 - 4
Unity/Assets/Scripts/Base/Network/TNet/TChannel.cs

@@ -33,13 +33,10 @@ namespace Base
 			{
 				return;
 			}
-
-			long id = this.Id;
-
+			
 			base.Dispose();
 
 			this.socket.Dispose();
-			this.service.Remove(id);
 		}
 
 		public override void ConnectAsync()

+ 1 - 1
Unity/Assets/Scripts/Base/Network/TNet/TPoller.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace Base
 {
-	public class TPoller: IPoller
+	public class TPoller
 	{
 		// 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
 		private Queue<Action> queue = new Queue<Action>();

+ 1 - 1
Unity/Assets/Scripts/Base/Network/TNet/TService.cs

@@ -6,7 +6,7 @@ namespace Base
 {
 	public sealed class TService: AService
 	{
-		private IPoller poller = new TPoller();
+		private TPoller poller = new TPoller();
 		
 		private readonly Dictionary<long, TChannel> idChannels = new Dictionary<long, TChannel>();
 

+ 2 - 2
Unity/Assets/Scripts/Base/Network/TNet/TSocket.cs

@@ -9,7 +9,7 @@ namespace Base
 	/// </summary>
 	public class TSocket: IDisposable
 	{
-		private readonly IPoller poller;
+		private readonly TPoller poller;
 		private Socket socket;
 		private readonly SocketAsyncEventArgs innArgs = new SocketAsyncEventArgs();
 		private readonly SocketAsyncEventArgs outArgs = new SocketAsyncEventArgs();
@@ -20,7 +20,7 @@ namespace Base
 		public Action<SocketError> OnDisconnect;
 		private string remoteAddress;
 
-		public TSocket(IPoller poller)
+		public TSocket(TPoller poller)
 		{
 			this.poller = poller;
 			this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

+ 1 - 5
Unity/Assets/Scripts/Base/Network/UNet/UChannel.cs

@@ -22,14 +22,10 @@ namespace Base
 			{
 				return;
 			}
-
-			long id = this.Id;
-
+			
 			base.Dispose();
 
 			this.socket.Dispose();
-
-			this.service.Remove(id);
 		}
 
 		public override void ConnectAsync()

+ 0 - 1
Unity/Assets/Scripts/Base/Network/UNet/UService.cs

@@ -42,7 +42,6 @@ namespace Base
 		public override void Dispose()
 		{
 			this.Dispose(true);
-			GC.SuppressFinalize(this);
 		}
 
 		public override void Add(Action action)

+ 9 - 12
Unity/Assets/Scripts/Base/Object/Component.cs

@@ -5,21 +5,18 @@ namespace Base
 	/// <summary>
 	/// Component的Id与Owner Entity Id一样
 	/// </summary>
-	public abstract class Component<T>: Object where T : Entity<T>
+	public abstract class Component: Object
 	{
-		private T owner;
+		private Entity owner;
+		
+		public T GetOwner<T>() where T: Entity
+		{
+			return this.owner as T;
+		}
 
-		[BsonIgnore]
-		public T Owner
+		public void SetOwner(Entity entity)
 		{
-			get
-			{
-				return this.owner;
-			}
-			set
-			{
-				this.owner = value;
-			}
+			this.owner = entity;
 		}
 
 		protected Component()

+ 30 - 40
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -6,33 +6,23 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Base
 {
-	public abstract class Entity<T>: Object where T : Entity<T>
+	public abstract class Entity: Object
 	{
 		[BsonElement, BsonIgnoreIfNull]
-		private HashSet<Component<T>> components = new HashSet<Component<T>>();
-		private Dictionary<Type, Component<T>> componentDict = new Dictionary<Type, Component<T>>();
-
-		public string Name { get; }
-
+		private HashSet<Component> components = new HashSet<Component>();
+		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
+		
 		protected Entity()
 		{
-			this.Name = "";
-			ObjectManager.Add(this);
-		}
-
-		protected Entity(string name)
-		{
-			this.Name = name;
 			ObjectManager.Add(this);
 		}
 
-		protected Entity(long id, string name): base(id)
+		protected Entity(long id): base(id)
 		{
-			this.Name = name;
 			ObjectManager.Add(this);
 		}
 
-		public T Clone()
+		public T Clone<T>() where T: Entity
 		{
 			return MongoHelper.FromBson<T>(MongoHelper.ToBson(this));
 		}
@@ -46,7 +36,7 @@ namespace Base
 
 			base.Dispose();
 
-			foreach (Component<T> component in this.GetComponents())
+			foreach (Component component in this.GetComponents())
 			{
 				try
 				{
@@ -60,10 +50,10 @@ namespace Base
 			ObjectManager.Remove(this.Id);
 		}
 
-		public K AddComponent<K>() where K : Component<T>, new()
+		public K AddComponent<K>() where K : Component, new()
 		{
 			K component = (K) Activator.CreateInstance(typeof (K));
-			component.Owner = (T) this;
+			component.SetOwner(this);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
@@ -72,7 +62,7 @@ namespace Base
 
 			if (this.components == null)
 			{
-				this.components = new HashSet<Component<T>>();
+				this.components = new HashSet<Component>();
 			}
 
 			this.components.Add(component);
@@ -81,10 +71,10 @@ namespace Base
 			return component;
 		}
 
-		public K AddComponent<K, P1>(P1 p1) where K : Component<T>, new()
+		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
 		{
 			K component = (K)Activator.CreateInstance(typeof(K));
-			component.Owner = (T)this;
+			component.SetOwner(this);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
@@ -93,7 +83,7 @@ namespace Base
 
 			if (this.components == null)
 			{
-				this.components = new HashSet<Component<T>>();
+				this.components = new HashSet<Component>();
 			}
 
 			this.components.Add(component);
@@ -102,10 +92,10 @@ namespace Base
 			return component;
 		}
 
-		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component<T>, new()
+		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
 		{
 			K component = (K)Activator.CreateInstance(typeof(K));
-			component.Owner = (T)this;
+			component.SetOwner(this);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
@@ -114,7 +104,7 @@ namespace Base
 
 			if (this.components == null)
 			{
-				this.components = new HashSet<Component<T>>();
+				this.components = new HashSet<Component>();
 			}
 
 			this.components.Add(component);
@@ -124,10 +114,10 @@ namespace Base
 		}
 
 
-		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component<T>, new()
+		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
 		{
 			K component = (K)Activator.CreateInstance(typeof(K));
-			component.Owner = (T)this;
+			component.SetOwner(this);
 
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
@@ -136,7 +126,7 @@ namespace Base
 
 			if (this.components == null)
 			{
-				this.components = new HashSet<Component<T>>();
+				this.components = new HashSet<Component>();
 			}
 
 			this.components.Add(component);
@@ -145,7 +135,7 @@ namespace Base
 			return component;
 		}
 
-		public void AddComponent(Component<T> component)
+		public void AddComponent(Component component)
 		{
 			if (this.componentDict.ContainsKey(component.GetType()))
 			{
@@ -154,16 +144,16 @@ namespace Base
 
 			if (this.components == null)
 			{
-				this.components = new HashSet<Component<T>>();
+				this.components = new HashSet<Component>();
 			}
 			this.components.Add(component);
 			this.componentDict.Add(component.GetType(), component);
 			ObjectManager.Awake(component.Id);
 		}
 
-		public void RemoveComponent<K>() where K : Component<T>
+		public void RemoveComponent<K>() where K : Component
 		{
-			Component<T> component;
+			Component component;
 			if (!this.componentDict.TryGetValue(typeof (K), out component))
 			{
 				return;
@@ -178,9 +168,9 @@ namespace Base
 			component.Dispose();
 		}
 
-		public K GetComponent<K>() where K : Component<T>
+		public K GetComponent<K>() where K : Component
 		{
-			Component<T> component;
+			Component component;
 			if (!this.componentDict.TryGetValue(typeof (K), out component))
 			{
 				return default(K);
@@ -188,7 +178,7 @@ namespace Base
 			return (K) component;
 		}
 
-		public Component<T>[] GetComponents()
+		public Component[] GetComponents()
 		{
 			return components.ToArray();
 		}
@@ -196,8 +186,8 @@ namespace Base
 		public override void BeginInit()
 		{
 			base.BeginInit();
-			this.components = new HashSet<Component<T>>();
-			this.componentDict = new Dictionary<Type, Component<T>>();
+			this.components = new HashSet<Component>();
+			this.componentDict = new Dictionary<Type, Component>();
 		}
 
 		public override void EndInit()
@@ -208,9 +198,9 @@ namespace Base
 				this.components = null;
 				return;
 			}
-			foreach (Component<T> component in this.components)
+			foreach (Component component in this.components)
 			{
-				component.Owner = (T) this;
+				component.SetOwner(this);
 				this.componentDict.Add(component.GetType(), component);
 			}
 		}

+ 0 - 1
Unity/Assets/Scripts/Base/Object/ObjectManager.cs

@@ -3,7 +3,6 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System.Text;
-using Base;
 
 namespace Base
 {

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

@@ -1,6 +1,6 @@
 namespace Base
 {
-	public sealed class Game: Entity<Game>
+	public sealed class Game: Entity
 	{
 		private static Scene game;
 

+ 10 - 10
Unity/Assets/Scripts/Component/KVComponent.cs

@@ -7,7 +7,7 @@ namespace Base
 	/// <summary>
 	/// Key Value组件用于保存一些数据
 	/// </summary>
-    public class KVComponent<T> : Component<T> where T: Entity<T>
+    public class KVComponent : Component
     {
 		[BsonElement]
 		private readonly Dictionary<string, object> kv = new Dictionary<string, object>();
@@ -22,14 +22,14 @@ namespace Base
 			this.kv.Remove(key);
 		}
 
-		public K Get<K>(string key)
+		public T Get<T>(string key)
 		{
 			object k;
 			if (!this.kv.TryGetValue(key, out k))
 			{
-				return default(K);
+				return default(T);
 			}
-			return (K)k;
+			return (T)k;
 		}
 
 		public override void Dispose()
@@ -45,19 +45,19 @@ namespace Base
 
 	public static class KVHelper
 	{
-		public static void Add<T>(this Entity<T> entity, string key, T value) where T : Entity<T>
+		public static void KVAdd(this Entity entity, string key, object value)
 		{
-			entity.GetComponent<KVComponent<T>>().Add(key, value);
+			entity.GetComponent<KVComponent>().Add(key, value);
 		}
 
-		public static void Remove<T>(this Entity<T> entity, string key) where T : Entity<T>
+		public static void KVRemove(this Entity entity, string key)
 		{
-			entity.GetComponent<KVComponent<T>>().Remove(key);
+			entity.GetComponent<KVComponent>().Remove(key);
 		}
 
-		public static void Get<T, K>(this Entity<T> entity, string key) where T : Entity<T>
+		public static void KVGet<T>(this Entity entity, string key)
 		{
-			entity.GetComponent<KVComponent<T>>().Get<K>(key);
+			entity.GetComponent<KVComponent>().Get<T>(key);
 		}
 	}
 }

+ 12 - 0
Unity/Assets/Scripts/Component/KVComponent.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 17265c1c4bf06a94da594ce31869e163
+timeCreated: 1475893345
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 0 - 106
Unity/Assets/Scripts/Component/LevelComponent.cs

@@ -1,106 +0,0 @@
-using System.Collections.Generic;
-using System.Linq;
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Base
-{
-	/// <summary>
-	/// 父子层级信息
-	/// </summary>
-    public class LevelComponent<T> : Component<T> where T: Entity<T>
-    {
-		[BsonIgnore]
-		public T Parent { get; private set; }
-
-		[BsonElement]
-		private readonly List<T> children = new List<T>();
-		
-		private readonly Dictionary<long, T> idChildren = new Dictionary<long, T>();
-		
-		private readonly Dictionary<string, T> nameChildren = new Dictionary<string, T>();
-
-		[BsonIgnore]
-		public int Count
-		{
-			get
-			{
-				return this.idChildren.Count;
-			}
-		}
-
-		public void Add(T t)
-		{
-			t.GetComponent<LevelComponent<T>>().Parent = this.Owner;
-			this.children.Add(t);
-			this.idChildren.Add(t.Id, t);
-			this.nameChildren.Add(t.Name, t);
-		}
-
-		public T[] GetChildren()
-		{
-			return this.children.ToArray();
-		}
-
-		private void Remove(T t)
-		{
-			this.idChildren.Remove(t.Id);
-			this.nameChildren.Remove(t.Name);
-			t.Dispose();
-		}
-
-		public void Remove(long id)
-		{
-			T t;
-			if (!this.idChildren.TryGetValue(id, out t))
-			{
-				return;
-			}
-			this.Remove(t);
-		}
-
-		public void Remove(string name)
-		{
-			T t;
-			if (!this.nameChildren.TryGetValue(name, out t))
-			{
-				return;
-			}
-			this.Remove(t);
-		}
-
-		public override void Dispose()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-
-			base.Dispose();
-
-			foreach (T t in this.children)
-			{
-				t.Dispose();
-			}
-
-			this.Parent?.GetComponent<LevelComponent<T>>().Remove(this.Id);
-		}
-    }
-
-	public static class LevelHelper
-	{
-		public static void AddChild<T>(this Entity<T> entity, T t) where T : Entity<T>
-		{
-			entity.GetComponent<LevelComponent<T>>().Add(t);
-		}
-
-		public static void RemoveChild<T>(this Entity<T> entity, long id) where T : Entity<T>
-		{
-			entity.GetComponent<LevelComponent<T>>().Remove(id);
-		}
-
-		public static void RemoveChild<T>(this Entity<T> entity, string name) where T : Entity<T>
-		{
-			entity.GetComponent<LevelComponent<T>>().Remove(name);
-		}
-	}
-}

+ 5 - 6
Unity/Assets/Scripts/Component/Scene.cs

@@ -10,18 +10,17 @@ namespace Base
 		Login,
 		Lobby,
 		Map,
-		Launcher,
-		Robot,
-        BehaviorTreeScene,
-		RobotClient,
 	}
 
-	public sealed class Scene: Entity<Scene>
+	public sealed class Scene: Entity
 	{
+		public string Name { get; }
+
 		public SceneType SceneType { get; }
 
-		public Scene(string name, SceneType sceneType): base(name)
+		public Scene(string name, SceneType sceneType)
 		{
+			this.Name = name;
 			this.SceneType = sceneType;
 		}
 

+ 102 - 0
Unity/Assets/Scripts/Component/Scene/ChildrenComponent.cs

@@ -0,0 +1,102 @@
+using System.Collections.Generic;
+using System.Linq;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Base
+{
+	/// <summary>
+	/// 父子层级信息
+	/// </summary>
+    public class ChildrenComponent : Component
+    {
+		[BsonIgnore]
+		public Scene Parent { get; private set; }
+		
+		private readonly Dictionary<long, Scene> idChildren = new Dictionary<long, Scene>();
+		
+		private readonly Dictionary<string, Scene> nameChildren = new Dictionary<string, Scene>();
+
+		[BsonIgnore]
+		public int Count
+		{
+			get
+			{
+				return this.idChildren.Count;
+			}
+		}
+
+		public void Add(Scene scene)
+		{
+			scene.GetComponent<ChildrenComponent>().Parent = this.GetOwner<Scene>();
+			this.idChildren.Add(scene.Id, scene);
+			this.nameChildren.Add(scene.Name, scene);
+		}
+
+		public Scene[] GetChildren()
+		{
+			return this.idChildren.Values.ToArray();
+		}
+
+		private void Remove(Scene scene)
+		{
+			this.idChildren.Remove(scene.Id);
+			this.nameChildren.Remove(scene.Name);
+			scene.Dispose();
+		}
+
+		public void Remove(long id)
+		{
+			Scene scene;
+			if (!this.idChildren.TryGetValue(id, out scene))
+			{
+				return;
+			}
+			this.Remove(scene);
+		}
+
+		public void Remove(string name)
+		{
+			Scene scene;
+			if (!this.nameChildren.TryGetValue(name, out scene))
+			{
+				return;
+			}
+			this.Remove(scene);
+		}
+
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+
+			base.Dispose();
+
+			foreach (Scene scene in this.idChildren.Values.ToArray())
+			{
+				scene.Dispose();
+			}
+
+			this.Parent?.GetComponent<ChildrenComponent>().Remove(this.Id);
+		}
+    }
+
+	public static class LevelHelper
+	{
+		public static void Add(this Scene scene, Scene child)
+		{
+			scene.GetComponent<ChildrenComponent>().Add(child);
+		}
+
+		public static void Remove(this Scene scene, long id)
+		{
+			scene.GetComponent<ChildrenComponent>().Remove(id);
+		}
+
+		public static void Remove(this Scene scene, string name)
+		{
+			scene.GetComponent<ChildrenComponent>().Remove(name);
+		}
+	}
+}

+ 12 - 0
Unity/Assets/Scripts/Component/Scene/ChildrenComponent.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 97ef5ac3bc0b59240b674d0385978404
+timeCreated: 1475893346
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 1
Unity/Assets/Scripts/Component/Scene/EventComponent.cs

@@ -21,7 +21,7 @@ namespace Base
 	/// <summary>
 	/// 事件分发
 	/// </summary>
-	public class EventComponent: Component<Scene>
+	public class EventComponent: Component
 	{
 		private Dictionary<EventIdType, List<object>> allEvents;
 

+ 1 - 1
Unity/Assets/Scripts/Component/Scene/GlobalConfigComponent.cs

@@ -14,7 +14,7 @@ namespace Base
 	/// <summary>
 	/// 全局配置
 	/// </summary>
-	public class GlobalConfigComponent : Component<Scene>
+	public class GlobalConfigComponent : Component
 	{
 		public GlobalProto GlobalProto;
 		

+ 1 - 1
Unity/Assets/Scripts/Component/Scene/LogComponent.cs

@@ -13,7 +13,7 @@ namespace Base
 		}
 	}
 
-	public class LogComponent : Component<Scene>
+	public class LogComponent : Component
 	{
 		private StreamWriter info;
 

+ 44 - 6
Unity/Assets/Scripts/Component/Scene/MessageComponent.cs

@@ -19,8 +19,7 @@ namespace Base
 	{
 		public void Load()
 		{
-			MessageComponent component = this.GetValue();
-			component.Load();
+			this.GetValue().Load();
 		}
 
 		public void Awake()
@@ -37,7 +36,7 @@ namespace Base
 	/// <summary>
 	/// 消息分发组件
 	/// </summary>
-	public class MessageComponent: Component<Scene>
+	public class MessageComponent: Component
 	{
 		private uint RpcId { get; set; }
 		private Dictionary<Opcode, List<Action<byte[], int, int>>> events;
@@ -67,7 +66,7 @@ namespace Base
 					}
 
 					MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
-					if (messageAttribute.SceneType != this.Owner.SceneType)
+					if (messageAttribute.SceneType != this.GetOwner<Scene>().SceneType)
 					{
 						continue;
 					}
@@ -109,7 +108,7 @@ namespace Base
 					Log.Debug(MongoHelper.ToJson(t));
 				}
 
-				action(this.Owner, t);
+				action(this.GetOwner<Scene>(), t);
 			});
 		}
 
@@ -208,7 +207,7 @@ namespace Base
 			List<Action<byte[], int, int>> actions;
 			if (!this.events.TryGetValue(opcode, out actions))
 			{
-				if (this.Owner.SceneType == SceneType.Game)
+				if (this.GetOwner<Scene>().SceneType == SceneType.Game)
 				{
 					Log.Error($"消息{opcode}没有处理");
 				}
@@ -228,6 +227,41 @@ namespace Base
 			}
 		}
 
+
+		public Task<Response> CallAsync<Response>(object request, CancellationToken cancellationToken) where Response : IErrorMessage
+		{
+			this.Send(request, ++this.RpcId);
+
+			var tcs = new TaskCompletionSource<Response>();
+
+			this.requestCallback[this.RpcId] = (bytes, offset, count) =>
+			{
+				try
+				{
+					Response response = MongoHelper.FromBson<Response>(bytes, offset, count);
+					Opcode opcode = EnumHelper.FromString<Opcode>(response.GetType().Name);
+					if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
+					{
+						Log.Debug(MongoHelper.ToJson(response));
+					}
+					if (response.ErrorMessage.errno != (int)ErrorCode.ERR_Success)
+					{
+						tcs.SetException(new RpcException((ErrorCode)response.ErrorMessage.errno, response.ErrorMessage.msg.Utf8ToStr()));
+						return;
+					}
+					tcs.SetResult(response);
+				}
+				catch (Exception e)
+				{
+					tcs.SetException(new GameException($"Rpc Error: {typeof(Response).FullName}", e));
+				}
+			};
+
+			cancellationToken.Register(() => { this.requestCallback.Remove(this.RpcId); });
+
+			return tcs.Task;
+		}
+
 		/// <summary>
 		/// Rpc调用,发送一个消息,等待返回一个消息
 		/// </summary>
@@ -275,6 +309,7 @@ namespace Base
 		{
 			var tcs = new TaskCompletionSource<Response>();
 			Opcode opcode = EnumHelper.FromString<Opcode>(typeof(Response).Name);
+
 			this.waitCallback[opcode] = (bytes, offset, count) =>
 			{
 				try
@@ -285,6 +320,7 @@ namespace Base
 					{
 						Log.Debug(MongoHelper.ToJson(response));
 					}
+					
 					tcs.SetResult(response);
 				}
 				catch (Exception e)
@@ -293,6 +329,8 @@ namespace Base
 				}
 			};
 
+			cancellationToken.Register(() => { this.waitCallback.Remove(opcode); });
+
 			return tcs.Task;
 		}
 

+ 1 - 1
Unity/Assets/Scripts/Component/Scene/NetworkComponent.cs

@@ -18,7 +18,7 @@ namespace Base
 		}
 	}
 
-	public class NetworkComponent: Component<Scene>
+	public class NetworkComponent: Component
 	{
 		private AService service;
 		

+ 1 - 1
Unity/Assets/Scripts/Component/Scene/TimeComponent.cs

@@ -3,7 +3,7 @@
 	/// <summary>
 	/// 用于同步服务端和客户端时间
 	/// </summary>
-    public class TimeComponent : Component<Scene>
+    public class TimeComponent : Component
     {
 		private long syncTime;
 

+ 1 - 1
Unity/Assets/Scripts/Component/Scene/TimerComponent.cs

@@ -59,7 +59,7 @@ namespace Base
 		}
 	}
 
-	public class TimerComponent: Component<Scene>
+	public class TimerComponent: Component
 	{
 		public readonly Dictionary<ObjectId, Timer> timers = new Dictionary<ObjectId, Timer>();
 

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

@@ -3,7 +3,7 @@
 	/// <summary>
 	/// 游戏和扩展编辑器都需要用到的数据放在这个Scene上面
 	/// </summary>
-	public sealed class Share : Entity<Share>
+	public sealed class Share : Entity
 	{
 		private static Scene share;
 

+ 16 - 0
Unity/Assets/Scripts/Config/BuffProto.cs

@@ -0,0 +1,16 @@
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Base
+{
+	[BsonIgnoreExtraElements]
+	public class BuffProto : AConfig
+	{
+		public string Name { get; set; }
+		public int Time { get; set; }
+    }
+
+	[Config]
+	public class BuffCategory : ACategory<BuffProto>
+	{
+	}
+}

+ 12 - 0
Unity/Assets/Scripts/Config/BuffProto.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 8433bdafcbe56e140b3fe0c93722e0cf
+timeCreated: 1475898829
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 0 - 0
Unity/Assets/Scripts/Other/MessageAttribute.cs → Unity/Assets/Scripts/Message/MessageAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Other/MessageAttribute.cs.meta → Unity/Assets/Scripts/Message/MessageAttribute.cs.meta


+ 1 - 0
Unity/Controller/Controller.csproj

@@ -72,6 +72,7 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <Folder Include="Factory\" />
     <Folder Include="Helper\" />
     <Folder Include="Logic\" />
   </ItemGroup>

+ 1 - 0
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs

@@ -22,6 +22,7 @@ namespace Controller
 			Object.ObjectManager.Register("Base", typeof(Game).Assembly);
 
 			Game.Scene.AddComponent<MessageComponent>();
+			Game.Scene.AddComponent<ChildrenComponent>();
 
 			try
 			{

+ 9 - 8
Unity/Unity.CSharp.csproj

@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -81,12 +81,14 @@
   <ItemGroup>
     <Compile Include="Assets\Scripts\Base\Config\ACategory.cs" />
     <Compile Include="Assets\Scripts\Base\Config\AConfig.cs" />
+    <Compile Include="Assets\Scripts\Base\Config\ConfigAttribute.cs" />
+    <Compile Include="Assets\Scripts\Base\Config\ICategory.cs" />
     <Compile Include="Assets\Scripts\Base\DoubleMap.cs" />
-    <Compile Include="Assets\Scripts\Base\FileHelper.cs" />
     <Compile Include="Assets\Scripts\Base\GameObjectHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\ArrayHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\ByteHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\EnumHelper.cs" />
+    <Compile Include="Assets\Scripts\Base\Helper\FileHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\IdGenerater.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\MD5Helper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\MongoHelper.cs" />
@@ -99,7 +101,6 @@
     <Compile Include="Assets\Scripts\Base\MultiMap.cs" />
     <Compile Include="Assets\Scripts\Base\Network\AChannel.cs" />
     <Compile Include="Assets\Scripts\Base\Network\AService.cs" />
-    <Compile Include="Assets\Scripts\Base\Network\TNet\IPoller.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\PacketParser.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TBuffer.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TChannel.cs" />
@@ -132,6 +133,7 @@
     <Compile Include="Assets\Scripts\Base\TryLocker.cs" />
     <Compile Include="Assets\Scripts\Component\Game.cs" />
     <Compile Include="Assets\Scripts\Component\KVComponent.cs" />
+    <Compile Include="Assets\Scripts\Component\Scene\ChildrenComponent.cs" />
     <Compile Include="Assets\Scripts\Component\Scene.cs" />
     <Compile Include="Assets\Scripts\Component\Scene\EventComponent.cs" />
     <Compile Include="Assets\Scripts\Component\Scene\GlobalConfigComponent.cs" />
@@ -141,16 +143,17 @@
     <Compile Include="Assets\Scripts\Component\Scene\TimeComponent.cs" />
     <Compile Include="Assets\Scripts\Component\Scene\TimerComponent.cs" />
     <Compile Include="Assets\Scripts\Component\Share.cs" />
-    <Compile Include="Assets\Scripts\Component\LevelComponent.cs" />
+    <Compile Include="Assets\Scripts\Config\BuffProto.cs" />
     <Compile Include="Assets\Scripts\Config\GlobalProto.cs" />
     <Compile Include="Assets\Scripts\Init.cs" />
     <Compile Include="Assets\Scripts\Message\AMEvent.cs" />
     <Compile Include="Assets\Scripts\Message\ErrorCode.cs" />
     <Compile Include="Assets\Scripts\Message\IErrorMessage.cs" />
     <Compile Include="Assets\Scripts\Message\IMRegister.cs" />
+    <Compile Include="Assets\Scripts\Message\Message.cs" />
+    <Compile Include="Assets\Scripts\Message\MessageAttribute.cs" />
     <Compile Include="Assets\Scripts\Message\Opcode.cs" />
     <Compile Include="Assets\Scripts\Message\OpcodeHelper.cs" />
-    <Compile Include="Assets\Scripts\Message\Message.cs" />
     <Compile Include="Assets\Scripts\Other\AEventAttribute.cs" />
     <Compile Include="Assets\Scripts\Other\EntityType.cs" />
     <Compile Include="Assets\Scripts\Other\Env.cs" />
@@ -158,13 +161,11 @@
     <Compile Include="Assets\Scripts\Other\EventAttribute.cs" />
     <Compile Include="Assets\Scripts\Other\EventIdType.cs" />
     <Compile Include="Assets\Scripts\Other\GameException.cs" />
-    <Compile Include="Assets\Scripts\Other\ICategory.cs" />
     <Compile Include="Assets\Scripts\Other\IEvent.cs" />
-    <Compile Include="Assets\Scripts\Other\MessageAttribute.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="Assets\CSharp 6.0 Support\AsyncTools\Plugins\AsyncBridge.Net35.xml" />
     <None Include="Assets\CSharp 6.0 Support\AsyncTools\Plugins\System.Threading.xml" />
   </ItemGroup>
   <Import Project="$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2015\UnityVS.CSharp.targets" />
-</Project>
+</Project>