Decompiled source of IronDeficiency v0.1.1

plugins/Ryocery-IronDeficiency/com.github.Ryocery.IronDeficiency.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using IronDeficiency.Handlers;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.Ryocery.IronDeficiency")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+a9cfcad0775ed17737c65d26126a53de70246947")]
[assembly: AssemblyProduct("com.github.Ryocery.IronDeficiency")]
[assembly: AssemblyTitle("IronDeficiency")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace IronDeficiency
{
	public class Config
	{
		public static Config Instance { get; private set; }

		public ConfigEntry<bool> EnableRandomTrips { get; private set; }

		public ConfigEntry<float> TripChancePerSecond { get; private set; }

		public ConfigEntry<float> MinMovementSpeed { get; private set; }

		public ConfigEntry<bool> EnableRandomFaints { get; private set; }

		public ConfigEntry<float> FaintChancePerSecond { get; private set; }

		public ConfigEntry<bool> CanFaintWhileClimbing { get; private set; }

		public Config(ConfigFile config)
		{
			Instance = this;
			EnableRandomTrips = config.Bind<bool>("Tripping", "EnableRandomTrips", true, "Enable random tripping.");
			TripChancePerSecond = config.Bind<float>("Tripping", "TripChancePerSecond", 0.02f, "Chance per second to trip (0.01 = 1% per second)");
			MinMovementSpeed = config.Bind<float>("Tripping", "MinMovementSpeed", 1.5f, "Minimum movement speed to be able to trip.");
			EnableRandomFaints = config.Bind<bool>("Fainting", "EnableRandomFaints", true, "Enable random fainting.");
			FaintChancePerSecond = config.Bind<float>("Fainting", "FaintChancePerSecond", 0.006f, "Chance per second to faint (0.005 = 0.5% per second)");
			CanFaintWhileClimbing = config.Bind<bool>("Fainting", "CanFaintWhileClimbing", true, "Whether you can faint while climbing.");
		}
	}
	[BepInPlugin("com.github.Ryocery.IronDeficiency", "IronDeficiency", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		public const string Id = "com.github.Ryocery.IronDeficiency";

		public static ManualLogSource Logger { get; private set; }

		public static string Name => "IronDeficiency";

		public static string Version => "0.1.1";

		private void Awake()
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)("Plugin " + Name + " is loading..."));
			new Config(((BaseUnityPlugin)this).Config);
			string text = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			if (string.IsNullOrEmpty(text))
			{
				text = Paths.PluginPath;
			}
			AssetHandler.Init(text);
			_harmony = new Harmony("com.github.Ryocery.IronDeficiency");
			_harmony.PatchAll();
			Logger.LogInfo((object)("Plugin " + Name + " loaded successfully!"));
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}
namespace IronDeficiency.Patches
{
	[HarmonyPatch(typeof(Character))]
	public static class CharacterPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void AwakePostfix(Character __instance)
		{
			((Component)__instance).gameObject.AddComponent<NetworkHandler>();
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		public static void UpdatePostfix(Character __instance)
		{
			if (__instance.IsLocal)
			{
				if (Config.Instance.EnableRandomTrips.Value)
				{
					ProcessRandomTrips(__instance);
				}
				if (Config.Instance.EnableRandomFaints.Value)
				{
					ProcessRandomFaints(__instance);
				}
			}
		}

		private static void ProcessRandomTrips(Character character)
		{
			if (character.data.isGrounded && !(((Vector3)(ref character.data.avarageVelocity)).magnitude < Config.Instance.MinMovementSpeed.Value) && !character.data.dead && !character.data.fullyPassedOut && !character.data.isClimbing && !character.data.isRopeClimbing && !character.data.isVineClimbing)
			{
				float num = Config.Instance.TripChancePerSecond.Value * Time.deltaTime;
				if (Random.Range(0f, 1f) < num)
				{
					TriggerTrip(character);
				}
			}
		}

		private static void ProcessRandomFaints(Character character)
		{
			if (!character.data.dead && !character.data.fullyPassedOut && !character.data.passedOut && (Config.Instance.CanFaintWhileClimbing.Value || (!character.data.isClimbing && !character.data.isRopeClimbing && !character.data.isVineClimbing)))
			{
				float num = Config.Instance.FaintChancePerSecond.Value * Time.deltaTime;
				if (Random.Range(0f, 1f) < num)
				{
					TriggerFaint(character);
				}
			}
		}

		private static void TriggerTrip(Character character)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: 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_017f: 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_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Plugin.Logger.LogInfo((object)("Random trip triggered for " + character.characterName + "!"));
				NetworkHandler component = ((Component)character).GetComponent<NetworkHandler>();
				if (Object.op_Implicit((Object)(object)component))
				{
					((MonoBehaviourPun)component).photonView.RPC("PlayTripSound", (RpcTarget)0, Array.Empty<object>());
				}
				else
				{
					AudioClip tripSound = AssetHandler.GetTripSound();
					if (Object.op_Implicit((Object)(object)tripSound))
					{
						AudioSource.PlayClipAtPoint(tripSound, character.Center, 0.7f);
					}
				}
				MethodInfo method = typeof(Character).GetMethod("GetBodypartRig", BindingFlags.Instance | BindingFlags.NonPublic);
				MethodInfo method2 = typeof(Character).GetMethod("Fall", BindingFlags.Instance | BindingFlags.NonPublic);
				if (!(method == null) && !(method2 == null))
				{
					object? obj = method.Invoke(character, new object[1] { (object)(BodypartType)16 });
					Rigidbody val = (Rigidbody)((obj is Rigidbody) ? obj : null);
					object? obj2 = method.Invoke(character, new object[1] { (object)(BodypartType)13 });
					Rigidbody val2 = (Rigidbody)((obj2 is Rigidbody) ? obj2 : null);
					object? obj3 = method.Invoke(character, new object[1] { (object)(BodypartType)0 });
					Rigidbody val3 = (Rigidbody)((obj3 is Rigidbody) ? obj3 : null);
					object? obj4 = method.Invoke(character, new object[1] { (object)(BodypartType)4 });
					Rigidbody val4 = (Rigidbody)((obj4 is Rigidbody) ? obj4 : null);
					method2.Invoke(character, new object[2] { 2f, 0f });
					Vector3 lookDirection_Flat = character.data.lookDirection_Flat;
					if (val != null)
					{
						val.AddForce((lookDirection_Flat + Vector3.up) * 200f, (ForceMode)1);
					}
					if (val2 != null)
					{
						val2.AddForce((lookDirection_Flat + Vector3.up) * 200f, (ForceMode)1);
					}
					if (val3 != null)
					{
						val3.AddForce(Vector3.up * 1500f, (ForceMode)1);
					}
					if (val4 != null)
					{
						val4.AddForce(lookDirection_Flat * -300f, (ForceMode)1);
					}
				}
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Exception in random trip: {arg}");
			}
		}

		private static void TriggerFaint(Character character)
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Plugin.Logger.LogInfo((object)("Random faint triggered for " + character.characterName + "!"));
				NetworkHandler component = ((Component)character).GetComponent<NetworkHandler>();
				if (Object.op_Implicit((Object)(object)component))
				{
					((MonoBehaviourPun)component).photonView.RPC("PlayFaintSound", (RpcTarget)0, Array.Empty<object>());
				}
				else
				{
					AudioClip faintSound = AssetHandler.GetFaintSound();
					if (Object.op_Implicit((Object)(object)faintSound))
					{
						AudioSource.PlayClipAtPoint(faintSound, character.Center, 0.7f);
					}
				}
				character.PassOutInstantly();
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Exception in random faint: {arg}");
			}
		}
	}
}
namespace IronDeficiency.Handlers
{
	public static class AssetHandler
	{
		private static AudioClip? _faintSound;

