Decompiled source of BuildTweaks v1.0.0

plugins/BuildTweaks/BuildTweaks.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace BuildTweaks;

[BepInPlugin("felih.BuildTweaks", "Build Tweaks", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "felih.BuildTweaks";

	public const string NAME = "Build Tweaks";

	public const string VERSION = "1.0.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> FreeRemove;

	internal static ConfigEntry<bool> RemovedToInventory;

	internal static ConfigEntry<bool> DetailedLog;

	private void Awake()
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		FreeRemove = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Removing", "NoStaminaCost", true, "Removing a building piece with the hammer costs no stamina.");
		RemovedToInventory = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Removing", "MaterialsToInventory", true, "The materials of a piece you remove go straight into your inventory. What doesn't fit drops on the ground as usual.");
		DetailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "DetailedLog", false, "Writes details to the BepInEx log.");
		new Harmony("felih.BuildTweaks").PatchAll(typeof(Plugin).Assembly);
		Log.LogInfo((object)"Build Tweaks 1.0.0 loaded");
	}

	private void Update()
	{
		Removing.Update();
	}

	internal static void Debug(string msg)
	{
		if (DetailedLog.Value)
		{
			Log.LogInfo((object)msg);
		}
	}
}
[HarmonyPatch]
internal static class Removing
{
	private struct Spot
	{
		public Vector3 Pos;

		public float Time;
	}

	private const float Window = 3f;

	private const float Radius = 4f;

	private static readonly List<Spot> Spots = new List<Spot>();

	private static readonly List<ItemDrop> Pending = new List<ItemDrop>();

	private static readonly MethodInfo RequestOwn = AccessTools.Method(typeof(ItemDrop), "RequestOwn", (Type[])null, (Type[])null);

	private static bool _inRemove;

	private static bool _skipStamina;

	private static bool _myRemove;

