Decompiled source of PuppetGuyMod v0.6.7

Mods/PuppetAvatar.dll

Decompiled 9 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppPuppetMasta;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Interaction;
using Il2CppSLZ.Marrow.PuppetMasta;
using Il2CppSLZ.Marrow.Warehouse;
using LabFusion.Entities;
using LabFusion.Grabbables;
using LabFusion.Marrow.Extenders;
using LabFusion.Network;
using MelonLoader;
using Microsoft.CodeAnalysis;
using PuppetAvatar;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Core), "PuppetAvatar", "1.0.0", "notnotnotswipez", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("PuppetAvatar")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PuppetAvatar")]
[assembly: AssemblyTitle("PuppetAvatar")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace PuppetAvatar
{
	public class Core : MelonMod
	{
		public static Vector3 highVector = new Vector3(0f, 1000f, 0f);

		public static List<string> VALID_AVATARS = new List<string> { "SLZ.BONELAB.NoBuild.Avatar.PolyDebugger" };

		public override void OnInitializeMelon()
		{
			((MelonBase)this).LoggerInstance.Msg("Initialized.");
		}
	}
	[HarmonyPatch(typeof(RigPose), "ReadSkeleton")]
	public static class RigPosePatch
	{
		public static void Postfix(RigPose __instance)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if (PuppeterAvatarApplication.isPuppeting)
			{
				__instance.PelvisPose.Position = Core.highVector;
			}
		}
	}
	[HarmonyPatch(typeof(GrabHelper), "SendObjectAttach")]
	public static class GrabCancelPatch
	{
		public static bool Prefix()
		{
			if (PuppeterAvatarApplication.isPuppeting)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ArtRig), "SetArtOutputAvatar")]
	public static class ArtRigChangeAvatarPatch
	{
		public static void Postfix(ArtRig __instance, PhysicsRig inRig, Avatar avatar)
		{
			if (Object.op_Implicit((Object)(object)Player.RigManager) && ((Object)((Rig)inRig).manager).GetInstanceID() == ((Object)Player.RigManager).GetInstanceID())
			{
				MelonCoroutines.Start(WaitForRigToUpdateAvatar(((Rig)inRig).manager));
			}
		}

		private static IEnumerator WaitForRigToUpdateAvatar(RigManager manager)
		{
			for (int i = 0; i < 3; i++)
			{
				yield return null;
			}
			if (manager._avatarCrate != null && Object.op_Implicit((Object)(object)manager.avatar))
			{
				string barcode = ((ScannableReference)manager._avatarCrate).Barcode.ID;
				if (Core.VALID_AVATARS.Contains(barcode) && !Object.op_Implicit((Object)(object)((Component)manager.avatar).gameObject.GetComponent<PuppeterAvatarApplication>()))
				{
					((Component)manager.avatar).gameObject.AddComponent<PuppeterAvatarApplication>();
				}
			}
		}
	}
	[RegisterTypeInIl2Cpp]
	public class PuppeterAvatarApplication : MonoBehaviour
	{
		public static bool isPuppeting = false;

		private RigManager manager;

		private BehaviourPowerLegs activePuppet;

		private Rigidbody rootPuppetBody;

		private BaseController leftController;

		private GameObject leftHandVisual;

		private GameObject rightHandVisual;

		private float _ownerInterval = 2f;

		private float ownerTime = 0f;

		public static Dictionary<string, PuppetRigRemap> remappings = new Dictionary<string, PuppetRigRemap>
		{
			{
				"Hip_L",
				new PuppetRigRemap("HipLf", flipForward: false, flipUp: true)
			},
			{
				"Spine_M",
				new PuppetRigRemap("Spine", flipForward: false, flipUp: false, flipRight: false, RemapStyle.PIN_POS)
			},
			{
				"Spine_M/Chest_M/Shoulder_L/Elbow_L/Wrist_L",
				new PuppetRigRemap("Hand (left)", flipForward: false, flipUp: true)
			},
			{
				"Spine_M/Chest_M/Shoulder_R/Elbow_R/Wrist_R",
				new PuppetRigRemap("Hand (right)", flipForward: true, flipUp: true)
			},
			{
				"Spine_M/Chest_M/Head_M",
				new PuppetRigRemap("Head", flipForward: true, flipUp: true, flipRight: true, RemapStyle.MATCH_HEAD)
			},
			{
				"Hip_L/Knee_L",
				new PuppetRigRemap("KneeLf", flipForward: false, flipUp: true)
			},
			{
				"Hip_R",
				new PuppetRigRemap("HipRt", flipForward: true, flipUp: true)
			},
			{
				"Hip_R/Knee_R",
				new PuppetRigRemap("KneeRt", flipForward: true, flipUp: true)
			},
			{
				"Hip_R/Knee_R/Ankle_R",
				new PuppetRigRemap("Foot (right)", flipForward: true, flipUp: true, flipRight: true, RemapStyle.MATCH_FOOT)
			}
		};

		private List<ConfigurableJoint> currentlyPinnedJoints = new List<ConfigurableJoint>();

		public bool isOnRigManager = false;

		public PuppeterAvatarApplication(IntPtr intPtr)
			: base(intPtr)
		{
		}

		public void Awake()
		{
			RigManager componentInParent = ((Component)this).GetComponentInParent<RigManager>();
			if (Object.op_Implicit((Object)(object)componentInParent) && ((Object)componentInParent).GetInstanceID() == ((Object)Player.RigManager).GetInstanceID())
			{
				manager = componentInParent;
				leftController = manager.controllerRig.leftController;
				isOnRigManager = true;
				if (Object.op_Implicit((Object)(object)((Component)this).transform.Find("ToonHand")))
				{
					leftHandVisual = ((Component)((Component)this).transform.Find("ToonHand/Cube")).gameObject;
					rightHandVisual = ((Component)((Component)this).transform.Find("ToonHand/Cube.001")).gameObject;
				}
			}
		}

		public void Update()
		{
			if (!isOnRigManager)
			{
				return;
			}
			if (isPuppeting)
			{
				ownerTime += Time.deltaTime;
				if (ownerTime >= _ownerInterval)
				{
					ownerTime = 0f;
					AttemptOwnGameobject(((Component)rootPuppetBody).gameObject);
					NetworkEntity val = default(NetworkEntity);
					if (Player.LeftHand.HasAttachedObject() && GripExtender.Cache.TryGet(((Il2CppObjectBase)Player.LeftHand.AttachedReceiver).TryCast<Grip>(), ref val))
					{
						NetworkEntityManager.TakeOwnership(val);
					}
					NetworkEntity val2 = default(NetworkEntity);
					if (Player.RightHand.HasAttachedObject() && GripExtender.Cache.TryGet(((Il2CppObjectBase)Player.RightHand.AttachedReceiver).TryCast<Grip>(), ref val2))
					{
						NetworkEntityManager.TakeOwnership(val2);
					}
				}
			}
			if (!leftController.GetMenuTap() || Player.LeftHand.HasAttachedObject())
			{
				return;
			}
			if (!isPuppeting)
			{
				if (!Player.RightHand.HasAttachedObject())
				{
					return;
				}
				MarrowEntity componentInParent = Player.RightHand.m_CurrentAttachedGO.GetComponentInParent<MarrowEntity>();
				if (Object.op_Implicit((Object)(object)componentInParent))
				{
					BehaviourPowerLegs componentInChildren = ((Component)componentInParent).GetComponentInChildren<BehaviourPowerLegs>();
					if (Object.op_Implicit((Object)(object)componentInChildren))
					{
						Player.RightHand.DetachJoint(true, (Grip)null);
						Player.LeftHand.DetachJoint(true, (Grip)null);
						ApplyPuppetToAvatar(componentInChildren);
					}
				}
			}
			else
			{
				ClearPuppet();
			}
		}

		public static void AttemptOwnGameobject(GameObject go)
		{
			if (!NetworkInfo.HasServer)
			{
				return;
			}
			MarrowBody val = MarrowBody.Cache.Get(go);
			NetworkEntity val2 = default(NetworkEntity);
			if (!((Object)(object)val == (Object)null) && MarrowBodyExtender.Cache.TryGet(val, ref val2) && !val2.IsOwnerLocked)
			{
				GripExtender extender = val2.GetExtender<GripExtender>();
				if (extender == null || !extender.CheckHeld())
				{
					NetworkEntityManager.TakeOwnership(val2);
				}
			}
		}

		public void ClearPuppet()
		{
			EnableHands(enable: true);
			foreach (ConfigurableJoint currentlyPinnedJoint in currentlyPinnedJoints)
			{
				Object.Destroy((Object)(object)currentlyPinnedJoint);
			}
			currentlyPinnedJoints.Clear();
			isPuppeting = false;
			IgnoreTotalCollisions(activePuppet, ignore: false);
			activePuppet = null;
			rootPuppetBody = null;
		}

		public void OnDisable()
		{
			if (isOnRigManager)
			{
				if (isPuppeting && Object.op_Implicit((Object)(object)activePuppet))
				{
					ClearPuppet();
				}
				isPuppeting = false;
				EnableHands(enable: true);
			}
		}

		public void EnableHands(bool enable)
		{
			if (Object.op_Implicit((Object)(object)leftHandVisual))
			{
				leftHandVisual.SetActive(enable);
				rightHandVisual.SetActive(enable);
			}
		}

		public void ApplyPuppetToAvatar(BehaviourPowerLegs behaviourPowerLegs)
		{
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02de: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0296: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_0356: Unknown result type (might be due to invalid IL or missing references)
			//IL_0365: Unknown result type (might be due to invalid IL or missing references)
			if (isPuppeting)
			{
				return;
			}
			EnableHands(enable: false);
			isPuppeting = true;
			activePuppet = behaviourPowerLegs;
			((BehaviourBase)activePuppet).puppetMaster.Kill();
			Transform transform = ((Component)((BehaviourBase)behaviourPowerLegs).puppetMaster).transform;
			rootPuppetBody = ((Component)transform).GetComponentInChildren<Rigidbody>();
			Transform transform2 = ((Component)manager.physicsRig).transform;
			IgnoreTotalCollisions(behaviourPowerLegs, ignore: true);
			foreach (KeyValuePair<string, PuppetRigRemap> remapping in remappings)
			{
				Rigidbody component = ((Component)transform.Find("Root_M/" + remapping.Key)).GetComponent<Rigidbody>();
				PuppetRigRemap value = remapping.Value;
				RemapStyle remapStyle = value.remapStyle;
				float num = (value.flipForward ? (-1f) : 1f);
				float num2 = (value.flipUp ? (-1f) : 1f);
				float num3 = (value.flipRight ? (-1f) : 1f);
				Rigidbody component2 = ((Component)transform2.Find(value.fromPhysRig)).GetComponent<Rigidbody>();
				Transform transform3 = ((Component)component2).transform;
				component.isKinematic = true;
				Transform transform4 = ((Component)component).transform;
				transform4.position = component2.position;
				Transform parent = transform4.parent;
				float positionSpring = 5000f;
				switch (remapStyle)
				{
				case RemapStyle.MATCH_STRANGE:
					PointTo(transform4, transform3.up * num, transform3.right * num2);
					break;
				case RemapStyle.MATCH_FOOT:
					PointTo(transform4, transform3.forward * num, transform3.right * num2);
					break;
				case RemapStyle.MATCH_HEAD:
					transform4.parent = transform3;
					transform4.localPosition = Vector3.zeroVector;
					transform4.localEulerAngles = new Vector3(4f, 93f, 279f);
					positionSpring = 30000f;
					break;
				case RemapStyle.PIN_POS:
					transform4.parent = transform3;
					transform4.localPosition = Vector3.zeroVector;
					break;
				}
				transform4.parent = parent;
				ConfigurableJoint val = ((Component)component).gameObject.AddComponent<ConfigurableJoint>();
				((Joint)val).connectedBody = component2;
				((Joint)val).connectedMassScale = 0.1f;
				string text = value.fromPhysRig.ToLower();
				JointDrive val2;
				if (remapStyle != RemapStyle.PIN_POS)
				{
					val2 = new JointDrive
					{
						m_PositionSpring = 5000f,
						m_PositionDamper = 0f
					};
					((JointDrive)(ref val2)).maximumForce = float.PositiveInfinity;
					val.angularXDrive = val2;
					val.angularYZDrive = val.angularXDrive;
				}
				else
				{
					val2 = new JointDrive
					{
						m_PositionSpring = positionSpring,
						m_PositionDamper = 0f
					};
					((JointDrive)(ref val2)).maximumForce = float.PositiveInfinity;
					val.xDrive = val2;
					val.yDrive = val.xDrive;
					val.zDrive = val.xDrive;
				}
				if (text.Contains("head") || text.Contains("hand"))
				{
					val2 = new JointDrive
					{
						m_PositionSpring = positionSpring,
						m_PositionDamper = 0f
					};
					((JointDrive)(ref val2)).maximumForce = float.PositiveInfinity;
					val.xDrive = val2;
					val.yDrive = val.xDrive;
					val.zDrive = val.xDrive;
				}
				component.isKinematic = false;
				currentlyPinnedJoints.Add(val);
			}
		}

		private void PointTo(Transform toPoint, Vector3 forward, Vector3 up)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = toPoint.position + forward;
			toPoint.LookAt(val, up);
		}

		public void IgnoreTotalCollisions(BehaviourPowerLegs behaviourPowerLegs, bool ignore)
		{
			Transform transform = ((Component)((BehaviourBase)behaviourPowerLegs).puppetMaster).transform;
			Transform transform2 = ((Component)manager.physicsRig).transform;
			foreach (Collider componentsInChild in ((Component)transform).GetComponentsInChildren<Collider>())
			{
				if (ignore)
				{
					if (((Component)componentsInChild).gameObject.layer == 12)
					{
						((Component)componentsInChild).gameObject.layer = 9;
					}
				}
				else if (((Component)componentsInChild).gameObject.layer == 9)
				{
					((Component)componentsInChild).gameObject.layer = 12;
				}
				foreach (Collider componentsInChild2 in ((Component)transform2).GetComponentsInChildren<Collider>())
				{
					Physics.IgnoreCollision(componentsInChild, componentsInChild2, ignore);
				}
			}
		}
	}
	public class PuppetRigRemap
	{
		public string fromPhysRig;

		public bool flipForward = false;

		public bool flipUp = false;

		public bool flipRight = false;

		public RemapStyle remapStyle = RemapStyle.MATCH_STRANGE;

		public PuppetRigRemap(string physPath, bool flipForward, bool flipUp)
		{
			fromPhysRig = physPath;
			this.flipForward = flipForward;
			this.flipUp = flipUp;
			flipRight = false;
		}

		public PuppetRigRemap(string physPath, bool flipForward, bool flipUp, bool flipRight, RemapStyle remapStyle)
		{
			fromPhysRig = physPath;
			this.flipForward = flipForward;
			this.flipUp = flipUp;
			this.flipRight = flipRight;
			this.remapStyle = remapStyle;
		}
	}
	public enum RemapStyle
	{
		MATCH_STRANGE,
		MATCH_FOOT,
		MATCH_HEAD,
		PIN_POS
	}
}