Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using Model;
  6. using MongoDB.Bson.Serialization.Attributes;
  7. namespace Hotfix
  8. {
  9. [BsonIgnoreExtraElements]
  10. public class Entity : Disposer, ISupportInitialize
  11. {
  12. [BsonId]
  13. [BsonElement(Order = 1)]
  14. public sealed override long Id { get; set; }
  15. [BsonIgnore]
  16. public Entity Parent { get; set; }
  17. [BsonElement]
  18. [BsonIgnoreIfNull]
  19. private HashSet<Component> components = new HashSet<Component>();
  20. [BsonIgnore]
  21. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  22. protected Entity()
  23. {
  24. this.Id = IdGenerater.GenerateId();
  25. }
  26. protected Entity(long id)
  27. {
  28. this.Id = id;
  29. }
  30. public override void Dispose()
  31. {
  32. if (this.Id == 0)
  33. {
  34. return;
  35. }
  36. base.Dispose();
  37. foreach (Component component in this.GetComponents())
  38. {
  39. try
  40. {
  41. component.Dispose();
  42. }
  43. catch (Exception e)
  44. {
  45. Log.Error(e.ToString());
  46. }
  47. }
  48. }
  49. public K AddComponent<K>() where K : Component, new()
  50. {
  51. K component = ComponentFactory.Create<K>(this);
  52. if (this.componentDict.ContainsKey(component.GetType()))
  53. {
  54. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  55. }
  56. if (this.components == null)
  57. {
  58. this.components = new HashSet<Component>();
  59. }
  60. if (component is ComponentDB)
  61. {
  62. this.components.Add(component);
  63. }
  64. this.componentDict.Add(component.GetType(), component);
  65. return component;
  66. }
  67. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  68. {
  69. K component = ComponentFactory.Create<K, P1>(this, p1);
  70. if (this.componentDict.ContainsKey(component.GetType()))
  71. {
  72. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  73. }
  74. if (this.components == null)
  75. {
  76. this.components = new HashSet<Component>();
  77. }
  78. if (component is ComponentDB)
  79. {
  80. this.components.Add(component);
  81. }
  82. this.componentDict.Add(component.GetType(), component);
  83. return component;
  84. }
  85. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  86. {
  87. K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
  88. if (this.componentDict.ContainsKey(component.GetType()))
  89. {
  90. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  91. }
  92. if (this.components == null)
  93. {
  94. this.components = new HashSet<Component>();
  95. }
  96. if (component is ComponentDB)
  97. {
  98. this.components.Add(component);
  99. }
  100. this.componentDict.Add(component.GetType(), component);
  101. return component;
  102. }
  103. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  104. {
  105. K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
  106. if (this.componentDict.ContainsKey(component.GetType()))
  107. {
  108. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  109. }
  110. if (this.components == null)
  111. {
  112. this.components = new HashSet<Component>();
  113. }
  114. if (component is ComponentDB)
  115. {
  116. this.components.Add(component);
  117. }
  118. this.componentDict.Add(component.GetType(), component);
  119. return component;
  120. }
  121. public void RemoveComponent<K>() where K : Component
  122. {
  123. Component component;
  124. if (!this.componentDict.TryGetValue(typeof(K), out component))
  125. {
  126. return;
  127. }
  128. this.components?.Remove(component);
  129. this.componentDict.Remove(typeof(K));
  130. if (this.components != null && this.components.Count == 0)
  131. {
  132. this.components = null;
  133. }
  134. component.Dispose();
  135. }
  136. public void RemoveComponent(Type type)
  137. {
  138. Component component;
  139. if (!this.componentDict.TryGetValue(type, out component))
  140. {
  141. return;
  142. }
  143. this.components?.Remove(component);
  144. this.componentDict.Remove(type);
  145. if (this.components != null && this.components.Count == 0)
  146. {
  147. this.components = null;
  148. }
  149. component.Dispose();
  150. }
  151. public K GetComponent<K>() where K : Component
  152. {
  153. Component component;
  154. if (!this.componentDict.TryGetValue(typeof(K), out component))
  155. {
  156. return default(K);
  157. }
  158. return (K)component;
  159. }
  160. public Component[] GetComponents()
  161. {
  162. return this.componentDict.Values.ToArray();
  163. }
  164. public virtual void BeginInit()
  165. {
  166. this.components = new HashSet<Component>();
  167. this.componentDict = new Dictionary<Type, Component>();
  168. }
  169. public virtual void EndInit()
  170. {
  171. ObjectEvents.Instance.Add(this);
  172. if (this.components != null && this.components.Count == 0)
  173. {
  174. this.components = null;
  175. }
  176. if (this.components != null)
  177. {
  178. foreach (Component component in this.components)
  179. {
  180. component.Entity = this;
  181. this.componentDict.Add(component.GetType(), component);
  182. }
  183. }
  184. }
  185. }
  186. }