Decompiled source of OreDropsOnMe v1.0.0

BepInEx/plugins/OreDropsOnMe/OreDropsOnMe.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace OreDropsOnMe;

[BepInPlugin("com.codex.oredropsonme", "Ore Drops on Me", "1.0.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class OreDropsOnMePlugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.codex.oredropsonme";

	public const string PluginName = "Ore Drops on Me";

	public const string PluginVersion = "1.0.0";

	internal static ConfigEntry<bool> OreDropsAtMiner;

	internal static ManualLogSource PluginLog;

	private void Awake()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		//IL_0037: Expected O, but got Unknown
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Expected O, but got Unknown
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: 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_00e0: Expected O, but got Unknown
		//IL_00e0: Expected O, but got Unknown
		//IL_00e0: Expected O, but got Unknown
		PluginLog = ((BaseUnityPlugin)this).Logger;
		OreDropsAtMiner = ((BaseUnityPlugin)this).Config.Bind<bool>(new ConfigDefinition("General", "OreDropsAtMiner"), true, new ConfigDescription("Move player-caused MineRock5 drops to a clear spot near the miner.", (AcceptableValueBase)null, new object[0]));
		MethodInfo methodInfo = AccessTools.Method(typeof(MineRock5), "DamageArea", new Type[2]
		{
			typeof(int),
			typeof(HitData)
		}, (Type[])null);
		if (methodInfo == null)
		{
			PluginLog.LogError((object)"MineRock5.DamageArea(int, HitData) was not found; no patch was applied.");
			return;
		}
		try
		{
			Harmony val = new Harmony("com.codex.oredropsonme");
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(MineRock5DropPositionPatch), "Prefix", (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(MineRock5DropPositionPatch), "Transpiler", (Type[])null), new HarmonyMethod(typeof(MineRock5DropPositionPatch), "Finalizer", (Type[])null), (HarmonyMethod)null);
			if (MineRock5DropPositionPatch.PositionPatternMatched)
			{
				PluginLog.LogInfo((object)"Player-centered ore drop placement is ready.");
			}
		}
		catch (Exception ex)
		{
			PluginLog.LogError((object)("Could not patch MineRock5 ore drops: " + ex));
		}
	}
}
public static class MineRock5DropPositionPatch
{
	private sealed class DropContext
	{
		internal readonly MineRock5 Rock;

		internal readonly Player Miner;

		internal DropContext(MineRock5 rock, Player miner)
		{
			Rock = rock;
			Miner = miner;
		}
	}

	[ThreadStatic]
	private static DropContext activeContext;

	private static int dropPlacementCounter;

	internal static bool PositionPatternMatched;

	private static readonly MethodInfo RandomInsideUnitSphere = typeof(Random).GetProperty("insideUnitSphere", BindingFlags.Static | BindingFlags.Public).GetGetMethod();

	private static readonly MethodInfo VectorScale = typeof(Vector3).GetMethod("op_Multiply", BindingFlags.Static | BindingFlags.Public, null, new Type[2]
	{
		typeof(Vector3),
		typeof(float)
	}, null);

	private static readonly MethodInfo VectorAdd = typeof(Vector3).GetMethod("op_Addition", BindingFlags.Static | BindingFlags.Public, null, new Type[2]
	{
		typeof(Vector3),
		typeof(Vector3)
	}, null);

	private static readonly MethodInfo GetDropPositionMethod = typeof(MineRock5DropPositionPatch).GetMethod("GetDropPosition", BindingFlags.Static | BindingFlags.Public);

	private static void Prefix(MineRock5 __instance, HitData hit, out DropContext __state)
	{
		__state = activeContext;
		activeContext = null;
		if (OreDropsOnMePlugin.OreDropsAtMiner != null && OreDropsOnMePlugin.OreDropsAtMiner.Value && hit != null)
		{
			Character attacker = hit.GetAttacker();
			Player val = (Player)(object)((attacker is Player) ? attacker : null);
			if ((Object)(object)val != (Object)null)
			{
				activeContext = new DropContext(__instance, val);
			}
		}
	}

	private static Exception Finalizer(Exception __exception, DropContext __state)
	{
		activeContext = __state;
		return __exception;
	}

