Decompiled source of FixedPhysicsMod v0.1.2

plugins/MaddaFixedPhysicsMod.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using MoveClasses;
using PlayerHelpers;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("MaddaFixedPhysicsMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+19c3fa45f91f42627f2247591fe3cf3eeb71b557")]
[assembly: AssemblyProduct("MaddaFixedPhysicsMod")]
[assembly: AssemblyTitle("MaddaFixedPhysicsMod")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("jp.madda.maddaFixedPhysicsMod", "madda Fixed Physics Mod", "1.0.0")]
public class JointSpringMod : BaseUnityPlugin
{
	internal static ManualLogSource LoggerInstance;

	private string sprRatioStr = "1.3";

	private string legRatioStr = "1";

	private string spineRatioStr = "1";

	private string armRatioStr = "1";

	private string handRatioStr = "1";

	private string dampRatioStr = "0.7";

	private string legacyDampRatioStr = "0";

	private string maxDegreeStr = "10";

	private string massScaleStr = "1";

	private string WeaponMassScaleStr = "1.8";

	private string NeckLegacyDampStr = "1";

	private string LegMassStr = "10";

	private string LegLegacyDampStr = "0.04";

	private string hipRatioStr = "2";

	private string magAtMaxDampStr = "5";

	private string dampingIntervalStr = "0.1";

	private float mag;

	private Coroutine dampingCoroutineInstance;

