Decompiled source of PeakRopes v1.0.0

PeakRopes.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[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("PeakRopes")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("My first plugin")]
[assembly: AssemblyTitle("PeakRopes")]
[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 PeakRopes
{
	[BepInPlugin("PeakRopes", "My first plugin", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("PeakRopes").PatchAll(typeof(ChainedPatch));
		}
	}
	[HarmonyPatch(typeof(Character), "Awake")]
	public static class ChainedPatch
	{
		[HarmonyPostfix]
		public static void AwakePatch(Character __instance)
		{
			if ((Object)(object)((Component)__instance).gameObject.GetComponent<ChainedController>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<ChainedController>();
			}
		}
	}
	public class ChainedController : MonoBehaviourPunCallbacks
	{
		public static bool DebugOverlayEnabled = false;

		private Character character;

		private Transform myHip;

		private Rigidbody myRigidbody;

		private static bool isChainedActive;

		private int localPlayerIndex = -1;

		private Character leftDebugTarget;

		private Character rightDebugTarget;

		private float leftDebugDistance;

		private float rightDebugDistance;

		private const float ChainLength = 5f;

		private const float ChainSlack = 0.5f;

		private const float MaxRopeLength = 8f;

		private const float PullStrength = 20f;

		private const float SuspensionStrength = 28f;

		private const float SuspensionDamping = 10f;

		private const float MaxPullForce = 110f;

		private Dictionary<string, LineRenderer> chainLines = new Dictionary<string, LineRenderer>();

		private static List<Character> sortedPlayers = new List<Character>();

		private static Material chainMaterial;

		private float nextCacheUpdate = 0f;

		private static GUIStyle debugStyle;

		private void Start()
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			character = ((Component)this).GetComponent<Character>();
			myHip = GetHip(((Component)this).transform);
			myRigidbody = (((Object)(object)myHip != (Object)null) ? ((Component)myHip).GetComponent<Rigidbody>() : null);
			if ((Object)(object)chainMaterial == (Object)null)
			{
				chainMaterial = new Material(Shader.Find("Sprites/Default"));
				chainMaterial.color = new Color(0.6f, 0.6f, 0.6f);
			}
		}

		private void Update()
		{
			if (PhotonNetwork.IsMasterClient && (Object)(object)character != (Object)null && character.IsLocal && Input.GetKeyDown((KeyCode)289))
			{
				((MonoBehaviourPun)this).photonView.RPC("RpcToggleChain", (RpcTarget)0, new object[1] { !isChainedActive });
			}
			if ((Object)(object)character == (Object)null || !character.IsLocal)
			{
				return;
			}
			if (!isChainedActive)
			{
				if (chainLines.Count > 0)
				{
					DestroyAllLines();
				}
			}
			else if (PhotonNetwork.InRoom && PhotonNetwork.PlayerList.Length < 2)
			{
				leftDebugTarget = null;
				rightDebugTarget = null;
				leftDebugDistance = 0f;
				rightDebugDistance = 0f;
				if (chainLines.Count > 0)
				{
					DestroyAllLines();
				}
			}
			else
			{
				if (Time.time > nextCacheUpdate)
				{
					UpdateSortedPlayerList();
					nextCacheUpdate = Time.time + 1.5f;
				}
				DrawAllConnections();
			}
		}

		private void FixedUpdate()
		{
			if (isChainedActive && !((Object)(object)character == (Object)null) && character.IsLocal && IsEligibleCharacter(character))
			{
				ApplyChainPhysics();
			}
		}

		private void OnGUI()
		{
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			if (DebugOverlayEnabled && !((Object)(object)character == (Object)null) && character.IsLocal)
			{
				if (debugStyle == null)
				{
					GUIStyle val = new GUIStyle(GUI.skin.label)
					{
						fontSize = 16
					};
					val.normal.textColor = Color.white;
					debugStyle = val;
				}
				GUILayout.BeginArea(new Rect(12f, 12f, 720f, 220f), GUI.skin.box);
				GUILayout.Label("PeakRopes: " + (isChainedActive ? "ON" : "OFF"), debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.Label("Joueur local: " + DescribeCharacter(character), debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.Label($"Index dans la chaîne: {localPlayerIndex}/{Mathf.Max(0, sortedPlayers.Count - 1)}", debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.Label($"Cibles détectées: {sortedPlayers.Count}", debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.Label("Cible active: " + DescribeActiveTarget(), debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.Label("Gauche: " + DescribeTarget(leftDebugTarget, leftDebugDistance), debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.Label("Droite: " + DescribeTarget(rightDebugTarget, rightDebugDistance), debugStyle, Array.Empty<GUILayoutOption>());
				GUILayout.EndArea();
			}
		}

		[PunRPC]
		public void RpcToggleChain(bool state)
		{
			Plugin.Log.LogInfo((object)("Chain toggled: " + (state ? "ON" : "OFF")));
			isChainedActive = state;
			if (!isChainedActive)
			{
				leftDebugTarget = null;
				rightDebugTarget = null;
				DestroyAllLines();
			}
		}

		private void UpdateSortedPlayerList()
		{
			if (PhotonNetwork.InRoom && PhotonNetwork.PlayerList.Length < 2)
			{
				sortedPlayers.Clear();
				return;
			}
			sortedPlayers = (from c in Object.FindObjectsByType<Character>((FindObjectsSortMode)0).Where(IsEligibleCharacter)
				orderby ((MonoBehaviourPun)c).photonView.OwnerActorNr, ((MonoBehaviourPun)c).photonView.ViewID
				select c).ToList();
		}

		private void ApplyChainPhysics()
		{
			localPlayerIndex = -1;
			leftDebugTarget = null;
			rightDebugTarget = null;
			leftDebugDistance = 0f;
			rightDebugDistance = 0f;
			if ((PhotonNetwork.InRoom && PhotonNetwork.PlayerList.Length < 2) || (Object)(object)myHip == (Object)null || sortedPlayers.Count < 2)
			{
				return;
			}
			int num = sortedPlayers.IndexOf(character);
			if (num != -1)
			{
				localPlayerIndex = num;
				if (num > 0)
				{
					Character target = (leftDebugTarget = sortedPlayers[num - 1]);
					leftDebugDistance = GetTargetDistance(target);
					ApplyPull(target);
				}
				if (num < sortedPlayers.Count - 1)
				{
					Character target2 = (rightDebugTarget = sortedPlayers[num + 1]);
					rightDebugDistance = GetTargetDistance(target2);
					ApplyPull(target2);
				}
			}
		}

		private void ApplyPull(Character target)
		{
			//IL_0059: 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_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: 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_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: 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)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)target == (Object)null || (Object)(object)target == (Object)(object)character)
			{
				return;
			}
			Transform hip = GetHip(((Component)target).transform);
			if ((Object)(object)hip == (Object)null || (Object)(object)myRigidbody == (Object)null)
			{
				return;
			}
			float num = Vector3.Distance(myHip.position, hip.position);
			Vector3 val = hip.position - myHip.position;
			Vector3 normalized = ((Vector3)(ref val)).normalized;
			float num2 = 5.5f;
			if (num > num2)
			{
				float num3 = num - num2;
				float num4 = Mathf.Clamp01(num3 / 5f);
				float num5 = num3 * 20f * (0.35f + num4);
				myRigidbody.AddForce(normalized * Mathf.Min(num5, 110f), (ForceMode)5);
			}
			if (num > 8f)
			{
				float num6 = num - 8f;
				float num7 = num6 * 28f;
				myRigidbody.AddForce(normalized * Mathf.Min(num7, 165f), (ForceMode)5);
				float num8 = Vector3.Dot(myRigidbody.linearVelocity, normalized);
				if (num8 < 0f)
				{
					myRigidbody.AddForce(normalized * ((0f - num8) * 10f), (ForceMode)5);
				}
			}
		}

		private void DrawAllConnections()
		{
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			HashSet<string> activeLineKeys = new HashSet<string>();
			for (int i = 0; i < sortedPlayers.Count - 1; i++)
			{
				Character val = sortedPlayers[i];
				Character val2 = sortedPlayers[i + 1];
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
				{
					Transform hip = GetHip(((Component)val).transform);
					Transform hip2 = GetHip(((Component)val2).transform);
					if ((Object)(object)hip != (Object)null && (Object)(object)hip2 != (Object)null && !((Object)(object)((MonoBehaviourPun)val).photonView == (Object)null) && !((Object)(object)((MonoBehaviourPun)val2).photonView == (Object)null))
					{
						string text = $"line_{((MonoBehaviourPun)val).photonView.ViewID}_{((MonoBehaviourPun)val2).photonView.ViewID}";
						UpdateLine(text, hip.position, hip2.position);
						activeLineKeys.Add(text);
					}
				}
			}
			List<string> list = chainLines.Keys.Where((string k) => !activeLineKeys.Contains(k)).ToList();
			foreach (string item in list)
			{
				if ((Object)(object)chainLines[item] != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)chainLines[item]).gameObject);
				}
				chainLines.Remove(item);
			}
		}

		private float GetTargetDistance(Character target)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)target == (Object)null || (Object)(object)myHip == (Object)null)
			{
				return 0f;
			}
			Transform hip = GetHip(((Component)target).transform);
			if ((Object)(object)hip == (Object)null)
			{
				return 0f;
			}
			return Vector3.Distance(myHip.position, hip.position);
		}

		private void UpdateLine(string key, Vector3 start, Vector3 end)
		{
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			if (!chainLines.TryGetValue(key, out var value))
			{
				GameObject val = new GameObject(key);
				value = val.AddComponent<LineRenderer>();
				value.positionCount = 2;
				((Renderer)value).material = chainMaterial;
				value.startWidth = 0.08f;
				value.endWidth = 0.08f;
				chainLines[key] = value;
			}
			value.SetPosition(0, start);
			value.SetPosition(1, end);
		}

		private static Transform GetHip(Transform root)
		{
			if ((Object)(object)root == (Object)null)
			{
				return null;
			}
			return ((IEnumerable<Transform>)((Component)root).GetComponentsInChildren<Transform>()).FirstOrDefault((Func<Transform, bool>)((Transform t) => ((Object)t).name == "Hip"));
		}

		private static bool IsEligibleCharacter(Character c)
		{
			return (Object)(object)c != (Object)null && ((Behaviour)c).isActiveAndEnabled && !Object.op_Implicit((Object)(object)c.Ghost) && c.IsRegisteredToPlayer && (Object)(object)c.player != (Object)null && (Object)(object)((MonoBehaviourPun)c).photonView != (Object)null && ((MonoBehaviourPun)c).photonView.OwnerActorNr > 0 && ((MonoBehaviourPun)c).photonView.Owner != null && (Object)(object)GetHip(((Component)c).transform) != (Object)null;
		}

		private static string DescribeCharacter(Character c)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)c == (Object)null)
			{
				return "<aucune>";
			}
			Transform hip = GetHip(((Component)c).transform);
			string text = (((Object)(object)hip != (Object)null) ? $"{hip.position:F2}" : "<sans Hip>");
			string[] obj = new string[7] { c.characterName, " | Actor ", null, null, null, null, null };
			PhotonView photonView = ((MonoBehaviourPun)c).photonView;
			obj[2] = ((photonView != null) ? photonView.OwnerActorNr.ToString() : null) ?? "?";
			obj[3] = " | ViewID ";
			PhotonView photonView2 = ((MonoBehaviourPun)c).photonView;
			obj[4] = ((photonView2 != null) ? photonView2.ViewID.ToString() : null) ?? "?";
			obj[5] = " | Pos ";
			obj[6] = text;
			return string.Concat(obj);
		}

		private static string DescribeTarget(Character c, float distance)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)c == (Object)null)
			{
				return "<aucune cible valide>";
			}
			Transform hip = GetHip(((Component)c).transform);
			object obj;
			if (!((Object)(object)hip != (Object)null))
			{
				obj = "<sans Hip>";
			}
			else
			{
				Vector3 position = hip.position;
				obj = ((Vector3)(ref position)).ToString("F2");
			}
			string text = (string)obj;
			object[] obj2 = new object[5] { c.characterName, null, null, null, null };
			PhotonView photonView = ((MonoBehaviourPun)c).photonView;
			obj2[1] = ((photonView != null) ? photonView.OwnerActorNr.ToString() : null) ?? "?";
			PhotonView photonView2 = ((MonoBehaviourPun)c).photonView;
			obj2[2] = ((photonView2 != null) ? photonView2.ViewID.ToString() : null) ?? "?";
			obj2[3] = distance;
			obj2[4] = text;
			return string.Format("{0} | Actor {1} | ViewID {2} | Dist {3:F2} | Pos {4}", obj2);
		}

		private string DescribeActiveTarget()
		{
			if ((Object)(object)leftDebugTarget == (Object)null && (Object)(object)rightDebugTarget == (Object)null)
			{
				return "<aucune>";
			}
			if ((Object)(object)leftDebugTarget != (Object)null && (Object)(object)rightDebugTarget != (Object)null)
			{
				return "G=" + leftDebugTarget.characterName + " / D=" + rightDebugTarget.characterName;
			}
			return ((Object)(object)leftDebugTarget != (Object)null) ? ("G=" + leftDebugTarget.characterName) : ("D=" + rightDebugTarget.characterName);
		}

		private void DestroyAllLines()
		{
			foreach (LineRenderer value in chainLines.Values)
			{
				if ((Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value).gameObject);
				}
			}
			chainLines.Clear();
		}

		public override void OnDisable()
		{
			((MonoBehaviourPunCallbacks)this).OnDisable();
			DestroyAllLines();
		}

		private void OnDestroy()
		{
			DestroyAllLines();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "PeakRopes";

		public const string PLUGIN_NAME = "My first plugin";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}