	private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Expected O, but got Unknown
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		List<int> list2 = new List<int>();
		for (int i = 0; i + 3 < list.Count; i++)
		{
			if (CodeInstructionExtensions.Calls(list[i], RandomInsideUnitSphere) && IsFloatConstant(list[i + 1], 0.3f) && CodeInstructionExtensions.Calls(list[i + 2], VectorScale) && CodeInstructionExtensions.Calls(list[i + 3], VectorAdd))
			{
				list2.Add(i + 3);
			}
		}
		if (list2.Count != 1)
		{
			if (OreDropsOnMePlugin.PluginLog != null)
			{
				OreDropsOnMePlugin.PluginLog.LogWarning((object)("Expected one native MineRock5 drop-position pattern, found " + list2.Count + "; native drop positions were kept."));
			}
			return list;
		}
		int num = list2[0];
		List<CodeInstruction> list3 = new List<CodeInstruction>(list.Count + 1);
		for (int j = 0; j < list.Count; j++)
		{
			list3.Add(list[j]);
			if (j == num)
			{
				list3.Add(new CodeInstruction(OpCodes.Call, (object)GetDropPositionMethod));
			}
		}
		PositionPatternMatched = true;
		if (OreDropsOnMePlugin.PluginLog != null)
		{
			OreDropsOnMePlugin.PluginLog.LogInfo((object)"Found the single native MineRock5 drop-position site.");
		}
		return list3;
	}

	private static bool IsFloatConstant(CodeInstruction instruction, float value)
	{
		if (instruction.opcode == OpCodes.Ldc_R4 && instruction.operand is float)
		{
			return Math.Abs((float)instruction.operand - value) < 0.0001f;
		}
		return false;
	}

	public static Vector3 GetDropPosition(Vector3 nativePosition)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: 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_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_027f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0189: 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_0193: Unknown result type (might be due to invalid IL or missing references)
		//IL_0195: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_0263: Unknown result type (might be due to invalid IL or missing references)
		//IL_022a: Unknown result type (might be due to invalid IL or missing references)
		DropContext dropContext = activeContext;
		if (dropContext == null || (Object)(object)dropContext.Rock == (Object)null || (Object)(object)dropContext.Miner == (Object)null)
		{
			return nativePosition;
		}
		Vector3 position = ((Component)dropContext.Miner).transform.position;
		int mask = LayerMask.GetMask(new string[5] { "Default", "static_solid", "Default_small", "terrain", "piece" });
		CapsuleCollider collider = ((Character)dropContext.Miner).GetCollider();
		Bounds val = (Bounds)(((Object)(object)collider != (Object)null) ? ((Collider)collider).bounds : new Bounds(position + Vector3.up, new Vector3(0.8f, 2f, 0.8f)));
		Vector3 val2 = default(Vector3);
		((Vector3)(ref val2))..ctor(position.x, ((Bounds)(ref val)).max.y + 0.45f, position.z);
		if (mask == 0 || !Physics.CheckSphere(val2, 0.35f, mask, (QueryTriggerInteraction)1))
		{
			return val2;
		}
		Vector3 val3 = -((Component)dropContext.Miner).transform.forward;
		val3.y = 0f;
		if (((Vector3)(ref val3)).sqrMagnitude < 0.001f)
		{
			val3 = Vector3.back;
		}
		((Vector3)(ref val3)).Normalize();
		float num = Mathf.Max(((Bounds)(ref val)).extents.x, ((Bounds)(ref val)).extents.z);
		float num2 = num + 0.35f + 0.1f;
		Vector3 val6 = default(Vector3);
		RaycastHit val7 = default(RaycastHit);
		Vector3 val8 = default(Vector3);
		for (float num3 = 0f; num3 <= 0.45f; num3 += 0.15f)
		{
			float num4 = num2 + num3;
			List<Vector3> list = new List<Vector3>();
			for (int i = -90; i <= 90; i += 15)
			{
				Vector3 val4 = Quaternion.AngleAxis((float)i, Vector3.up) * val3;
				Vector3 val5 = position + val4 * num4;
				((Vector3)(ref val6))..ctor(val5.x, ((Bounds)(ref val)).min.y + 1f, val5.z);
				if (Physics.Raycast(val6, Vector3.down, ref val7, 4f, mask, (QueryTriggerInteraction)1))
				{
					((Vector3)(ref val8))..ctor(val5.x, ((RaycastHit)(ref val7)).point.y + 0.35f + 0.05f, val5.z);
					if (!Physics.CheckSphere(val8, 0.35f, mask, (QueryTriggerInteraction)1))
					{
						list.Add(val8);
					}
				}
			}
			if (list.Count > 0)
			{
				int index = (int)((uint)Interlocked.Increment(ref dropPlacementCounter) % (uint)list.Count);
				return list[index];
			}
		}
		return val2;
	}
}