tried to create a universal unity event hook

This commit is contained in:
2025-03-12 18:35:16 +02:00
parent a7769147e3
commit 756deee097
5 changed files with 71 additions and 17 deletions

View File

@@ -98,6 +98,7 @@
<Compile Include="Networking\Messages\GunSyncMessage.cs" />
<Compile Include="Networking\Messages\SceneChangeMessage.cs" />
<Compile Include="Patching\AIHealthPatches.cs" />
<Compile Include="Patching\ButtonTogglePatches.cs" />
<Compile Include="Patching\HolsterSlotPatches.cs" />
<Compile Include="Networking\Messages\MagazineSyncMessage.cs" />
<Compile Include="Networking\Messages\ObjectDamageMessage.cs" />

View File

@@ -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<T>
{
private T arg;
private UnityEvent unityEvent;
private Action<T> handler;
private UnityEventPatch(T arg, UnityEvent unityEvent, Action<T> 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<T> handler)
{
UnityEventPatch<T> patch = new UnityEventPatch<T>(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);
}
}
}

View File

@@ -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);
}
}
}*/
}
}

View File

@@ -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<object[]> action, object[] args)
{
unityEvent.AddListener((UnityAction)(() => action(args)));
}
public static void AddListenerWithArgs<T>(this UnityEvent unityEvent, Action<T, object[]> action, params object[] args)
{
unityEvent.AddListenerWithArgsRaw((object[] argss) => action((T)argss[0], argss), args);
}
}
public partial class Syncable : MonoBehaviour
{
private HashSet<ButtonToggle> patchedButtonToggles = new HashSet<ButtonToggle>();
@@ -86,8 +74,10 @@ namespace BoneSync.Sync.Components
{
if (patchedButtonToggles.Contains(buttonToggle)) return;
MelonLogger.Msg("Patching ButtonToggle: " + buttonToggle.transform.GetPath());
buttonToggle.onPress.AddListenerWithArgs<ButtonToggle>((btn, args) => ButtonOnPress(btn), buttonToggle);
buttonToggle.onDepress.AddListenerWithArgs<ButtonToggle>((btn, args) => ButtonOnRelease(btn), buttonToggle);
//buttonToggle.onPress.AddListenerWithArgs<ButtonToggle>((btn, args) => ButtonOnPress(btn), buttonToggle);
//buttonToggle.onDepress.AddListenerWithArgs<ButtonToggle>((btn, args) => ButtonOnRelease(btn), buttonToggle);
UnityEventPatch<ButtonToggle>.Patch(buttonToggle, buttonToggle.onPress, ButtonOnPress);
UnityEventPatch<ButtonToggle>.Patch(buttonToggle, buttonToggle.onDepress, ButtonOnRelease);
patchedButtonToggles.Add(buttonToggle);
}

View File

@@ -162,9 +162,18 @@ namespace BoneSync.Sync
return syncable;
}
public static Syncable MakeOrGetSyncable(ButtonToggle buttonToggle)
{
Syncable parentSyncable = buttonToggle.GetComponentInParent<Syncable>();
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);