ObjectManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using Base;
  7. namespace Model
  8. {
  9. public interface IObjectEvent
  10. {
  11. Type ValueType();
  12. void SetValue(object value);
  13. }
  14. public abstract class ObjectEvent<T> : IObjectEvent
  15. {
  16. private T value;
  17. protected T GetValue()
  18. {
  19. return value;
  20. }
  21. public void SetValue(object v)
  22. {
  23. this.value = (T)v;
  24. }
  25. public Type ValueType()
  26. {
  27. return typeof(T);
  28. }
  29. }
  30. public class ObjectManager : IDisposable
  31. {
  32. private readonly Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
  33. private Dictionary<Type, IObjectEvent> objectEvents;
  34. private readonly Dictionary<long, Object> objects = new Dictionary<long, Object>();
  35. private List<long> starts = new List<long>();
  36. private List<long> newStarts = new List<long>();
  37. private List<long> updates = new List<long>(3000);
  38. private List<long> newUpdates = new List<long>(3000);
  39. private readonly List<long> loaders = new List<long>();
  40. public void Dispose()
  41. {
  42. foreach (Object o in this.objects.Values.ToArray())
  43. {
  44. o.Dispose();
  45. }
  46. }
  47. public void Register(string name, Assembly assembly)
  48. {
  49. this.assemblies[name] = assembly;
  50. objectEvents = new Dictionary<Type, IObjectEvent>();
  51. foreach (Assembly ass in this.assemblies.Values)
  52. {
  53. Type[] types = ass.GetTypes();
  54. foreach (Type type in types)
  55. {
  56. object[] attrs = type.GetCustomAttributes(typeof(ObjectEventAttribute), false);
  57. if (attrs.Length == 0)
  58. {
  59. continue;
  60. }
  61. object obj = Activator.CreateInstance(type);
  62. IObjectEvent objectEvent = obj as IObjectEvent;
  63. if (objectEvent == null)
  64. {
  65. Log.Error($"组件事件没有继承IComponentEvent: {type.Name}");
  66. }
  67. objectEvents[objectEvent.ValueType()] = objectEvent;
  68. }
  69. }
  70. this.Load();
  71. }
  72. public Assembly GetAssembly(string name)
  73. {
  74. return this.assemblies[name];
  75. }
  76. public Assembly[] GetAssemblies()
  77. {
  78. return this.assemblies.Values.ToArray();
  79. }
  80. private void Load()
  81. {
  82. foreach (long id in this.loaders)
  83. {
  84. Object obj;
  85. if (!this.objects.TryGetValue(id, out obj))
  86. {
  87. continue;
  88. }
  89. IObjectEvent objectEvent;
  90. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  91. {
  92. continue;
  93. }
  94. ILoader iLoader = objectEvent as ILoader;
  95. if (iLoader == null)
  96. {
  97. continue;
  98. }
  99. objectEvent.SetValue(obj);
  100. iLoader.Load();
  101. }
  102. }
  103. public void Add(Object obj)
  104. {
  105. if (objectEvents == null)
  106. {
  107. return;
  108. }
  109. this.objects.Add(obj.Id, obj);
  110. IObjectEvent objectEvent;
  111. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  112. {
  113. return;
  114. }
  115. IStart iStart = objectEvent as IStart;
  116. if (iStart != null)
  117. {
  118. this.newStarts.Add(obj.Id);
  119. }
  120. IUpdate iUpdate = objectEvent as IUpdate;
  121. if (iUpdate != null)
  122. {
  123. this.newUpdates.Add(obj.Id);
  124. }
  125. ILoader iLoader = objectEvent as ILoader;
  126. if (iLoader != null)
  127. {
  128. this.loaders.Add(obj.Id);
  129. }
  130. }
  131. public void Remove(long id)
  132. {
  133. this.objects.Remove(id);
  134. }
  135. public void Open(long id)
  136. {
  137. Object obj;
  138. if (!objects.TryGetValue(id, out obj))
  139. {
  140. return;
  141. }
  142. IObjectEvent e;
  143. if (!objectEvents.TryGetValue(obj.GetType(), out e))
  144. {
  145. return;
  146. }
  147. IOpen open = e as IOpen;
  148. if (open == null)
  149. {
  150. return;
  151. }
  152. try
  153. {
  154. e.SetValue(obj);
  155. open.Open();
  156. }
  157. catch (Exception exc)
  158. {
  159. Log.Error(exc.ToString());
  160. }
  161. }
  162. public void Close(long id)
  163. {
  164. Object obj;
  165. if (!objects.TryGetValue(id, out obj))
  166. {
  167. return;
  168. }
  169. IObjectEvent e;
  170. if (!objectEvents.TryGetValue(obj.GetType(), out e))
  171. {
  172. return;
  173. }
  174. IClose close = e as IClose;
  175. if (close == null)
  176. {
  177. return;
  178. }
  179. try
  180. {
  181. e.SetValue(obj);
  182. close.Close();
  183. }
  184. catch (Exception exc)
  185. {
  186. Log.Error(exc.ToString());
  187. }
  188. }
  189. public void Awake(long id)
  190. {
  191. Object obj;
  192. if (!objects.TryGetValue(id, out obj))
  193. {
  194. return;
  195. }
  196. IObjectEvent objectEvent;
  197. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  198. {
  199. return;
  200. }
  201. IAwake iAwake = objectEvent as IAwake;
  202. if (iAwake == null)
  203. {
  204. return;
  205. }
  206. objectEvent.SetValue(obj);
  207. iAwake.Awake();
  208. }
  209. public void Awake<P1>(long id, P1 p1)
  210. {
  211. Object obj;
  212. if (!objects.TryGetValue(id, out obj))
  213. {
  214. return;
  215. }
  216. IObjectEvent objectEvent;
  217. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  218. {
  219. return;
  220. }
  221. IAwake<P1> iAwake = objectEvent as IAwake<P1>;
  222. if (iAwake == null)
  223. {
  224. return;
  225. }
  226. objectEvent.SetValue(obj);
  227. iAwake.Awake(p1);
  228. }
  229. public void Awake<P1, P2>(long id, P1 p1, P2 p2)
  230. {
  231. Object obj;
  232. if (!objects.TryGetValue(id, out obj))
  233. {
  234. return;
  235. }
  236. IObjectEvent objectEvent;
  237. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  238. {
  239. return;
  240. }
  241. IAwake<P1, P2> iAwake = objectEvent as IAwake<P1, P2>;
  242. if (iAwake == null)
  243. {
  244. return;
  245. }
  246. objectEvent.SetValue(obj);
  247. iAwake.Awake(p1, p2);
  248. }
  249. public void Awake<P1, P2, P3>(long id, P1 p1, P2 p2, P3 p3)
  250. {
  251. Object obj;
  252. if (!objects.TryGetValue(id, out obj))
  253. {
  254. return;
  255. }
  256. IObjectEvent objectEvent;
  257. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  258. {
  259. return;
  260. }
  261. IAwake<P1, P2, P3> iAwake = objectEvent as IAwake<P1, P2, P3>;
  262. if (iAwake == null)
  263. {
  264. return;
  265. }
  266. objectEvent.SetValue(obj);
  267. iAwake.Awake(p1, p2, p3);
  268. }
  269. private void Start()
  270. {
  271. starts = newStarts;
  272. newStarts = new List<long>();
  273. foreach (long id in starts)
  274. {
  275. Object obj;
  276. if (!this.objects.TryGetValue(id, out obj))
  277. {
  278. continue;
  279. }
  280. IObjectEvent objectEvent;
  281. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  282. {
  283. continue;
  284. }
  285. IStart iStart = objectEvent as IStart;
  286. if (iStart == null)
  287. {
  288. continue;
  289. }
  290. objectEvent.SetValue(obj);
  291. iStart.Start();
  292. }
  293. }
  294. public string IgnorComp = "";
  295. public int FrameCount = 0;
  296. private long _mainWatch = 0;
  297. private Dictionary<string, double> _timeCount = new Dictionary<string, double>();
  298. public bool UpdateEnable = true;
  299. public void Update()
  300. {
  301. this.Start();
  302. if (!UpdateEnable)
  303. {
  304. return;
  305. }
  306. // 交换update
  307. ++FrameCount;
  308. List<long> tmpUpdate = updates;
  309. updates = newUpdates;
  310. newUpdates = tmpUpdate;
  311. newUpdates.Clear();
  312. foreach (long id in updates)
  313. {
  314. Object obj;
  315. if (!objects.TryGetValue(id, out obj))
  316. {
  317. continue;
  318. }
  319. string fullName = obj.GetType().FullName;
  320. IObjectEvent objectEvent;
  321. if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
  322. {
  323. continue;
  324. }
  325. IUpdate iUpdate = objectEvent as IUpdate;
  326. if (iUpdate == null)
  327. {
  328. continue;
  329. }
  330. newUpdates.Add(id);
  331. objectEvent.SetValue(obj);
  332. try
  333. {
  334. if (fullName != IgnorComp)
  335. {
  336. iUpdate.Update();
  337. }
  338. }
  339. catch (Exception e)
  340. {
  341. Log.Error(e.ToString());
  342. }
  343. }
  344. }
  345. public override string ToString()
  346. {
  347. var info = new Dictionary<string, int>();
  348. foreach (Object obj in objects.Values)
  349. {
  350. if (info.ContainsKey(obj.GetType().Name))
  351. {
  352. info[obj.GetType().Name] += 1;
  353. }
  354. else
  355. {
  356. info[obj.GetType().Name] = 1;
  357. }
  358. }
  359. info = info.OrderByDescending(s => s.Value).ToDictionary(p => p.Key, p => p.Value);
  360. StringBuilder sb = new StringBuilder();
  361. sb.Append("\r\n");
  362. foreach (string key in info.Keys)
  363. {
  364. sb.Append($"{info[key],10} {key}\r\n");
  365. }
  366. sb.Append($"\r\n start: {newStarts.Count}, update: {newUpdates.Count} total: {this.objects.Count}");
  367. return sb.ToString();
  368. }
  369. public string ToFrameCount(bool recount)
  370. {
  371. StringBuilder builder = new StringBuilder();
  372. _timeCount = _timeCount.OrderByDescending(s => s.Value).ToDictionary(p => p.Key, p => p.Value);
  373. double value = 0;
  374. foreach (KeyValuePair<string, double> pair in _timeCount)
  375. {
  376. double preCe = pair.Value / _mainWatch;
  377. builder.Append($"{pair.Key} 占比:{(preCe * 100).ToString("f2")}%\n");
  378. value += preCe;
  379. }
  380. builder.Append($"总计:{(value * 100).ToString("f2")}%");
  381. if (recount)
  382. {
  383. FrameCount = 0;
  384. _timeCount.Clear();
  385. _mainWatch = 0;
  386. }
  387. return builder.ToString();
  388. }
  389. }
  390. }