Files
BoneSync/BoneSync/Patching/SkeletonHandPatches.cs
2025-03-28 22:30:09 +02:00

95 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using HarmonyLib;
using MelonLoader;
using StressLevelZero.Rig;
using BoneSync.Data;
using StressLevelZero;
namespace BoneSync.Patching
{
[HarmonyPatch(typeof(SkeletonHand))]
internal static class SkeletonHandPatches
{
public static byte poseIndexRight = 0;
public static float radiusRight = 0.0f;
public static byte poseIndexLeft = 0;
public static float radiusLeft = 0.0f;
private static Dictionary<string, byte> handPoses = new Dictionary<string, byte>();
private static byte GetHandPoseIndex(string handPoseName)
{
if (handPoses.ContainsKey(handPoseName))
return handPoses[handPoseName];
if (PlayerScripts.playerHandPoses == null || PlayerScripts.playerHandPoses.Count == 0)
{
SyncLogger.Msg("PlayerScripts.playerHandPoses is empty, getting hand poses...");
PlayerScripts.GetHandPoses();
}
bool found = PlayerScripts.playerHandPoses.Contains(handPoseName);
if (!found)
{
//SyncLogger.Error($"Hand pose {handPoseName} not found in playerHandPoses!");
return 0;
}
byte index = (byte)PlayerScripts.playerHandPoses.IndexOf(handPoseName);
handPoses.Add(handPoseName, index);
return index;
}
[HarmonyPatch(nameof(SkeletonHand.SetHandPose)), HarmonyPrefix]
private static void SetHandPosePostfix(SkeletonHand __instance, string handPoseName)
{
if (!__instance.GetCharacterAnimationManager()) return;
SyncLogger.Msg($"SetHandPosePostfix: {handPoseName}");
int poseIndex = GetHandPoseIndex(handPoseName);
switch (__instance.handedness)
{
case Handedness.LEFT:
poseIndexLeft = (byte)poseIndex;
break;
case Handedness.RIGHT:
poseIndexRight = (byte)poseIndex;
break;
}
}
[HarmonyPatch(nameof(SkeletonHand.SetCylinderRadius)), HarmonyPrefix]
private static void SetCylinderRadiusPrefix(SkeletonHand __instance, float radius)
{
if (!__instance.GetCharacterAnimationManager()) return;
//SyncLogger.Msg($"SetCylinderRadiusPrefix: {radius}");
switch (__instance.handedness)
{
case Handedness.LEFT:
if (radiusLeft == radius) return;
SyncLogger.Msg($"SetCylinderRadiusPrefixLeft: {radius}");
radiusLeft = radius;
break;
case Handedness.RIGHT:
if (radiusRight == radius) return;
SyncLogger.Msg($"SetCylinderRadiusPrefixRight: {radius}");
radiusRight = radius;
break;
}
}
}
}