Decompiled source of CarryRevifier v0.1.1

plugins/com.github.Kirshoo.CarryRevifier.dll

Decompiled a year 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.CarryRevifier")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1")]
[assembly: AssemblyProduct("com.github.Kirshoo.CarryRevifier")]
[assembly: AssemblyTitle("CarryRevifier")]
[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 CarryRevifier
{
	[BepInPlugin("com.github.Kirshoo.CarryRevifier", "CarryRevifier", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		private class PassoutPatch
		{
			[HarmonyPatch(typeof(Character), "UpdateVariablesFixed")]
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> AddTimeIfCarried(IEnumerable<CodeInstruction> instructions)
			{
				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 = default(Label);
						list[i - 4].labels.Add(label2);
						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(label2));
						List<CodeInstruction> collection = new List<CodeInstruction>(GetDeathTimerIncrementInstructions(label));
						Log.LogDebug((object)"Loaded the code instructions to inject");
						i -= 4;
						list.InsertRange(i, list2);
						i += list2.Count;
						list.InsertRange(i, collection);
						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_S, (object)labelToJumpTo)
				};
			}

			private static IEnumerable<CodeInstruction> GetDeathTimerIncrementInstructions(Label exitCodeBlockLabel)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0015: Expected O, but got Unknown
				//IL_0030: Unknown result type (might be due to invalid IL or missing references)
				//IL_0036: Expected O, but got Unknown
				//IL_003e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0044: Expected O, but got Unknown
				//IL_005f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0065: Expected O, but got Unknown
				//IL_0080: Unknown result type (might be due to invalid IL or missing references)
				//IL_0086: Expected O, but got Unknown
				//IL_0097: Unknown result type (might be due to invalid IL or missing references)
				//IL_009d: Expected O, but got Unknown
				//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ab: Expected O, but got Unknown
				//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: Expected O, but got Unknown
				//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00da: Expected O, but got Unknown
				//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f2: Expected O, but got Unknown
				//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
				//IL_0101: Expected O, but got Unknown
				//IL_011d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0123: Expected O, but got Unknown
				//IL_013f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0145: Expected O, but got Unknown
				//IL_0182: Unknown result type (might be due to invalid IL or missing references)
				//IL_0188: Expected O, but got Unknown
				//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
				//IL_01aa: Expected O, but got Unknown
				//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
				//IL_01be: Expected O, but got Unknown
				return (IEnumerable<CodeInstruction>)(object)new CodeInstruction[16]
				{
					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.Call, (object)AccessTools.PropertyGetter(typeof(Time), "fixedDeltaTime")),
					new CodeInstruction(OpCodes.Ldc_R4, (object)120f),
					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.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_S, (object)exitCodeBlockLabel)
				};
			}
		}

		private static ConfigEntry<bool> ResetTimer;

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

		internal static ManualLogSource Log { get; private set; }

		public static string Name => "CarryRevifier";

		public static string Version => "0.1.1";

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			ResetTimer = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ResetDeathTimer", false, "Flag to reset the timer when passed out player is picked up. If set to false, will slowly increase death timer instead.");
			new Harmony("com.github.Kirshoo.CarryRevifier").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)
		{
		}
	}
}