LSEntitySystemSingleton.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. public class LSEntitySystemSingleton: SingletonLock<LSEntitySystemSingleton>, ISingletonAwake
  13. {
  14. public TypeSystems TypeSystems { get; private set; }
  15. public void Awake()
  16. {
  17. this.TypeSystems = new(LSQueneUpdateIndex.Max);
  18. foreach (Type type in EventSystem.Instance.GetTypes(typeof (LSEntitySystemAttribute)))
  19. {
  20. object obj = Activator.CreateInstance(type);
  21. if (obj is not ISystemType iSystemType)
  22. {
  23. continue;
  24. }
  25. TypeSystems.OneTypeSystems oneTypeSystems = this.TypeSystems.GetOrCreateOneTypeSystems(iSystemType.Type());
  26. oneTypeSystems.Map.Add(iSystemType.SystemType(), obj);
  27. int index = iSystemType.GetInstanceQueueIndex();
  28. if (index > LSQueneUpdateIndex.None && index < LSQueneUpdateIndex.Max)
  29. {
  30. oneTypeSystems.QueueFlag[index] = true;
  31. }
  32. }
  33. }
  34. public TypeSystems.OneTypeSystems GetOneTypeSystems(Type type)
  35. {
  36. return this.TypeSystems.GetOneTypeSystems(type);
  37. }
  38. public void LSRollback(Entity entity)
  39. {
  40. if (entity is not ILSRollback)
  41. {
  42. return;
  43. }
  44. List<object> iLSRollbackSystems = this.TypeSystems.GetSystems(entity.GetType(), typeof (ILSRollbackSystem));
  45. if (iLSRollbackSystems == null)
  46. {
  47. return;
  48. }
  49. foreach (ILSRollbackSystem iLSRollbackSystem in iLSRollbackSystems)
  50. {
  51. if (iLSRollbackSystem == null)
  52. {
  53. continue;
  54. }
  55. try
  56. {
  57. iLSRollbackSystem.Run(entity);
  58. }
  59. catch (Exception e)
  60. {
  61. Log.Error(e);
  62. }
  63. }
  64. }
  65. public void LSUpdate(LSEntity entity)
  66. {
  67. if (entity is not ILSUpdate)
  68. {
  69. return;
  70. }
  71. List<object> iLSUpdateSystems = TypeSystems.GetSystems(entity.GetType(), typeof (ILSUpdateSystem));
  72. if (iLSUpdateSystems == null)
  73. {
  74. return;
  75. }
  76. foreach (ILSUpdateSystem iLSUpdateSystem in iLSUpdateSystems)
  77. {
  78. try
  79. {
  80. iLSUpdateSystem.Run(entity);
  81. }
  82. catch (Exception e)
  83. {
  84. Log.Error(e);
  85. }
  86. }
  87. }
  88. }
  89. }