Decompiled source of HexWaterproofBuilding v1.0.0

plugins/HexWaterproofBuilding.dll

Decompiled a day 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 HexWaterproofBuilding.Core;
using HexWaterproofBuilding.Discovery;
using HexWaterproofBuilding.Rules;
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", "WaterproofBuilding", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private const string PluginGuid = "hex.waterproofbuilding";

		private const string PluginName = "WaterproofBuilding";

		private const string PluginVersion = "1.0.0";

		private ConfigEntry<bool> _modEnabled;

		internal static Plugin Instance { get; private set; }

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

		private void Awake()
		{
			Instance = this;
			_modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable or disable the Waterproof Building mod.");
			_modEnabled.SettingChanged += OnModEnabledSettingChanged;
			if (IsModEnabled)
			{
				PrefabManager.OnVanillaPrefabsAvailable += WaterproofPieceRegistrar.RegisterPieces;
			}
			else
			{
				Logger.LogInfo((object)"Mod is disabled. No pieces will be registered.");
			}
			Logger.LogInfo((object)"WaterproofBuilding v1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			if (_modEnabled != null)
			{
				_modEnabled.SettingChanged -= OnModEnabledSettingChanged;
			}
			PrefabManager.OnVanillaPrefabsAvailable -= WaterproofPieceRegistrar.RegisterPieces;
			Instance = null;
			Logger.LogInfo((object)"WaterproofBuilding v1.0.0 unloaded.");
		}

		private void OnModEnabledSettingChanged(object sender, EventArgs args)
		{
			Logger.LogInfo((object)$"Enabled changed to: {IsModEnabled}");
			Logger.LogWarning((object)"Changes require a restart to take effect.");
		}
	}
}
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
			};
		}
	}
}
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.Core
{
	internal static class Constants
	{
		internal const string WaterproofCategory = "Waterproof Building";

		internal const string PrefabPrefix = "Waterproof";
	}
	internal static class ItemList
	{
		internal const string Resin = "Resin";
	}
	internal static class WaterproofPieceRegistrar
	{
		private static bool _registered;

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

		private static int CreateWaterproofPieces()
		{
			int num = 0;
			foreach (GameObject prefab in PrefabDiscovery.GetPrefabs(WaterproofPrefabRules.IsValidPrefab))
			{
				if (CreateWaterproofPiece(prefab))
				{
					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();
		}
	}
}
namespace HexWaterproofBuilding.Config
{
	internal static class ModConfig
	{
	}
}