Browse Source

【优化】Entity序列化反序列化,Dispose都要保证有序

johnclot69 1 year ago
parent
commit
02466d53c2

+ 114 - 111
Unity/Assets/Scripts/Core/Module/Entity/Entity.cs

@@ -22,11 +22,7 @@ namespace ET
 #endif
 
         [BsonIgnore]
-        public long InstanceId
-        {
-            get;
-            protected set;
-        }
+        public long InstanceId { get; protected set; }
 
         protected Entity()
         {
@@ -72,7 +68,6 @@ namespace ET
                     this.status &= ~EntityStatus.IsRegister;
                 }
 
-
                 if (!value)
                 {
                     Root.Instance.Remove(this.InstanceId);
@@ -195,6 +190,7 @@ namespace ET
                         Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
                         return;
                     }
+
                     this.parent.RemoveFromChildren(this);
                 }
 
@@ -202,6 +198,20 @@ namespace ET
                 this.IsComponent = false;
                 this.parent.AddToChildren(this);
                 this.Domain = this.parent.domain;
+
+#if ENABLE_VIEW && UNITY_EDITOR
+                this.viewGO.GetComponent<ComponentView>().Component = this;
+                this.viewGO.transform.SetParent(this.Parent == null ?
+                        UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
+                foreach (var child in this.Children.Values)
+                {
+                    child.viewGO.transform.SetParent(this.viewGO.transform);
+                }
+                foreach (var comp in this.Components.Values)
+                {
+                    comp.viewGO.transform.SetParent(this.viewGO.transform);
+                }
+#endif
             }
         }
 
@@ -235,6 +245,7 @@ namespace ET
                         Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
                         return;
                     }
+
                     this.parent.RemoveFromComponents(this);
                 }
 
@@ -254,11 +265,7 @@ namespace ET
         [BsonDefaultValue(0L)]
         [BsonElement]
         [BsonId]
-        public long Id
-        {
-            get;
-            set;
-        }
+        public long Id { get; set; }
 
         [BsonIgnore]
         protected Entity domain;
@@ -296,7 +303,7 @@ namespace ET
                         foreach (Entity component in this.componentsDB)
                         {
                             component.IsComponent = true;
-                            this.Components.Add(component.GetType(), component);
+                            this.Components.Add(component.GetType().Name, component);
                             component.parent = this;
                         }
                     }
@@ -339,24 +346,23 @@ namespace ET
 
         [BsonElement("Children")]
         [BsonIgnoreIfNull]
-        private HashSet<Entity> childrenDB;
+        private List<Entity> childrenDB;
 
         [BsonIgnore]
-        private Dictionary<long, Entity> children;
+        private SortedDictionary<long, Entity> children;
 
         [BsonIgnore]
-        public Dictionary<long, Entity> Children
+        public SortedDictionary<long, Entity> Children
         {
             get
             {
-                return this.children ??= ObjectPool.Instance.Fetch<Dictionary<long, Entity>>();
+                return this.children ??= ObjectPool.Instance.Fetch<SortedDictionary<long, Entity>>();
             }
         }
 
         private void AddToChildren(Entity entity)
         {
             this.Children.Add(entity.Id, entity);
-            this.AddToChildrenDB(entity);
         }
 
         private void RemoveFromChildren(Entity entity)
@@ -373,56 +379,21 @@ namespace ET
                 ObjectPool.Instance.Recycle(this.children);
                 this.children = null;
             }
-
-            this.RemoveFromChildrenDB(entity);
-        }
-
-        private void AddToChildrenDB(Entity entity)
-        {
-            if (!(entity is ISerializeToEntity))
-            {
-                return;
-            }
-
-            this.childrenDB ??= ObjectPool.Instance.Fetch<HashSet<Entity>>();
-
-            this.childrenDB.Add(entity);
-        }
-
-        private void RemoveFromChildrenDB(Entity entity)
-        {
-            if (!(entity is ISerializeToEntity))
-            {
-                return;
-            }
-
-            if (this.childrenDB == null)
-            {
-                return;
-            }
-
-            this.childrenDB.Remove(entity);
-
-            if (this.childrenDB.Count == 0 && this.IsNew)
-            {
-                ObjectPool.Instance.Recycle(this.childrenDB);
-                this.childrenDB = null;
-            }
         }
 
         [BsonElement("C")]
         [BsonIgnoreIfNull]
-        private HashSet<Entity> componentsDB;
+        private List<Entity> componentsDB;
 
         [BsonIgnore]
-        private Dictionary<Type, Entity> components;
+        private SortedDictionary<string, Entity> components;
 
         [BsonIgnore]
-        public Dictionary<Type, Entity> Components
+        public SortedDictionary<string, Entity> Components
         {
             get
             {
-                return this.components ??= ObjectPool.Instance.Fetch<Dictionary<Type, Entity>>();
+                return this.components ??= ObjectPool.Instance.Fetch<SortedDictionary<string, Entity>>();
             }
         }
 
