Decompiled source of HexWaterproofBuilding v1.2.1

plugins/HexWaterproofBuilding.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using HexWaterproofBuilding.Core;
using HexWaterproofBuilding.Discovery;
using HexWaterproofBuilding.Rules;
using HexWaterproofBuilding.Utils;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("HexWaterproofBuilding")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HexWaterproofBuilding")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("69555090-1b3b-4323-9007-2b83a7a50186")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace HexWaterproofBuilding
{
	[BepInPlugin("hex.waterproofbuilding", "HexWaterproofBuilding", "1.2.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private const string PluginGuid = "hex.waterproofbuilding";

		private const string PluginName = "HexWaterproofBuilding";

		private const string PluginVersion = "1.2.1";

		private Harmony _harmony;

		private ConfigEntry<bool> _modEnabled;

		private ConfigEntry<bool> _extendedPlacementRangeEnabled;

		internal static Plugin Instance { get; private set; }

		internal bool IsModEnabled
		{
			get
			{
				if (_modEnabled != null)
				{
					return _modEnabled.Value;
				}
				return false;
			}
		}

		internal bool IsExtendedPlacementRangeEnabled
		{
			get
			{
				if (_extendedPlacementRangeEnabled != null)
				{
					return _extendedPlacementRangeEnabled.Value;
				}
				return false;
			}
		}

		private void Awake()
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			Instance = this;
			_modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable or disable the Waterproof Building mod.");
			_extendedPlacementRangeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Extended Placement Range", "Enabled", true, "Enable extended placement range for waterproof pieces.");
			_modEnabled.SettingChanged += OnModEnabledSettingChanged;
			_extendedPlacementRangeEnabled.SettingChanged += OnModEnabledSettingChanged;
			_harmony = new Harmony("hex.waterproofbuilding");
			_harmony.PatchAll();
			if (IsModEnabled)
			{
				PrefabManager.OnVanillaPrefabsAvailable += WaterproofPieceRegistrar.RegisterPieces;
			}
			else
			{
				Logger.LogInfo((object)"Mod is disabled. No pieces will be registered.");
			}
			Logger.LogInfo((object)"HexWaterproofBuilding v1.2.1 loaded.");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"HexWaterproofBuilding v1.2.1 unloaded.");
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			_harmony = null;
			if (_modEnabled != null)
			{
				_modEnabled.SettingChanged -= OnModEnabledSettingChanged;
			}
			if (_extendedPlacementRangeEnabled != null)
			{
				_extendedPlacementRangeEnabled.SettingChanged -= OnModEnabledSettingChanged;
			}
			PrefabManager.OnVanillaPrefabsAvailable -= WaterproofPieceRegistrar.RegisterPieces;
			Instance = null;
		}

		private void OnModEnabledSettingChanged(object sender, EventArgs args)
		{
			Logger.LogInfo((object)$"Mod enabled: {IsModEnabled}");
			Logger.LogInfo((object)$"Extended placement range enabled: {IsExtendedPlacementRangeEnabled}");
			Logger.LogWarning((object)"Changes require a restart to take effect.");
		}
	}
}
namespace HexWaterproofBuilding.Utils
{
	internal static class PieceUtility
	{
		internal static bool IsWaterproofPiece(Piece piece)
		{
			if ((Object)(object)piece == (Object)null)
			{
				return false;
			}
			return ((Object)piece).name.Replace("(Clone)", "").StartsWith("Waterproof_");
		}
	}
	internal static class FeatureFlags
	{
		internal static bool CanUseExtendedPlacement()
		{
			if ((Object)(object)Plugin.Instance != (Object)null && Plugin.Instance.IsModEnabled)
			{
				return Plugin.Instance.IsExtendedPlacementRangeEnabled;
			}
			return false;
		}
	}
}
namespace HexWaterproofBuilding.Discovery
{
	internal static class PrefabDiscovery
	{
		[CompilerGenerated]
		private sealed class <GetPrefabs>d__0 : IEnumerable<GameObject>, IEnumerable, IEnumerator<GameObject>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private GameObject <>2__current;

			private int <>l__initialThreadId;

			private Func<GameObject, bool> predicate;

			public Func<GameObject, bool> <>3__predicate;

			private Dictionary<string, Object>.Enumerator <>7__wrap1;

