LSEntitySystemSingleton.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. private TypeSystems TypeSystems { get; 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. SystemObject obj = (SystemObject)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. long hash = type.FullName.GetLongHashCode();
  41. try
  42. {
  43. this.lsEntityTypeLongHashCode.Add(type, type.FullName.GetLongHashCode());
  44. }
  45. catch (Exception e)
  46. {
  47. Type sameHashType = this.lsEntityTypeLongHashCode.GetKeyByValue(hash);
  48. throw new Exception($"long hash add fail: {type.FullName} {sameHashType.FullName}", e);
  49. }
  50. }
  51. }
  52. }
  53. public long GetLongHashCode(Type type)
  54. {
  55. return this.lsEntityTypeLongHashCode.GetValueByKey(type);
  56. }
  57. public TypeSystems.OneTypeSystems GetOneTypeSystems(Type type)
  58. {
  59. return this.TypeSystems.GetOneTypeSystems(type);
  60. }
  61. public void LSRollback(Entity entity)
  62. {
  63. if (entity is not ILSRollback)
  64. {
  65. return;
  66. }
  67. List<SystemObject> iLSRollbackSystems = this.TypeSystems.GetSystems(entity.GetType(), typeof (ILSRollbackSystem));
  68. if (iLSRollbackSystems == null)
  69. {
  70. return;
  71. }
  72. foreach (ILSRollbackSystem iLSRollbackSystem in iLSRollbackSystems)
  73. {
  74. if (iLSRollbackSystem == null)
  75. {
  76. continue;
  77. }
  78. try
  79. {
  80. iLSRollbackSystem.Run(entity);
  81. }
  82. catch (Exception e)
  83. {
  84. Log.Error(e);
  85. }
  86. }
  87. }
  88. public void LSUpdate(LSEntity entity)
  89. {
  90. if (entity is not ILSUpdate)
  91. {
  92. return;
  93. }
  94. List<SystemObject> iLSUpdateSystems = TypeSystems.GetSystems(entity.GetType(), typeof (ILSUpdateSystem));
  95. if (iLSUpdateSystems == null)
  96. {
  97. return;
  98. }
  99. foreach (ILSUpdateSystem iLSUpdateSystem in iLSUpdateSystems)
  100. {
  101. try
  102. {
  103. iLSUpdateSystem.Run(entity);
  104. }
  105. catch (Exception e)
  106. {
  107. Log.Error(e);
  108. }
  109. }
  110. }
  111. }
  112. }