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.

149 lines
6.0KB

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