diff --git a/BoneSync/BoneSync.csproj b/BoneSync/BoneSync.csproj index d0251e1..25ba64e 100644 --- a/BoneSync/BoneSync.csproj +++ b/BoneSync/BoneSync.csproj @@ -98,6 +98,7 @@ + diff --git a/BoneSync/Patching/ButtonTogglePatches.cs b/BoneSync/Patching/ButtonTogglePatches.cs new file mode 100644 index 0000000..e4555d8 --- /dev/null +++ b/BoneSync/Patching/ButtonTogglePatches.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BoneSync.Sync; +using BoneSync.Sync.Components; +using HarmonyLib; +using MelonLoader; +using StressLevelZero.Interaction; +using UnityEngine.Events; + +namespace BoneSync.Patching +{ + public class UnityEventPatch + { + private T arg; + private UnityEvent unityEvent; + private Action handler; + private UnityEventPatch(T arg, UnityEvent unityEvent, Action handler) + { + this.arg = arg; + this.unityEvent = unityEvent; + this.handler = handler; + } + + private void Listener() + { + handler(arg); + } + private void PatchUnityEvent() + { + if (unityEvent == null) return; + unityEvent.AddListener((UnityAction)Listener); + + } + + public static void Patch(T arg, UnityEvent unityEvent, Action handler) + { + UnityEventPatch patch = new UnityEventPatch(arg, unityEvent, handler); + patch.PatchUnityEvent(); + } + } + + [HarmonyPatch(typeof(ButtonToggle))] + internal class ButtonTogglePatches + { + [HarmonyPatch(nameof(ButtonToggle.OnEnable)), HarmonyPostfix] + private static void OnEnablePostfix(ButtonToggle __instance) + { + Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance); + } + } +} diff --git a/BoneSync/Patching/SceneManagerPatches.cs b/BoneSync/Patching/SceneManagerPatches.cs index 3efbb97..4f86945 100644 --- a/BoneSync/Patching/SceneManagerPatches.cs +++ b/BoneSync/Patching/SceneManagerPatches.cs @@ -13,7 +13,7 @@ namespace BoneSync.Patching [HarmonyPatch(typeof(BoneworksSceneManager))] internal class SceneManagerPatches { - [HarmonyPatch(nameof(BoneworksSceneManager.LoadScene)), HarmonyPostfix] + /*[HarmonyPatch(nameof(BoneworksSceneManager.LoadScene)), HarmonyPostfix] private static void LoadScenePrefix(int sceneBuildIndex) { MelonLogger.Msg("LoadScenePrefix: " + sceneBuildIndex); @@ -23,6 +23,6 @@ namespace BoneSync.Patching MelonLogger.Msg("Host is loading scene, sending message to clients..."); SceneSync.SendSceneSyncMessage(sceneBuildIndex); } - } + }*/ } } diff --git a/BoneSync/Sync/Components/SyncablePhysics.cs b/BoneSync/Sync/Components/SyncablePhysics.cs index ce94609..7ffb379 100644 --- a/BoneSync/Sync/Components/SyncablePhysics.cs +++ b/BoneSync/Sync/Components/SyncablePhysics.cs @@ -14,18 +14,6 @@ using UnityEngine; using UnityEngine.Events; namespace BoneSync.Sync.Components { - public static class UnityEventExtentions - { - public static void AddListenerWithArgsRaw(this UnityEvent unityEvent, Action action, object[] args) - { - unityEvent.AddListener((UnityAction)(() => action(args))); - } - - public static void AddListenerWithArgs(this UnityEvent unityEvent, Action action, params object[] args) - { - unityEvent.AddListenerWithArgsRaw((object[] argss) => action((T)argss[0], argss), args); - } - } public partial class Syncable : MonoBehaviour { private HashSet patchedButtonToggles = new HashSet(); @@ -86,8 +74,10 @@ namespace BoneSync.Sync.Components { if (patchedButtonToggles.Contains(buttonToggle)) return; MelonLogger.Msg("Patching ButtonToggle: " + buttonToggle.transform.GetPath()); - buttonToggle.onPress.AddListenerWithArgs((btn, args) => ButtonOnPress(btn), buttonToggle); - buttonToggle.onDepress.AddListenerWithArgs((btn, args) => ButtonOnRelease(btn), buttonToggle); + //buttonToggle.onPress.AddListenerWithArgs((btn, args) => ButtonOnPress(btn), buttonToggle); + //buttonToggle.onDepress.AddListenerWithArgs((btn, args) => ButtonOnRelease(btn), buttonToggle); + UnityEventPatch.Patch(buttonToggle, buttonToggle.onPress, ButtonOnPress); + UnityEventPatch.Patch(buttonToggle, buttonToggle.onDepress, ButtonOnRelease); patchedButtonToggles.Add(buttonToggle); } diff --git a/BoneSync/Sync/ObjectSync.cs b/BoneSync/Sync/ObjectSync.cs index bf15177..565d724 100644 --- a/BoneSync/Sync/ObjectSync.cs +++ b/BoneSync/Sync/ObjectSync.cs @@ -162,9 +162,18 @@ namespace BoneSync.Sync return syncable; } + public static Syncable MakeOrGetSyncable(ButtonToggle buttonToggle) + { + Syncable parentSyncable = buttonToggle.GetComponentInParent(); + if (parentSyncable) + { + return parentSyncable; + } + return _MakeOrGetSyncable(buttonToggle.gameObject); + } public static Syncable MakeOrGetSyncable(GameObject gameObject) { - Syncable syncable = _GetSyncableFromCache(gameObject); + Syncable syncable = _GetSyncableFromCache(gameObject); // buttons must never be a sub syncable if (syncable == null) { syncable = _MakeOrGetSyncable(gameObject);