Decompiled source of Carturs Flooring v1.1.0

plugins/CarturFlooring.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CarturFlooring")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+82c88a96edee9bb90168a49680d99a4c8c1c7080")]
[assembly: AssemblyProduct("CarturFlooring")]
[assembly: AssemblyTitle("CarturFlooring")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace CarturFlooring
{
	[BepInPlugin("com.jekkle.valheim.carturflooring", "Cartur's Flooring", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.jekkle.valheim.carturflooring";

		public const string PluginName = "Cartur's Flooring";

		public const string PluginVersion = "1.1.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> FloorsAreRoofs;

		internal static ConfigEntry<bool> FiresOnFloors;

		internal static ConfigEntry<bool> SmeltersOnFloors;

		internal static ConfigEntry<bool> FloorsDontDecay;

		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.");
			FloorsDontDecay = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FloorsDontDecay", true, "Floor tiles take no rain damage, roofed or not, so an open deck or a jetty stops rotting. They still darken in the rain, and still fall down without support. Unlike the three above, this one is read every tick, so it takes effect the moment you change it.");
			try
			{
				Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "com.jekkle.valheim.carturflooring");
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)string.Format("{0} failed to patch, game continues unmodded: {1}", "Cartur's Flooring", arg));
			}
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class ZNetScene_Awake_Patch
	{
		private const string LeakyTag = "leaky";

		private static void Postfix(ZNetScene __instance)
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Invalid comparison between Unknown and I4
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Invalid comparison between Unknown and I4
			bool value = Plugin.FloorsAreRoofs.Value;
			bool value2 = Plugin.FiresOnFloors.Value;
			bool value3 = Plugin.SmeltersOnFloors.Value;
			bool value4 = Plugin.FloorsDontDecay.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>();
			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);
				}
				if (value4 && flag && (Object)(object)prefab.GetComponent<WearNTear>() != (Object)null)
				{
					list2.Add(((Object)prefab).name);
				}
				bool flag2 = (Object)(object)prefab.GetComponentInChildren<Fireplace>(true) != (Object)null || (int)component.m_comfortGroup == 1;
				if (value2 && flag2)
				{
					string text = FreeForFloors(component);
					if (text.Length > 0)
					{
						list3.Add(((Object)prefab).name + " [" + text + "]");
					}
					else
					{
						list4.Add(((Object)prefab).name);
					}
				}
				if (value3 && (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)
					{
						component2.m_noSupportWear = false;
						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);
				}
			}
			Plugin.Log.LogInfo((object)("Cartur's Flooring 1.1.0 loaded. Floors now count as roof: " + (value ? (list.Count + " (" + string.Join(", ", list.ToArray()) + ")") : "off") + ". Fires freed for floor placement: " + (value2 ? (list3.Count + " (" + string.Join(", ", list3.ToArray()) + ")") : "off") + "."));
			if (value4)
			{
				Plugin.Log.LogInfo((object)string.Format("{0}: floors no longer take rain damage: {1} ({2})", "Cartur's Flooring", list2.Count, string.Join(", ", list2.ToArray())));
			}
			if (value3)
			{
				Plugin.Log.LogInfo((object)string.Format("{0}: smelters and kilns freed for floor placement: {1} ({2})", "Cartur's Flooring", list5.Count, string.Join(", ", list5.ToArray())));
			}
			if (value3 && list6.Count > 0)
			{
				Plugin.Log.LogInfo((object)("Cartur's Flooring: smelter-type pieces that were already free to place: " + string.Join(", ", list6.ToArray())));
			}
			if (value2 && list4.Count > 0)
			{
				Plugin.Log.LogInfo((object)("Cartur's Flooring: fire pieces that were already free to place: " + string.Join(", ", list4.ToArray())));
			}
			if (value && list7.Count > 0)
			{
				Plugin.Log.LogWarning((object)("Cartur's Flooring: still leaky, no Floor usage flag: " + string.Join(", ", list7.ToArray())));
			}
		}

		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;
		}
	}
	[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 (Plugin.FloorsDontDecay.Value && !((Object)(object)___m_piece == (Object)null) && (___m_piece.m_usage & 8) != 0)
			{
				___m_rainTimer = time;
			}
		}
	}
}