LSEntitySystemSingleton.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. [UniqueId(-1, 1)]
  6. public static class LSQueneUpdateIndex
  7. {
  8. public const int None = -1;
  9. public const int LSUpdate = 0;
  10. public const int Max = 1;
  11. }
  12. [Code]
  13. public class LSEntitySystemSingleton: Singleton<LSEntitySystemSingleton>, ISingletonAwake
  14. {
  15. public TypeSystems TypeSystems { get; private set; }
  16. private readonly DoubleMap<Type, long> lsEntityTypeLongHashCode = new();
  17. public void Awake()
  18. {
  19. this.TypeSystems = new(LSQueneUpdateIndex.Max);
  20. foreach (Type type in CodeTypes.Instance.GetTypes(typeof (LSEntitySystemAttribute)))
  21. {
  22. object obj = Activator.CreateInstance(type);
  23. if (obj is not ISystemType iSystemType)
  24. {
  25. continue;
  26. }
  27. TypeSystems.OneTypeSystems oneTypeSystems = this.TypeSystems.GetOrCreateOneTypeSystems(iSystemType.Type());
  28. oneTypeSystems.Map.Add(iSystemType.SystemType(), obj);
  29. int index = iSystemType.GetInstanceQueueIndex();
  30. if (index > LSQueneUpdateIndex.None && index < LSQueneUpdateIndex.Max)
  31. {
  32. oneTypeSystems.QueueFlag[index] = true;
  33. }
  34. }
  35. foreach (var kv in CodeTypes.Instance.GetTypes())
  36. {
  37. Type type = kv.Value;
  38. if (typeof(LSEntity).IsAssignableFrom(type))
  39. {
  40. this.lsEntityTypeLongHashCode.Add(type, type.FullName.GetLongHashCode());
  41. }
  42. }
  43. }
  44. public long GetLongHashCode(Type type)
  45. {
  46. return this.lsEntityTypeLongHashCode.GetValueByKey(type);
  47. }
  48. public TypeSystems.OneTypeSystems GetOneTypeSystems(Type type)
  49. {
  50. return this.TypeSystems.GetOneTypeSystems(type);
  51. }
  52. public void LSRollback(Entity entity)
  53. {
  54. if (entity is not ILSRollback)
  55. {
  56. return;
  57. }
  58. List<object> iLSRollbackSystems = this.TypeSystems.GetSystems(entity.GetType(), typeof (ILSRollbackSystem));
  59. if (iLSRollbackSystems == null)
  60. {
  61. return;
  62. }
  63. foreach (ILSRollbackSystem iLSRollbackSystem in iLSRollbackSystems)
  64. {
  65. if (iLSRollbackSystem == null)
  66. {
  67. continue;
  68. }
  69. try
  70. {
  71. iLSRollbackSystem.Run(entity);
  72. }
  73. catch (Exception e)
  74. {
  75. entity.Fiber().Error(e);
  76. }
  77. }
  78. }
  79. public void LSUpdate(LSEntity entity)
  80. {
  81. if (entity is not ILSUpdate)
  82. {
  83. return;
  84. }
  85. List<object> iLSUpdateSystems = TypeSystems.GetSystems(entity.GetType(), typeof (ILSUpdateSystem));
  86. if (iLSUpdateSystems == null)
  87. {
  88. return;
  89. }
  90. foreach (ILSUpdateSystem iLSUpdateSystem in iLSUpdateSystems)
  91. {
  92. try
  93. {
  94. iLSUpdateSystem.Run(entity);
  95. }
  96. catch (Exception e)
  97. {
  98. entity.Fiber().Error(e);
  99. }
  100. }
  101. }
  102. }
  103. }