Decompiled source of LzD ExtinguishFire v1.0.1

LzD_ExtinguishFire.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LzD_ExtinguishFire")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LzD_ExtinguishFire")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("53f58781-0f9b-4e4f-af7c-5ecd9941e286")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LzD_ExtinguishFire;

[BepInPlugin("lzd_extinguishfire", "LzD Extinguish Fire", "1.0.1")]
[BepInProcess("valheim.exe")]
public class LzD_ExtinguishFire : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Fireplace), "GetHoverText")]
	private static class Fireplace_GetHoverText_Patch
	{
		private static void Postfix(ref string __result)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (modEnabled.Value)
			{
				string obj = __result;
				KeyboardShortcut value = actionKey.Value;
				__result = obj + $"\n[<color=yellow><b>{((KeyboardShortcut)(ref value)).MainKey}</b></color>] Collect fuel / Extinguish fire";
			}
		}
	}

	[HarmonyPatch(typeof(Player), "Update")]
	private static class Player_Update_Patch
	{
		private static void Postfix(Player __instance)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value)
			{
				return;
			}
			IInputSystem current = UnityInput.Current;
			KeyboardShortcut value = actionKey.Value;
			if (!current.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey))
			{
				return;
			}
			GameObject hoverObject = ((Humanoid)__instance).GetHoverObject();
			Fireplace val = ((hoverObject != null) ? hoverObject.GetComponentInParent<Fireplace>() : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)val).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			float @float = component.GetZDO().GetFloat("fuel", 0f);
			if (@float <= 0f)
			{
				return;
			}
			int num = (int)Mathf.Floor(@float);
			if (num == 0)
			{
				Log("Fire extinguished");
				component.GetZDO().Set("fuel", 0f);
				val.m_fuelAddedEffects.Create(((Component)val).transform.position, ((Component)val).transform.rotation, (Transform)null, 1f, -1);
				return;
			}
			Log($"{num} fuel units recovered");
			component.GetZDO().Set("fuel", @float - (float)num);
			GameObject prefab = ZNetScene.instance.GetPrefab(((Object)val.m_fuelItem).name);
			while (num > 0)
			{
				ItemDrop component2 = Object.Instantiate<GameObject>(prefab, ((Component)val).transform.position + Vector3.up, Quaternion.identity).GetComponent<ItemDrop>();
				int num2 = Mathf.Min(component2.m_itemData.m_shared.m_maxStackSize, num);
				component2.SetStack(num2);
				num -= num2;
			}
		}
	}

	public const string modID = "lzd_extinguishfire";

	public const string modName = "LzD Extinguish Fire";

	public const string modVersion = "1.0.1";

	private readonly Harmony harmony = new Harmony("lzd_extinguishfire");

	private static ConfigEntry<bool> modEnabled;

	private static ConfigEntry<bool> logEnabled;

	private static ConfigEntry<KeyboardShortcut> actionKey;

	private void Awake()
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Global", "a. Enable mod", true, "Enable or disable the mod completely");
		logEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Global", "b. Enable logs", true, "Enable or disable logs completely");
		actionKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("2 - Controls", "a. Collect/Extinguish key", new KeyboardShortcut((KeyCode)308, Array.Empty<KeyCode>()), "Defines the key to collect fuel and extinguish fireplaces");
		if (modEnabled.Value)
		{
			harmony.PatchAll();
			Log("LzD Extinguish Fire mod initialized");
		}
	}

	private void OnDestroy()
	{
		harmony.UnpatchSelf();
	}

	private static void Log(string msg)
	{
		if (logEnabled.Value)
		{
			Debug.Log((object)msg);
		}
	}
}