@@ -463,7 +434,7 @@ namespace ET
             // 清理Component
             if (this.components != null)
             {
-                foreach (KeyValuePair<Type, Entity> kv in this.components)
+                foreach (KeyValuePair<string, Entity> kv in this.components)
                 {
                     kv.Value.Dispose();
                 }
@@ -512,44 +483,13 @@ namespace ET
             {
                 ObjectPool.Instance.Recycle(this);
             }
-            status = EntityStatus.None;
-        }
-
-        private void AddToComponentsDB(Entity component)
-        {
-            if (!(component is ISerializeToEntity))
-            {
-                return;
-            }
 
-            this.componentsDB ??= ObjectPool.Instance.Fetch<HashSet<Entity>>();
-            this.componentsDB.Add(component);
-        }
-
-        private void RemoveFromComponentsDB(Entity component)
-        {
-            if (!(component is ISerializeToEntity))
-            {
-                return;
-            }
-
-            if (this.componentsDB == null)
-            {
-                return;
-            }
-
-            this.componentsDB.Remove(component);
-            if (this.componentsDB.Count == 0 && this.IsNew)
-            {
-                ObjectPool.Instance.Recycle(this.componentsDB);
-                this.componentsDB = null;
-            }
+            status = EntityStatus.None;
         }
 
         private void AddToComponents(Entity component)
         {
-            this.Components.Add(component.GetType(), component);
-            this.AddToComponentsDB(component);
+            this.Components.Add(component.GetType().Name, component);
         }
 
         private void RemoveFromComponents(Entity component)
@@ -559,23 +499,22 @@ namespace ET
                 return;
             }
 
-            this.components.Remove(component.GetType());
+            this.components.Remove(component.GetType().Name);
 
             if (this.components.Count == 0)
             {
                 ObjectPool.Instance.Recycle(this.components);
                 this.components = null;
             }
-
-            this.RemoveFromComponentsDB(component);
         }
 
-        public K GetChild<K>(long id) where K: Entity
+        public K GetChild<K>(long id) where K : Entity
         {
             if (this.children == null)
             {
                 return null;
             }
+
             this.children.TryGetValue(id, out Entity child);
             return child as K;
         }
@@ -671,7 +610,7 @@ namespace ET
             }
 
             Entity component;
-            if (!this.components.TryGetValue(typeof (K), out component))
+            if (!this.components.TryGetValue(typeof (K).Name, out component))
             {
                 return default;
             }
@@ -693,7 +632,7 @@ namespace ET
             }
 
             Entity component;
-            if (!this.components.TryGetValue(type, out component))
+            if (!this.components.TryGetValue(type.Name, out component))
             {
                 return null;
             }
@@ -712,12 +651,13 @@ namespace ET
             Entity component;
             if (isFromPool)
             {
-                component = (Entity)ObjectPool.Instance.Fetch(type);
+                component = (Entity) ObjectPool.Instance.Fetch(type);
             }
             else
             {
                 component = Activator.CreateInstance(type) as Entity;
             }
+
             component.IsFromPool = isFromPool;
             component.IsCreated = true;
             component.IsNew = true;
@@ -728,7 +668,7 @@ namespace ET
         public Entity AddComponent(Entity component)
         {
             Type type = component.GetType();
-            if (this.components != null && this.components.ContainsKey(type))
+            if (this.components != null && this.components.ContainsKey(type.Name))
             {
                 throw new Exception($"entity already has component: {type.FullName}");
             }
@@ -739,12 +679,13 @@ namespace ET
             {
                 EventSystem.Instance.AddComponent(this, component);
             }
+
             return component;
         }
 
         public Entity AddComponent(Type type, bool isFromPool = false)
         {
-            if (this.components != null && this.components.ContainsKey(type))
+            if (this.components != null && this.components.ContainsKey(type.Name))
             {
                 throw new Exception($"entity already has component: {type.FullName}");
             }
@@ -758,19 +699,20 @@ namespace ET
             {
                 EventSystem.Instance.AddComponent(this, component);
             }
+
             return component;
         }
 
-        public K AddComponent<K>(bool isFromPool = false) where K : Entity, IAwake, new()
+        public K AddComponentWithId<K>(long id, bool isFromPool = false) where K : Entity, IAwake, new()
         {
             Type type = typeof (K);
-            if (this.components != null && this.components.ContainsKey(type))
+            if (this.components != null && this.components.ContainsKey(type.Name))
             {
                 throw new Exception($"entity already has component: {type.FullName}");
             }
 
             Entity component = Create(type, isFromPool);
-            component.Id = this.Id;
+            component.Id = id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component);
 
@@ -778,19 +720,20 @@ namespace ET
             {
                 EventSystem.Instance.AddComponent(this, component);
             }
+
             return component as K;
         }
 
-        public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
+        public K AddComponentWithId<K, P1>(long id, P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
         {
             Type type = typeof (K);
-            if (this.components != null && this.components.ContainsKey(type))
+            if (this.components != null && this.components.ContainsKey(type.Name))
             {
                 throw new Exception($"entity already has component: {type.FullName}");
             }
 
             Entity component = Create(type, isFromPool);
-            component.Id = this.Id;
+            component.Id = id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component, p1);
 
@@ -798,19 +741,20 @@ namespace ET
             {
                 EventSystem.Instance.AddComponent(this, component);
             }
+
             return component as K;
         }
 
