Decompiled source of ExtinguishedCampfire v0.1.0

ExtinguishedCampfire.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ExtinguishedCampfire")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("ExtinguishedCampfire")]
[assembly: AssemblyTitle("ExtinguishedCampfire")]
[assembly: AssemblyVersion("0.1.0.0")]
namespace ExtinguishedCampfire;

[BepInPlugin("irti.extinguishedcampfire", "Extinguished Campfire", "0.1.0")]
public class Plugin : BaseUnityPlugin
{
	public const string PluginGUID = "irti.extinguishedcampfire";

	public const string PluginName = "Extinguished Campfire";

	public const string PluginVersion = "0.1.0";

	internal static ConfigEntry<KeyboardShortcut> PlaceUnlitKey;

	internal static ConfigEntry<string> UnlitName;

	internal static ConfigEntry<string> UnlitDescription;

	internal static ConfigEntry<bool> SinkIntoGround;

	internal static ConfigEntry<float> SinkDepth;

	private static Sprite s_litIcon;

	private static bool s_litIconApplied;

	internal static Sprite UnlitIcon { get; private set; }

	private void Awake()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Expected O, but got Unknown
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		PlaceUnlitKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "PlaceUnlitKey", new KeyboardShortcut((KeyCode)306, Array.Empty<KeyCode>()), "Hold this while placing a campfire to place it with no fuel, and for less of its fuel item.");
		UnlitName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "UnlitName", "Extinguished Campfire", "The build menu name of the campfire while PlaceUnlitKey is held.");
		UnlitDescription = ((BaseUnityPlugin)this).Config.Bind<string>("General", "UnlitDescription", "Useful for spawn-proofing", "The build menu description of the campfire while PlaceUnlitKey is held.");
		SinkIntoGround = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SinkIntoGround", false, "Place the campfire a little lower in the ground when placed with PlaceUnlitKey held.");
		SinkDepth = ((BaseUnityPlugin)this).Config.Bind<float>("General", "SinkDepth", 0.1f, new ConfigDescription("How far in meters to sink the campfire when SinkIntoGround is on.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.2f), Array.Empty<object>()));
		s_litIcon = LoadIcon("lit");
		UnlitIcon = LoadIcon("unlit");
		new Harmony("irti.extinguishedcampfire").PatchAll();
	}

	internal static void ApplyLitIcon()
	{
		if (!s_litIconApplied && Object.op_Implicit((Object)(object)ZNetScene.instance))
		{
			s_litIconApplied = true;
			GameObject prefab = ZNetScene.instance.GetPrefab("fire_pit");
			Piece val = (Object.op_Implicit((Object)(object)prefab) ? prefab.GetComponent<Piece>() : null);
			if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)s_litIcon) && Object.op_Implicit((Object)(object)UnlitIcon))
			{
				val.m_icon = s_litIcon;
			}
		}
	}

	private Sprite LoadIcon(string name)
	{
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Expected O, but got Unknown
		using Stream stream = typeof(Plugin).Assembly.GetManifestResourceStream("ExtinguishedCampfire." + name + "-icon.rgba.gz");
		if (stream == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Built-in " + name + " icon not found; the build menu icon won't change."));
			return null;
		}
		byte[] array = new byte[262144];
		using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress))
		{
			int i;
			int num;
			for (i = 0; i < array.Length; i += num)
			{
				num = gZipStream.Read(array, i, array.Length - i);
				if (num <= 0)
				{
					break;
				}
			}
			if (i != array.Length)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Built-in " + name + " icon is the wrong size; the build menu icon won't change."));
				return null;
			}
		}
		Texture2D val = new Texture2D(256, 256, (TextureFormat)4, false);
		val.LoadRawTextureData(array);
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, 256f, 256f), new Vector2(0.5f, 0.5f));
	}

	internal static bool IsCampfire(Piece piece)
	{
		if (Object.op_Implicit((Object)(object)piece))
		{
			return ((Object)((Component)piece).gameObject).name == "fire_pit";
		}
		return false;
	}
}
[HarmonyPatch(typeof(Player), "Update")]
internal static class CostPatch
{
	private static Piece s_piece;

	private static string s_fullName;

	private static string s_fullDescription;

	private static Sprite s_fullIcon;

	private static Requirement s_discounted;

	private static int s_fullAmount;

