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

View file

@ -42,7 +42,7 @@ namespace ml_amt
switch(m_type)
{
case ParameterType.Moving:
SetBoolean(p_tweaker.GetMoving());
SetBoolean(p_tweaker.IsMoving());
break;
}
}

105
ml_amt/GameEvents.cs Normal file
View file

@ -0,0 +1,105 @@
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK;
using System;
using System.Reflection;
namespace ml_amt
{
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 OnAvatarReuse = new GameEvent();
public static readonly GameEvent OnPlayspaceScale = 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.NonPublic | BindingFlags.Static))
);
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),
null,
new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarReinitialize_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
p_instance.Patch(
typeof(PlayerSetup).GetMethod("SetPlaySpaceScale", BindingFlags.NonPublic | BindingFlags.Instance),
null,
new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnPlayspaceScale_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 l_exception)
{
MelonLoader.MelonLogger.Error(l_exception);
}
}
static void OnAvatarReinitialize_Postfix()
{
try
{
OnAvatarReuse.Invoke();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnPlayspaceScale_Postfix()
{
try
{
OnPlayspaceScale.Invoke();
}
catch(Exception l_exception)
{
MelonLoader.MelonLogger.Error(l_exception);
}
}
}
}

View file

@ -1,44 +1,16 @@
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK;
using System;
using System.Collections;
using System.Reflection;
namespace ml_amt
{
public class AvatarMotionTweaker : MelonLoader.MelonMod
{
static AvatarMotionTweaker ms_instance = null;
MotionTweaker m_localTweaker = null;
public override void OnInitializeMelon()
{
if(ms_instance == null)
ms_instance = this;
Settings.Init();
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.NonPublic | BindingFlags.Static))
);
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public),
null,
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnAvatarReinitialize_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod("SetPlaySpaceScale", BindingFlags.NonPublic | BindingFlags.Instance),
null,
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnPlayspaceScale_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
GameEvents.Init(HarmonyInstance);
MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
}
@ -53,68 +25,9 @@ namespace ml_amt
public override void OnDeinitializeMelon()
{
if(ms_instance == this)
ms_instance = null;
if(m_localTweaker != null)
UnityEngine.Object.Destroy(m_localTweaker);
m_localTweaker = null;
}
static void OnAvatarClear_Postfix() => ms_instance?.OnAvatarClear();
void OnAvatarClear()
{
try
{
if(m_localTweaker != null)
m_localTweaker.OnAvatarClear();
}
catch(Exception l_exception)
{
MelonLoader.MelonLogger.Error(l_exception);
}
}
static void OnSetupAvatar_Postfix() => ms_instance?.OnSetupAvatar();
void OnSetupAvatar()
{
try
{
if(m_localTweaker != null)
m_localTweaker.OnSetupAvatar();
}
catch(Exception l_exception)
{
MelonLoader.MelonLogger.Error(l_exception);
}
}
static void OnAvatarReinitialize_Postfix() => ms_instance?.OnAvatarReinitialize();
void OnAvatarReinitialize()
{
try
{
if(m_localTweaker != null)
m_localTweaker.OnAvatarReinitialize();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnPlayspaceScale_Postfix() => ms_instance?.OnPlayspaceScale();
void OnPlayspaceScale()
{
try
{
if(m_localTweaker != null)
m_localTweaker.OnPlayspaceScale();
}
catch(Exception l_exception)
{
MelonLoader.MelonLogger.Error(l_exception);
}
}
}
}

View file

