Decompiled source of FrelsiHammer v1.0.1

FrelsiHammer.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
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: AssemblyVersion("0.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 FrelsiHammer
{
	[BepInPlugin("com.zeecamelsnake.frelsihammer", "Frelsi Hammer", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<bool> cfgWorkbench;

		public static ConfigEntry<bool> cfgForge;

		public static ConfigEntry<bool> cfgStonecutter;

		public static ConfigEntry<bool> cfgBlackForge;

		public static ConfigEntry<bool> cfgGaldrTable;

		public static ConfigEntry<bool> cfgArtisanTable;

		public static ConfigEntry<float> cfgDurabilityMultiplier;

		public static ConfigEntry<bool> cfgUnbreakableAtMaxLevel;

		public static ConfigEntry<bool> cfgRequireLevel5WorkbenchForMaxLevel;

		public static Dictionary<Piece, CraftingStation> m_originalStations = new Dictionary<Piece, CraftingStation>();

		private void Awake()
		{
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Expected O, but got Unknown
			cfgWorkbench = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Station-less Building", "Workbench", true, "Enable building without a Workbench (if you have built one before).");
			cfgForge = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Station-less Building", "Forge", true, "Enable building without a Forge (if you have built one before).");
			cfgStonecutter = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Station-less Building", "Stonecutter", true, "Enable building without a Stonecutter (if you have built one before).");
			cfgBlackForge = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Station-less Building", "BlackForge", true, "Enable building without a Black Forge (if you have built one before).");
			cfgGaldrTable = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Station-less Building", "GaldrTable", true, "Enable building without a Galdr Table (if you have built one before).");
			cfgArtisanTable = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Station-less Building", "ArtisanTable", true, "Enable building without an Artisan Table (if you have built one before).");
			cfgDurabilityMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("2. Durability", "DurabilityMultiplier", 2f, new ConfigDescription("Multiplier for the Hammer's max durability.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 3f), Array.Empty<object>()));
			cfgUnbreakableAtMaxLevel = ((BaseUnityPlugin)this).Config.Bind<bool>("3. Max Level Upgrade", "UnbreakableAtMaxLevel", true, "If true, a level 4 Hammer never loses durability.");
			cfgRequireLevel5WorkbenchForMaxLevel = ((BaseUnityPlugin)this).Config.Bind<bool>("3. Max Level Upgrade", "RequireLevel5WorkbenchForMaxLevel", true, "If true, upgrading the Hammer to level 4 requires a Level 5 Workbench.");
			((BaseUnityPlugin)this).Config.SettingChanged += Config_SettingChanged;
			Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "com.zeecamelsnake.frelsihammer");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Frelsi Hammer initialized.");
		}

		private void Config_SettingChanged(object sender, SettingChangedEventArgs e)
		{
			if ((Object)(object)Player.m_localPlayer != (Object)null)
			{
				UpdatePieceRequirements(Player.m_localPlayer);
				Inventory inventory = ((Humanoid)Player.m_localPlayer).GetInventory();
				if (inventory != null)
				{
					foreach (ItemData allItem in inventory.GetAllItems())
					{
						if (allItem != null && allItem.m_shared != null && allItem.m_shared.m_name == "$item_hammer" && allItem.m_durability > allItem.GetMaxDurability())
						{
							allItem.m_durability = allItem.GetMaxDurability();
						}
					}
				}
			}
			UpdateHammerPrefab();
		}

		public static void UpdateHammerPrefab()
		{
			if ((Object)(object)ObjectDB.instance == (Object)null || ObjectDB.instance.m_items == null)
			{
				return;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Hammer");
			if ((Object)(object)itemPrefab != (Object)null)
			{
				ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
				if ((Object)(object)component != (Object)null && component.m_itemData != null && component.m_itemData.m_shared != null)
				{
					component.m_itemData.m_shared.m_maxQuality = 4;
				}
			}
		}

		public static void UpdatePieceRequirements(Player p)
		{
			if ((Object)(object)p == (Object)null || (Object)(object)ObjectDB.instance == (Object)null)
			{
				return;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Hammer");
			if ((Object)(object)itemPrefab == (Object)null)
			{
				return;
			}
			ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null || (Object)(object)component.m_itemData.m_shared.m_buildPieces == (Object)null)
			{
				return;
			}
			PieceTable buildPieces = component.m_itemData.m_shared.m_buildPieces;
			if (buildPieces.m_pieces == null)
			{
				return;
			}
			foreach (GameObject piece in buildPieces.m_pieces)
			{
				Piece component2 = piece.GetComponent<Piece>();
				if ((Object)(object)component2 == (Object)null)
				{
					continue;
				}
				if (!m_originalStations.ContainsKey(component2))
				{
					m_originalStations[component2] = component2.m_craftingStation;
				}
				CraftingStation val = m_originalStations[component2];
				if (!((Object)(object)val != (Object)null))
				{
					continue;
				}
				bool flag = false;
				string name = val.m_name;
				if (p.m_customData.ContainsKey("FrelsiHammer_Built_" + name))
				{
					if (name == "$piece_workbench" && cfgWorkbench.Value)
					{
						flag = true;
					}
					else if (name == "$piece_forge" && cfgForge.Value)
					{
						flag = true;
					}
					else if (name == "$piece_stonecutter" && cfgStonecutter.Value)
					{
						flag = true;
					}
					else if (name == "$piece_blackforge" && cfgBlackForge.Value)
					{
						flag = true;
					}
					else if (name == "$piece_magetable" && cfgGaldrTable.Value)
					{
						flag = true;
					}
					else if (name == "$piece_artisanstation" && cfgArtisanTable.Value)
					{
						flag = true;
					}
				}
				component2.m_craftingStation = (flag ? null : val);
			}
		}

		private void Update()
		{
			if (!((Object)(object)Player.m_localPlayer == (Object)null))
			{
				ItemData value = Traverse.Create((object)Player.m_localPlayer).Field<ItemData>("m_rightItem").Value;
				if (value != null && value.m_shared.m_name == "$item_hammer" && cfgUnbreakableAtMaxLevel.Value && value.m_quality >= 4)
				{
					value.m_durability = value.GetMaxDurability();
				}
			}
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	public static class ObjectDB_Awake_Patch
	{
		public static void Postfix(ObjectDB __instance)
		{
			Plugin.UpdateHammerPrefab();
		}
	}
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	public static class Player_OnSpawned_Patch
	{
		public static void Postfix(Player __instance)
		{
			Plugin.UpdatePieceRequirements(__instance);
			foreach (ItemData allItem in ((Humanoid)__instance).GetInventory().GetAllItems())
			{
				if (!(allItem.m_shared.m_name == "$item_hammer"))
				{
				}
			}
		}
	}
	[HarmonyPatch(typeof(Player), "PlacePiece")]
	public static class Player_PlacePiece_Patch
	{
		public static void Postfix(Player __instance, Piece piece)
		{
			if ((Object)(object)piece == (Object)null || (Object)(object)__instance == (Object)null || __instance.m_customData == null)
			{
				return;
			}
			CraftingStation component = ((Component)piece).GetComponent<CraftingStation>();
			if ((Object)(object)component != (Object)null)
			{
				string key = "FrelsiHammer_Built_" + component.m_name;
				if (!__instance.m_customData.ContainsKey(key))
				{
					__instance.m_customData[key] = "true";
					Plugin.UpdatePieceRequirements(__instance);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Recipe), "GetRequiredStationLevel")]
	public static class Recipe_GetRequiredStationLevel_Patch
	{
		public static void Postfix(Recipe __instance, int quality, ref int __result)
		{
			if ((Object)(object)__instance.m_item != (Object)null && __instance.m_item.m_itemData.m_shared.m_name == "$item_hammer" && quality == 4 && Plugin.cfgRequireLevel5WorkbenchForMaxLevel.Value)
			{
				__result = 5;
			}
		}
	}
	[HarmonyPatch(typeof(ItemData), "GetMaxDurability", new Type[] { typeof(int) })]
	public static class ItemDrop_ItemData_GetMaxDurability_Patch
	{
		public static void Postfix(ItemData __instance, ref float __result)
		{
			if (__instance != null && __instance.m_shared != null && __instance.m_shared.m_name == "$item_hammer")
			{
				__result *= Plugin.cfgDurabilityMultiplier.Value;
			}
		}
	}
	[HarmonyPatch(typeof(Player), "CheckCanRemovePiece")]
	public static class Player_CheckCanRemovePiece_Patch
	{
		public static bool Prefix(Player __instance, Piece piece, ref bool __result)
		{
			if ((Object)(object)__instance == (Object)null || Traverse.Create((object)__instance).Field<bool>("m_noPlacementCost").Value)
			{
				return true;
			}
			if ((Object)(object)piece == (Object)null || (Object)(object)piece.m_craftingStation == (Object)null)
			{
				return true;
			}
			string name = piece.m_craftingStation.m_name;
			bool flag = false;
			if (__instance.m_customData != null && __instance.m_customData.ContainsKey("FrelsiHammer_Built_" + name))
			{
				if (name == "$piece_workbench" && Plugin.cfgWorkbench.Value)
				{
					flag = true;
				}
				else if (name == "$piece_forge" && Plugin.cfgForge.Value)
				{
					flag = true;
				}
				else if (name == "$piece_stonecutter" && Plugin.cfgStonecutter.Value)
				{
					flag = true;
				}
				else if (name == "$piece_blackforge" && Plugin.cfgBlackForge.Value)
				{
					flag = true;
				}
				else if (name == "$piece_magetable" && Plugin.cfgGaldrTable.Value)
				{
					flag = true;
				}
				else if (name == "$piece_artisanstation" && Plugin.cfgArtisanTable.Value)
				{
					flag = true;
				}
			}
			if (flag)
			{
				__result = true;
				return false;
			}
			return true;
		}
	}
}