Decompiled source of PotteryBarn v1.13.0

PotteryBarn.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using ComfyLib;
using HarmonyLib;
using Jotunn.Managers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("PotteryBarn")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PotteryBarn")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cda4d928-01d6-4d77-af0d-9caff7369336")]
[assembly: AssemblyFileVersion("1.13.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.13.0.0")]
[module: UnverifiableCode]
namespace PotteryBarn
{
	public class DvergrPieces
	{
		public static readonly Dictionary<string, Dictionary<string, int>> DvergrPrefabs = new Dictionary<string, Dictionary<string, int>>
		{
			{
				"blackmarble_head01",
				new Dictionary<string, int> { { "Copper", 4 } }
			},
			{
				"blackmarble_head02",
				new Dictionary<string, int> { { "Copper", 4 } }
			},
			{
				"metalbar_1x2",
				new Dictionary<string, int> { { "Copper", 8 } }
			},
			{
				"blackmarble_2x2_enforced",
				new Dictionary<string, int>
				{
					{ "BlackMarble", 8 },
					{ "Copper", 4 }
				}
			},
			{
				"blackmarble_out_2",
				new Dictionary<string, int> { { "BlackMarble", 8 } }
			},
			{
				"blackmarble_slope_1x2",
				new Dictionary<string, int> { { "BlackMarble", 3 } }
			},
			{
				"blackmarble_tile_floor_1x1",
				new Dictionary<string, int> { { "BlackMarble", 2 } }
			},
			{
				"blackmarble_tile_floor_2x2",
				new Dictionary<string, int> { { "BlackMarble", 4 } }
			},
			{
				"blackmarble_tile_wall_2x4",
				new Dictionary<string, int> { { "BlackMarble", 8 } }
			},
			{
				"blackmarble_base_2",
				new Dictionary<string, int> { { "BlackMarble", 10 } }
			},
			{
				"blackmarble_column_3",
				new Dictionary<string, int> { { "BlackMarble", 24 } }
			},
			{
				"blackmarble_floor_large",
				new Dictionary<string, int> { { "BlackMarble", 80 } }
			},
			{
				"blackmarble_head_big01",
				new Dictionary<string, int> { { "BlackMarble", 8 } }
			},
			{
				"blackmarble_head_big02",
				new Dictionary<string, int> { { "BlackMarble", 8 } }
			},
			{
				"dvergrprops_wood_floor",
				new Dictionary<string, int> { { "YggdrasilWood", 2 } }
			},
			{
				"dvergrprops_wood_stair",
				new Dictionary<string, int> { { "YggdrasilWood", 2 } }
			},
			{
				"piece_dvergr_pole",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 2 },
					{ "Copper", 1 }
				}
			},
			{
				"piece_dvergr_wood_door",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 12 },
					{ "Copper", 12 }
				}
			},
			{
				"piece_dvergr_wood_wall",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 10 },
					{ "Copper", 5 }
				}
			},
			{
				"dvergrprops_banner",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 2 },
					{ "JuteBlue", 4 }
				}
			},
			{
				"dvergrprops_curtain",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 2 },
					{ "JuteBlue", 4 }
				}
			},
			{
				"dvergrprops_wood_beam",
				new Dictionary<string, int> { { "YggdrasilWood", 12 } }
			},
			{
				"dvergrprops_wood_pole",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 8 },
					{ "Copper", 4 }
				}
			},
			{
				"dvergrprops_wood_wall",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 32 },
					{ "Copper", 16 }
				}
			},
			{
				"dvergrtown_stair_corner_wood_left",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 6 },
					{ "Copper", 3 }
				}
			},
			{
				"dvergrprops_lantern_standing",
				new Dictionary<string, int> { { "Lantern", 1 } }
			}
		};

		public static readonly Dictionary<string, Dictionary<string, int>> DvergrPrefabDefaultDrops = new Dictionary<string, Dictionary<string, int>>
		{
			{
				"blackmarble_head01",
				new Dictionary<string, int> { { "CopperScrap", 2 } }
			},
			{
				"blackmarble_head02",
				new Dictionary<string, int> { { "CopperScrap", 2 } }
			},
			{
				"metalbar_1x2",
				new Dictionary<string, int> { { "Copper", 8 } }
			},
			{
				"blackmarble_2x2_enforced",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"blackmarble_out_2",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"blackmarble_slope_1x2",
				new Dictionary<string, int> { { "BlackMarble", 3 } }
			},
			{
				"blackmarble_tile_floor_1x1",
				new Dictionary<string, int> { { "BlackMarble", 2 } }
			},
			{
				"blackmarble_tile_floor_2x2",
				new Dictionary<string, int> { { "BlackMarble", 2 } }
			},
			{
				"blackmarble_tile_wall_2x4",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"blackmarble_base_2",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"blackmarble_column_3",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"blackmarble_floor_large",
				new Dictionary<string, int> { { "BlackMarble", 8 } }
			},
			{
				"blackmarble_head_big01",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"blackmarble_head_big02",
				new Dictionary<string, int> { { "BlackMarble", 1 } }
			},
			{
				"dvergrprops_wood_floor",
				new Dictionary<string, int> { { "Wood", 2 } }
			},
			{
				"dvergrprops_wood_stair",
				new Dictionary<string, int> { { "YggdrasilWood", 4 } }
			},
			{
				"piece_dvergr_pole",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 1 },
					{ "Copper", 1 }
				}
			},
			{
				"piece_dvergr_wood_door",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 2 },
					{ "Copper", 2 }
				}
			},
			{
				"piece_dvergr_wood_wall",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 6 },
					{ "Copper", 3 }
				}
			},
			{
				"dvergrprops_banner",
				new Dictionary<string, int> { { "JuteBlue", 1 } }
			},
			{
				"dvergrprops_curtain",
				new Dictionary<string, int> { { "JuteBlue", 2 } }
			},
			{
				"dvergrprops_wood_beam",
				new Dictionary<string, int> { { "Wood", 1 } }
			},
			{
				"dvergrprops_wood_pole",
				new Dictionary<string, int> { { "CopperScrap", 1 } }
			},
			{
				"dvergrprops_wood_wall",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "CopperScrap", 1 }
				}
			},
			{
				"dvergrtown_stair_corner_wood_left",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "CopperScrap", 1 }
				}
			},
			{
				"dvergrprops_lantern_standing",
				new Dictionary<string, int> { { "SurtlingCore", 2 } }
			},
			{
				"dvergrprops_hooknchain",
				new Dictionary<string, int>
				{
					{ "Chain", 1 },
					{ "Copper", 1 }
				}
			}
		};

		public static readonly Dictionary<string, string> DvergrPrefabCraftingStationRequirements = new Dictionary<string, string>
		{
			{ "blackmarble_head01", "blackforge" },
			{ "blackmarble_head02", "blackforge" },
			{ "metalbar_1x2", "blackforge" },
			{ "blackmarble_2x2_enforced", "blackforge" },
			{ "blackmarble_out_2", "piece_stonecutter" },
			{ "blackmarble_slope_1x2", "piece_stonecutter" },
			{ "blackmarble_tile_floor_1x1", "piece_stonecutter" },
			{ "blackmarble_tile_floor_2x2", "piece_stonecutter" },
			{ "blackmarble_tile_wall_2x4", "piece_stonecutter" },
			{ "blackmarble_base_2", "piece_stonecutter" },
			{ "blackmarble_column_3", "piece_stonecutter" },
			{ "blackmarble_floor_large", "piece_stonecutter" },
			{ "blackmarble_head_big01", "piece_stonecutter" },
			{ "blackmarble_head_big02", "piece_stonecutter" },
			{ "dvergrprops_wood_floor", "piece_workbench" },
			{ "dvergrprops_wood_stair", "piece_workbench" },
			{ "piece_dvergr_pole", "blackforge" },
			{ "piece_dvergr_wood_door", "blackforge" },
			{ "piece_dvergr_wood_wall", "blackforge" },
			{ "dvergrprops_banner", "piece_workbench" },
			{ "dvergrprops_curtain", "piece_workbench" },
			{ "dvergrprops_wood_beam", "piece_workbench" },
			{ "dvergrprops_wood_pole", "blackforge" },
			{ "dvergrprops_wood_wall", "blackforge" },
			{ "dvergrtown_stair_corner_wood_left", "blackforge" },
			{ "dvergrprops_lantern_standing", "blackforge" },
			{ "dvergrprops_hooknchain", "blackforge" }
		};
	}
	public static class PieceRequirementExtensions
	{
		public static Requirement SetAmount(this Requirement requirement, int amount)
		{
			requirement.m_amount = amount;
			return requirement;
		}

		public static Requirement SetRecover(this Requirement requirement, bool recover)
		{
			requirement.m_recover = recover;
			return requirement;
		}
	}
	public static class PieceTableExtensions
	{
		public static bool AddPiece(this PieceTable pieceTable, Piece piece)
		{
			if (!Object.op_Implicit((Object)(object)piece) || !Object.op_Implicit((Object)(object)pieceTable) || pieceTable.m_pieces == null || pieceTable.m_pieces.Contains(((Component)piece).gameObject))
			{
				return false;
			}
			pieceTable.m_pieces.Add(((Component)piece).gameObject);
			return true;
		}
	}
	public static class PlacementGhostUtils
	{
		public static bool HasActiveComponents(GameObject gameObject)
		{
			MonsterAI val = default(MonsterAI);
			AnimalAI val2 = default(AnimalAI);
			Tameable val3 = default(Tameable);
			Ragdoll val4 = default(Ragdoll);
			Humanoid val5 = default(Humanoid);
			if (!gameObject.TryGetComponent<MonsterAI>(ref val) && !gameObject.TryGetComponent<AnimalAI>(ref val2) && !gameObject.TryGetComponent<Tameable>(ref val3) && !gameObject.TryGetComponent<Ragdoll>(ref val4))
			{
				return gameObject.TryGetComponent<Humanoid>(ref val5);
			}
			return true;
		}

		public static void DestroyActiveComponents(GameObject gameObject)
		{
			MonsterAI val = default(MonsterAI);
			if (gameObject.TryGetComponent<MonsterAI>(ref val))
			{
				Object.DestroyImmediate((Object)(object)val);
			}
			AnimalAI val2 = default(AnimalAI);
			if (gameObject.TryGetComponent<AnimalAI>(ref val2))
			{
				Object.DestroyImmediate((Object)(object)val2);
			}
			Tameable val3 = default(Tameable);
			if (gameObject.TryGetComponent<Tameable>(ref val3))
			{
				Object.DestroyImmediate((Object)(object)val3);
			}
			Ragdoll val4 = default(Ragdoll);
			if (gameObject.TryGetComponent<Ragdoll>(ref val4))
			{
				Object.DestroyImmediate((Object)(object)val4);
			}
			Humanoid val5 = default(Humanoid);
			if (gameObject.TryGetComponent<Humanoid>(ref val5))
			{
				Humanoid val6 = val5;
				if (val6.m_defaultItems == null)
				{
					val6.m_defaultItems = Array.Empty<GameObject>();
				}
				val6 = val5;
				if (val6.m_randomWeapon == null)
				{
					val6.m_randomWeapon = Array.Empty<GameObject>();
				}
				val6 = val5;
				if (val6.m_randomArmor == null)
				{
					val6.m_randomArmor = Array.Empty<GameObject>();
				}
				val6 = val5;
				if (val6.m_randomShield == null)
				{
					val6.m_randomShield = Array.Empty<GameObject>();
				}
				val6 = val5;
				if (val6.m_randomSets == null)
				{
					val6.m_randomSets = Array.Empty<ItemSet>();
				}
			}
		}
	}
	[HarmonyPatch(typeof(DropOnDestroyed))]
	internal static class DropOnDestroyedPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("OnDestroyed")]
		private static bool OnDestroyedPrefix(DropOnDestroyed __instance)
		{
			Piece val = default(Piece);
			if (((Component)__instance).TryGetComponent<Piece>(ref val) && DvergrPieces.DvergrPrefabs.Keys.Contains(val.m_description) && val.IsPlacedByPlayer())
			{
				return false;
			}
			if (PotteryBarn.IsDropTableDisabled)
			{
				PotteryBarn.IsDropTableDisabled = false;
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(EffectList))]
	internal static class EffectListPatch
	{
		[HarmonyTranspiler]
		[HarmonyPatch("Create")]
		private static IEnumerable<CodeInstruction> CreateTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).Start().MatchStartForward((CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Ldloc_2, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(EffectData), "m_enabled"), (string)null)
			}).ThrowIfInvalid("Could not patch EffectList.Create()! (m_enabled)")
				.Advance(1)
				.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<EffectData, EffectData>>((Func<EffectData, EffectData>)CheckEffectDataDelegate) })
				.InstructionEnumeration();
		}

		private static EffectData CheckEffectDataDelegate(EffectData effectData)
		{
			if (PluginConfig.IsModEnabled.Value && effectData.m_enabled && !Object.op_Implicit((Object)(object)effectData.m_prefab))
			{
				effectData.m_enabled = false;
			}
			return effectData;
		}
	}
	[HarmonyPatch(typeof(Piece))]
	internal static class PiecePatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("DropResources")]
		private static bool DropResourcePrefix(Piece __instance)
		{
			if (Requirements.HammerCreatorShopItems.Keys.Contains(__instance.m_description))
			{
				Container container = default(Container);
				if (((Component)__instance).TryGetComponent<Container>(ref container))
				{
					DropContainerContents(container);
				}
				if (__instance.IsCreator())
				{
					PotteryBarn.IsDropTableDisabled = true;
					return true;
				}
				return false;
			}
			if (DvergrPieces.DvergrPrefabs.Keys.Contains(__instance.m_description) && !__instance.IsPlacedByPlayer())
			{
				DropDefaultResources(__instance);
				return false;
			}
			return true;
		}

		private static void DropDefaultResources(Piece piece)
		{
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			foreach (KeyValuePair<string, int> item in DvergrPieces.DvergrPrefabDefaultDrops[piece.m_description])
			{
				Container val = null;
				GameObject prefab = ZNetScene.instance.GetPrefab(item.Key);
				int num = item.Value;
				if (Object.op_Implicit((Object)(object)piece.m_destroyedLootPrefab))
				{
					while (num > 0)
					{
						ItemData val2 = prefab.GetComponent<ItemDrop>().m_itemData.Clone();
						val2.m_dropPrefab = prefab;
						val2.m_stack = Mathf.Min(num, val2.m_shared.m_maxStackSize);
						num -= val2.m_stack;
						if ((Object)(object)val == (Object)null || !val.GetInventory().HaveEmptySlot())
						{
							val = Object.Instantiate<GameObject>(piece.m_destroyedLootPrefab, ((Component)piece).transform.position + Vector3.up, Quaternion.identity).GetComponent<Container>();
						}
						val.GetInventory().AddItem(val2);
					}
				}
				else
				{
					while (num > 0)
					{
						ItemDrop component = Object.Instantiate<GameObject>(prefab, ((Component)piece).transform.position + Vector3.up, Quaternion.identity).GetComponent<ItemDrop>();
						component.SetStack(Mathf.Min(num, component.m_itemData.m_shared.m_maxStackSize));
						num -= component.m_itemData.m_stack;
					}
				}
			}
		}

		private static void DropContainerContents(Container container)
		{
			container.DropAllItems();
		}
	}
	[HarmonyPatch(typeof(Player))]
	internal static class PlayerPatch
	{
		[HarmonyTranspiler]
		[HarmonyPatch("SetupPlacementGhost")]
		private static IEnumerable<CodeInstruction> SetupPlacementGhostTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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)
			//IL_0065: Expected O, but got Unknown
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).Start().MatchStartForward((CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Call, (object)ReflectionUtils.GetGenericMethod(typeof(Object), "Instantiate", 1, new Type[1] { typeof(Type) }).MakeGenericMethod(typeof(GameObject)), (string)null),
				new CodeMatch((OpCode?)OpCodes.Stfld, (object)AccessTools.Field(typeof(Player), "m_placementGhost"), (string)null)
			}).ThrowIfInvalid("Could not patch Player.SetupPlacementGhost()! (m_placementGhost)")
				.SetInstructionAndAdvance(Transpilers.EmitDelegate<Func<GameObject, GameObject>>((Func<GameObject, GameObject>)SetupPlacementGhostInstantiateDelegate))
				.InstructionEnumeration();
		}

		private static GameObject SetupPlacementGhostInstantiateDelegate(GameObject selectedPrefab)
		{
			bool flag = false;
			if (PluginConfig.IsModEnabled.Value && PlacementGhostUtils.HasActiveComponents(selectedPrefab))
			{
				flag = selectedPrefab.activeSelf;
				selectedPrefab.SetActive(false);
			}
			GameObject val = Object.Instantiate<GameObject>(selectedPrefab);
			if (PluginConfig.IsModEnabled.Value && flag)
			{
				selectedPrefab.SetActive(true);
				PlacementGhostUtils.DestroyActiveComponents(val);
				val.SetActive(true);
			}
			return val;
		}

		[HarmonyPrefix]
		[HarmonyPatch("CheckCanRemovePiece")]
		private static bool CheckCanRemovePrefix(Piece piece, ref bool __result)
		{
			if (PluginConfig.IsModEnabled.Value)
			{
				if (!piece.IsPlacedByPlayer() && PotteryBarn.IsCreatorShopPiece(piece))
				{
					__result = false;
					return false;
				}
				if (PotteryBarn.IsCreatorShopPiece(piece) && !piece.IsCreator())
				{
					__result = false;
					return false;
				}
				if (!PotteryBarn.IsDestructibleCreatorShopPiece(piece) && PotteryBarn.IsCreatorShopPiece(piece))
				{
					__result = false;
					return false;
				}
			}
			return true;
		}
	}
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			IsModEnabled = config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod (restart required).");
		}
	}
	public static class PieceExtensions
	{
		public static Piece SetCanBeRemoved(this Piece piece, bool canBeRemoved)
		{
			piece.m_canBeRemoved = canBeRemoved;
			return piece;
		}

		public static Piece SetCategory(this Piece piece, PieceCategory pieceCategory)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			piece.m_category = pieceCategory;
			return piece;
		}

		public static Piece SetCraftingStation(this Piece piece, CraftingStation craftingStation)
		{
			piece.m_craftingStation = craftingStation;
			return piece;
		}

		public static Piece SetGroundOnly(this Piece piece, bool groundOnly)
		{
			piece.m_groundOnly = groundOnly;
			return piece;
		}

		public static Piece SetName(this Piece piece, string name)
		{
			piece.m_name = name;
			return piece;
		}

		public static Piece SetResources(this Piece piece, params Requirement[] requirements)
		{
			piece.m_resources = requirements;
			return piece;
		}

		public static Piece SetResource(this Piece piece, string resourceName, Action<Requirement> modifyResourceAction)
		{
			modifyResourceAction?.Invoke(piece.GetResource(resourceName));
			return piece;
		}

		public static Piece SetTargetNonPlayerBuilt(this Piece piece, bool canTarget)
		{
			piece.m_targetNonPlayerBuilt = canTarget;
			return piece;
		}

		public static Requirement GetResource(this Piece piece, string resourceName)
		{
			return piece.Ref<Piece>()?.m_resources?.Where(delegate(Requirement req)
			{
				object obj;
				if (req == null)
				{
					obj = null;
				}
				else
				{
					ItemDrop obj2 = req.m_resItem.Ref<ItemDrop>();
					obj = ((obj2 != null) ? ((Object)obj2).name : null);
				}
				return (string?)obj == resourceName;
			}).FirstOrDefault();
		}
	}
	[BepInPlugin("redseiko.valheim.potterybarn", "PotteryBarn", "1.13.0")]
	[BepInDependency("com.jotunn.jotunn", "2.19.2")]
	public class PotteryBarn : BaseUnityPlugin
	{
		public const string PluginGuid = "redseiko.valheim.potterybarn";

		public const string PluginName = "PotteryBarn";

		public const string PluginVersion = "1.13.0";

		private Harmony _harmony;

		private static PieceCategory _hammerCreatorShopCategory;

		private static PieceCategory _hammerBuildingCategory;

		private static PieceCategory _cultivatorCreatorShopCategory;

		private static PieceCategory _hammerMiscCategory;

		private static Sprite _standardPrefabIconSprite;

		private static Quaternion _prefabIconRenderRotation;

		private static readonly Regex PrefabNameRegex = new Regex("([a-z])([A-Z])");

		public static bool IsDropTableDisabled { get; set; } = false;


		private void Awake()
		{
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			PieceManager.OnPiecesRegistered += AddPieces;
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.potterybarn");
		}

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

		public static void AddPieces()
		{
			PieceManager.OnPiecesRegistered -= AddPieces;
			AddHammerPieces(PieceManager.Instance.GetPieceTable("_HammerPieceTable"));
			AddCultivatorPieces(PieceManager.Instance.GetPieceTable("_CultivatorPieceTable"));
		}

		private static void AddHammerPieces(PieceTable pieceTable)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: 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)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0294: Unknown result type (might be due to invalid IL or missing references)
			//IL_0337: Unknown result type (might be due to invalid IL or missing references)
			_hammerCreatorShopCategory = PieceManager.Instance.AddPieceCategory("_HammerPieceTable", "CreatorShop");
			_hammerBuildingCategory = PieceManager.Instance.AddPieceCategory("_HammerPieceTable", "Building");
			_hammerMiscCategory = PieceManager.Instance.AddPieceCategory("_HammerPieceTable", "Misc");
			_standardPrefabIconSprite = _standardPrefabIconSprite ?? (_standardPrefabIconSprite = CreateColorSprite(Color32.op_Implicit(new Color32((byte)34, (byte)132, (byte)73, (byte)64))));
			_prefabIconRenderRotation = Quaternion.Euler(0f, -45f, 0f);
			Piece val = GetExistingPiece("ArmorStand_Female").SetName("ArmorStand_Female");
			val.m_comfort = 0;
			pieceTable.AddPiece(GetExistingPiece("turf_roof").SetName("turf_roof"));
			pieceTable.AddPiece(GetExistingPiece("turf_roof_top").SetName("turf_roof_top"));
			pieceTable.AddPiece(GetExistingPiece("turf_roof_wall").SetName("turf_roof_wall"));
			pieceTable.AddPiece(val);
			pieceTable.AddPiece(GetExistingPiece("ArmorStand_Male").SetName("ArmorStand_Male"));
			pieceTable.AddPiece(GetExistingPiece("stone_floor").SetResource("Stone", delegate(Requirement r)
			{
				r.SetAmount(12).SetRecover(recover: true);
			}));
			pieceTable.AddPiece(GetExistingPiece("wood_ledge").SetResource("Wood", delegate(Requirement r)
			{
				r.SetAmount(1).SetRecover(recover: true);
			}));
			foreach (KeyValuePair<string, Dictionary<string, int>> item in Requirements.HammerCreatorShopItems.OrderBy((KeyValuePair<string, Dictionary<string, int>> o) => o.Key).ToList())
			{
				GetOrAddPieceComponent(item.Key, pieceTable).SetResources(CreateRequirements(item.Value)).SetCategory(_hammerCreatorShopCategory).SetCraftingStation(GetCraftingStation(Requirements.craftingStationRequirements, item.Key))
					.SetCanBeRemoved(canBeRemoved: true)
					.SetTargetNonPlayerBuilt(canTarget: false);
			}
			foreach (KeyValuePair<string, Dictionary<string, int>> item2 in Requirements.MiscPrefabs.OrderBy((KeyValuePair<string, Dictionary<string, int>> o) => o.Key).ToList())
			{
				GetOrAddPieceComponent(item2.Key, pieceTable).SetResources(CreateRequirements(item2.Value)).SetCategory(_hammerMiscCategory).SetCraftingStation(GetCraftingStation(Requirements.craftingStationRequirements, item2.Key))
					.SetCanBeRemoved(canBeRemoved: true)
					.SetTargetNonPlayerBuilt(canTarget: true);
			}
			foreach (KeyValuePair<string, Dictionary<string, int>> item3 in DvergrPieces.DvergrPrefabs.OrderBy((KeyValuePair<string, Dictionary<string, int>> o) => o.Key).ToList())
			{
				GetOrAddPieceComponent(item3.Key, pieceTable).SetResources(CreateRequirements(item3.Value)).SetCategory(_hammerBuildingCategory).SetCraftingStation(GetCraftingStation(DvergrPieces.DvergrPrefabCraftingStationRequirements, item3.Key))
					.SetCanBeRemoved(canBeRemoved: true)
					.SetTargetNonPlayerBuilt(canTarget: false);
			}
		}

		private static void AddCultivatorPieces(PieceTable pieceTable)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected I4, but got Unknown
			pieceTable.m_useCategories = true;
			PieceManager.Instance.AddPieceCategory("_CultivatorPieceTable", "Misc");
			_cultivatorCreatorShopCategory = PieceManager.Instance.AddPieceCategory("_CultivatorPieceTable", "CreatorShop");
			foreach (KeyValuePair<string, Dictionary<string, int>> item in Requirements.CultivatorCreatorShopItems.OrderBy((KeyValuePair<string, Dictionary<string, int>> o) => o.Key).ToList())
			{
				GetOrAddPieceComponent(item.Key, pieceTable).SetResources(CreateRequirements(item.Value)).SetCategory(_cultivatorCreatorShopCategory).SetCraftingStation(GetCraftingStation(Requirements.craftingStationRequirements, item.Key))
					.SetCanBeRemoved(canBeRemoved: true)
					.SetTargetNonPlayerBuilt(canTarget: false);
			}
			ResizePieceTableCategories(pieceTable, _cultivatorCreatorShopCategory + 1);
		}

		private static void ResizePieceTableCategories(PieceTable pieceTable, int pieceCategoryMax)
		{
			while (pieceTable.m_availablePieces.Count < pieceCategoryMax)
			{
				pieceTable.m_availablePieces.Add(new List<Piece>());
			}
			Array.Resize(ref pieceTable.m_selectedPiece, pieceCategoryMax);
		}

		private static Requirement[] CreateRequirements(Dictionary<string, int> data)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_0042: Expected O, but got Unknown
			Requirement[] array = (Requirement[])(object)new Requirement[data.Count];
			for (int i = 0; i < data.Count; i++)
			{
				KeyValuePair<string, int> keyValuePair = data.ElementAt(i);
				Requirement val = new Requirement
				{
					m_resItem = Cache.GetPrefab<GameObject>(keyValuePair.Key).GetComponent<ItemDrop>(),
					m_amount = keyValuePair.Value
				};
				array[i] = val;
			}
			return array;
		}

		private static Piece GetExistingPiece(string prefabName)
		{
			ZNetScene obj = ZNetScene.s_instance.Ref<ZNetScene>();
			if (obj == null)
			{
				return null;
			}
			GameObject obj2 = obj.GetPrefab(prefabName).Ref<GameObject>();
			if (obj2 == null)
			{
				return null;
			}
			return obj2.GetComponent<Piece>();
		}

		private static Piece GetOrAddPieceComponent(string prefabName, PieceTable pieceTable)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			GameObject prefab = ZNetScene.instance.GetPrefab(prefabName);
			Piece val = default(Piece);
			if (!prefab.TryGetComponent<Piece>(ref val))
			{
				val = prefab.AddComponent<Piece>();
				val.m_name = FormatPrefabName(((Object)prefab).name);
				SetPlacementRestrictions(val);
			}
			if (!Object.op_Implicit((Object)(object)val.m_icon))
			{
				val.m_icon = LoadOrRenderIcon(prefab, _prefabIconRenderRotation, _standardPrefabIconSprite);
			}
			if (!pieceTable.m_pieces.Contains(prefab))
			{
				pieceTable.m_pieces.Add(prefab);
			}
			val.m_description = ((Object)prefab).name;
			return val;
		}

		private static string FormatPrefabName(string prefabName)
		{
			return PrefabNameRegex.Replace(prefabName, "$1 $2").TrimStart(new char[1] { ' ' }).Replace('_', ' ')
				.Replace("  ", " ");
		}

		private static Sprite LoadOrRenderIcon(GameObject prefab, Quaternion renderRotation, Sprite defaultSprite)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Expected O, but got Unknown
			RenderRequest val = new RenderRequest(prefab)
			{
				Rotation = renderRotation
			};
			return RenderManager.Instance.Render(val).Ref<Sprite>() ?? defaultSprite;
		}

		private static Piece SetPlacementRestrictions(Piece piece)
		{
			piece.m_repairPiece = false;
			piece.m_groundOnly = false;
			piece.m_groundPiece = false;
			piece.m_cultivatedGroundOnly = false;
			piece.m_waterPiece = false;
			piece.m_noInWater = false;
			piece.m_notOnWood = false;
			piece.m_notOnTiltingSurface = false;
			piece.m_inCeilingOnly = false;
			piece.m_notOnFloor = false;
			piece.m_onlyInTeleportArea = false;
			piece.m_allowedInDungeons = false;
			piece.m_clipEverything = true;
			return piece;
		}

		private static Sprite CreateColorSprite(Color color)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: 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)
			//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)
			//IL_0039: Expected O, but got Unknown
			Texture2D val = new Texture2D(1, 1);
			val.SetPixel(0, 0, color);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, 1f, 1f), Vector2.zero);
		}

		public static bool IsNewDvergrPiece(Piece piece)
		{
			if (DvergrPieces.DvergrPrefabs.Keys.Contains(piece.m_description))
			{
				return true;
			}
			return false;
		}

		public static bool IsCreatorShopPiece(Piece piece)
		{
			if (Requirements.HammerCreatorShopItems.Keys.Contains(piece.m_description))
			{
				return true;
			}
			return false;
		}

		public static bool IsDestructibleCreatorShopPiece(string prefabName)
		{
			if (Requirements.HammerCreatorShopItems.Keys.Contains(prefabName))
			{
				return true;
			}
			return false;
		}

		public static bool IsDestructibleCreatorShopPiece(Piece piece)
		{
			if (Requirements.HammerCreatorShopItems.Keys.Contains(piece.m_description))
			{
				return true;
			}
			return false;
		}

		public static bool IsBuildHammerItem(string prefabName)
		{
			if (Requirements.HammerCreatorShopItems.Keys.Contains(prefabName))
			{
				return true;
			}
			return false;
		}

		public static bool HasCraftingStationRequirement(string prefabName)
		{
			if (Requirements.craftingStationRequirements.Keys.Contains(prefabName))
			{
				return true;
			}
			return false;
		}

		public static CraftingStation GetCraftingStation(Dictionary<string, string> requirements, string prefabName)
		{
			if (requirements.ContainsKey(prefabName))
			{
				return PrefabManager.Instance.GetPrefab(requirements[prefabName]).GetComponent<CraftingStation>();
			}
			return null;
		}
	}
	public class Requirements
	{
		public static readonly Dictionary<string, Dictionary<string, int>> HammerCreatorShopItems = new Dictionary<string, Dictionary<string, int>>
		{
			{
				"goblin_banner",
				new Dictionary<string, int>
				{
					{ "FineWood", 2 },
					{ "LeatherScraps", 6 },
					{ "Bloodbag", 2 },
					{ "BoneFragments", 2 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_fence",
				new Dictionary<string, int>
				{
					{ "Wood", 4 },
					{ "BoneFragments", 8 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_pole",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "BoneFragments", 4 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_pole_small",
				new Dictionary<string, int>
				{
					{ "Wood", 1 },
					{ "BoneFragments", 2 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_roof_45d",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "DeerHide", 2 },
					{ "BoneFragments", 8 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_roof_45d_corner",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "DeerHide", 2 },
					{ "BoneFragments", 8 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_roof_cap",
				new Dictionary<string, int>
				{
					{ "Wood", 10 },
					{ "DeerHide", 6 },
					{ "BoneFragments", 12 },
					{ "MushroomBlue", 4 }
				}
			},
			{
				"goblin_stairs",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "BoneFragments", 4 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_stepladder",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "BoneFragments", 4 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_woodwall_1m",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "BoneFragments", 4 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_woodwall_2m",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "BoneFragments", 4 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"goblin_woodwall_2m_ribs",
				new Dictionary<string, int>
				{
					{ "Wood", 2 },
					{ "BoneFragments", 4 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"Skull1",
				new Dictionary<string, int>
				{
					{ "BoneFragments", 10 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"Skull2",
				new Dictionary<string, int>
				{
					{ "BoneFragments", 50 },
					{ "MushroomBlue", 10 }
				}
			},
			{
				"StatueCorgi",
				new Dictionary<string, int>
				{
					{ "Stone", 20 },
					{ "MushroomBlue", 5 }
				}
			},
			{
				"StatueDeer",
				new Dictionary<string, int>
				{
					{ "Stone", 20 },
					{ "MushroomBlue", 5 }
				}
			},
			{
				"StatueEvil",
				new Dictionary<string, int>
				{
					{ "Stone", 20 },
					{ "MushroomBlue", 5 }
				}
			},
			{
				"StatueHare",
				new Dictionary<string, int>
				{
					{ "Stone", 20 },
					{ "MushroomBlue", 5 }
				}
			},
			{
				"StatueSeed",
				new Dictionary<string, int>
				{
					{ "Stone", 20 },
					{ "MushroomBlue", 5 }
				}
			},
			{
				"stonechest",
				new Dictionary<string, int>
				{
					{ "Stone", 20 },
					{ "MushroomBlue", 10 }
				}
			},
			{
				"root07",
				new Dictionary<string, int> { { "ElderBark", 2 } }
			},
			{
				"root08",
				new Dictionary<string, int> { { "ElderBark", 2 } }
			},
			{
				"root11",
				new Dictionary<string, int> { { "ElderBark", 2 } }
			},
			{
				"root12",
				new Dictionary<string, int> { { "ElderBark", 2 } }
			},
			{
				"trader_wagon_destructable",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 50 },
					{ "BronzeNails", 80 },
					{ "Tar", 10 },
					{ "MushroomBlue", 10 }
				}
			},
			{
				"dvergrtown_wood_support",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 20 },
					{ "MushroomBlue", 3 }
				}
			},
			{
				"dvergrprops_chair",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 4 },
					{ "Copper", 1 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"dvergrprops_bed",
				new Dictionary<string, int>
				{
					{ "YggdrasilWood", 8 },
					{ "Copper", 1 },
					{ "WolfPelt", 2 },
					{ "LoxPelt", 1 },
					{ "MushroomBlue", 1 }
				}
			},
			{
				"dverger_demister",
				new Dictionary<string, int>
				{
					{ "Wisp", 20 },
					{ "MushroomBlue", 8 }
				}
			},
			{
				"dverger_demister_large",
				new Dictionary<string, int>
				{
					{ "Wisp", 50 },
					{ "MushroomBlue", 30 }
				}
			}
		};

		public static readonly Dictionary<string, Dictionary<string, int>> MiscPrefabs = new Dictionary<string, Dictionary<string, int>>
		{
			{
				"portal",
				new Dictionary<string, int>
				{
					{ "Stone", 30 },
					{ "SurtlingCore", 2 },
					{ "GreydwarfEye", 10 }
				}
			},
			{
				"CastleKit_brazier",
				new Dictionary<string, int>
				{
					{ "Bronze", 5 },
					{ "Coal", 2 },
					{ "WolfClaw", 3 }
				}
			}
		};

		public static readonly Dictionary<string, Dictionary<string, int>> CultivatorCreatorShopItems = new Dictionary<string, Dictionary<string, int>>
		{
			{
				"vines",
				new Dictionary<string, int> { { "Wood", 2 } }
			},
			{
				"GlowingMushroom",
				new Dictionary<string, int>
				{
					{ "MushroomYellow", 3 },
					{ "MushroomBlue", 1 }
				}
			}
		};

		public static readonly Dictionary<string, string> craftingStationRequirements = new Dictionary<string, string>
		{
			{ "dvergrtown_wood_support", "blackforge" },
			{ "dvergrprops_chair", "blackforge" },
			{ "dvergrprops_bed", "blackforge" },
			{ "goblin_banner", "piece_workbench" },
			{ "goblin_fence", "piece_workbench" },
			{ "goblin_pole", "piece_workbench" },
			{ "goblin_pole_small", "piece_workbench" },
			{ "goblin_roof_45d", "piece_workbench" },
			{ "goblin_roof_45d_corner", "piece_workbench" },
			{ "goblin_roof_cap", "piece_workbench" },
			{ "goblin_stairs", "piece_workbench" },
			{ "goblin_stepladder", "piece_workbench" },
			{ "goblin_woodwall_1m", "piece_workbench" },
			{ "goblin_woodwall_2m", "piece_workbench" },
			{ "goblin_woodwall_2m_ribs", "piece_workbench" },
			{ "portal", "piece_stonecutter" },
			{ "Skull1", "piece_workbench" },
			{ "Skull2", "piece_workbench" },
			{ "StatueCorgi", "piece_stonecutter" },
			{ "StatueDeer", "piece_stonecutter" },
			{ "StatueEvil", "piece_stonecutter" },
			{ "StatueHare", "piece_stonecutter" },
			{ "StatueSeed", "piece_stonecutter" },
			{ "stonechest", "piece_stonecutter" },
			{ "trader_wagon_destructable", "piece_workbench" }
		};
	}
}
namespace ComfyLib
{
	public static class ObjectExtensions
	{
		public static T FirstByNameOrThrow<T>(this T[] unityObjects, string name) where T : Object
		{
			foreach (T val in unityObjects)
			{
				if (((Object)val).name == name)
				{
					return val;
				}
			}
			throw new InvalidOperationException($"Could not find Unity object of type {typeof(T)} with name: {name}");
		}

		public static T Ref<T>(this T unityObject) where T : Object
		{
			if (!Object.op_Implicit((Object)(object)unityObject))
			{
				return default(T);
			}
			return unityObject;
		}
	}
	public static class ReflectionUtils
	{
		public const BindingFlags AllBindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty;

		public static MethodInfo GetGenericMethod(Type type, string name, int genericParameterCount, Type[] types)
		{
			MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty);
			foreach (MethodInfo methodInfo in methods)
			{
				if (methodInfo.IsGenericMethod && methodInfo.ContainsGenericParameters && methodInfo.Name == name && HasMatchingParameterTypes(genericParameterCount, types, methodInfo.GetParameters()))
				{
					return methodInfo;
				}
			}
			return null;
		}

		private static bool HasMatchingParameterTypes(int genericParameterCount, Type[] types, ParameterInfo[] parameters)
		{
			if (parameters.Length < genericParameterCount || parameters.Length != types.Length)
			{
				return false;
			}
			int num = 0;
			for (int i = 0; i < parameters.Length; i++)
			{
				if (parameters[i].ParameterType.IsGenericParameter)
				{
					num++;
				}
				else if (types[i] != parameters[i].ParameterType)
				{
					return false;
				}
			}
			if (num != genericParameterCount)
			{
				return false;
			}
			return true;
		}
	}
}