Custom event classes for patched methods

Update to LeapCSharp 6.15.0
This commit is contained in:
SDraw 2024-04-26 23:52:25 +03:00
parent 4b879d53d5
commit 85925a7072
No known key found for this signature in database
GPG key ID: BB95B4DAB2BB8BB5
76 changed files with 3443 additions and 2187 deletions

99
ml_pmc/GameEvents.cs Normal file
View file

@ -0,0 +1,99 @@
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK;
using System;
using System.Reflection;
namespace ml_pmc
{
static class GameEvents
{
internal class GameEvent
{
event Action m_action;
public void AddHandler(Action p_listener) => m_action += p_listener;
public void RemoveHandler(Action p_listener) => m_action -= p_listener;
public void Invoke() => m_action?.Invoke();
}
public static readonly GameEvent OnAvatarSetup = new GameEvent();
public static readonly GameEvent OnAvatarClear = new GameEvent();
public static readonly GameEvent OnAvatarPreReuse = new GameEvent();
public static readonly GameEvent OnAvatarPostReuse = new GameEvent();
internal static void Init(HarmonyLib.Harmony p_instance)
{
try
{
p_instance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
p_instance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
p_instance.Patch(
typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public),
new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarReinitialize_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarReinitialize_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnAvatarClear_Postfix()
{
try
{
OnAvatarClear.Invoke();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnSetupAvatar_Postfix()
{
try
{
OnAvatarSetup.Invoke();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnAvatarReinitialize_Prefix()
{
try
{
OnAvatarPreReuse.Invoke();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnAvatarReinitialize_Postfix()
{
try
{
OnAvatarPostReuse.Invoke();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
}
}

View file

@ -1,55 +1,22 @@
using ABI_RC.Core.Networking.IO.Social;
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK;
using ABI_RC.Systems.Movement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using ABI_RC.Core.Player;
namespace ml_pmc
{
public class PlayerMovementCopycat : MelonLoader.MelonMod
{
static PlayerMovementCopycat ms_instance = null;
PoseCopycat m_localCopycat = null;
public override void OnInitializeMelon()
{
if(ms_instance == null)
ms_instance = this;
Settings.Init();
ModUi.Init();
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(PlayerMovementCopycat).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.NonPublic | BindingFlags.Static))
);
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(PlayerMovementCopycat).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public),
new HarmonyLib.HarmonyMethod(typeof(PlayerMovementCopycat).GetMethod(nameof(OnAvatarReinitialize_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
new HarmonyLib.HarmonyMethod(typeof(PlayerMovementCopycat).GetMethod(nameof(OnAvatarReinitialize_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
GameEvents.Init(HarmonyInstance);
MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
}
public override void OnDeinitializeMelon()
{
if(ms_instance == this)
ms_instance = null;
ModUi.CopySwitch -= this.OnTargetSelect;
if(m_localCopycat != null)
UnityEngine.Object.Destroy(m_localCopycat);
m_localCopycat = null;
@ -61,111 +28,6 @@ namespace ml_pmc
yield return null;
m_localCopycat = PlayerSetup.Instance.gameObject.AddComponent<PoseCopycat>();
ModUi.CopySwitch += this.OnTargetSelect;
}
void OnTargetSelect(string p_id)
{
if(m_localCopycat != null)
{
if(m_localCopycat.IsActive())
m_localCopycat.SetTarget(null);
else
{
if(Friends.FriendsWith(p_id))
{
if(CVRPlayerManager.Instance.GetPlayerPuppetMaster(p_id, out PuppetMaster l_puppetMaster))
{
if(IsInSight(BetterBetterCharacterController.Instance.KinematicTriggerProxy.Collider, l_puppetMaster.GetComponent<CapsuleCollider>(), Utils.GetWorldMovementLimit()))
m_localCopycat.SetTarget(l_puppetMaster);
else
ModUi.ShowAlert("Selected player is too far away or obstructed");
}
else
ModUi.ShowAlert("Selected player isn't connected or ready yet");
}
else
ModUi.ShowAlert("Selected player isn't your friend");
}
}
}
static bool IsInSight(CapsuleCollider p_source, CapsuleCollider p_target, float p_limit)
{
bool l_result = false;
if((p_source != null) && (p_target != null))
{
Ray l_ray = new Ray();
l_ray.origin = p_source.bounds.center;
l_ray.direction = p_target.bounds.center - l_ray.origin;
List<RaycastHit> l_hits = Physics.RaycastAll(l_ray, p_limit).ToList();
if(l_hits.Count > 0)
{
l_hits.RemoveAll(hit => hit.collider.gameObject.layer == LayerMask.NameToLayer("UI Internal")); // Somehow layer mask in RaycastAll just ignores players entirely
l_hits.RemoveAll(hit => hit.collider.gameObject.layer == LayerMask.NameToLayer("PlayerLocal"));
l_hits.RemoveAll(hit => hit.collider.gameObject.layer == LayerMask.NameToLayer("PlayerClone"));
l_hits.Sort((a, b) => ((a.distance < b.distance) ? -1 : 1));
l_result = (l_hits.First().collider.gameObject.transform.root == p_target.transform.root);
}
}
return l_result;
}
// Patches
static void OnAvatarClear_Postfix() => ms_instance?.OnAvatarClear();
void OnAvatarClear()
{
try
{
if(m_localCopycat != null)
m_localCopycat.OnAvatarClear();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnSetupAvatar_Postfix() => ms_instance?.OnSetupAvatar();
void OnSetupAvatar()
{
try
{
if(m_localCopycat != null)
m_localCopycat.OnAvatarSetup();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnAvatarReinitialize_Prefix() => ms_instance?.OnPreAvatarReinitialize();
void OnPreAvatarReinitialize()
{
try
{
if(m_localCopycat != null)
m_localCopycat.OnPreAvatarReinitialize();
}
catch(System.Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnAvatarReinitialize_Postfix() => ms_instance?.OnPostAvatarReinitialize();
void OnPostAvatarReinitialize()
{
try
{
if(m_localCopycat != null)
m_localCopycat.OnPostAvatarReinitialize();
}
catch(System.Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
}
}

View file

@ -9,6 +9,14 @@ namespace ml_pmc
{
static class ModUi
{
internal class UiEvent<T>
{
event Action<T> m_action;
public void AddHandler(Action<T> p_listener) => m_action += p_listener;
public void RemoveHandler(Action<T> p_listener) => m_action -= p_listener;
public void Invoke(T p_value) => m_action?.Invoke(p_value);
}
enum UiIndex
{
Toggle,
@ -22,7 +30,7 @@ namespace ml_pmc
Reset
}
internal static Action<string> CopySwitch;
public static readonly UiEvent<string> OnTargetSelect = new UiEvent<string>();
static List<QMUIElement> ms_uiElements = null;
static string ms_selectedPlayer;
@ -64,10 +72,10 @@ namespace ml_pmc
(ms_uiElements[(int)UiIndex.Reset] as Button).OnPress += Reset;
BTKUILib.QuickMenuAPI.OnPlayerSelected += (_, id) => ms_selectedPlayer = id;
PoseCopycat.OnActivityChange += UpdateToggleColor;
PoseCopycat.OnCopycatChanged.AddHandler(OnCopycatChanged);
}
static void OnCopySwitch() => CopySwitch?.Invoke(ms_selectedPlayer);
static void OnCopySwitch() => OnTargetSelect.Invoke(ms_selectedPlayer);
static void OnToggleUpdate(UiIndex p_index, bool p_value, bool p_force = false)
{
@ -119,8 +127,7 @@ namespace ml_pmc
internal static void ShowAlert(string p_text) => BTKUILib.QuickMenuAPI.ShowAlertToast(p_text, 2);
// Currently broken in BTKUILib, waiting for fix
static void UpdateToggleColor(bool p_state)
static void OnCopycatChanged(bool p_state)
{
(ms_uiElements[(int)UiIndex.Toggle] as Button).ButtonIcon = (p_state ? "PMC-Dancing-On" : "PMC-Dancing");
}

View file

@ -1,4 +1,5 @@
using ABI_RC.Core.Player;
using ABI_RC.Core.Networking.IO.Social;
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK;
using ABI_RC.Systems.IK.SubSystems;
using ABI_RC.Systems.InputManagement;
@ -11,10 +12,18 @@ namespace ml_pmc
[DisallowMultipleComponent]
public class PoseCopycat : MonoBehaviour
{
public class CopycatEvent<T1>
{
event System.Action<T1> m_action;
public void AddHandler(System.Action<T1> p_listener) => m_action += p_listener;
public void RemoveHandler(System.Action<T1> p_listener) => m_action -= p_listener;
public void Invoke(T1 p_value) => m_action?.Invoke(p_value);
}
static readonly Vector4 ms_pointVector = new Vector4(0f, 0f, 0f, 1f);
public static PoseCopycat Instance { get; private set; } = null;
internal static System.Action<bool> OnActivityChange;
internal static readonly CopycatEvent<bool> OnCopycatChanged = new CopycatEvent<bool>();
Animator m_animator = null;
VRIK m_vrIk = null;
@ -36,6 +45,13 @@ namespace ml_pmc
{
if(Instance == null)
Instance = this;
GameEvents.OnAvatarClear.AddHandler(this.OnAvatarClear);
GameEvents.OnAvatarSetup.AddHandler(this.OnAvatarSetup);
GameEvents.OnAvatarPreReuse.AddHandler(this.OnAvatarPreReuse);
GameEvents.OnAvatarPostReuse.AddHandler(this.OnAvatarPostReuse);
ModUi.OnTargetSelect.AddHandler(this.OnTargetSelect);
}
void OnDestroy()
{
@ -52,6 +68,13 @@ namespace ml_pmc
m_animator = null;
m_vrIk = null;
m_lookAtIk = null;
GameEvents.OnAvatarClear.RemoveHandler(this.OnAvatarClear);
GameEvents.OnAvatarSetup.RemoveHandler(this.OnAvatarSetup);
GameEvents.OnAvatarPreReuse.RemoveHandler(this.OnAvatarPreReuse);
GameEvents.OnAvatarPostReuse.RemoveHandler(this.OnAvatarPostReuse);
ModUi.OnTargetSelect.RemoveHandler(this.OnTargetSelect);
}
// Unity events
@ -196,14 +219,14 @@ namespace ml_pmc
}
}
// Patches
internal void OnAvatarClear()
// Game events
void OnAvatarClear()
{
if(m_active)
{
RestoreIK();
RestoreFingerTracking();
OnActivityChange?.Invoke(false);
OnCopycatChanged.Invoke(false);
}
m_active = false;
@ -222,7 +245,8 @@ namespace ml_pmc
m_fingerTracking = false;
m_pose = new HumanPose();
}
internal void OnAvatarSetup()
void OnAvatarSetup()
{
m_inVr = Utils.IsInVR();
m_animator = PlayerSetup.Instance._animator;
@ -250,12 +274,12 @@ namespace ml_pmc
m_animator = null;
}
internal void OnPreAvatarReinitialize()
void OnAvatarPreReuse()
{
if(m_active)
SetTarget(null);
}
internal void OnPostAvatarReinitialize()
void OnAvatarPostReuse()
{
m_inVr = Utils.IsInVR();
@ -276,6 +300,35 @@ namespace ml_pmc
}
}
// Ui events
void OnTargetSelect(string p_id)
{
if(m_active)
SetTarget(null);
else
{
if(m_animator != null)
{
if(Friends.FriendsWith(p_id))
{
if(CVRPlayerManager.Instance.GetPlayerPuppetMaster(p_id, out PuppetMaster l_puppetMaster))
{
if(Utils.IsInSight(BetterBetterCharacterController.Instance.KinematicTriggerProxy.Collider, l_puppetMaster.GetComponent<CapsuleCollider>(), Utils.GetWorldMovementLimit()))
SetTarget(l_puppetMaster);
else
ModUi.ShowAlert("Selected player is too far away or obstructed");
}
else
ModUi.ShowAlert("Selected player isn't connected or ready yet");
}
else
ModUi.ShowAlert("Selected player isn't your friend");
}
else
ModUi.ShowAlert("Local avatar isn't ready yet");
}
}
// IK updates
void OnVRIKPreUpdate()
{
@ -319,7 +372,7 @@ namespace ml_pmc
m_distanceLimit = Utils.GetWorldMovementLimit();
m_active = true;
OnActivityChange?.Invoke(m_active);
OnCopycatChanged.Invoke(m_active);
}
}
else
@ -341,7 +394,7 @@ namespace ml_pmc
m_fingerTracking = false;
m_active = false;
OnActivityChange?.Invoke(m_active);
OnCopycatChanged.Invoke(m_active);
}
}
}
@ -371,7 +424,7 @@ namespace ml_pmc
if(!CVRInputManager.Instance.individualFingerTracking)
{
// Left hand
CVRInputManager.Instance.finger1StretchedLeftThumb = -0f;
CVRInputManager.Instance.finger1StretchedLeftThumb = 0f;
CVRInputManager.Instance.finger2StretchedLeftThumb = 0f;
CVRInputManager.Instance.finger3StretchedLeftThumb = 0f;
CVRInputManager.Instance.fingerSpreadLeftThumb = 0f;

View file

@ -1,4 +1,4 @@
[assembly: MelonLoader.MelonInfo(typeof(ml_pmc.PlayerMovementCopycat), "PlayerMovementCopycat", "1.0.6", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_pmc.PlayerMovementCopycat), "PlayerMovementCopycat", "1.0.7", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonGame(null, "ChilloutVR")]
[assembly: MelonLoader.MelonPriority(3)]
[assembly: MelonLoader.MelonAdditionalDependencies("BTKUILib")]

View file

@ -5,6 +5,14 @@ namespace ml_pmc
{
static class Settings
{
internal class SettingEvent<T>
{
event Action<T> m_action;
public void AddHandler(Action<T> p_listener) => m_action += p_listener;
public void RemoveHandler(Action<T> p_listener) => m_action -= p_listener;
public void Invoke(T p_value) => m_action?.Invoke(p_value);
}
public enum ModSetting
{
Position,
@ -24,13 +32,13 @@ namespace ml_pmc
public static bool MirrorPosition { get; private set; } = false;
public static bool MirrorRotation { get; private set; } = false;
public static Action<bool> PositionChange;
public static Action<bool> RotationChange;
public static Action<bool> GesturesChange;
public static Action<bool> LookAtMixChange;
public static Action<bool> MirrorPoseChange;
public static Action<bool> MirrorPositionChange;
public static Action<bool> MirrorRotationChange;
public static readonly SettingEvent<bool> OnPositionChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnRotationChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnGesturesChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnLookAtMixChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnMirrorPoseChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnMirrorPositionChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnMirrorRotationChanged = new SettingEvent<bool>();
static MelonLoader.MelonPreferences_Category ms_category = null;
static List<MelonLoader.MelonPreferences_Entry> ms_entries = null;
@ -60,61 +68,68 @@ namespace ml_pmc
public static void SetSetting(ModSetting p_setting, object p_value)
{
switch(p_setting)
try
{
case ModSetting.Position:
switch(p_setting)
{
Position = (bool)p_value;
PositionChange?.Invoke((bool)p_value);
}
break;
case ModSetting.Position:
{
Position = (bool)p_value;
OnPositionChanged.Invoke(Position);
}
break;
case ModSetting.Rotation:
{
Rotation = (bool)p_value;
RotationChange?.Invoke((bool)p_value);
case ModSetting.Rotation:
{
Rotation = (bool)p_value;
OnRotationChanged.Invoke(Rotation);
break;
}
case ModSetting.Gestures:
{
Gestures = (bool)p_value;
OnGesturesChanged.Invoke(Gestures);
}
break;
case ModSetting.LookAtMix:
{
LookAtMix = (bool)p_value;
OnLookAtMixChanged.Invoke(LookAtMix);
}
break;
//
case ModSetting.MirrorPose:
{
MirrorPose = (bool)p_value;
OnMirrorPoseChanged.Invoke(MirrorPose);
}
break;
case ModSetting.MirrorPosition:
{
MirrorPosition = (bool)p_value;
OnMirrorPositionChanged.Invoke(MirrorPosition);
}
break;
case ModSetting.MirrorRotation:
{
MirrorRotation = (bool)p_value;
OnMirrorRotationChanged.Invoke(MirrorRotation);
}
break;
}
case ModSetting.Gestures:
{
Gestures = (bool)p_value;
GesturesChange?.Invoke((bool)p_value);
}
break;
case ModSetting.LookAtMix:
{
LookAtMix = (bool)p_value;
LookAtMixChange?.Invoke((bool)p_value);
}
break;
//
case ModSetting.MirrorPose:
{
MirrorPose = (bool)p_value;
MirrorPoseChange?.Invoke((bool)p_value);
}
break;
case ModSetting.MirrorPosition:
{
MirrorPosition = (bool)p_value;
MirrorPositionChange?.Invoke((bool)p_value);
}
break;
case ModSetting.MirrorRotation:
{
MirrorRotation = (bool)p_value;
MirrorRotationChange?.Invoke((bool)p_value);
}
break;
if(ms_entries != null)
ms_entries[(int)p_setting].BoxedValue = p_value;
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
if(ms_entries != null)
ms_entries[(int)p_setting].BoxedValue = p_value;
}
}
}

View file

@ -1,6 +1,8 @@
using ABI.CCK.Components;
using ABI_RC.Core.Savior;
using ABI_RC.Systems.InputManagement;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace ml_pmc
@ -72,5 +74,26 @@ namespace ml_pmc
p_pose.bodyRotation.w *= -1f;
p_pose.bodyPosition.x *= -1f;
}
public static bool IsInSight(CapsuleCollider p_source, CapsuleCollider p_target, float p_limit)
{
bool l_result = false;
if((p_source != null) && (p_target != null))
{
Ray l_ray = new Ray();
l_ray.origin = p_source.bounds.center;
l_ray.direction = p_target.bounds.center - l_ray.origin;
List<RaycastHit> l_hits = Physics.RaycastAll(l_ray, p_limit).ToList();
if(l_hits.Count > 0)
{
l_hits.RemoveAll(hit => hit.collider.gameObject.layer == LayerMask.NameToLayer("UI Internal")); // Somehow layer mask in RaycastAll just ignores players entirely
l_hits.RemoveAll(hit => hit.collider.gameObject.layer == LayerMask.NameToLayer("PlayerLocal"));
l_hits.RemoveAll(hit => hit.collider.gameObject.layer == LayerMask.NameToLayer("PlayerClone"));
l_hits.Sort((a, b) => ((a.distance < b.distance) ? -1 : 1));
l_result = (l_hits.First().collider.gameObject.transform.root == p_target.transform.root);
}
}
return l_result;
}
}
}

View file

@ -7,7 +7,7 @@
<Authors>SDraw</Authors>
<Company>None</Company>
<Product>PlayerMovementCopycat</Product>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">