Decompiled source of ValheimResinFuel v1.0.0

plugins/ValheimResinFuel.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ValheimResinFuel")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ValheimResinFuel")]
[assembly: AssemblyTitle("ValheimResinFuel")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ValheimResinFuel;

[BepInPlugin("local.valheim.resinfuel", "ValheimResinFuel", "1.0.0")]
public class ResinFuelPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "local.valheim.resinfuel";

	public const string PluginName = "ValheimResinFuel";

	public const string PluginVersion = "1.0.0";

	internal static ManualLogSource Log;

	private Harmony _harmony;

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		_harmony = new Harmony("local.valheim.resinfuel");
		_harmony.PatchAll(typeof(Patches));
		Log.LogInfo((object)"ValheimResinFuel loaded (v1.0.0)");
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
internal static class Patches
{
	private static readonly FieldInfo FZNview = AccessTools.Field(typeof(Smelter), "m_nview");

	private static string _resinSharedName;

	internal static string ResinSharedName()
	{
		if (_resinSharedName != null)
		{
			return _resinSharedName;
		}
		ObjectDB instance = ObjectDB.instance;
		if ((Object)(object)instance != (Object)null)
		{
			GameObject itemPrefab = instance.GetItemPrefab("Resin");
			ItemDrop val = (((Object)(object)itemPrefab != (Object)null) ? itemPrefab.GetComponent<ItemDrop>() : null);
			if ((Object)(object)val != (Object)null)
			{
				_resinSharedName = val.m_itemData.m_shared.m_name;
				return _resinSharedName;
			}
		}
		return "$item_resin";
	}

	private static bool IsResin(ItemData item)
	{
		if (item == null)
		{
			return false;
		}
		if ((Object)(object)item.m_dropPrefab != (Object)null && ((Object)item.m_dropPrefab).name == "Resin")
		{
			return true;
		}
		return item.m_shared.m_name == ResinSharedName();
	}

	private static ZNetView NView(Smelter s)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		return (ZNetView)FZNview.GetValue(s);
	}

	private static float GetFuel(Smelter s)
	{
		ZNetView val = NView(s);
		if ((Object)(object)val == (Object)null || !val.IsValid())
		{
			return 0f;
		}
		return val.GetZDO().GetFloat(ZDOVars.s_fuel, 0f);
	}

	[HarmonyPatch(typeof(Smelter), "OnAddFuel")]
	[HarmonyPrefix]
	[HarmonyPriority(200)]
	private static bool OnAddFuelPrefix(Smelter __instance, Humanoid user, ItemData item, ref bool __result)
	{
		if ((Object)(object)__instance.m_fuelItem == (Object)null)
		{
			return true;
		}
		bool flag = IsResin(item);
		if (item != null && !flag)
		{
			return true;
		}
		if (GetFuel(__instance) > (float)(__instance.m_maxFuel - 1))
		{
			((Character)user).Message((MessageType)2, "$msg_itsfull", 0, (Sprite)null, false);
			__result = false;
			return false;
		}
		Inventory inventory = user.GetInventory();
		if (inventory == null)
		{
			return true;
		}
		string text = ResinSharedName();
		if ((item == null && inventory.HaveItem(text, true)) || flag)
		{
			if (!inventory.HaveItem(text, true))
			{
				return true;
			}
			inventory.RemoveItem(text, 1, -1, true);
			ZNetView obj = NView(__instance);
			if (obj != null)
			{
				obj.InvokeRPC("RPC_AddFuel", Array.Empty<object>());
			}
			((Character)user).Message((MessageType)2, "$msg_added " + text, 0, (Sprite)null, false);
			__result = true;
			return false;
		}
		return true;
	}

	[HarmonyPatch(typeof(Smelter), "OnHoverAddFuel")]
	[HarmonyPostfix]
	private static void OnHoverAddFuelPostfix(Smelter __instance, ref string __result)
	{
		if (!((Object)(object)__instance.m_fuelItem == (Object)null))
		{
			__result = __result + "\n[<color=yellow><b>$KEY_Use</b></color>] " + Localization.instance.Localize("$piece_smelter_add " + ResinSharedName());
		}
	}

	[HarmonyPatch(typeof(Smelter), "TryGetItems")]
	[HarmonyPostfix]
	private static void TryGetItemsPostfix(Smelter __instance, Switch switchRef, bool __result, List<string> items)
	{
		if (__result && (Object)(object)switchRef == (Object)(object)__instance.m_addWoodSwitch)
		{
			items?.Add(ResinSharedName());
		}
	}

	[HarmonyPatch(typeof(Smelter), "CanUseItems")]
	[HarmonyPrefix]
	private static bool CanUseItemsPrefix(Smelter __instance, Player player, Switch switchRef, ref bool __result)
	{
		if ((Object)(object)switchRef != (Object)(object)__instance.m_addWoodSwitch || (Object)(object)__instance.m_fuelItem == (Object)null)
		{
			return true;
		}
		if (GetFuel(__instance) > (float)(__instance.m_maxFuel - 1))
		{
			return true;
		}
		if ((Object)(object)player != (Object)null && ((Humanoid)player).GetInventory().HaveItem(ResinSharedName(), true))
		{
			__result = true;
			return false;
		}
		return true;
	}
}