Decompiled source of FloorsStairsLaddersAreRoofs v1.0.3

FloorsStairsLaddersAreRoofs.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Floors-Stairs-Ladders-Are-Roofs")]
[assembly: AssemblyDescription("A tiny mod that makes Valheim floors count as a roof so you can build a flat-roofed house")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FloorsAreRoofsPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f31344ca-e6be-44d2-8f6b-41fd7ac15b76")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
[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 FloorsLaddersStairsAreRoofs
{
	[BepInPlugin("TastyChickenLegs.Floors-Stairs-Ladders-Are-Roofs", "Floors-Stairs-Ladders-Are-Roofs", "1.0.3")]
	public class FloorsLaddersStairsAreRoofsPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(WearNTear), "UpdateWear")]
		internal static class WearNTear_UpdateWear_Patch
		{
			private static void Prefix(float time, Piece ___m_piece, ref float ___m_rainTimer)
			{
				//IL_0017: 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)
				if (RemoveRainDamage.Value && !((Object)(object)___m_piece == (Object)null) && (___m_piece.m_usage & 8) != 0)
				{
					___m_rainTimer = time;
				}
			}
		}

		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		internal static class ZNetScene_Awake_Patch
		{
			private static void Postfix(ZNetScene __instance)
			{
				//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_00b8: Invalid comparison between Unknown and I4
				//IL_00db: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e7: Invalid comparison between Unknown and I4
				//IL_0153: Unknown result type (might be due to invalid IL or missing references)
				//IL_0159: Invalid comparison between Unknown and I4
				bool value = FloorsAreRoofs.Value;
				bool value2 = StairsLaddersAreRoofs.Value;
				bool value3 = FiresOnFloors.Value;
				bool value4 = SmeltersOnFloors.Value;
				bool value5 = RemoveRainDamage.Value;
				List<string> list = new List<string>();
				List<string> list2 = new List<string>();
				List<string> list3 = new List<string>();
				List<string> list4 = new List<string>();
				List<string> list5 = new List<string>();
				List<string> list6 = new List<string>();
				List<string> list7 = new List<string>();
				List<string> list8 = new List<string>();
				foreach (GameObject prefab in __instance.m_prefabs)
				{
					if ((Object)(object)prefab == (Object)null)
					{
						continue;
					}
					Piece component = prefab.GetComponent<Piece>();
					if ((Object)(object)component == (Object)null)
					{
						continue;
					}
					bool flag = (component.m_usage & 8) > 0;
					if (value && flag && ClearLeakyTags(prefab))
					{
						list.Add(((Object)prefab).name);
					}
					bool flag2 = (component.m_usage & 0x20000) > 0;
					if (!flag2 && IsListedStair(((Object)prefab).name))
					{
						flag2 = true;
					}
					if (value2 && flag2 && ClearLeakyTags(prefab))
					{
						list8.Add(((Object)prefab).name);
					}
					if (value5 && flag && (Object)(object)prefab.GetComponent<WearNTear>() != (Object)null)
					{
						list2.Add(((Object)prefab).name);
					}
					bool flag3 = (Object)(object)prefab.GetComponentInChildren<Fireplace>(true) != (Object)null || (int)component.m_comfortGroup == 1;
					if (value3 && flag3)
					{
						string text = FreeForFloors(component);
						if (text.Length > 0)
						{
							list3.Add(((Object)prefab).name + " [" + text + "]");
						}
						else
						{
							list4.Add(((Object)prefab).name);
						}
					}
					if (value4 && (Object)(object)prefab.GetComponentInChildren<Smelter>(true) != (Object)null)
					{
						string text2 = FreeForFloors(component);
						WearNTear component2 = prefab.GetComponent<WearNTear>();
						if ((Object)(object)component2 != (Object)null)
						{
							component2.m_noSupportWear = true;
							text2 = ((text2.Length > 0) ? (text2 + " ") : "") + "noSupportWear";
						}
						if (text2.Length > 0)
						{
							list5.Add(((Object)prefab).name + " [" + text2 + "]");
						}
						else
						{
							list6.Add(((Object)prefab).name);
						}
					}
					if (value && !flag && ((Object)prefab).name.IndexOf("floor", StringComparison.OrdinalIgnoreCase) >= 0 && HasLeakyTag(prefab))
					{
						list7.Add(((Object)prefab).name);
					}
					if (value2 && !flag2 && ((Object)prefab).name.IndexOf("floor", StringComparison.OrdinalIgnoreCase) >= 0 && HasLeakyTag(prefab))
					{
						list8.Add(((Object)prefab).name);
					}
				}
				Log.LogInfo((object)("loaded. Floors now count as roof: " + (value ? (list.Count + " (" + string.Join(", ", list.ToArray()) + ")") : "off") + ". Fires freed for floor placement: " + (value3 ? (list3.Count + " (" + string.Join(", ", list3.ToArray()) + ")") : "off") + "."));
				if (value5)
				{
					Log.LogInfo((object)string.Format("{0}: floors no longer take rain damage: {1} ({2})", "FloorsStairsLaddersAreRoofs", list2.Count, string.Join(", ", list2.ToArray())));
				}
				if (value4)
				{
					Log.LogInfo((object)string.Format("{0}: smelters and kilns can be placed on wood floors: {1} ({2})", "FloorsStairsLaddersAreRoofs", list5.Count, string.Join(", ", list5.ToArray())));
				}
				if (value4 && list6.Count > 0)
				{
					Log.LogInfo((object)("FloorsStairsLaddersAreRoofs: Smelters and Variants can be placed on floors: " + string.Join(", ", list6.ToArray())));
				}
				if (value3 && list4.Count > 0)
				{
					Log.LogInfo((object)("FloorsStairsLaddersAreRoofs: Fires can be placed on floors: " + string.Join(", ", list4.ToArray())));
				}
				if (value && list7.Count > 0)
				{
					Log.LogWarning((object)("FloorsStairsLaddersAreRoofs: still leaky, no Floor usage flag: " + string.Join(", ", list7.ToArray())));
				}
				if (value2 && list8.Count > 0)
				{
					Log.LogWarning((object)("FloorsStairsLaddersAreRoofs: still leaky, no usage flag: " + string.Join(", ", list8.ToArray())));
				}
			}

			private static bool IsListedStair(string prefabName)
			{
				if (string.IsNullOrEmpty(prefabName) || string.IsNullOrWhiteSpace(ExtraStairPrefabs.Value))
				{
					return false;
				}
				string[] array = ExtraStairPrefabs.Value.Split(new char[1] { ',' });
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i].Trim();
					if (text.Length != 0 && string.Equals(text, prefabName, StringComparison.OrdinalIgnoreCase))
					{
						return true;
					}
				}
				return false;
			}

			private static string FreeForFloors(Piece piece)
			{
				string text = (piece.m_notOnWood ? "notOnWood " : "") + (piece.m_groundOnly ? "groundOnly " : "") + (piece.m_groundPiece ? "groundPiece" : "");
				piece.m_notOnWood = false;
				piece.m_groundOnly = false;
				piece.m_groundPiece = false;
				return text.Trim();
			}

			private static bool ClearLeakyTags(GameObject prefab)
			{
				bool result = false;
				Transform[] componentsInChildren = prefab.GetComponentsInChildren<Transform>(true);
				foreach (Transform val in componentsInChildren)
				{
					if (((Component)val).gameObject.CompareTag("leaky"))
					{
						((Component)val).gameObject.tag = "Untagged";
						result = true;
					}
				}
				return result;
			}

			private static bool HasLeakyTag(GameObject prefab)
			{
				Transform[] componentsInChildren = prefab.GetComponentsInChildren<Transform>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					if (((Component)componentsInChildren[i]).gameObject.CompareTag("leaky"))
					{
						return true;
					}
				}
				return false;
			}
		}

		public const string ModVersion = "1.0.3";

		public const string ModName = "Floors-Stairs-Ladders-Are-Roofs";

		private const string ModGUID = "TastyChickenLegs.Floors-Stairs-Ladders-Are-Roofs";

		private const string Author = "TastyChickenLegs";

		private static string ConfigFileName = "TastyChickenLegs.Floors-Stairs-Ladders-Are-Roofs.cfg";

		private static string ConfigFileFullPath;

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> FloorsAreRoofs;

		internal static ConfigEntry<bool> FiresOnFloors;

		internal static ConfigEntry<bool> SmeltersOnFloors;

		internal static ConfigEntry<bool> FloorsDontDecay;

		internal static ConfigEntry<bool> RemoveRainDamage;

		internal static ConfigEntry<bool> StairsLaddersAreRoofs;

		private const string LeakyTag = "leaky";

		internal static ConfigEntry<string> ExtraStairPrefabs;

		private readonly Harmony harmony = new Harmony("TastyChickenLegs.Floors-Stairs-Ladders-Are-Roofs");

		public static readonly ManualLogSource FloorsLaddersStairsAreRoofsLogger;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			FloorsAreRoofs = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FloorsAreRoofs", true, "Floor tiles count as roof - they shelter the room below from rain. Restart the game after changing.");
			FiresOnFloors = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FiresOnFloors", true, "Fires and other fireplace pieces can be placed on floor tiles instead of bare ground. Restart the game after changing.");
			SmeltersOnFloors = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SmeltersOnFloors", true, "Smelters, charcoal kilns, blast furnaces and the other Smelter-driven stations can be placed on floor tiles instead of bare ground. Restart the game after changing.");
			RemoveRainDamage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RemoveRainDamage", true, "Prevent the floor pieces from taking wear and tear damage from rain.");
			StairsLaddersAreRoofs = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "StairsLaddersAreRoofs", true, "Stairs and Ladders count as roof - they allow shelter and do not leak rain.");
			ExtraStairPrefabs = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ExtraStairPrefabs", "wood_stair,wood_stepladder,ashwood_stair,piece_dvergr_spiralstair,piece_dvergr_spiralstair_right", "Stairs and ladders to treat as roof even though Valheim gave them no Stairs usage tag. The wooden stairs and ladders ship with no tag at all, so the flag check cannot find them. Comma-separated prefab names. Restart the game after changing.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			harmony.PatchAll(executingAssembly);
			CleanConfig();
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				FloorsLaddersStairsAreRoofsLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				FloorsLaddersStairsAreRoofsLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				FloorsLaddersStairsAreRoofsLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private void CleanConfig()
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				string text = File.ReadAllText(ConfigFileFullPath);
				if (string.IsNullOrEmpty(text))
				{
					return;
				}
				string[] array = text.Split(new char[1] { '\n' });
				List<string> list = new List<string>();
				bool flag = false;
				int num = 0;
				for (int i = 0; i < array.Length; i++)
				{
					string text2 = array[i];
					if (text2.EndsWith("\r"))
					{
						text2 = text2.Substring(0, text2.Length - 1);
					}
					string text3 = text2.Trim();
					if (string.IsNullOrEmpty(text3) || text3.StartsWith("[") || text3.StartsWith(";"))
					{
						list.Add(text2);
						flag = false;
					}
					else if (text3.StartsWith("##"))
					{
						flag = true;
						list.Add(text2);
					}
					else if (text3.StartsWith("#"))
					{
						list.Add(text2);
					}
					else if (text3.Contains("="))
					{
						if (flag)
						{
							list.Add(text2);
						}
						else
						{
							num++;
							FloorsLaddersStairsAreRoofsLogger.LogInfo((object)("[FloorsStairsLaddersAreRoofs] Cleaned orphaned config: " + text3));
						}
						flag = false;
					}
					else
					{
						list.Add(text2);
						flag = false;
					}
				}
				if (num > 0)
				{
					File.WriteAllLines(ConfigFileFullPath, list);
					FloorsLaddersStairsAreRoofsLogger.LogInfo((object)$"[FloorsStairsLaddersAreRoofs] Cleaned {num} orphaned config entr(y/ies)");
					((BaseUnityPlugin)this).Config.Reload();
				}
			}
			catch (Exception ex)
			{
				FloorsLaddersStairsAreRoofsLogger.LogWarning((object)("[FloorsStairsLaddersAreRoofs] Config cleanup skipped (" + ex.Message + ")"));
			}
		}

		static FloorsLaddersStairsAreRoofsPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			FloorsLaddersStairsAreRoofsLogger = Logger.CreateLogSource("Floors-Stairs-Ladders-Are-Roofs");
		}
	}
}