Decompiled source of RaftAmmoFix v1.1.0

RaftAmmoFix.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace RaftAmmoFix;

[BepInPlugin("com.burks.raftammofix", "Raft Ammo Fix", "1.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class RaftAmmoFixPlugin : BaseUnityPlugin
{
	public const string GUID = "com.burks.raftammofix";

	public const string NAME = "Raft Ammo Fix";

	public const string VERSION = "1.1.0";

	public static ManualLogSource Log;

	private static int _skipped;

	private float _next;

	private static int _groundErrors;

	private static readonly List<ItemData> _empty = new List<ItemData>();

	private void Awake()
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Invalid comparison between Unknown and I4
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Expected O, but got Unknown
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Expected O, but got Unknown
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Expected O, but got Unknown
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		if ((int)SystemInfo.graphicsDeviceType == 4)
		{
			return;
		}
		Harmony val = new Harmony("com.burks.raftammofix");
		Type type = AccessTools.TypeByName("ValheimVehicles.SharedScripts.AmmoController");
		MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "OnContainerChanged", (Type[])null, (Type[])null) : null);
		if (methodInfo != null)
		{
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(RaftAmmoFixPlugin), "OnContainerChanged_Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Raft Ammo Fix 1.1.0 loaded; ValheimRAFT cannon listener guarded.");
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"ValheimRAFT's AmmoController.OnContainerChanged was not found; only the empty-stack cleanup is active.");
		}
		try
		{
			val.Patch((MethodBase)AccessTools.Method(typeof(Heightmap), "GetWorldHeight", new Type[2]
			{
				typeof(Vector3),
				typeof(float).MakeByRefType()
			}, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(RaftAmmoFixPlugin), "GetWorldHeight_Finalizer", (Type[])null), (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Heightmap), "GetPaintMask", new Type[1] { typeof(Vector3) }, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(RaftAmmoFixPlugin), "GetPaintMask_Finalizer", (Type[])null), (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Ground lookups under boats guarded.");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not guard ground lookups: " + ex.Message));
		}
	}

	public static Exception GetWorldHeight_Finalizer(Exception __exception, ref bool __result, ref float height)
	{
		if (__exception == null)
		{
			return null;
		}
		__result = false;
		height = 0f;
		if (_groundErrors++ == 0)
		{
			Log.LogInfo((object)"Ground data not ready under a boat; answered 'no ground' (further ones are quiet).");
		}
		return null;
	}

	public static Exception GetPaintMask_Finalizer(Exception __exception, ref Color __result)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		if (__exception == null)
		{
			return null;
		}
		__result = Color.black;
		if (_groundErrors++ == 0)
		{
			Log.LogInfo((object)"Ground data not ready under a boat; answered 'no ground' (further ones are quiet).");
		}
		return null;
	}

	public static bool OnContainerChanged_Prefix(object __instance)
	{
		if ((Object)((__instance is Object) ? __instance : null) != (Object)null)
		{
			return true;
		}
		if (_skipped++ == 0)
		{
			Log.LogInfo((object)"Ignored a change notice for a cannon that no longer exists (further ones are ignored quietly).");
		}
		return false;
	}

	private void Update()
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Expected O, but got Unknown
		if (Time.unscaledTime < _next)
		{
			return;
		}
		_next = Time.unscaledTime + 2f;
		try
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			Clean(((Humanoid)localPlayer).GetInventory());
			if ((Object)(object)InventoryGui.instance != (Object)null && InventoryGui.IsVisible())
			{
				Container val = (Container)AccessTools.Field(typeof(InventoryGui), "m_currentContainer").GetValue(InventoryGui.instance);
				if ((Object)(object)val != (Object)null)
				{
					Clean(val.GetInventory());
				}
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Cleanup failed: " + ex.Message));
			_next = Time.unscaledTime + 30f;
		}
	}

	private static void Clean(Inventory inv)
	{
		if (inv == null)
		{
			return;
		}
		_empty.Clear();
		foreach (ItemData allItem in inv.GetAllItems())
		{
			if (allItem != null && allItem.m_stack <= 0 && !allItem.m_equipped)
			{
				_empty.Add(allItem);
			}
		}
		foreach (ItemData item in _empty)
		{
			inv.RemoveItem(item);
			Log.LogInfo((object)("Removed an empty stack of " + item.m_shared.m_name + "."));
		}
	}
}