	[HarmonyPatch(typeof(Player), "RemovePiece")]
	[HarmonyPrefix]
	private static void RemoveStart(Player __instance)
	{
		_inRemove = (Object)(object)__instance == (Object)(object)Player.m_localPlayer;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Player), "RemovePiece")]
	private static void RemoveDone(bool __result)
	{
		if (_inRemove && __result && Plugin.FreeRemove.Value)
		{
			_skipStamina = true;
		}
	}

	[HarmonyPatch(typeof(Player), "RemovePiece")]
	[HarmonyFinalizer]
	private static void RemoveEnd()
	{
		_inRemove = false;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Player), "GetBuildStamina")]
	private static bool NoStamina(ref float __result)
	{
		if (!_skipStamina)
		{
			return true;
		}
		_skipStamina = false;
		__result = 0f;
		return false;
	}

	[HarmonyPatch(typeof(WearNTear), "Remove")]
	[HarmonyPrefix]
	private static void WntRemoved(WearNTear __instance)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		if (_inRemove)
		{
			Remember(((Component)__instance).transform.position, ((Object)__instance).name);
		}
	}

	[HarmonyPatch(typeof(WearNTear), "RPC_Remove")]
	[HarmonyPrefix]
	private static void RpcRemoveStart(long sender)
	{
		_myRemove = Plugin.RemovedToInventory.Value && (Object)(object)Player.m_localPlayer != (Object)null && sender == ZNet.GetUID();
	}

	[HarmonyFinalizer]
	[HarmonyPatch(typeof(WearNTear), "RPC_Remove")]
	private static void RpcRemoveEnd()
	{
		_myRemove = false;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Piece), "DropResources")]
	private static bool PieceDropped(Piece __instance, HitData hitData)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		if (_inRemove)
		{
			Remember(((Component)__instance).transform.position, ((Object)__instance).name);
		}
		if ((!_myRemove && !_inRemove) || !Plugin.RemovedToInventory.Value)
		{
			return true;
		}
		if ((Object)(object)__instance.m_destroyedLootPrefab != (Object)null || (Object)(object)((Component)__instance).GetComponent<Feast>() != (Object)null)
		{
			return true;
		}
		if ((Object)(object)ZoneSystem.instance != (Object)null && ZoneSystem.instance.GetGlobalKey(__instance.FreeBuildKey()))
		{
			return true;
		}
		Player localPlayer = Player.m_localPlayer;
		Inventory val = (((Object)(object)localPlayer != (Object)null) ? ((Humanoid)localPlayer).GetInventory() : null);
		if (val == null)
		{
			return true;
		}
		List<GameObject> list = new List<GameObject>();
		List<int> list2 = new List<int>();
		Requirement[] resources = __instance.m_resources;
		foreach (Requirement val2 in resources)
		{
			if ((Object)(object)val2.m_resItem == (Object)null || !val2.m_recover)
			{
				continue;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(Utils.GetPrefabName(((Object)val2.m_resItem).name));
			int num = val2.m_amount;
			if (num <= 0)
			{
				continue;
			}
			itemPrefab = Game.instance.CheckDropConversion(hitData, val2.m_resItem, itemPrefab, ref num);
			if (!((Object)(object)itemPrefab == (Object)null) && num > 0)
			{
				if (!__instance.IsPlacedByPlayer())
				{
					num = Mathf.Max(1, num / 3);
				}
				list.Add(itemPrefab);
				list2.Add(num);
			}
		}
		for (int j = 0; j < list.Count; j++)
		{
			if (!val.CanAddItem(list[j], list2[j]))
			{
				Plugin.Debug("[remove] inventory full, dropping materials of " + ((Object)__instance).name);
				return true;
			}
		}
		for (int k = 0; k < list.Count; k++)
		{
			val.AddItem(list[k], list2[k]);
			ItemDrop component = list[k].GetComponent<ItemDrop>();
			if ((Object)(object)component != (Object)null)
			{
				((Character)localPlayer).Message((MessageType)1, "$msg_added " + component.m_itemData.m_shared.m_name, list2[k], component.m_itemData.GetIcon(), false);
			}
			Plugin.Debug("[remove] to inventory (direct): " + ((Object)list[k]).name + " x" + list2[k]);
		}
		return false;
	}

	private static void Remember(Vector3 pos, string name)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		if (Plugin.RemovedToInventory.Value)
		{
			Spot item = default(Spot);
			item.Pos = pos;
			item.Time = Time.time;
			Spots.Add(item);
			Plugin.Debug("[remove] " + name);
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(ItemDrop), "Awake")]
	private static void Spawned(ItemDrop __instance)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		if (Spots.Count == 0)
		{
			return;
		}
		Vector3 position = ((Component)__instance).transform.position;
		foreach (Spot spot in Spots)
		{
			if (Time.time - spot.Time <= 3f && Vector3.Distance(position, spot.Pos) <= 4f)
			{
				Pending.Add(__instance);
				break;
			}
		}
	}

	internal static void Update()
	{
		for (int num = Spots.Count - 1; num >= 0; num--)
		{
			if (Time.time - Spots[num].Time > 3f)
			{
				Spots.RemoveAt(num);
			}
		}
		if (Pending.Count == 0)
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		for (int num2 = Pending.Count - 1; num2 >= 0; num2--)
		{
			ItemDrop val = Pending[num2];
			if ((Object)(object)val == (Object)null || (Object)(object)localPlayer == (Object)null || Spots.Count == 0)
			{
				Pending.RemoveAt(num2);
			}
			else
			{
				ZNetView component = ((Component)val).GetComponent<ZNetView>();
				if (!((Object)(object)component == (Object)null) && component.IsValid())
				{
					if (!component.IsOwner())
					{
						RequestOwn.Invoke(val, null);
					}
					else
					{
						string text = ((val.m_itemData != null) ? (val.m_itemData.m_shared.m_name + " x" + val.m_itemData.m_stack) : ((Object)val).name);
						if (((Humanoid)localPlayer).Pickup(((Component)val).gameObject, false, false))
						{
							Plugin.Debug("[remove] to inventory: " + text);
							Pending.RemoveAt(num2);
						}
					}
				}
			}
		}
	}
}