Decompiled source of DigForHoe v1.0.1

plugins/DigForHoe/DigForHoe.dll

Decompiled 13 hours ago
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 DigForHoe;

[BepInPlugin("felih.DigForHoe", "DigForHoe", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "felih.DigForHoe";

	public const string NAME = "DigForHoe";

	public const string VERSION = "1.0.1";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> DetailedLog;

	private void Awake()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		DetailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "DetailedLog", false, "Writes details to the BepInEx log.");
		new Harmony("felih.DigForHoe").PatchAll(typeof(Plugin).Assembly);
		Log.LogInfo((object)"DigForHoe 1.0.1 loaded");
	}

	internal static void Debug(string msg)
	{
		if (DetailedLog.Value)
		{
			Log.LogInfo((object)msg);
		}
	}
}
[HarmonyPatch]
internal static class DigPiece
{
	internal const string PieceName = "DigForHoe_dig";

	private static GameObject _holder;

	private static GameObject _piece;

	private static int _digHash;

	private static string _digName;

	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	[HarmonyPostfix]
	private static void OnObjectDBAwake(ObjectDB __instance)
	{
		Setup(__instance);
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	private static void OnCopyOtherDB(ObjectDB __instance)
	{
		Setup(__instance);
	}

	private static void Setup(ObjectDB db)
	{
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Expected O, but got Unknown
		GameObject itemPrefab = db.GetItemPrefab("Hoe");
		GameObject itemPrefab2 = db.GetItemPrefab("PickaxeAntler");
		if ((Object)(object)itemPrefab == (Object)null || (Object)(object)itemPrefab2 == (Object)null)
		{
			return;
		}
		PieceTable buildPieces = itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_buildPieces;
		GameObject spawnOnHitTerrain = itemPrefab2.GetComponent<ItemDrop>().m_itemData.m_shared.m_spawnOnHitTerrain;
		if ((Object)(object)buildPieces == (Object)null || (Object)(object)spawnOnHitTerrain == (Object)null)
		{
			return;
		}
		TerrainOp componentInChildren = spawnOnHitTerrain.GetComponentInChildren<TerrainOp>();
		if ((Object)(object)componentInChildren == (Object)null)
		{
			Plugin.Log.LogWarning((object)("Pickaxe dig has no TerrainOp: " + ((Object)spawnOnHitTerrain).name));
		}
		else
		{
			if (buildPieces.m_pieces.Exists((GameObject p) => (Object)(object)p != (Object)null && ((Object)p).name == "DigForHoe_dig"))
			{
				return;
			}
			if ((Object)(object)_piece == (Object)null)
			{
				GameObject val = FindRaise(buildPieces.m_pieces);
				if ((Object)(object)val == (Object)null)
				{
					Plugin.Log.LogWarning((object)"Hoe 'raise ground' piece not found");
					return;
				}
				_holder = new GameObject("DigForHoe_templates");
				_holder.SetActive(false);
				Object.DontDestroyOnLoad((Object)(object)_holder);
				_piece = Object.Instantiate<GameObject>(val, _holder.transform);
				((Object)_piece).name = "DigForHoe_dig";
				ZNetView component = _piece.GetComponent<ZNetView>();
				if ((Object)(object)component != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)component);
				}
				TerrainOp component2 = _piece.GetComponent<TerrainOp>();
				component2.m_settings = componentInChildren.m_settings;
				component2.m_onPlacedEffect = componentInChildren.m_onPlacedEffect;
				component2.m_spawnOnPlaced = componentInChildren.m_spawnOnPlaced;
				component2.m_chanceToSpawn = componentInChildren.m_chanceToSpawn;
				component2.m_maxSpawned = componentInChildren.m_maxSpawned;
				component2.m_spawnAtMaxLevelDepth = componentInChildren.m_spawnAtMaxLevelDepth;
				Piece component3 = _piece.GetComponent<Piece>();
				component3.m_name = "Dig";
				component3.m_description = "Lowers the ground like a pickaxe hit, where you aim.";
				component3.m_resources = (Requirement[])(object)new Requirement[0];
				if ((Object)(object)component3.m_craftingStation != (Object)null)
				{
					Plugin.Debug("[setup] dig no longer needs " + component3.m_craftingStation.m_name + " nearby");
				}
				component3.m_craftingStation = null;
				Sprite icon = itemPrefab2.GetComponent<ItemDrop>().m_itemData.GetIcon();
				if ((Object)(object)icon != (Object)null)
				{
					component3.m_icon = icon;
				}
				_digName = Utils.GetPrefabName(((Object)((Component)componentInChildren).gameObject).name);
				_digHash = StringExtensionMethods.GetStableHashCode(_digName);
				Plugin.Debug("[setup] dig piece built from " + ((Object)val).name + ", uses " + _digName + " (raise " + componentInChildren.m_settings.m_raise + " delta " + componentInChildren.m_settings.m_raiseDelta + " radius " + componentInChildren.m_settings.m_raiseRadius + ")");
			}
			int num = buildPieces.m_pieces.FindIndex((GameObject p) => (Object)(object)p != (Object)null && (Object)(object)p.GetComponent<TerrainOp>() != (Object)null && p.GetComponent<TerrainOp>().m_settings.m_raise && p.GetComponent<TerrainOp>().m_settings.m_raiseDelta > 0f);
			if (num >= 0)
			{
				buildPieces.m_pieces.Insert(num + 1, _piece);
			}
			else
			{
				buildPieces.m_pieces.Add(_piece);
			}
			Plugin.Debug("[setup] added Dig to the hoe");
		}
	}

	private static GameObject FindRaise(List<GameObject> pieces)
	{
		foreach (GameObject piece in pieces)
		{
			if (!((Object)(object)piece == (Object)null))
			{
				TerrainOp component = piece.GetComponent<TerrainOp>();
				if ((Object)(object)component != (Object)null && component.m_settings.m_raise && component.m_settings.m_raiseDelta > 0f)
				{
					return piece;
				}
			}
		}
		return null;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(PieceTable), "UpdateAvailable")]
	private static void ShowInMenu(PieceTable __instance)
	{
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Expected I4, but got Unknown
		if ((Object)(object)_piece == (Object)null || !__instance.m_pieces.Contains(_piece))
		{
			return;
		}
		Piece component = _piece.GetComponent<Piece>();
		Traverse val = Traverse.Create((object)__instance);
		HashSet<Piece> value = val.Field<HashSet<Piece>>("m_availablePieces").Value;
		if (value == null || value.Contains(component))
		{
			return;
		}
		val.Field<HashSet<Piece>>("m_enabledPieces").Value?.Add(component);
		value.Add(component);
		List<List<Piece>> value2 = val.Field<List<List<Piece>>>("m_availablePiecesByCategory").Value;
		if (value2 == null)
		{
			return;
		}
		int num = (int)component.m_category;
		for (int i = 0; i < value2.Count; i++)
		{
			if (num == 100 || i == num)
			{
				List<Piece> list = value2[i];
				int num2 = list.FindIndex((Piece p) => (Object)(object)p != (Object)null && ((Object)((Component)p).gameObject).name == "raise_v2");
				if (num2 >= 0)
				{
					list.Insert(num2 + 1, component);
				}
				else
				{
					list.Add(component);
				}
			}
		}
	}

	[HarmonyPatch(typeof(Settings), "Serialize")]
	[HarmonyPrefix]
	private static bool Serialize(ZPackage pkg, GameObject prefab)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		if (_digHash == 0 || (Object)(object)prefab == (Object)null || Utils.GetPrefabName(((Object)prefab).name) != "DigForHoe_dig")
		{
			return true;
		}
		pkg.Write(_digHash);
		Plugin.Debug("[dig] at " + prefab.transform.position);
		return false;
	}
}