Decompiled source of SnuffableFirepit v1.1.2

SnuffableFirepit.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("SnuffableFirepit")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SnuffableFirepit")]
[assembly: AssemblyTitle("SnuffableFirepit")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SnuffableFirepit
{
	[BepInPlugin("yourname.valheim.snuffablefirepit", "Snuffable Firepit", "1.1.2")]
	public class SnuffableFirepitPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "yourname.valheim.snuffablefirepit";

		public const string PluginName = "Snuffable Firepit";

		public const string PluginVersion = "1.1.2";

		internal const string SnuffedKey = "SFP_Snuffed";

		internal static ConfigEntry<KeyboardShortcut> ToggleShortcut;

		internal static ConfigEntry<bool> AffectIronFirepit;

		internal static ConfigEntry<bool> DebugLogging;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			ToggleShortcut = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Controls", "ToggleShortcut", new KeyboardShortcut((KeyCode)101, (KeyCode[])(object)new KeyCode[1] { (KeyCode)304 }), new ConfigDescription("Key (or key combo) used to extinguish / relight the fire pit you are looking at. Examples: 'G', 'LeftShift + E', 'LeftControl + F'.", (AcceptableValueBase)null, Array.Empty<object>()));
			AffectIronFirepit = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AffectIronFirepit", false, "Also allow the iron fire pit (fire_pit_iron) to be snuffed.");
			DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugLogging", true, "Write detailed troubleshooting messages to the BepInEx log.");
			_harmony = new Harmony("yourname.valheim.snuffablefirepit");
			Type[] array = new Type[3]
			{
				typeof(IsBurning_Patch),
				typeof(Interact_Patch),
				typeof(HoverText_Patch)
			};
			Type[] array2 = array;
			foreach (Type type in array2)
			{
				try
				{
					_harmony.CreateClassProcessor(type).Patch();
					Log.LogInfo((object)("Patched OK: " + type.Name));
				}
				catch (Exception ex)
				{
					Log.LogError((object)("PATCH FAILED: " + type.Name + "\n" + ex));
				}
			}
			Log.LogInfo((object)string.Format("{0} {1} loaded. Shortcut = {2}", "Snuffable Firepit", "1.1.2", ToggleShortcut.Value));
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private unsafe void Update()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			KeyboardShortcut value = ToggleShortcut.Value;
			if (!((KeyboardShortcut)(ref value)).IsDown())
			{
				return;
			}
			value = ToggleShortcut.Value;
			Debug("Shortcut pressed: " + ((object)(*(KeyboardShortcut*)(&value))/*cast due to .constrained prefix*/).ToString());
			string text = BlockedBy();
			if (text != null)
			{
				Debug("Ignored, input blocked by: " + text);
				return;
			}
			GameObject hoverObject = ((Humanoid)localPlayer).GetHoverObject();
			if ((Object)(object)hoverObject == (Object)null)
			{
				Debug("Ignored, you are not looking at any interactable object.");
				return;
			}
			Debug("Hover object: " + ((Object)hoverObject).name);
			Fireplace componentInParent = hoverObject.GetComponentInParent<Fireplace>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				Debug("Ignored, hovered object is not a Fireplace.");
			}
			else if (!IsSupported(componentInParent))
			{
				Debug("Ignored, fireplace '" + ((Object)((Component)componentInParent).gameObject).name + "' is not supported (see AffectIronFirepit).");
			}
			else
			{
				Toggle(componentInParent, localPlayer);
			}
		}

		internal static void Debug(string msg)
		{
			if (DebugLogging != null && DebugLogging.Value)
			{
				Log.LogInfo((object)msg);
			}
		}

		internal static bool IsSupported(Fireplace fp)
		{
			string name = ((Object)((Component)fp).gameObject).name;
			if (name.StartsWith("fire_pit_iron") || name.StartsWith("piece_firepit_iron"))
			{
				return AffectIronFirepit.Value;
			}
			return name.StartsWith("fire_pit") || name.StartsWith("piece_firepit");
		}

		internal static bool IsSnuffed(ZNetView nview)
		{
			return (Object)(object)nview != (Object)null && nview.IsValid() && nview.GetZDO().GetBool("SFP_Snuffed", false);
		}

		internal static string BlockedBy()
		{
			if ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus())
			{
				return "Chat";
			}
			if (Console.IsVisible())
			{
				return "Console";
			}
			if (Menu.IsVisible())
			{
				return "Menu";
			}
			if (TextInput.IsVisible())
			{
				return "TextInput";
			}
			if (InventoryGui.IsVisible())
			{
				return "Inventory";
			}
			if (Minimap.IsOpen())
			{
				return "Map";
			}
			return null;
		}

		internal static void Toggle(Fireplace fp, Player player)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			ZNetView componentInParent = ((Component)fp).GetComponentInParent<ZNetView>();
			if ((Object)(object)componentInParent == (Object)null || !componentInParent.IsValid())
			{
				Debug("Toggle failed: ZNetView missing or invalid.");
				return;
			}
			if (!PrivateArea.CheckAccess(((Component)fp).transform.position, 0f, true, false))
			{
				Debug("Toggle blocked by a ward.");
				return;
			}
			bool flag = IsSnuffed(componentInParent);
			componentInParent.ClaimOwnership();
			componentInParent.GetZDO().Set("SFP_Snuffed", !flag);
			AccessTools.Method(typeof(Fireplace), "UpdateState", (Type[])null, (Type[])null)?.Invoke(fp, null);
			Debug($"Toggled: snuffed {flag} -> {!flag}. IsBurning() now = {fp.IsBurning()}");
			((Character)player).Message((MessageType)2, flag ? "You relight the fire" : "You snuff out the fire", 0, (Sprite)null, false);
		}
	}
	[HarmonyPatch(typeof(Fireplace), "IsBurning")]
	internal static class IsBurning_Patch
	{
		private static void Postfix(Fireplace __instance, ZNetView ___m_nview, ref bool __result)
		{
			if (__result && SnuffableFirepitPlugin.IsSupported(__instance) && SnuffableFirepitPlugin.IsSnuffed(___m_nview))
			{
				__result = false;
			}
		}
	}
	[HarmonyPatch(typeof(Fireplace), "Interact")]
	internal static class Interact_Patch
	{
		private static bool Prefix(Fireplace __instance, Humanoid user, bool hold, ref bool __result)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)user != (Object)(object)Player.m_localPlayer)
			{
				return true;
			}
			if (!SnuffableFirepitPlugin.IsSupported(__instance))
			{
				return true;
			}
			KeyboardShortcut value = SnuffableFirepitPlugin.ToggleShortcut.Value;
			if (!((KeyboardShortcut)(ref value)).IsPressed())
			{
				return true;
			}
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(Fireplace), "GetHoverText")]
	internal static class HoverText_Patch
	{
		private static void Postfix(Fireplace __instance, ZNetView ___m_nview, ref string __result)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			if (SnuffableFirepitPlugin.IsSupported(__instance) && !((Object)(object)___m_nview == (Object)null) && ___m_nview.IsValid())
			{
				string text = ((object)SnuffableFirepitPlugin.ToggleShortcut.Value/*cast due to .constrained prefix*/).ToString();
				string text2 = (SnuffableFirepitPlugin.IsSnuffed(___m_nview) ? "Relight" : "Extinguish");
				__result = __result + "\n[<color=yellow><b>" + text + "</b></color>] " + text2;
			}
		}
	}
}