-        public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
+        public K AddComponentWithId<K, P1, P2>(long id, P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
         {
             Type type = typeof (K);
-            if (this.components != null && this.components.ContainsKey(type))
+            if (this.components != null && this.components.ContainsKey(type.Name))
             {
                 throw new Exception($"entity already has component: {type.FullName}");
             }
 
             Entity component = Create(type, isFromPool);
-            component.Id = this.Id;
+            component.Id = id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component, p1, p2);
 
@@ -818,19 +762,20 @@ namespace ET
             {
                 EventSystem.Instance.AddComponent(this, component);
             }
+
             return component as K;
         }
 
-        public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
+        public K AddComponentWithId<K, P1, P2, P3>(long id, P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
         {
             Type type = typeof (K);
-            if (this.components != null && this.components.ContainsKey(type))
+            if (this.components != null && this.components.ContainsKey(type.Name))
             {
                 throw new Exception($"entity already has component: {type.FullName}");
             }
 
             Entity component = Create(type, isFromPool);
-            component.Id = this.Id;
+            component.Id = id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component, p1, p2, p3);
 
@@ -838,9 +783,30 @@ namespace ET
             {
                 EventSystem.Instance.AddComponent(this, component);
             }
+
             return component as K;
         }
 
+        public K AddComponent<K>(bool isFromPool = false) where K : Entity, IAwake, new()
+        {
+            return this.AddComponentWithId<K>(this.Id, isFromPool);
+        }
+
+        public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
+        {
+            return this.AddComponentWithId<K, P1>(this.Id, p1, isFromPool);
+        }
+
+        public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
+        {
+            return this.AddComponentWithId<K, P1, P2>(this.Id, p1, p2, isFromPool);
+        }
+
+        public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
+        {
+            return this.AddComponentWithId<K, P1, P2, P3>(this.Id, p1, p2, p3, isFromPool);
+        }
+
         public Entity AddChild(Entity entity)
         {
             entity.Parent = this;
@@ -944,5 +910,42 @@ namespace ET
             EventSystem.Instance.Awake(component, a, b, c);
             return component;
         }
+
+        public override void BeginInit()
+        {
+            this.componentsDB?.Clear();
+            if (this.components != null && this.components.Count != 0)
+            {
+                foreach (Entity entity in this.components.Values)
+                {
+                    if (entity is not ISerializeToEntity)
+                    {
+                        continue;
+                    }
+
+                    this.componentsDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
+                    this.componentsDB.Add(entity);
+
+                    entity.BeginInit();
+                }
+            }
+
+            this.childrenDB?.Clear();
+            if (this.children != null && this.children.Count != 0)
+            {
+                foreach (Entity entity in this.children.Values)
+                {
+                    if (entity is not ISerializeToEntity)
+                    {
+                        continue;
+                    }
+
+                    this.childrenDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
+                    this.childrenDB.Add(entity);
+
+                    entity.BeginInit();
+                }
+            }
+        }
     }
 }

+ 10 - 0
Unity/Assets/Scripts/Core/Object/Object.cs

@@ -6,5 +6,15 @@
         {
             return JsonHelper.ToJson(this);
         }
+
+        public string ToJson()
+        {
+            return MongoHelper.ToJson(this);
+        }
+
+        public byte[] ToBson()
+        {
+            return MongoHelper.Serialize(this);
+        }
     }
 }

+ 17 - 1
Unity/Assets/Scripts/Core/Serialize/MongoHelper.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.IO;
 using System.Reflection;
 using MongoDB.Bson;
@@ -106,7 +107,6 @@ namespace ET
                     continue;
                 }
 
-                //Log.Debug($"LookupClassMap: {type.Name}");
                 BsonClassMap.LookupClassMap(type);
             }
         }
@@ -122,11 +122,19 @@ namespace ET
 
         public static string ToJson(object obj)
         {
+            if (obj is ISupportInitialize supportInitialize)
+            {
+                supportInitialize.BeginInit();
+            }
             return obj.ToJson(defaultSettings);
         }
 
         public static string ToJson(object obj, JsonWriterSettings settings)
         {
+            if (obj is ISupportInitialize supportInitialize)
+            {
+                supportInitialize.BeginInit();
+            }
             return obj.ToJson(settings);
         }
 
@@ -149,11 +157,19 @@ namespace ET
 
         public static byte[] Serialize(object obj)
         {
+            if (obj is ISupportInitialize supportInitialize)
+            {
+                supportInitialize.BeginInit();
+            }
             return obj.ToBson();
         }
 
         public static void Serialize(object message, MemoryStream stream)
         {
+            if (message is ISupportInitialize supportInitialize)
+            {
+                supportInitialize.BeginInit();
+            }
             using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
             {
                 BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);