Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.6KB

  1. using HarmonyLib;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS;
  4. namespace CLre.API.Engines
  5. {
  6. /// <summary>
  7. /// Engine to be registered before vanilla engines are built.
  8. /// This should be called by any other constructor because this alerts CLre of its existence.
  9. /// </summary>
  10. public abstract class FrontEndEnginePreBuild : ICLreEngine
  11. {
  12. /// <summary>
  13. /// Construct a new instance of a FrontEndEngine.
  14. /// This should be called by any other constructor because this alerts CLre of its existence.
  15. /// </summary>
  16. public FrontEndEnginePreBuild()
  17. {
  18. MainFrontEnd_BuildEngines_Patch.beforeBuildEngines.Add(this);
  19. }
  20. public abstract void Ready();
  21. public abstract IEntitiesDB entitiesDB { get; set; }
  22. public abstract IEntityFactory entityFactory { get; set; }
  23. }
  24. /// <summary>
  25. /// Engine to be registered before obsolete vanilla engines are built.
  26. /// This should be called by any other constructor because this alerts CLre of its existence.
  27. /// </summary>
  28. public abstract class FrontEndObsoleteEnginePreBuild : ICLreEngine
  29. {
  30. /// <summary>
  31. /// Construct a new instance of a FrontEndEngine.
  32. /// This should be called by any other constructor because this alerts CLre of its existence.
  33. /// </summary>
  34. public FrontEndObsoleteEnginePreBuild()
  35. {
  36. MainFrontEnd_BuildObsoleteEngines_Patch.beforeBuildEngines.Add(this);
  37. }
  38. public abstract void Ready();
  39. public abstract IEntitiesDB entitiesDB { get; set; }
  40. public abstract IEntityFactory entityFactory { get; set; }
  41. }
  42. /// <summary>
  43. /// Engine to be registered after vanilla engines are built.
  44. /// </summary>
  45. public abstract class FrontEndEnginePostBuild : ICLreEngine
  46. {
  47. /// <summary>
  48. /// Construct a new instance of a FrontEndEngine.
  49. /// This should be called by any other constructor because this alerts CLre of its existence.
  50. /// </summary>
  51. public FrontEndEnginePostBuild()
  52. {
  53. MainFrontEnd_BuildEngines_Patch.afterBuildEngines.Add(this);
  54. }
  55. public abstract void Ready();
  56. public abstract IEntitiesDB entitiesDB { get; set; }
  57. public abstract IEntityFactory entityFactory { get; set; }
  58. }
  59. /// <summary>
  60. /// Engine to be registered after vanilla obsolete engines are built.
  61. /// </summary>
  62. public abstract class FrontEndObsoleteEnginePostBuild : ICLreEngine
  63. {
  64. /// <summary>
  65. /// Construct a new instance of a FrontEndEngine.
  66. /// This should be called by any other constructor because this alerts CLre of its existence.
  67. /// </summary>
  68. public FrontEndObsoleteEnginePostBuild()
  69. {
  70. MainFrontEnd_BuildObsoleteEngines_Patch.afterBuildEngines.Add(this);
  71. }
  72. public abstract void Ready();
  73. public abstract IEntitiesDB entitiesDB { get; set; }
  74. public abstract IEntityFactory entityFactory { get; set; }
  75. }
  76. [HarmonyPatch(typeof(FrontEnd.MainFrontEnd), "BuildEngines")]
  77. class MainFrontEnd_BuildEngines_Patch
  78. {
  79. internal static FasterList<FrontEndEnginePreBuild> beforeBuildEngines = new FasterList<FrontEndEnginePreBuild>();
  80. internal static FasterList<FrontEndEnginePostBuild> afterBuildEngines = new FasterList<FrontEndEnginePostBuild>();
  81. [HarmonyPrefix]
  82. public static void BeforeMethodCall(FrontEnd.MainFrontEnd __instance)
  83. {
  84. IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory;
  85. foreach (ICLreEngine e in beforeBuildEngines)
  86. {
  87. e.entityFactory = factory;
  88. __instance.AddEngine(e);
  89. }
  90. }
  91. [HarmonyPostfix]
  92. public static void AfterMethodCall(FrontEnd.MainFrontEnd __instance)
  93. {
  94. IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory;
  95. foreach (ICLreEngine e in afterBuildEngines)
  96. {
  97. e.entityFactory = factory;
  98. __instance.AddEngine(e);
  99. }
  100. }
  101. }
  102. [HarmonyPatch(typeof(FrontEnd.MainFrontEnd), "BuildObsoleteEngines")]
  103. class MainFrontEnd_BuildObsoleteEngines_Patch
  104. {
  105. internal static FasterList<FrontEndObsoleteEnginePreBuild> beforeBuildEngines = new FasterList<FrontEndObsoleteEnginePreBuild>();
  106. internal static FasterList<FrontEndObsoleteEnginePostBuild> afterBuildEngines = new FasterList<FrontEndObsoleteEnginePostBuild>();
  107. [HarmonyPrefix]
  108. public static void BeforeMethodCall(FrontEnd.MainFrontEnd __instance)
  109. {
  110. IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory;
  111. foreach (ICLreEngine e in beforeBuildEngines)
  112. {
  113. e.entityFactory = factory;
  114. __instance.AddEngine(e);
  115. }
  116. }
  117. [HarmonyPostfix]
  118. public static void AfterMethodCall(FrontEnd.MainFrontEnd __instance)
  119. {
  120. IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory;
  121. foreach (ICLreEngine e in afterBuildEngines)
  122. {
  123. e.entityFactory = factory;
  124. __instance.AddEngine(e);
  125. }
  126. }
  127. }
  128. }