	private Rect windowRect = new Rect(100f, 100f, 300f, 480f);

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		LoggerInstance = ((BaseUnityPlugin)this).Logger;
		Harmony val = new Harmony("com.example.jointmod");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"JointSpringMod loaded");
		SceneManager.sceneLoaded += OnSceneLoaded;
		((MonoBehaviour)this).StartCoroutine(DampingCoroutine());
	}

	private IEnumerator DampingCoroutine()
	{
		while (true)
		{
			if (MaddaFixedDamper.rigidJointSprPairs != null && MaddaFixedDamper.rigidJointSprPairs.Count > 0)
			{
				Debug.Log((object)$"rigidJointSprPairs.Count:{MaddaFixedDamper.rigidJointSprPairs.Count}");
				foreach (var rigidJointSprPair in MaddaFixedDamper.rigidJointSprPairs)
				{
					Rigidbody rigid = rigidJointSprPair.rigid;
					ConfigurableJoint joint = rigidJointSprPair.joint;
					float value = rigidJointSprPair.value;
					JointSpringMod jointSpringMod = this;
					Vector3 angularVelocity = rigid.angularVelocity;
					jointSpringMod.mag = Mathf.Max(((Vector3)(ref angularVelocity)).sqrMagnitude, MaddaPysicsModState.MagAtMaxDamp);
					JointDrive xDrive = joint.angularXDrive;
					JointDrive yzDrive = joint.angularYZDrive;
					((JointDrive)(ref xDrive)).positionDamper = MaddaPysicsModState.damp_ratio / mag * value;
					((JointDrive)(ref yzDrive)).positionDamper = MaddaPysicsModState.damp_ratio / mag * value;
					if (((Object)rigid).name == "KNEE_RIGHT" || ((Object)rigid).name == "KNEE_LEFT" || ((Object)rigid).name == "HIP_JOINT_RIGHT" || ((Object)rigid).name == "HIP_JOINT_LEFT")
					{
						((JointDrive)(ref xDrive)).positionDamper = ((JointDrive)(ref xDrive)).positionDamper + MaddaPysicsModState.LegLegacyDamp * value;
						((JointDrive)(ref yzDrive)).positionDamper = ((JointDrive)(ref yzDrive)).positionDamper + MaddaPysicsModState.LegLegacyDamp * value;
					}
					((JointDrive)(ref xDrive)).positionDamper = ((JointDrive)(ref xDrive)).positionDamper + MaddaPysicsModState.LegacyDamp * value;
					((JointDrive)(ref yzDrive)).positionDamper = ((JointDrive)(ref yzDrive)).positionDamper + MaddaPysicsModState.LegacyDamp * value;
					joint.angularXDrive = xDrive;
					joint.angularYZDrive = yzDrive;
					xDrive = default(JointDrive);
					yzDrive = default(JointDrive);
				}
			}
			else
			{
				Debug.LogWarning((object)"rigidJointSprPairsがありません");
			}
			yield return (object)new WaitForSeconds(MaddaPysicsModState.dampingInterval);
		}
	}

	private void OnGUI()
	{
		//IL_0004: 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)
		//IL_001f: Expected O, but got Unknown
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		windowRect = GUI.Window(0, windowRect, new WindowFunction(DrawWindowContents), "Ratio Input");
	}

	private void DrawWindowContents(int windowID)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0219: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0269: Unknown result type (might be due to invalid IL or missing references)
		//IL_028e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b9: 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_0309: Unknown result type (might be due to invalid IL or missing references)
		//IL_032e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0359: Unknown result type (might be due to invalid IL or missing references)
		//IL_037e: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_041e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0449: Unknown result type (might be due to invalid IL or missing references)
		//IL_046e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0499: Unknown result type (might be due to invalid IL or missing references)
		//IL_04be: Unknown result type (might be due to invalid IL or missing references)
		//IL_04e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_050e: Unknown result type (might be due to invalid IL or missing references)
		//IL_06dc: Unknown result type (might be due to invalid IL or missing references)
		GUIStyle val = new GUIStyle(GUI.skin.box);
		val.normal.background = MakeTex(1, 1, new Color(0f, 0f, 0f, 0.6f));
		GUI.Box(new Rect(0f, 20f, ((Rect)(ref windowRect)).width, ((Rect)(ref windowRect)).height - 20f), GUIContent.none, val);
		GUI.Label(new Rect(10f, 30f, 100f, 20f), "Spr_ratio:");
		sprRatioStr = GUI.TextField(new Rect(110f, 30f, 100f, 20f), sprRatioStr, 10);
		GUI.Label(new Rect(10f, 60f, 100f, 20f), "damp_ratio:");
		dampRatioStr = GUI.TextField(new Rect(110f, 60f, 100f, 20f), dampRatioStr, 10);
		GUI.Label(new Rect(10f, 90f, 100f, 20f), "Max_degree:");
		maxDegreeStr = GUI.TextField(new Rect(110f, 90f, 100f, 20f), maxDegreeStr, 10);
		GUI.Label(new Rect(10f, 120f, 100f, 20f), "Mass_scale");
		massScaleStr = GUI.TextField(new Rect(110f, 120f, 100f, 20f), massScaleStr, 10);
		GUI.Label(new Rect(10f, 150f, 100f, 20f), "Leg_ratio:");
		legRatioStr = GUI.TextField(new Rect(110f, 150f, 100f, 20f), legRatioStr, 10);
		GUI.Label(new Rect(10f, 180f, 100f, 20f), "Spine_ratio:");
		spineRatioStr = GUI.TextField(new Rect(110f, 180f, 100f, 20f), spineRatioStr, 10);
		GUI.Label(new Rect(10f, 210f, 100f, 20f), "Arm_ratio:");
		armRatioStr = GUI.TextField(new Rect(110f, 210f, 100f, 20f), armRatioStr, 10);
		GUI.Label(new Rect(10f, 240f, 100f, 20f), "Hand_ratio:");
		handRatioStr = GUI.TextField(new Rect(110f, 240f, 100f, 20f), handRatioStr, 10);
		GUI.Label(new Rect(10f, 270f, 100f, 20f), "MagAtMaxDamp:");
		magAtMaxDampStr = GUI.TextField(new Rect(110f, 270f, 100f, 20f), magAtMaxDampStr, 10);
		GUI.Label(new Rect(10f, 300f, 100f, 20f), "LegacyDamp_ratio:");
		legacyDampRatioStr = GUI.TextField(new Rect(110f, 300f, 100f, 20f), legacyDampRatioStr, 10);
		GUI.Label(new Rect(10f, 330f, 100f, 20f), "WeaponMassScaleStr:");
		WeaponMassScaleStr = GUI.TextField(new Rect(110f, 330f, 100f, 20f), WeaponMassScaleStr, 10);
		GUI.Label(new Rect(10f, 360f, 100f, 20f), "hipRatioStr:");
		hipRatioStr = GUI.TextField(new Rect(110f, 360f, 100f, 20f), hipRatioStr, 10);
		GUI.Label(new Rect(10f, 390f, 100f, 20f), "LegMassStr:");
		LegMassStr = GUI.TextField(new Rect(110f, 390f, 100f, 20f), LegMassStr, 10);
		GUI.Label(new Rect(10f, 420f, 100f, 20f), "LegLegacyDampStr:");
		LegLegacyDampStr = GUI.TextField(new Rect(110f, 420f, 100f, 20f), LegLegacyDampStr, 10);
		GUI.Label(new Rect(10f, 450f, 100f, 20f), "dampingIntervalStr:");
		dampingIntervalStr = GUI.TextField(new Rect(110f, 450f, 100f, 20f), dampingIntervalStr, 10);
		if (float.TryParse(sprRatioStr, out var result))
		{
			MaddaPysicsModState.Spr_ratio = result;
		}
		if (float.TryParse(dampRatioStr, out var result2))
		{
			MaddaPysicsModState.damp_ratio = result2;
		}
		if (float.TryParse(maxDegreeStr, out var result3))
		{
			MaddaPysicsModState.M_degree = result3;
		}
		if (float.TryParse(massScaleStr, out var result4))
		{
			MaddaPysicsModState.mass_scale = result4;
		}
		if (float.TryParse(legRatioStr, out var result5))
		{
			MaddaPysicsModState.Leg_ratio = result5;
		}
		if (float.TryParse(spineRatioStr, out var result6))
		{
			MaddaPysicsModState.Spine_ratio = result6;
		}
		if (float.TryParse(armRatioStr, out var result7))
		{
			MaddaPysicsModState.Arm_ratio = result7;
		}
		if (float.TryParse(handRatioStr, out var result8))
		{
			MaddaPysicsModState.Hand_ratio = result8;
		}
		if (float.TryParse(magAtMaxDampStr, out var result9))
		{
			MaddaPysicsModState.MagAtMaxDamp = result9;
		}
		if (float.TryParse(legacyDampRatioStr, out var result10))
		{
			MaddaPysicsModState.LegacyDamp = result10;
		}
		if (float.TryParse(WeaponMassScaleStr, out var result11))
		{
			MaddaPysicsModState.WeaponMassScale = result11;
		}
		if (float.TryParse(NeckLegacyDampStr, out var result12))
		{
			MaddaPysicsModState.NeckLegacyDamp = result12;
		}
		if (float.TryParse(LegMassStr, out var result13))
		{
			MaddaPysicsModState.LegMass = result13;
		}
		if (float.TryParse(LegLegacyDampStr, out var result14))
		{
			MaddaPysicsModState.LegLegacyDamp = result14;
		}
		if (float.TryParse(dampingIntervalStr, out var result15))
		{
			MaddaPysicsModState.dampingInterval = result15;
		}
		if (float.TryParse(hipRatioStr, out var result16))
		{
			MaddaPysicsModState.hipRatio = result16;
		}
		GUI.DragWindow(new Rect(0f, 0f, ((Rect)(ref windowRect)).width, 20f));
	}

	private Texture2D MakeTex(int width, int height, Color col)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		Color[] array = (Color[])(object)new Color[width * height];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = col;
		}
		Texture2D val = new Texture2D(width, height);
		val.SetPixels(array);
		val.Apply();
		return val;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		if (MaddaFixedDamper.rigidJointSprPairs != null)
		{
			MaddaFixedDamper.rigidJointSprPairs = new List<(Rigidbody, ConfigurableJoint, float)>();
		}
		if (dampingCoroutineInstance != null)
		{
			((MonoBehaviour)this).StopCoroutine(dampingCoroutineInstance);
		}
		dampingCoroutineInstance = ((MonoBehaviour)this).StartCoroutine(DampingCoroutine());
	}
}
public static class MaddaFixedDamper
{
	public static List<(Rigidbody rigid, ConfigurableJoint joint, float value)> rigidJointSprPairs = new List<(Rigidbody, ConfigurableJoint, float)>();

