Decompiled source of BodyRigTools v0.3.0

mods/BodyRigTools.dll

Decompiled 16 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BodyRigTools;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using MelonLoader;
using Microsoft.CodeAnalysis;
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(Mod), "BodyRigTools", "0.3.0", "You", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("BodyRigTools")]
[assembly: AssemblyConfiguration("Release.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BodyRigTools")]
[assembly: AssemblyTitle("BodyRigTools")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 BodyRigTools
{
	public class Mod : MelonMod
	{
		private readonly Dictionary<int, float> _origMass = new Dictionary<int, float>();

		private float _massScale = 1f;

		private readonly List<GameObject> _rigViz = new List<GameObject>();

		private readonly List<GameObject> _worldViz = new List<GameObject>();

		private int _skippedMeshColliders;

		private const float WorldRadius = 4f;

		private const int WorldMax = 400;

		public override void OnInitializeMelon()
		{
			((MelonBase)this).LoggerInstance.Msg("BodyRigTools loaded. F6 dump rig | F7/F8 body mass | F9 reset mass | F10 show body+hands colliders | F11 show nearby world colliders");
		}

		public override void OnSceneWasInitialized(int buildIndex, string sceneName)
		{
			_origMass.Clear();
			_massScale = 1f;
			_rigViz.Clear();
			_worldViz.Clear();
		}

		public override void OnUpdate()
		{
			if (Input.GetKeyDown((KeyCode)287))
			{
				DumpRig();
			}
			if (Input.GetKeyDown((KeyCode)288))
			{
				_massScale = Mathf.Min(_massScale * 1.25f, 4f);
				ApplyMass();
			}
			if (Input.GetKeyDown((KeyCode)289))
			{
				_massScale = Mathf.Max(_massScale / 1.25f, 0.25f);
				ApplyMass();
			}
			if (Input.GetKeyDown((KeyCode)290))
			{
				_massScale = 1f;
				ApplyMass();
			}
			if (Input.GetKeyDown((KeyCode)291))
			{
				ToggleRigColliders();
			}
			if (Input.GetKeyDown((KeyCode)292))
			{
				ToggleWorldColliders();
			}
		}

		private RigManager GetRig()
		{
			RigManager val = Object.FindObjectOfType<RigManager>();
			if ((Object)(object)val == (Object)null)
			{
				((MelonBase)this).LoggerInstance.Warning("No RigManager found (are you in a level?).");
			}
			return val;
		}

		private void ToggleRigColliders()
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			if (ClearViz(_rigViz))
			{
				((MelonBase)this).LoggerInstance.Msg("Body/hand colliders hidden.");
				return;
			}
			RigManager rig = GetRig();
			if ((Object)(object)rig == (Object)null)
			{
				return;
			}
			_skippedMeshColliders = 0;
			foreach (Collider componentsInChild in ((Component)rig).GetComponentsInChildren<Collider>(true))
			{
				GameObject val = MakeViz(componentsInChild, new Color(0.1f, 1f, 0.2f));
				if ((Object)(object)val != (Object)null)
				{
					_rigViz.Add(val);
				}
			}
			((MelonBase)this).LoggerInstance.Msg($"Showing {_rigViz.Count} body/hand colliders (green). Skipped {_skippedMeshColliders} mesh colliders.");
		}

		private void ToggleWorldColliders()
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			if (ClearViz(_worldViz))
			{
				((MelonBase)this).LoggerInstance.Msg("World colliders hidden.");
				return;
			}
			RigManager rig = GetRig();
			if ((Object)(object)rig == (Object)null)
			{
				return;
			}
			_skippedMeshColliders = 0;
			Il2CppReferenceArray<Collider> val = Physics.OverlapSphere(((Component)rig).transform.position, 4f);
			foreach (Collider item in (Il2CppArrayBase<Collider>)(object)val)
			{
				if (_worldViz.Count >= 400)
				{
					break;
				}
				if (!((Object)(object)((Component)item).GetComponentInParent<RigManager>() != (Object)null))
				{
					GameObject val2 = MakeViz(item, new Color(1f, 0.6f, 0.1f));
					if ((Object)(object)val2 != (Object)null)
					{
						_worldViz.Add(val2);
					}
				}
			}
			((MelonBase)this).LoggerInstance.Msg($"Showing {_worldViz.Count} nearby colliders (orange, within {4f} m). Skipped {_skippedMeshColliders} mesh colliders.");
		}

		private static bool ClearViz(List<GameObject> list)
		{
			if (list.Count == 0)
			{
				return false;
			}
			foreach (GameObject item in list)
			{
				if ((Object)(object)item != (Object)null)
				{
					Object.Destroy((Object)(object)item);
				}
			}
			list.Clear();
			return true;
		}

		private GameObject MakeViz(Collider col, Color color)
		{
			//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_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: 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_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Quaternion localRotation = Quaternion.identity;
				BoxCollider val = ((Il2CppObjectBase)col).TryCast<BoxCollider>();
				SphereCollider val2 = ((Il2CppObjectBase)col).TryCast<SphereCollider>();
				CapsuleCollider val3 = ((Il2CppObjectBase)col).TryCast<CapsuleCollider>();
				PrimitiveType val4;
				Vector3 center;
				Vector3 localScale = default(Vector3);
				if ((Object)(object)val != (Object)null)
				{
					val4 = (PrimitiveType)3;
					center = val.center;
					localScale = val.size;
				}
				else if ((Object)(object)val2 != (Object)null)
				{
					val4 = (PrimitiveType)0;
					center = val2.center;
					localScale = Vector3.one * (val2.radius * 2f);
				}
				else
				{
					if (!((Object)(object)val3 != (Object)null))
					{
						_skippedMeshColliders++;
						return null;
					}
					val4 = (PrimitiveType)1;
					center = val3.center;
					float num = val3.radius * 2f;
					float num2 = Mathf.Max(val3.height, num);
					((Vector3)(ref localScale))..ctor(num, num2 / 2f, num);
					if (val3.direction == 0)
					{
						localRotation = Quaternion.Euler(0f, 0f, 90f);
					}
					else if (val3.direction == 2)
					{
						localRotation = Quaternion.Euler(90f, 0f, 0f);
					}
				}
				GameObject val5 = GameObject.CreatePrimitive(val4);
				((Object)val5).name = "BRT_ColliderViz";
				Collider component = val5.GetComponent<Collider>();
				if ((Object)(object)component != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)component);
				}
				val5.transform.SetParent(((Component)col).transform, false);
				val5.transform.localPosition = center;
				val5.transform.localRotation = localRotation;
				val5.transform.localScale = localScale;
				Renderer component2 = val5.GetComponent<Renderer>();
				if ((Object)(object)component2 != (Object)null)
				{
					component2.material.color = color;
				}
				return val5;
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("Could not draw a collider: " + ex.Message);
				return null;
			}
		}

		private void ApplyMass()
		{
			RigManager rig = GetRig();
			if ((Object)(object)rig == (Object)null)
			{
				return;
			}
			int num = 0;
			foreach (Rigidbody componentsInChild in ((Component)rig).GetComponentsInChildren<Rigidbody>(true))
			{
				int instanceID = ((Object)componentsInChild).GetInstanceID();
				if (!_origMass.ContainsKey(instanceID))
				{
					_origMass[instanceID] = componentsInChild.mass;
				}
				componentsInChild.mass = _origMass[instanceID] * _massScale;
				num++;
			}
			((MelonBase)this).LoggerInstance.Msg($"Body mass scale x{_massScale:0.00} applied to {num} rigidbodies.");
		}

		private void DumpRig()
		{
			RigManager rig = GetRig();
			if (!((Object)(object)rig == (Object)null))
			{
				StringBuilder stringBuilder = new StringBuilder("\nRig hierarchy:\n");
				Walk(((Component)rig).transform, 0, stringBuilder);
				((MelonBase)this).LoggerInstance.Msg(stringBuilder.ToString());
			}
		}

		private static void Walk(Transform t, int depth, StringBuilder sb)
		{
			sb.Append(' ', depth * 2).Append(((Object)t).name).Append('\n');
			for (int i = 0; i < t.childCount; i++)
			{
				Walk(t.GetChild(i), depth + 1, sb);
			}
		}
	}
}