using HarmonyLib; using Svelto.DataStructures; using Svelto.ECS; namespace CLre.API.Engines { /// /// Engine to be registered before vanilla engines are built. /// This should be called by any other constructor because this alerts CLre of its existence. /// public abstract class FrontEndEnginePreBuild : ICLreEngine { /// /// Construct a new instance of a FrontEndEngine. /// This should be called by any other constructor because this alerts CLre of its existence. /// public FrontEndEnginePreBuild() { MainFrontEnd_BuildEngines_Patch.beforeBuildEngines.Add(this); } public abstract void Ready(); public abstract IEntitiesDB entitiesDB { get; set; } public abstract IEntityFactory entityFactory { get; set; } } /// /// Engine to be registered before obsolete vanilla engines are built. /// This should be called by any other constructor because this alerts CLre of its existence. /// public abstract class FrontEndObsoleteEnginePreBuild : ICLreEngine { /// /// Construct a new instance of a FrontEndEngine. /// This should be called by any other constructor because this alerts CLre of its existence. /// public FrontEndObsoleteEnginePreBuild() { MainFrontEnd_BuildObsoleteEngines_Patch.beforeBuildEngines.Add(this); } public abstract void Ready(); public abstract IEntitiesDB entitiesDB { get; set; } public abstract IEntityFactory entityFactory { get; set; } } /// /// Engine to be registered after vanilla engines are built. /// public abstract class FrontEndEnginePostBuild : ICLreEngine { /// /// Construct a new instance of a FrontEndEngine. /// This should be called by any other constructor because this alerts CLre of its existence. /// public FrontEndEnginePostBuild() { MainFrontEnd_BuildEngines_Patch.afterBuildEngines.Add(this); } public abstract void Ready(); public abstract IEntitiesDB entitiesDB { get; set; } public abstract IEntityFactory entityFactory { get; set; } } /// /// Engine to be registered after vanilla obsolete engines are built. /// public abstract class FrontEndObsoleteEnginePostBuild : ICLreEngine { /// /// Construct a new instance of a FrontEndEngine. /// This should be called by any other constructor because this alerts CLre of its existence. /// public FrontEndObsoleteEnginePostBuild() { MainFrontEnd_BuildObsoleteEngines_Patch.afterBuildEngines.Add(this); } public abstract void Ready(); public abstract IEntitiesDB entitiesDB { get; set; } public abstract IEntityFactory entityFactory { get; set; } } [HarmonyPatch(typeof(FrontEnd.MainFrontEnd), "BuildEngines")] class MainFrontEnd_BuildEngines_Patch { internal static FasterList beforeBuildEngines = new FasterList(); internal static FasterList afterBuildEngines = new FasterList(); [HarmonyPrefix] public static void BeforeMethodCall(FrontEnd.MainFrontEnd __instance) { IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory; foreach (ICLreEngine e in beforeBuildEngines) { e.entityFactory = factory; __instance.AddEngine(e); } } [HarmonyPostfix] public static void AfterMethodCall(FrontEnd.MainFrontEnd __instance) { IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory; foreach (ICLreEngine e in afterBuildEngines) { e.entityFactory = factory; __instance.AddEngine(e); } } } [HarmonyPatch(typeof(FrontEnd.MainFrontEnd), "BuildObsoleteEngines")] class MainFrontEnd_BuildObsoleteEngines_Patch { internal static FasterList beforeBuildEngines = new FasterList(); internal static FasterList afterBuildEngines = new FasterList(); [HarmonyPrefix] public static void BeforeMethodCall(FrontEnd.MainFrontEnd __instance) { IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory; foreach (ICLreEngine e in beforeBuildEngines) { e.entityFactory = factory; __instance.AddEngine(e); } } [HarmonyPostfix] public static void AfterMethodCall(FrontEnd.MainFrontEnd __instance) { IEntityFactory factory = AccessTools.Field(typeof(FrontEnd.MainFrontEnd), "_entityFactory").GetValue(__instance) as IEntityFactory; foreach (ICLreEngine e in afterBuildEngines) { e.entityFactory = factory; __instance.AddEngine(e); } } } }