LSEntitySystemSingleton.cs 2.9 KB

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