@ -31,12 +31,8 @@ namespace ml_amt
bool m_avatarReady = false;
bool m_grounded = false;
bool m_moving = false;
bool m_locomotionOverride = false;
bool m_ikOverrideFly = true;
bool m_ikOverrideJump = true;
bool m_detectEmotes = true;
bool m_emoteActive = false;
Vector3 m_massCenter = Vector3.zero;
@ -53,18 +49,17 @@ namespace ml_amt
// Unity events
void Start()
{
SetCrouchLimit(Settings.CrouchLimit);
SetProneLimit(Settings.ProneLimit);
SetIKOverrideFly(Settings.IKOverrideFly);
SetIKOverrideJump(Settings.IKOverrideJump);
SetDetectEmotes(Settings.DetectEmotes);
OnCrouchLimitChanged(Settings.CrouchLimit);
OnProneLimitChanged(Settings.ProneLimit);
Settings.CrouchLimitChange += this.SetCrouchLimit;
Settings.ProneLimitChange += this.SetProneLimit;
Settings.IKOverrideFlyChange += this.SetIKOverrideFly;
Settings.IKOverrideJumpChange += this.SetIKOverrideJump;
Settings.DetectEmotesChange += this.SetDetectEmotes;
Settings.MassCenterChange += this.OnMassCenterChange;
Settings.OnCrouchLimitChanged.AddHandler(this.OnCrouchLimitChanged);
Settings.OnProneLimitChanged.AddHandler(this.OnProneLimitChanged);
Settings.OnMassCenterChanged.AddHandler(this.OnMassCenterChanged);
GameEvents.OnAvatarSetup.AddHandler(this.OnAvatarSetup);
GameEvents.OnAvatarClear.AddHandler(this.OnAvatarClear);
GameEvents.OnAvatarReuse.AddHandler(this.OnAvatarReuse);
GameEvents.OnPlayspaceScale.AddHandler(this.OnPlayspaceScale);
}
void OnDestroy()
@ -73,12 +68,14 @@ namespace ml_amt
m_ikLimits = null;
m_parameters.Clear();
Settings.CrouchLimitChange -= this.SetCrouchLimit;
Settings.ProneLimitChange -= this.SetProneLimit;
Settings.IKOverrideFlyChange -= this.SetIKOverrideFly;
Settings.IKOverrideJumpChange -= this.SetIKOverrideJump;
Settings.DetectEmotesChange -= this.SetDetectEmotes;
Settings.MassCenterChange -= this.OnMassCenterChange;
Settings.OnCrouchLimitChanged.RemoveHandler(this.OnCrouchLimitChanged);
Settings.OnProneLimitChanged.RemoveHandler(this.OnProneLimitChanged);
Settings.OnMassCenterChanged.RemoveHandler(this.OnMassCenterChanged);
GameEvents.OnAvatarSetup.RemoveHandler(this.OnAvatarSetup);
GameEvents.OnAvatarClear.RemoveHandler(this.OnAvatarClear);
GameEvents.OnAvatarReuse.RemoveHandler(this.OnAvatarReuse);
GameEvents.OnPlayspaceScale.RemoveHandler(this.OnPlayspaceScale);
}
void Update()
@ -91,7 +88,7 @@ namespace ml_amt
UpdateIKLimits();
m_emoteActive = false;
if(m_detectEmotes && (m_locomotionLayer >= 0))
if(Settings.DetectEmotes && (m_locomotionLayer >= 0))
{
AnimatorStateInfo l_animState = PlayerSetup.Instance._animator.GetCurrentAnimatorStateInfo(m_locomotionLayer);
m_emoteActive = (l_animState.tagHash == ms_emoteHash);
@ -103,7 +100,7 @@ namespace ml_amt
}
// Game events
internal void OnAvatarClear()
void OnAvatarClear()
{
m_vrIk = null;
m_locomotionLayer = -1;
@ -122,7 +119,7 @@ namespace ml_amt
BetterBetterCharacterController.Instance.avatarProneLimit = Mathf.Clamp01(Settings.ProneLimit);
}
internal void OnSetupAvatar()
void OnAvatarSetup()
{
Utils.SetAvatarTPose();
@ -164,20 +161,20 @@ namespace ml_amt
m_vrIk.solver.locomotion.offset = (Settings.MassCenter ? m_massCenter : m_locomotionOffset);
m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreUpdate);
m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostUpdate);
m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreSolverUpdate);
m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate);
}
m_avatarReady = true;
}
internal void OnPlayspaceScale()
void OnPlayspaceScale()
{
if((m_vrIk != null) && Settings.MassCenter)
m_vrIk.solver.locomotion.offset = m_massCenter * GetRelativeScale();
}
internal void OnAvatarReinitialize()
void OnAvatarReuse()
{
// Old VRIK is destroyed by game
Utils.SetAvatarTPose();
@ -187,13 +184,13 @@ namespace ml_amt
{
m_vrIk.solver.locomotion.offset = (Settings.MassCenter ? m_massCenter : m_locomotionOffset);
m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreUpdate);
m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostUpdate);
m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreSolverUpdate);
m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate);
}
}
// IK events
void OnIKPreUpdate()
void OnIKPreSolverUpdate()
{
bool l_locomotionOverride = false;
@ -203,7 +200,7 @@ namespace ml_amt
m_ikState.m_bendNormalLeft = m_vrIk.solver.leftLeg.useAnimatedBendNormal;
m_ikState.m_bendNormalRight = m_vrIk.solver.rightLeg.useAnimatedBendNormal;
if(m_detectEmotes && m_emoteActive)
if(Settings.DetectEmotes && m_emoteActive)
m_vrIk.solver.IKPositionWeight = 0f;
if(!BodySystem.isCalibratedAsFullBody)
@ -214,14 +211,14 @@ namespace ml_amt
m_vrIk.solver.rightLeg.useAnimatedBendNormal = true;
l_locomotionOverride = true;
}
if(m_ikOverrideFly && BetterBetterCharacterController.Instance.IsFlying())
if(Settings.IKOverrideFly && BetterBetterCharacterController.Instance.IsFlying())
{
m_vrIk.solver.locomotion.weight = 0f;
m_vrIk.solver.leftLeg.useAnimatedBendNormal = true;
m_vrIk.solver.rightLeg.useAnimatedBendNormal = true;
l_locomotionOverride = true;
}
if(m_ikOverrideJump && !m_grounded && !BetterBetterCharacterController.Instance.IsFlying())
if(Settings.IKOverrideJump && !m_grounded && !BetterBetterCharacterController.Instance.IsFlying())
{
m_vrIk.solver.locomotion.weight = 0f;
m_vrIk.solver.leftLeg.useAnimatedBendNormal = true;
@ -235,7 +232,7 @@ namespace ml_amt
m_locomotionOverride = l_locomotionOverride;
}
void OnIKPostUpdate()
void OnIKPostSolverUpdate()
{
m_vrIk.solver.IKPositionWeight = m_ikState.m_weight;
m_vrIk.solver.locomotion.weight = m_ikState.m_locomotionWeight;
@ -245,29 +242,17 @@ namespace ml_amt
}
// Settings
void SetCrouchLimit(float p_value)
void OnCrouchLimitChanged(float p_value)
{
if(m_ikLimits == null)
BetterBetterCharacterController.Instance.avatarCrouchLimit = Mathf.Clamp01(p_value);
}
void SetProneLimit(float p_value)
void OnProneLimitChanged(float p_value)
{
if(m_ikLimits == null)
BetterBetterCharacterController.Instance.avatarProneLimit = Mathf.Clamp01(p_value);
}
void SetIKOverrideFly(bool p_state)
{
m_ikOverrideFly = p_state;
}
void SetIKOverrideJump(bool p_state)
{
m_ikOverrideJump = p_state;
}
void SetDetectEmotes(bool p_state)
{
m_detectEmotes = p_state;
}
void OnMassCenterChange(bool p_state)
void OnMassCenterChanged(bool p_state)
{
if(m_vrIk != null)
m_vrIk.solver.locomotion.offset = (Settings.MassCenter ? (m_massCenter * GetRelativeScale()) : m_locomotionOffset);
@ -290,6 +275,6 @@ namespace ml_amt
}
// Parameters access
public bool GetMoving() => m_moving;
public bool IsMoving() => m_moving;
}
}