	public static void FindAllConnectedRigits()
	{
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		Rigidbody[] array = Object.FindObjectsOfType<Rigidbody>();
		JointSpringMod.LoggerInstance.LogInfo((object)$"rigidbody count: {array.Length}");
		rigidJointSprPairs = new List<(Rigidbody, ConfigurableJoint, float)>();
		Rigidbody[] array2 = array;
		foreach (Rigidbody val in array2)
		{
			JointSpringMod.LoggerInstance.LogInfo((object)$"Checking rigidbody: {val}");
			ConfigurableJoint val2 = FindJointInParents(((Component)val).transform, val);
			JointSpringMod.LoggerInstance.LogInfo((object)$"Found joint: {val2}");
			if (val2 != null)
			{
				List<(Rigidbody rigid, ConfigurableJoint joint, float value)> list = rigidJointSprPairs;
				JointDrive angularXDrive = val2.angularXDrive;
				list.Add((val, val2, ((JointDrive)(ref angularXDrive)).positionSpring));
				ManualLogSource loggerInstance = JointSpringMod.LoggerInstance;
				string name = ((Object)((Component)val).gameObject).name;
				angularXDrive = val2.angularXDrive;
				loggerInstance.LogInfo((object)$"Added: {name} {((JointDrive)(ref angularXDrive)).positionSpring}");
			}
		}
		if (rigidJointSprPairs.Count > 0)
		{
			JointSpringMod.LoggerInstance.LogInfo((object)$": ConfigurableJointを持つRigidbodyが {rigidJointSprPairs.Count} 個見つかりました。");
		}
		else
		{
			JointSpringMod.LoggerInstance.LogWarning((object)": ConfigurableJointを持つRigidbodyが見つかりませんでした。");
		}
	}

