Decompiled source of Carry Regeneration v1.1.0

plugins/com.github.Kirshoo.CarryRegeneration.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
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 Microsoft.CodeAnalysis;
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.Kirshoo.CarryRegeneration")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("com.github.Kirshoo.CarryRegeneration")]
[assembly: AssemblyTitle("CarryRegeneration")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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.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 CarryRegeneration
{
	[BepInPlugin("com.github.Kirshoo.CarryRegeneration", "CarryRegeneration", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private class PassoutPatch
		{
			private static List<int> IndicesOfLoadingCharacterData(List<CodeInstruction> list)
			{
				List<int> list2 = new List<int>();
				for (int i = 0; i < list.Count; i++)
				{
					if (i >= 1 && list[i - 1].opcode == OpCodes.Ldarg_0 && list[i].opcode == OpCodes.Ldfld && (FieldInfo)list[i].operand == AccessTools.Field(typeof(Character), "data"))
					{
						list2.Add(i - 1);
					}
				}
				return list2;
			}

			[HarmonyPatch(typeof(Character), "UpdateVariablesFixed")]
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> AddTimeIfCarried(IEnumerable<CodeInstruction> instructions, ILGenerator il)
			{
				List<CodeInstruction> list = new List<CodeInstruction>(instructions);
				FieldInfo fieldInfo = AccessTools.Field(typeof(Character), "data");
				FieldInfo fieldInfo2 = AccessTools.Field(typeof(CharacterData), "carrier");
				MethodInfo methodInfo = AccessTools.Method(typeof(Object), "op_Implicit", new Type[1] { typeof(Object) }, (Type[])null);
				Log.LogDebug((object)"Start transpiling...");
				for (int i = 0; i < list.Count; i++)
				{
					if (i >= 4 && list[i - 4].opcode == OpCodes.Ldarg_0 && list[i - 3].opcode == OpCodes.Ldfld && (FieldInfo)list[i - 3].operand == fieldInfo && list[i - 2].opcode == OpCodes.Ldfld && (FieldInfo)list[i - 2].operand == fieldInfo2 && list[i - 1].opcode == OpCodes.Call && (MethodInfo)list[i - 1].operand == methodInfo && (list[i].opcode == OpCodes.Brtrue_S || list[i].opcode == OpCodes.Brtrue))
					{
						Log.LogDebug((object)$"Found carrier check on {i}");
						Label label = (Label)list[i].operand;
						Log.LogDebug((object)$"Next code block: {label}");
						Label label2 = il.DefineLabel();
						Label label3 = il.DefineLabel();
						list[i - 4].labels.Add(label3);
						Log.LogDebug((object)string.Format("Added label to {0}. All lables: [{1}]", list[i - 4].opcode, string.Join(", ", list[i - 4].labels)));
						List<CodeInstruction> list2 = new List<CodeInstruction>(GetIsCarriedCheckInstructions(label3));
						List<CodeInstruction> collection = new List<CodeInstruction>(GetDeathTimerIncrementInstructions(label));
						Log.LogDebug((object)"Loaded the code instructions to inject");
						int num = i - 4;
						while (list[i].opcode != OpCodes.Brfalse_S && list[i].opcode != OpCodes.Brfalse && i > 0)
						{
							i--;
						}
						if (i <= 0)
						{
							Log.LogError((object)"End of interact is pressed code block. Aborting transpilation");
							return instructions;
						}
						Log.LogDebug((object)$"Found end of interact is pressed check at {i}");
						list2.First().labels.Add(label2);
						list[i].operand = label2;
						Log.LogDebug((object)"Changed label to point to injected check");
						list.InsertRange(num, list2);
						list.InsertRange(num + list2.Count, collection);
						Log.LogDebug((object)"DEBUG INSTRUCTIONS");
						for (int j = 0; j < list.Count; j++)
						{
							Log.LogDebug((object)$"{j}: {list[j]}");
						}
						return list.AsEnumerable();
					}
				}
				return instructions;
			}

			private static IEnumerable<CodeInstruction> GetIsCarriedCheckInstructions(Label labelToJumpTo)
			{
				//IL_000e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: Expected O, but got Unknown
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0035: Expected O, but got Unknown
				//IL_0050: Unknown result type (might be due to invalid IL or missing references)
				//IL_0056: Expected O, but got Unknown
				//IL_0085: Unknown result type (might be due to invalid IL or missing references)
				//IL_008b: Expected O, but got Unknown
				//IL_0098: Unknown result type (might be due to invalid IL or missing references)
				//IL_009e: Expected O, but got Unknown
				return (IEnumerable<CodeInstruction>)(object)new CodeInstruction[5]
				{
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Character), "data")),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(CharacterData), "carrier")),
					new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(Object), "op_Implicit", new Type[1] { typeof(Object) }, (Type[])null)),
					new CodeInstruction(OpCodes.Brfalse, (object)labelToJumpTo)
				};
			}

			private static IEnumerable<CodeInstruction> GetDeathTimerIncrementInstructions(Label exitCodeBlockLabel)
			{
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				//IL_002a: Expected O, but got Unknown
				//IL_0045: Unknown result type (might be due to invalid IL or missing references)
				//IL_004b: Expected O, but got Unknown
				//IL_0053: Unknown result type (might be due to invalid IL or missing references)
				//IL_0059: Expected O, but got Unknown
				//IL_0074: Unknown result type (might be due to invalid IL or missing references)
				//IL_007a: Expected O, but got Unknown
				//IL_0087: Unknown result type (might be due to invalid IL or missing references)
				//IL_008d: Expected O, but got Unknown
				//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ae: Expected O, but got Unknown
				//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00bc: Expected O, but got Unknown
				//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d3: Expected O, but got Unknown
				//IL_00db: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e1: Expected O, but got Unknown
				//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f0: Expected O, but got Unknown
				//IL_010c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0112: Expected O, but got Unknown
				//IL_011b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0121: Expected O, but got Unknown
				//IL_013d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0143: Expected O, but got Unknown
				//IL_0155: Unknown result type (might be due to invalid IL or missing references)
				//IL_015b: Expected O, but got Unknown
				//IL_0164: Unknown result type (might be due to invalid IL or missing references)
				//IL_016a: Expected O, but got Unknown
				//IL_0186: Unknown result type (might be due to invalid IL or missing references)
				//IL_018c: Expected O, but got Unknown
				//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
				//IL_01ae: Expected O, but got Unknown
				//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
				//IL_01f1: Expected O, but got Unknown
				//IL_020d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0213: Expected O, but got Unknown
				//IL_0221: Unknown result type (might be due to invalid IL or missing references)
				//IL_0227: Expected O, but got Unknown
				float num = Mathf.Max(RegenerationRate.Value, 0f);
				return (IEnumerable<CodeInstruction>)(object)new CodeInstruction[20]
				{
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Character), "data")),
					new CodeInstruction(OpCodes.Dup, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(CharacterData), "deathTimer")),
					new CodeInstruction(OpCodes.Ldc_R4, (object)num),
					new CodeInstruction(OpCodes.Call, (object)AccessTools.PropertyGetter(typeof(Time), "fixedDeltaTime")),
					new CodeInstruction(OpCodes.Mul, (object)null),
					new CodeInstruction(OpCodes.Ldc_R4, (object)60f),
					new CodeInstruction(OpCodes.Div, (object)null),
					new CodeInstruction(OpCodes.Sub, (object)null),
					new CodeInstruction(OpCodes.Stfld, (object)AccessTools.Field(typeof(CharacterData), "deathTimer")),
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Character), "data")),
					new CodeInstruction(OpCodes.Ldc_R4, (object)0f),
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Character), "data")),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(CharacterData), "deathTimer")),
					new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(Mathf), "Max", new Type[2]
					{
						typeof(float),
						typeof(float)
					}, (Type[])null)),
					new CodeInstruction(OpCodes.Stfld, (object)AccessTools.Field(typeof(CharacterData), "deathTimer")),
					new CodeInstruction(OpCodes.Br, (object)exitCodeBlockLabel)
				};
			}
		}

		internal static ConfigEntry<float> RegenerationRate;

		public const string Id = "com.github.Kirshoo.CarryRegeneration";

		internal static ManualLogSource Log { get; private set; }

		public static string Name => "CarryRegeneration";

		public static string Version => "1.1.0";

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			RegenerationRate = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RegenerationRate", 0.5f, "Rate at which death timer will decrease at.\r\nHigher numbers will make it 'regenerate' faster, while lower - slower.\r\nIf set to 0 or lower, will disable regeneration all together.");
			new Harmony("com.github.Kirshoo.CarryRegeneration").PatchAll(typeof(PassoutPatch));
			Log.LogInfo((object)("Plugin " + Name + " is loaded!"));
		}

		private void Update()
		{
			if (Input.GetKeyDown((KeyCode)284) && Object.op_Implicit((Object)(object)Character.localCharacter))
			{
				Character.PassOut();
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}