View file

@ -1,4 +1,4 @@
[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.3.8", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.3.9", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonGame(null, "ChilloutVR")]
[assembly: MelonLoader.MelonPlatform(MelonLoader.MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)]
[assembly: MelonLoader.MelonPlatformDomain(MelonLoader.MelonPlatformDomainAttribute.CompatibleDomains.MONO)]

View file

@ -6,6 +6,14 @@ namespace ml_amt
{
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);
}
enum ModSetting
{
CrouchLimit,
@ -26,12 +34,12 @@ namespace ml_amt
static MelonLoader.MelonPreferences_Category ms_category = null;
static List<MelonLoader.MelonPreferences_Entry> ms_entries = null;
public static event Action<float> CrouchLimitChange;
public static event Action<float> ProneLimitChange;
public static event Action<bool> IKOverrideFlyChange;
public static event Action<bool> IKOverrideJumpChange;
public static event Action<bool> DetectEmotesChange;
public static event Action<bool> MassCenterChange;
public static readonly SettingEvent<float> OnCrouchLimitChanged = new SettingEvent<float>();
public static readonly SettingEvent<float> OnProneLimitChanged = new SettingEvent<float>();
public static readonly SettingEvent<bool> OnIKOverrideFlyChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnIKOverrideJumpChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnDetectEmotesChanged = new SettingEvent<bool>();
public static readonly SettingEvent<bool> OnMassCenterChanged = new SettingEvent<bool>();
internal static void Init()
{
@ -82,65 +90,79 @@ namespace ml_amt
static void OnSliderUpdate(string p_name, string p_value)
{
if(Enum.TryParse(p_name, out ModSetting l_setting))
try
{
switch(l_setting)
if(Enum.TryParse(p_name, out ModSetting l_setting))
{
case ModSetting.CrouchLimit:
switch(l_setting)
{
CrouchLimit = int.Parse(p_value) * 0.01f;
CrouchLimitChange?.Invoke(CrouchLimit);
}
break;
case ModSetting.CrouchLimit:
{
CrouchLimit = int.Parse(p_value) * 0.01f;
OnCrouchLimitChanged.Invoke(CrouchLimit);
}
break;
case ModSetting.ProneLimit:
{
ProneLimit = int.Parse(p_value) * 0.01f;
ProneLimitChange?.Invoke(ProneLimit);
case ModSetting.ProneLimit:
{
ProneLimit = int.Parse(p_value) * 0.01f;
OnProneLimitChanged.Invoke(ProneLimit);
}
break;
}
break;
ms_entries[(int)l_setting].BoxedValue = int.Parse(p_value);
}
ms_entries[(int)l_setting].BoxedValue = int.Parse(p_value);
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
static void OnToggleUpdate(string p_name, string p_value)
{
if(Enum.TryParse(p_name, out ModSetting l_setting))
try
{
switch(l_setting)
if(Enum.TryParse(p_name, out ModSetting l_setting))
{
case ModSetting.IKOverrideFly:
switch(l_setting)
{
IKOverrideFly = bool.Parse(p_value);
IKOverrideFlyChange?.Invoke(IKOverrideFly);
}
break;
case ModSetting.IKOverrideFly:
{
IKOverrideFly = bool.Parse(p_value);
OnIKOverrideFlyChanged.Invoke(IKOverrideFly);
}
break;
case ModSetting.IKOverrideJump:
{
IKOverrideJump = bool.Parse(p_value);
IKOverrideJumpChange?.Invoke(IKOverrideJump);
}
break;
case ModSetting.IKOverrideJump:
{
IKOverrideJump = bool.Parse(p_value);
OnIKOverrideJumpChanged.Invoke(IKOverrideJump);
}
break;
case ModSetting.DetectEmotes:
{
DetectEmotes = bool.Parse(p_value);
DetectEmotesChange?.Invoke(DetectEmotes);
}
break;
case ModSetting.DetectEmotes:
{
DetectEmotes = bool.Parse(p_value);
OnDetectEmotesChanged.Invoke(DetectEmotes);
}
break;
case ModSetting.MassCenter:
{
MassCenter = bool.Parse(p_value);
MassCenterChange?.Invoke(MassCenter);
case ModSetting.MassCenter:
{
MassCenter = bool.Parse(p_value);
OnMassCenterChanged.Invoke(MassCenter);
}
break;
}
break;
ms_entries[(int)l_setting].BoxedValue = bool.Parse(p_value);
}
ms_entries[(int)l_setting].BoxedValue = bool.Parse(p_value);
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
}

View file

@ -6,7 +6,7 @@
<Company>None</Company>
<Product>AvatarMotionTweaker</Product>
<PackageId>AvatarMotionTweaker</PackageId>
<Version>1.3.8</Version>
<Version>1.3.9</Version>
<Platforms>x64</Platforms>
<AssemblyName>ml_amt</AssemblyName>
</PropertyGroup>