From d1baf62bea385c75ad4e7612e5caa285400ffaa7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 20 Feb 2025 21:39:04 -0500 Subject: Opening a door via an item! --- Plugin.cs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'Plugin.cs') diff --git a/Plugin.cs b/Plugin.cs index 5f49f5f..50e6e2b 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -1,5 +1,8 @@ using BepInEx; using BepInEx.Logging; +using HarmonyLib; +using System.Linq; +using System.Reflection; namespace ManifoldGardenArchipelago { @@ -8,11 +11,82 @@ namespace ManifoldGardenArchipelago { internal static new ManualLogSource Logger; + public static ArchipelagoManager archipelagoManager = new(); + private void Awake() { // Plugin startup logic Logger = base.Logger; Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!"); + + Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly()); + + archipelagoManager.Connect("localhost:38281", "Manifold", "").RunSynchronously(); + } + } + + [HarmonyPatch(typeof(GameManager), "Update")] + static class GameManagerUpdatePatch + { + static void Postfix() + { + Plugin.archipelagoManager.Update(); } } + + [HarmonyPatch(typeof(LineDoorController), "SetDoorOpenCloseStateAnimated")] + static class LineDoorControllerSetDoorOpenCloseStateAnimatedPatch + { + static bool Prefix(LineDoorController __instance, bool open) + { + if (Plugin.archipelagoManager.chainListenersByScene.TryGetValue(__instance.gameObject.scene.name, out var activatedChainListeners)) + { + LevelSystems levelSystem = __instance.gameObject.scene.GetRootGameObjects().Single((obj) => obj.name == "Level Systems").GetComponent(); + + foreach (var listener in activatedChainListeners) + { + if (levelSystem.chainListeners[listener.Key] == __instance) + { + if (open != listener.Value) + { + return false; + } + } + } + } + + return true; + } + } + + [HarmonyPatch(typeof(LineDoorController), nameof(LineDoorController.OnLevelEnable))] + static class LineDoorControllerOnLevelEnablePatch + { + static void Prefix(LineDoorController __instance) + { + if (Plugin.archipelagoManager.chainListenersByScene.TryGetValue(__instance.gameObject.scene.name, out var activatedChainListeners)) + { + LevelSystems levelSystem = __instance.gameObject.scene.GetRootGameObjects().Single((obj) => obj.name == "Level Systems").GetComponent(); + + foreach (var listener in activatedChainListeners) + { + if (levelSystem.chainListeners[listener.Key] == __instance) + { + FieldInfo fieldInfo = typeof(LineDoorController).GetField("doorShouldOpenImmediatelyOnEnable", BindingFlags.Instance | BindingFlags.NonPublic); + fieldInfo.SetValue(__instance, listener.Value); + } + } + } + } + } + + [HarmonyPatch(typeof(ButtonLineActivator), nameof(ButtonLineActivator.OnInteract))] + static class ButtonLineActivatorOnInteractPatch + { + public static void Postfix(ButtonLineActivator __instance) + { + Plugin.Logger.LogInfo($"Interacted with {__instance.name} in {__instance.gameObject.scene.name}: {__instance.isButtonPressed}"); + } + } + } -- cgit 1.4.1