	private static void Prefix(Player __instance)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return;
		}
		Plugin.ApplyLitIcon();
		Piece selectedPiece = __instance.GetSelectedPiece();
		int num;
		if (Plugin.IsCampfire(selectedPiece))
		{
			KeyboardShortcut value = Plugin.PlaceUnlitKey.Value;
			num = (((KeyboardShortcut)(ref value)).IsPressed() ? 1 : 0);
		}
		else
		{
			num = 0;
		}
		bool flag = (byte)num != 0;
		if (s_discounted != null && (!flag || FuelRequirement(selectedPiece) != s_discounted))
		{
			s_discounted.m_amount = s_fullAmount;
			s_piece.m_name = s_fullName;
			s_piece.m_description = s_fullDescription;
			s_piece.m_icon = s_fullIcon;
			s_discounted = null;
		}
		if (!flag || s_discounted != null)
		{
			return;
		}
		Requirement val = FuelRequirement(selectedPiece);
		if (val != null)
		{
			s_piece = selectedPiece;
			s_fullName = selectedPiece.m_name;
			s_fullDescription = selectedPiece.m_description;
			s_fullIcon = selectedPiece.m_icon;
			s_discounted = val;
			s_fullAmount = val.m_amount;
			val.m_amount = Mathf.Max(0, val.m_amount - FuelRefund(selectedPiece));
			selectedPiece.m_name = Plugin.UnlitName.Value;
			selectedPiece.m_description = Plugin.UnlitDescription.Value;
			if (Object.op_Implicit((Object)(object)Plugin.UnlitIcon))
			{
				selectedPiece.m_icon = Plugin.UnlitIcon;
			}
		}
	}

	internal static int FuelRefund(Piece campfire)
	{
		return Mathf.FloorToInt(((Component)campfire).GetComponent<Fireplace>().m_startFuel);
	}

	private static Requirement FuelRequirement(Piece campfire)
	{
		Fireplace component = ((Component)campfire).GetComponent<Fireplace>();
		if (!Object.op_Implicit((Object)(object)component) || !Object.op_Implicit((Object)(object)component.m_fuelItem))
		{
			return null;
		}
		Requirement[] resources = campfire.m_resources;
		foreach (Requirement val in resources)
		{
			if (Object.op_Implicit((Object)(object)val.m_resItem) && val.m_resItem.m_itemData.m_shared.m_name == component.m_fuelItem.m_itemData.m_shared.m_name)
			{
				return val;
			}
		}
		return null;
	}
}
[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
internal static class SinkGhostPatch
{
	private static void Postfix(GameObject ___m_placementGhost)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)___m_placementGhost) && ___m_placementGhost.activeSelf && Plugin.SinkIntoGround.Value && Plugin.IsCampfire(___m_placementGhost.GetComponent<Piece>()))
		{
			KeyboardShortcut value = Plugin.PlaceUnlitKey.Value;
			if (((KeyboardShortcut)(ref value)).IsPressed())
			{
				Transform transform = ___m_placementGhost.transform;
				transform.position += Vector3.down * Plugin.SinkDepth.Value;
			}
		}
	}
}
[HarmonyPatch(typeof(Player), "PlacePiece")]
internal static class PlacePiecePatch
{
	internal static bool PlacingUnlit;

	private static void Prefix(Piece piece)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		int placingUnlit;
		if (Plugin.IsCampfire(piece))
		{
			KeyboardShortcut value = Plugin.PlaceUnlitKey.Value;
			placingUnlit = (((KeyboardShortcut)(ref value)).IsPressed() ? 1 : 0);
		}
		else
		{
			placingUnlit = 0;
		}
		PlacingUnlit = (byte)placingUnlit != 0;
	}

	private static void Finalizer()
	{
		PlacingUnlit = false;
	}
}
[HarmonyPatch(typeof(Fireplace), "Awake")]
internal static class FireplaceAwakePatch
{
	private static void Prefix(Fireplace __instance)
	{
		if (PlacePiecePatch.PlacingUnlit)
		{
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			ZDO val = (Object.op_Implicit((Object)(object)component) ? component.GetZDO() : null);
			if (val != null && component.IsOwner() && val.GetFloat(ZDOVars.s_fuel, -1f) == -1f)
			{
				val.Set(ZDOVars.s_fuel, 0f);
			}
		}
	}
}
internal static class PluginVersionInfo
{
	public const string Version = "0.1.0";
}