			GameObject IEnumerator<GameObject>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <GetPrefabs>d__0(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || num == 1)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<>7__wrap1 = default(Dictionary<string, Object>.Enumerator);
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
					{
						<>1__state = -1;
						Dictionary<string, Object> prefabs = Cache.GetPrefabs(typeof(GameObject));
						<>7__wrap1 = prefabs.GetEnumerator();
						<>1__state = -3;
						break;
					}
					case 1:
						<>1__state = -3;
						break;
					}
					while (<>7__wrap1.MoveNext())
					{
						Object value = <>7__wrap1.Current.Value;
						GameObject val = (GameObject)(object)((value is GameObject) ? value : null);
						if (!((Object)(object)val == (Object)null) && (predicate == null || predicate(val)))
						{
							<>2__current = val;
							<>1__state = 1;
							return true;
						}
					}
					<>m__Finally1();
					<>7__wrap1 = default(Dictionary<string, Object>.Enumerator);
					return false;
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				((IDisposable)<>7__wrap1).Dispose();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}

			[DebuggerHidden]
			IEnumerator<GameObject> IEnumerable<GameObject>.GetEnumerator()
			{
				<GetPrefabs>d__0 <GetPrefabs>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<GetPrefabs>d__ = this;
				}
				else
				{
					<GetPrefabs>d__ = new <GetPrefabs>d__0(0);
				}
				<GetPrefabs>d__.predicate = <>3__predicate;
				return <GetPrefabs>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<GameObject>)this).GetEnumerator();
			}
		}

		[IteratorStateMachine(typeof(<GetPrefabs>d__0))]
		internal static IEnumerable<GameObject> GetPrefabs(Func<GameObject, bool> predicate)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <GetPrefabs>d__0(-2)
			{
				<>3__predicate = predicate
			};
		}

		internal static Dictionary<string, int> GetHammerPieceIndexes()
		{
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
			Object prefab = Cache.GetPrefab(typeof(GameObject), "Hammer");
			GameObject val = (GameObject)(object)((prefab is GameObject) ? prefab : null);
			if ((Object)(object)val == (Object)null)
			{
				return dictionary;
			}
			ItemDrop component = val.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null || (Object)(object)component.m_itemData.m_shared.m_buildPieces == (Object)null || component.m_itemData.m_shared.m_buildPieces.m_pieces == null)
			{
				return dictionary;
			}
			List<GameObject> pieces = component.m_itemData.m_shared.m_buildPieces.m_pieces;
			for (int i = 0; i < pieces.Count; i++)
			{
				GameObject val2 = pieces[i];
				if (!((Object)(object)val2 == (Object)null) && !dictionary.ContainsKey(((Object)val2).name))
				{
					dictionary.Add(((Object)val2).name, i);
				}
			}
			return dictionary;
		}
	}
}
namespace HexWaterproofBuilding.Rules
{
	internal static class WaterproofPrefabRules
	{
		private static readonly string LowerPrefabPrefix = "Waterproof".ToLower();

		private static readonly string[] InvalidTokens = new string[8] { "door", "stack", "gate", "spiral", "treasure", "dvergr", "old_", "turf" };