		private static AudioClip? _tripSound;

		private static string _pluginDir = "";

		public static void Init(string pluginDir)
		{
			_pluginDir = pluginDir;
		}

		public static AudioClip? GetFaintSound()
		{
			if (Object.op_Implicit((Object)(object)_faintSound))
			{
				return _faintSound;
			}
			if (string.IsNullOrEmpty(_pluginDir))
			{
				return null;
			}
			string path = Path.Combine(_pluginDir, "sounds", "faint_sound.wav");
			if (!File.Exists(path))
			{
				return null;
			}
			try
			{
				byte[] fileBytes = File.ReadAllBytes(path);
				_faintSound = AudioHandler.ToAudioClip(fileBytes, "faint_sound");
				return _faintSound;
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Failed to load custom sound: {arg}");
			}
			return null;
		}

		public static AudioClip? GetTripSound()
		{
			if (Object.op_Implicit((Object)(object)_tripSound))
			{
				return _tripSound;
			}
			AudioClip[] source = Resources.FindObjectsOfTypeAll<AudioClip>();
			_tripSound = ((IEnumerable<AudioClip>)source).FirstOrDefault((Func<AudioClip, bool>)((AudioClip clip) => ((Object)clip).name == "Au_Slip1"));
			return _tripSound;
		}
	}
	public static class AudioHandler
	{
		public static AudioClip? ToAudioClip(byte[] fileBytes, string name = "wav")
		{
			try
			{
				int num = BitConverter.ToInt32(fileBytes, 24);
				int num2 = BitConverter.ToInt16(fileBytes, 22);
				int num3 = BitConverter.ToInt16(fileBytes, 34);
				int num4 = num3 / 8;
				int num5 = (fileBytes.Length - 44) / num4;
				int num6 = num5 / num2;
				float[] array = new float[num5];
				for (int i = 0; i < num5; i++)
				{
					short num7 = (short)((fileBytes[44 + i * 2 + 1] << 8) | fileBytes[44 + i * 2]);
					array[i] = (float)num7 / 32768f;
				}
				AudioClip val = AudioClip.Create(name, num6, num2, num, false);
				val.SetData(array, 0);
				return val;
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Failed to convert WAV data: {arg}");
				return null;
			}
		}
	}
	public class NetworkHandler : MonoBehaviourPun
	{
		private Character? _character;

		private void Start()
		{
			_character = ((Component)this).GetComponent<Character>();
		}

		[PunRPC]
		public void PlayTripSound()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			AudioClip tripSound = AssetHandler.GetTripSound();
			if (Object.op_Implicit((Object)(object)tripSound) && Object.op_Implicit((Object)(object)_character))
			{
				AudioSource.PlayClipAtPoint(tripSound, _character.Center, 0.7f);
			}
		}

		[PunRPC]
		public void PlayFaintSound()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			AudioClip faintSound = AssetHandler.GetFaintSound();
			if (Object.op_Implicit((Object)(object)faintSound) && Object.op_Implicit((Object)(object)_character))
			{
				AudioSource.PlayClipAtPoint(faintSound, _character.Center, 0.7f);
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}