	private static ConfigurableJoint FindJointInParents(Transform current, Rigidbody target)
	{
		while ((Object)(object)current != (Object)null)
		{
			ConfigurableJoint[] components = ((Component)current).GetComponents<ConfigurableJoint>();
			ConfigurableJoint[] array = components;
			foreach (ConfigurableJoint val in array)
			{
				if ((Object)(object)((Joint)val).connectedBody == (Object)(object)target)
				{
					return val;
				}
			}
			current = current.parent;
		}
		return null;
	}
}
public static class MaddaPysicsModState
{
	public static float Spr_ratio = 1.3f;

	public static float damp_ratio = 1f;

	public static float M_degree = 180f;

	public static float mass_scale = 1f;

	public static float WeaponMassScale = 0f;

	public static float Leg_ratio = 1f;

	public static float Spine_ratio = 1f;

	public static float Arm_ratio = 1f;

	public static float Hand_ratio = 1f;

	public static float MagAtMaxDamp = 0.01f;

	public static float LegacyDamp = 0f;

	public static float NeckLegacyDamp = 1f;

	public static float LegMass = 10f;

	public static float LegLegacyDamp = 0.3f;

	public static float dampingInterval = 0.1f;

	public static float hipRatio = 2f;
}
[HarmonyPatch(typeof(PlayerJointHelpers), "GetMaxJointSpringForJointType")]
public static class JointSpringPatch
{
	private static void Postfix(JointType jointType, bool legacy, ref float __result)
	{
		//IL_0012: 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_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		JointType[] array = new JointType[4];
		RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		if (((IEnumerable<JointType>)(object)array).Contains(jointType))
		{
			__result *= MaddaPysicsModState.Leg_ratio;
		}
		else if (((IEnumerable<JointType>)(object)new JointType[2]
		{
			(JointType)1,
			(JointType)2
		}).Contains(jointType))
		{
			__result *= MaddaPysicsModState.Spine_ratio;
		}
		else
		{
			JointType[] array2 = new JointType[6];
			RuntimeHelpers.InitializeArray(array2, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
			if (((IEnumerable<JointType>)(object)array2).Contains(jointType))
			{
				__result *= MaddaPysicsModState.Arm_ratio;
			}
			else if (((IEnumerable<JointType>)(object)new JointType[2]
			{
				(JointType)10,
				(JointType)9
			}).Contains(jointType))
			{
				__result *= MaddaPysicsModState.Hand_ratio;
			}
			else if (((IEnumerable<JointType>)(object)new JointType[1]).Contains(jointType))
			{
				__result *= MaddaPysicsModState.hipRatio;
			}
		}
		__result *= MaddaPysicsModState.Spr_ratio;
	}
}
[HarmonyPatch(typeof(PlayerJointHelpers), "GetDamperMultiplierForJointType")]
public static class JointDamperPatch
{
	private static void Postfix(JointType jointType, bool legacy, ref float __result)
	{
		MaddaFixedDamper.FindAllConnectedRigits();
	}
}
[HarmonyPatch(typeof(PlayerJointHelpers), "GetMaxForceForJointType")]
public static class JointMaxPatch
{
	private static void Postfix(JointType jointType, bool legacy, ref float __result)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		float maxJointSpringForJointType = PlayerJointHelpers.GetMaxJointSpringForJointType(jointType, legacy);
		__result = maxJointSpringForJointType * MaddaPysicsModState.M_degree * 3.14f / 180f;
	}
}
[HarmonyPatch(typeof(StaminaManager), "SetInitialStrengths")]
public class Patch_MassScale
{
	private static void Postfix(StaminaManager __instance)
	{
		Debug.Log((object)"SetInitialStrengths() が実行されました。質量を変更します(MOD)");
		Rigidbody[] array = Object.FindObjectsOfType<Rigidbody>();
		Rigidbody[] array2 = array;
		foreach (Rigidbody val in array2)
		{
			Weapon component = ((Component)val).GetComponent<Weapon>();
			if ((Object)(object)component != (Object)null)
			{
				val.mass *= MaddaPysicsModState.WeaponMassScale;
			}
			val.mass *= MaddaPysicsModState.mass_scale;
			if ((Object)(object)val != (Object)null && ((Object)val).name.Contains("KNEE"))
			{
				Transform val2 = ((Component)val).transform.Find("FootBallMesh");
				if ((Object)(object)val2 == (Object)null)
				{
					Debug.LogWarning((object)("FootBallMesh not found as child of " + ((Object)val).name));
					break;
				}
				GameObject gameObject = ((Component)val2).gameObject;
				Rigidbody val3 = gameObject.GetComponent<Rigidbody>();
				if ((Object)(object)val3 == (Object)null)
				{
					val3 = gameObject.AddComponent<Rigidbody>();
				}
				val3.mass = MaddaPysicsModState.LegMass;
				val3.interpolation = (RigidbodyInterpolation)1;
				val3.collisionDetectionMode = (CollisionDetectionMode)2;
				FixedJoint val4 = ((Component)val).gameObject.AddComponent<FixedJoint>();
				((Joint)val4).connectedBody = val3;
				Debug.Log((object)("FootBallMesh attached to " + ((Object)val).name + " with FixedJoint."));
			}
		}
	}
}