		internal static bool IsValidPrefab(GameObject prefab)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Invalid comparison between Unknown and I4
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Invalid comparison between Unknown and I4
			if ((Object)(object)prefab == (Object)null)
			{
				return false;
			}
			Piece component = prefab.GetComponent<Piece>();
			WearNTear component2 = prefab.GetComponent<WearNTear>();
			if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null)
			{
				return false;
			}
			string lowerName = ((Object)prefab).name.ToLowerInvariant();
			if (lowerName.StartsWith(LowerPrefabPrefix))
			{
				return false;
			}
			if (InvalidTokens.Any((string token) => lowerName.Contains(token)))
			{
				return false;
			}
			if ((int)component.m_category != 2)
			{
				return false;
			}
			if ((int)component2.m_materialType != 0)
			{
				return (int)component2.m_materialType == 3;
			}
			return true;
		}
	}
}
namespace HexWaterproofBuilding.Patches
{
	[HarmonyPatch(typeof(Player), "CopyPiece")]
	internal static class PatchPlayerCopyPiece
	{
		[HarmonyPrefix]
		private static bool Prefix(Player __instance, ref bool __result)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || !FeatureFlags.CanUseExtendedPlacement())
			{
				return true;
			}
			if ((Object)(object)GameCamera.instance == (Object)null || (Object)(object)((Component)GameCamera.instance).transform == (Object)null)
			{
				return true;
			}
			Vector3 position = ((Component)GameCamera.instance).transform.position;
			Vector3 forward = ((Component)GameCamera.instance).transform.forward;
			int value = Traverse.Create((object)__instance).Field("m_removeRayMask").GetValue<int>();
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(position, forward, ref val, 100f, value) || (Object)(object)((RaycastHit)(ref val)).collider == (Object)null)
			{
				__result = false;
				return false;
			}
			Piece val2 = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Piece>();
			if ((Object)(object)val2 == (Object)null && (Object)(object)((Component)((RaycastHit)(ref val)).collider).GetComponent<Heightmap>() != (Object)null)
			{
				val2 = TerrainModifier.FindClosestModifierPieceInRange(((RaycastHit)(ref val)).point, 2.5f);
			}
			if (!PieceUtility.IsWaterproofPiece(val2))
			{
				return true;
			}
			if (!Traverse.Create((object)__instance).Method("SetSelectedPiece", new object[1] { val2 }).GetValue<bool>())
			{
				((Character)__instance).Message((MessageType)2, "$msg_missingrequirement", 0, (Sprite)null);
				__result = false;
				return false;
			}
			Quaternion rotation = ((Component)val2).transform.rotation;
			float value2 = Traverse.Create((object)__instance).Field("m_placeRotationDegrees").GetValue<float>();
			Traverse.Create((object)__instance).Field("m_placeRotation").SetValue((object)(int)Math.Round(((Quaternion)(ref rotation)).eulerAngles.y / value2));
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(Player), "PieceRayTest")]
	internal static class PatchPlayerPieceRayTest
	{
		private static bool Prefix(Player __instance, ref bool __result, out Vector3 point, out Vector3 normal, out Piece piece, out Heightmap heightmap, out Collider waterSurface, bool water)
		{
			//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_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: 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_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			point = Vector3.zero;
			normal = Vector3.zero;
			piece = null;
			heightmap = null;
			waterSurface = null;
			if ((Object)(object)__instance == (Object)null || !FeatureFlags.CanUseExtendedPlacement())
			{
				return true;
			}
			GameObject value = Traverse.Create((object)__instance).Field("m_placementGhost").GetValue<GameObject>();
			if ((Object)(object)value == (Object)null)
			{
				return true;
			}
			if (!PieceUtility.IsWaterproofPiece(value.GetComponent<Piece>()))
			{
				return true;
			}
			int num = (water ? Traverse.Create((object)__instance).Field("m_placeWaterRayMask").GetValue<int>() : Traverse.Create((object)__instance).Field("m_placeRayMask").GetValue<int>());
			if ((Object)(object)GameCamera.instance == (Object)null)
			{
				return true;
			}
			Transform transform = ((Component)GameCamera.instance).transform;
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(transform.position, transform.forward, ref val, 100f, num))
			{
				__result = false;
				return false;
			}
			if ((Object)(object)((RaycastHit)(ref val)).collider == (Object)null || (Object)(object)((RaycastHit)(ref val)).collider.attachedRigidbody != (Object)null)
			{
				__result = false;
				return false;
			}
			point = ((RaycastHit)(ref val)).point;
			normal = ((RaycastHit)(ref val)).normal;
			piece = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Piece>();
			heightmap = ((Component)((RaycastHit)(ref val)).collider).GetComponent<Heightmap>();
			waterSurface = ((((Component)((RaycastHit)(ref val)).collider).gameObject.layer == LayerMask.NameToLayer("Water")) ? ((RaycastHit)(ref val)).collider : null);
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(Player), "RemovePiece")]
	internal static class PatchPlayerRemovePiece
	{
		private static bool Prefix(Player __instance, ref bool __result)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//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_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Expected O, but got Unknown
			if ((Object)(object)__instance == (Object)null || !FeatureFlags.CanUseExtendedPlacement())
			{
				return true;
			}
			if ((Object)(object)GameCamera.instance == (Object)null || (Object)(object)((Component)GameCamera.instance).transform == (Object)null)
			{
				return true;
			}
			Vector3 position = ((Component)GameCamera.instance).transform.position;
			Vector3 forward = ((Component)GameCamera.instance).transform.forward;
			int value = Traverse.Create((object)__instance).Field("m_removeRayMask").GetValue<int>();
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(position, forward, ref val, 100f, value))
			{
				return true;
			}
			if ((Object)(object)((RaycastHit)(ref val)).collider == (Object)null)
			{
				return true;
			}
			Piece val2 = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Piece>();
			Heightmap component = ((Component)((RaycastHit)(ref val)).collider).GetComponent<Heightmap>();
			if ((Object)(object)val2 == (Object)null && (Object)(object)component != (Object)null)
			{
				val2 = TerrainModifier.FindClosestModifierPieceInRange(((RaycastHit)(ref val)).point, 2.5f);
			}
			if (!PieceUtility.IsWaterproofPiece(val2))
			{
				return true;
			}
			if (!val2.m_canBeRemoved)
			{
				__result = false;
				return false;
			}
			Vector3 position2 = ((Component)val2).transform.position;
			Quaternion rotation = ((Component)val2).transform.rotation;
			Transform transform = ((Component)val2).transform;
			if (Location.IsInsideNoBuildLocation(position2))
			{
				((Character)__instance).Message((MessageType)2, "$msg_nobuildzone", 0, (Sprite)null);
				__result = false;
				return false;
			}
			if (!PrivateArea.CheckAccess(position2, 0f, true, false))
			{
				((Character)__instance).Message((MessageType)2, "$msg_privatezone", 0, (Sprite)null);
				__result = false;
				return false;
			}
			if (!Traverse.Create((object)__instance).Method("CheckCanRemovePiece", new object[1] { val2 }).GetValue<bool>())
			{
				__result = false;
				return false;
			}
			ZNetView component2 = ((Component)val2).GetComponent<ZNetView>();
			if ((Object)(object)component2 == (Object)null)
			{
				__result = false;
				return false;
			}
			if (!val2.CanBeRemoved())
			{
				((Character)__instance).Message((MessageType)2, "$msg_cantremovenow", 0, (Sprite)null);
				__result = false;
				return false;
			}
			IRemoved component3 = ((Component)val2).GetComponent<IRemoved>();
			if (component3 != null)
			{
				component3.OnRemoved();
			}
			WearNTear component4 = ((Component)val2).GetComponent<WearNTear>();
			if ((Object)(object)component4 != (Object)null)
			{
				component4.Remove(false);
			}
			else
			{
				Character component5 = ((Component)val2).GetComponent<Character>();
				if ((Object)(object)component5 != (Object)null)
				{
					component5.Damage(new HitData(1E+10f));
				}
				else
				{
					ZLog.Log((object)("Removing non WNT object with hammer " + ((Object)val2).name));
					component2.ClaimOwnership();
					val2.DropResources((HitData)null);
					val2.m_placeEffect.Create(position2, rotation, transform, 1f, -1);
					Traverse.Create((object)__instance).Field("m_removeEffects").GetValue<EffectList>()
						.Create(position2, Quaternion.identity, (Transform)null, 1f, -1);
					ZNetScene.instance.Destroy(((Component)val2).gameObject);
				}
			}
			ItemData value2 = Traverse.Create((object)__instance).Field("m_rightItem").GetValue<ItemData>();
			if (value2 != null)
			{
				Traverse.Create((object)__instance).Method("FaceLookDirection", Array.Empty<object>()).GetValue();
				ZSyncAnimation value3 = Traverse.Create((object)__instance).Field("m_zanim").GetValue<ZSyncAnimation>();
				if ((Object)(object)value3 != (Object)null)
				{
					value3.SetTrigger(value2.m_shared.m_attack.m_attackAnimation);
				}
			}
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(Player), "UpdateWearNTearHover")]
	internal static class PatchPlayerUpdateWearNTearHover
	{
		private static void Postfix(Player __instance)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || !FeatureFlags.CanUseExtendedPlacement() || !((Character)__instance).InPlaceMode() || (Object)(object)GameCamera.instance == (Object)null || (Object)(object)((Component)GameCamera.instance).transform == (Object)null)
			{
				return;
			}
			Vector3 position = ((Component)GameCamera.instance).transform.position;
			Vector3 forward = ((Component)GameCamera.instance).transform.forward;
			int value = Traverse.Create((object)__instance).Field("m_removeRayMask").GetValue<int>();
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(position, forward, ref val, 100f, value) || (Object)(object)((RaycastHit)(ref val)).collider == (Object)null)
			{
				return;
			}
			Piece componentInParent = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Piece>();
			if (PieceUtility.IsWaterproofPiece(componentInParent))
			{
				Traverse.Create((object)__instance).Field("m_hoveringPiece").SetValue((object)componentInParent);
				WearNTear component = ((Component)componentInParent).GetComponent<WearNTear>();
				if (!((Object)(object)component == (Object)null))
				{
					component.Highlight();
				}
			}
		}
	}
}
namespace HexWaterproofBuilding.Core
{
	internal static class Constants
	{
		internal const string WaterproofCategory = "Waterproof Building";

