Decompiled source of Erenshor Loot Restriction v1.0.1

BepInEx\plugins\LootRestriction\Erenshor-LootRestriction.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using Brad522.ModUtils.Harmony;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Erenshor-LootRestriction")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Erenshor-LootRestriction")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e2a51fcb-5318-4d9b-a9c8-7b81a077b7f8")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Erenshor_LootRestriction
{
	[BepInPlugin("Brad522.LootRestrictionAutoRemove", "Loot Restriction Auto-Remove Corpses", "1.0.0")]
	public class LootRestriction : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Character))]
		[HarmonyPatch("DoDeath")]
		[HarmonyPriority(0)]
		private class LootRestrictionPatch
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator il)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Expected O, but got Unknown
				//IL_001e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0024: Expected O, but got Unknown
				//IL_0032: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Expected O, but got Unknown
				//IL_0046: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: Expected O, but got Unknown
				//IL_005a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0060: Expected O, but got Unknown
				//IL_006e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0074: Expected O, but got Unknown
				//IL_0082: Unknown result type (might be due to invalid IL or missing references)
				//IL_0088: Expected O, but got Unknown
				//IL_0096: Unknown result type (might be due to invalid IL or missing references)
				//IL_009c: Expected O, but got Unknown
				//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b0: Expected O, but got Unknown
				//IL_00be: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c4: Expected O, but got Unknown
				//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d9: Expected O, but got Unknown
				//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ee: Expected O, but got Unknown
				//IL_0112: Unknown result type (might be due to invalid IL or missing references)
				//IL_0118: Expected O, but got Unknown
				//IL_012b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0131: Expected O, but got Unknown
				//IL_0178: Unknown result type (might be due to invalid IL or missing references)
				//IL_017e: Expected O, but got Unknown
				CodeMatcher val = new CodeMatcher(instructions, il);
				if (val.MatchStartForward((CodeMatch[])(object)new CodeMatch[11]
				{
					new CodeMatch((OpCode?)OpCodes.Ldloc_3, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Brfalse, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldsfld, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldnull, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Call, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Brfalse, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldsfld, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Dup, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_I4_1, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Add, (object)null, (string)null)
				}).IsValid)
				{
					val.Advance(1).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1]
					{
						new CodeInstruction(OpCodes.Dup, (object)null)
					}).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1]
					{
						new CodeInstruction(OpCodes.Ldarg_0, (object)null)
					})
						.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1]
						{
							new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(Character_Extension), "SetExtra", new Type[2]
							{
								typeof(bool),
								typeof(Character)
							}, (Type[])null))
						});
				}
				TranspilerUtils.FixLeaveNops(val);
				return val.InstructionEnumeration();
			}
		}

		[HarmonyPatch(typeof(Character))]
		public static class Character_Extension
		{
			private class Holder
			{
				public bool PlayerInvolved;
			}

			private static readonly ConditionalWeakTable<Character, Holder> extraData = new ConditionalWeakTable<Character, Holder>();

			public static void SetExtra(bool value, Character instance)
			{
				extraData.GetOrCreateValue(instance).PlayerInvolved = value;
			}

			public static bool GetExtra(Character instance)
			{
				if (extraData.TryGetValue(instance, out var value))
				{
					return value.PlayerInvolved;
				}
				return false;
			}
		}

		[HarmonyPatch(typeof(NPC))]
		[HarmonyPatch("Update")]
		public static class NPCPatch
		{
			public static void Postfix(NPC __instance)
			{
				if ((Object)(object)__instance == (Object)null || __instance.CheckLiving())
				{
					return;
				}
				Character @char = __instance.GetChar();
				if (!Character_Extension.GetExtra(@char) && (!__instance.SimPlayer || (__instance.SimPlayer && !__instance.ThisSim.InGroup && !GameData.SimMngr.IsSimGrouped(GameData.SimMngr.Sims[__instance.ThisSim.myIndex]))))
				{
					if (GameData.AttackingPlayer.Contains(__instance))
					{
						GameData.AttackingPlayer.Remove(__instance);
					}
					if (GameData.GroupMatesInCombat.Contains(__instance))
					{
						GameData.GroupMatesInCombat.Remove(__instance);
					}
					CorpseDataManager.RemoveCorpseData(@char.savedCorpse);
					Object.Destroy((Object)(object)((Component)__instance).gameObject);
				}
			}
		}

		internal const string ModName = "LootRestrictionAutoRemove";

		internal const string ModVersion = "1.0.0";

		internal const string ModDescription = "Loot Restriction Auto-Remove Corpses";

		internal const string Author = "Brad522";

		private const string ModGUID = "Brad522.LootRestrictionAutoRemove";

		private readonly Harmony harmony = new Harmony("Brad522.LootRestrictionAutoRemove");

		public void Awake()
		{
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogMessage((object)"LootRestriction loaded successfully!");
		}
	}
}
namespace Brad522.ModUtils.Harmony
{
	public static class TranspilerUtils
	{
		public static void FixLeaveNops(CodeMatcher matcher)
		{
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			matcher.Start();
			while (matcher.MatchStartForward((CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Leave, (object)null, (string)null)
			}).IsValid)
			{
				matcher.Advance(-1);
				OpCode opcode = matcher.Instruction.opcode;
				matcher.Advance(-1);
				OpCode opcode2 = matcher.Instruction.opcode;
				bool flag = false;
				if ((opcode2 == OpCodes.Leave || opcode2 == OpCodes.Leave_S) && opcode == OpCodes.Nop)
				{
					flag = true;
				}
				matcher.Advance(3);
				if (matcher.Instruction.opcode != OpCodes.Nop && !flag)
				{
					matcher.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1]
					{
						new CodeInstruction(OpCodes.Nop, (object)null)
					});
				}
			}
		}
	}
}