		internal const string PrefabPrefix = "Waterproof";

		internal const float ClosestRangeModifier = 2.5f;

		internal const float CharacterHitDataValue = 1E+10f;

		internal const float ExtendedHoverDistance = 100f;

		internal const float ExtendedPlacementDistance = 100f;

		internal const float ExtendedRemoveDistance = 100f;

		internal const float ExtendedCopyDistance = 100f;
	}
	internal static class ItemList
	{
		internal const string Resin = "Resin";
	}
	internal static class WaterproofPieceRegistrar
	{
		private static bool _registered;

		internal static void RegisterPieces()
		{
			if (!_registered)
			{
				if ((Object)(object)Plugin.Instance == (Object)null || !Plugin.Instance.IsModEnabled)
				{
					PrefabManager.OnVanillaPrefabsAvailable -= RegisterPieces;
					return;
				}
				int num = CreateWaterproofPieces(PrefabDiscovery.GetHammerPieceIndexes());
				_registered = true;
				PrefabManager.OnVanillaPrefabsAvailable -= RegisterPieces;
				Logger.LogInfo((object)$"Waterproof pieces registered. Count: {num}");
			}
		}

		private static int CreateWaterproofPieces(Dictionary<string, int> hammerIndexes)
		{
			int num = 0;
			int value;
			foreach (GameObject item in (from prefab in PrefabDiscovery.GetPrefabs(WaterproofPrefabRules.IsValidPrefab)
				orderby (hammerIndexes != null && hammerIndexes.TryGetValue(((Object)prefab).name, out value)) ? value : int.MaxValue, ((Object)prefab).name
				select prefab).ToList())
			{
				if (CreateWaterproofPiece(item))
				{
					num++;
				}
			}
			return num;
		}

		private static bool CreateWaterproofPiece(GameObject vanillaPrefab)
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Expected O, but got Unknown
			if ((Object)(object)vanillaPrefab == (Object)null)
			{
				return false;
			}
			Piece component = vanillaPrefab.GetComponent<Piece>();
			if ((Object)(object)component == (Object)null)
			{
				Logger.LogWarning((object)(((Object)vanillaPrefab).name + " has no Piece component. Skipping."));
				return false;
			}
			string text = "Waterproof_" + ((Object)vanillaPrefab).name;
			GameObject val = PrefabManager.Instance.CreateClonedPrefab(text, ((Object)vanillaPrefab).name);
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogWarning((object)("Failed to clone prefab: " + ((Object)vanillaPrefab).name));
				return false;
			}
			ApplyModifiersToCustomPrefab(val);
			PieceConfig val2 = BuildPieceConfig(text, component);
			CustomPiece val3 = new CustomPiece(val, true, val2);
			PieceManager.Instance.AddPiece(val3);
			Logger.LogInfo((object)("Registered waterproof building piece: " + text));
			return true;
		}

		private static void ApplyModifiersToCustomPrefab(GameObject customPrefab)
		{
			if (!((Object)(object)customPrefab == (Object)null))
			{
				WearNTear component = customPrefab.GetComponent<WearNTear>();
				if ((Object)(object)component == (Object)null)
				{
					Logger.LogWarning((object)(((Object)customPrefab).name + " has no WearNTear component."));
				}
				else
				{
					component.m_noRoofWear = false;
				}
			}
		}

		private static PieceConfig BuildPieceConfig(string customPrefabName, Piece vanillaPiece)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: 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_004c: Expected O, but got Unknown
			return new PieceConfig
			{
				Name = customPrefabName,
				Description = "A waterproof building piece that resists water damage.",
				PieceTable = PieceTables.Hammer,
				Category = "Waterproof Building",
				Requirements = BuildRequirementConfigs(vanillaPiece, new Dictionary<string, int> { { "Resin", 1 } })
			};
		}

		private static RequirementConfig[] BuildRequirementConfigs(Piece vanillaPiece, Dictionary<string, int> customRequirements, bool keepVanillaRequirements = true)
		{
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			List<RequirementConfig> list = new List<RequirementConfig>();
			if (keepVanillaRequirements && (Object)(object)vanillaPiece != (Object)null && vanillaPiece.m_resources != null)
			{
				Requirement[] resources = vanillaPiece.m_resources;
				foreach (Requirement val in resources)
				{
					if (val != null && !((Object)(object)val.m_resItem == (Object)null))
					{
						list.Add(new RequirementConfig(((Object)val.m_resItem).name, val.m_amount, val.m_amountPerLevel, val.m_recover));
					}
				}
			}
			if (customRequirements != null)
			{
				foreach (KeyValuePair<string, int> customRequirement in customRequirements)
				{
					if (!list.Any((RequirementConfig requirement) => string.Equals(requirement.Item, customRequirement.Key, StringComparison.OrdinalIgnoreCase)))
					{
						list.Add(new RequirementConfig(customRequirement.Key, customRequirement.Value, 0, true));
					}
				}
			}
			return list.ToArray();
		}
	}
}