Decompiled source of WiresComfortInfo v0.1.0

WiresComfortInfo.dll

Decompiled 7 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 HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[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("WiresComfortInfo")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WiresComfortInfo")]
[assembly: AssemblyTitle("WiresComfortInfo")]
[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 WiresComfortInfo
{
	[BepInPlugin("wire.valheim.comfortinfo", "Wire's Comfort Info", "0.1.0")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "wire.valheim.comfortinfo";

		public const string Name = "Wire's Comfort Info";

		public const string Version = "0.1.0";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("wire.valheim.comfortinfo");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Wire's Comfort Info 0.1.0 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class Comfort
	{
		private struct Entry
		{
			internal Piece Piece;

			internal int Value;
		}

		private static readonly List<Piece> Nearby = new List<Piece>(64);

		private static readonly List<Entry> Entries = new List<Entry>(65);

		private static readonly Comparison<Entry> SortEntries = CompareEntries;

		private static int _baseline;

		private static bool _sheltered;

		private static int CompareEntries(Entry a, Entry b)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			ref ComfortGroup comfortGroup = ref a.Piece.m_comfortGroup;
			object target = b.Piece.m_comfortGroup;
			int num = ((Enum)Unsafe.As<ComfortGroup, ComfortGroup>(ref comfortGroup)/*cast due to .constrained prefix*/).CompareTo(target);
			if (num != 0)
			{
				return num;
			}
			if (a.Value != b.Value)
			{
				return b.Value.CompareTo(a.Value);
			}
			return b.Piece.m_name.CompareTo(a.Piece.m_name);
		}

		internal static void Begin(Player player)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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)
			_sheltered = (Object)(object)player != (Object)null && player.InShelter();
			Nearby.Clear();
			if (!_sheltered)
			{
				_baseline = 1;
				return;
			}
			Vector3 position = ((Component)player).transform.position;
			Piece.GetAllComfortPiecesInRadius(position, 10f, Nearby);
			_baseline = SE_Rested.CalculateComfortLevel(true, position);
		}

		internal static int Gain(Piece hypothetical)
		{
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			if (!_sheltered || (Object)(object)hypothetical == (Object)null || hypothetical.m_comfort <= 0)
			{
				return 0;
			}
			if ((Object)(object)hypothetical.m_comfortObject != (Object)null && !hypothetical.m_comfortObject.activeSelf)
			{
				return 0;
			}
			Entries.Clear();
			foreach (Piece item in Nearby)
			{
				if (!((Object)(object)item == (Object)null))
				{
					Entries.Add(new Entry
					{
						Piece = item,
						Value = item.GetComfort()
					});
				}
			}
			Entries.Add(new Entry
			{
				Piece = hypothetical,
				Value = hypothetical.m_comfort
			});
			Entries.Sort(SortEntries);
			int num = 2;
			for (int i = 0; i < Entries.Count; i++)
			{
				Entry entry = Entries[i];
				if (i != 0)
				{
					Entry entry2 = Entries[i - 1];
					if (((int)entry.Piece.m_comfortGroup != 0 && entry.Piece.m_comfortGroup == entry2.Piece.m_comfortGroup) || entry.Piece.m_name == entry2.Piece.m_name)
					{
						continue;
					}
				}
				num += entry.Value;
			}
			return Mathf.Max(0, num - _baseline);
		}
	}
	internal sealed class ComfortNumber : MonoBehaviour
	{
		private TextMeshProUGUI _number;

		internal void Set(int value)
		{
			if ((Object)(object)_number == (Object)null)
			{
				Create();
			}
			if (!((Object)(object)_number == (Object)null))
			{
				bool flag = value > 0;
				if (((Component)_number).gameObject.activeSelf != flag)
				{
					((Component)_number).gameObject.SetActive(flag);
				}
				if (flag)
				{
					((TMP_Text)_number).text = "+" + value;
				}
			}
		}

		private void Create()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: 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_0047: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Wire Comfort Addition", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(((Component)this).transform, false);
			RectTransform val2 = (RectTransform)val.transform;
			val2.anchorMin = Vector2.one;
			val2.anchorMax = Vector2.one;
			val2.pivot = Vector2.one;
			val2.anchoredPosition = new Vector2(-3f, -2f);
			val2.sizeDelta = new Vector2(34f, 22f);
			((Transform)val2).SetAsLastSibling();
			_number = val.AddComponent<TextMeshProUGUI>();
			Hud instance = Hud.instance;
			if ((Object)(object)instance != (Object)null && (Object)(object)instance.m_buildSelection != (Object)null)
			{
				((TMP_Text)_number).font = instance.m_buildSelection.font;
				((TMP_Text)_number).fontSharedMaterial = instance.m_buildSelection.fontSharedMaterial;
			}
			((TMP_Text)_number).fontSize = 15f;
			((TMP_Text)_number).alignment = (TextAlignmentOptions)260;
			((Graphic)_number).color = Color.white;
			((TMP_Text)_number).textWrappingMode = (TextWrappingModes)0;
			((Graphic)_number).raycastTarget = false;
			((TMP_Text)_number).outlineWidth = 0.12f;
			((TMP_Text)_number).outlineColor = Color32.op_Implicit(Color.black);
			((TMP_Text)_number).text = "";
			val.SetActive(false);
		}
	}
	internal static class Icons
	{
		private static float _nextRefresh;

		private static float _nextLegacyRefresh;

		internal static void UpdateAdvanced(BuildUi ui)
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)ui == (Object)null || !((Behaviour)ui).isActiveAndEnabled || (Object)(object)localPlayer == (Object)null || !((Character)localPlayer).InPlaceMode() || Time.unscaledTime < _nextRefresh)
			{
				return;
			}
			_nextRefresh = Time.unscaledTime + 0.45f;
			if (!(AccessTools.Field(typeof(BuildUi), "m_pieceButtons")?.GetValue(ui) is List<BuildUiPieceButton> list))
			{
				return;
			}
			Comfort.Begin(localPlayer);
			foreach (BuildUiPieceButton item in list)
			{
				RefreshButton(item);
			}
		}

		internal static void RefreshButton(BuildUiPieceButton button)
		{
			if ((Object)(object)button == (Object)null)
			{
				return;
			}
			ComfortNumber comfortNumber = ((Component)button).GetComponent<ComfortNumber>();
			Piece piece = button.Piece;
			if ((Object)(object)piece == (Object)null || piece.m_comfort <= 0)
			{
				if ((Object)(object)comfortNumber != (Object)null)
				{
					comfortNumber.Set(0);
				}
				return;
			}
			if ((Object)(object)comfortNumber == (Object)null)
			{
				comfortNumber = ((Component)button).gameObject.AddComponent<ComfortNumber>();
			}
			comfortNumber.Set(Comfort.Gain(piece));
		}

		internal static void UpdateLegacy(Hud hud, Player player)
		{
			if ((Object)(object)hud == (Object)null || (Object)(object)player == (Object)null || (Object)(object)hud.m_pieceListRoot == (Object)null || Time.unscaledTime < _nextLegacyRefresh)
			{
				return;
			}
			_nextLegacyRefresh = Time.unscaledTime + 0.45f;
			Comfort.Begin(player);
			List<Piece> buildPieces = player.GetBuildPieces();
			RectTransform pieceListRoot = hud.m_pieceListRoot;
			for (int i = 0; i < ((Transform)pieceListRoot).childCount; i++)
			{
				Transform child = ((Transform)pieceListRoot).GetChild(i);
				ComfortNumber comfortNumber = ((Component)child).GetComponent<ComfortNumber>();
				Piece val = ((i < buildPieces.Count) ? buildPieces[i] : null);
				if ((Object)(object)val == (Object)null || val.m_comfort <= 0)
				{
					if ((Object)(object)comfortNumber != (Object)null)
					{
						comfortNumber.Set(0);
					}
					continue;
				}
				if ((Object)(object)comfortNumber == (Object)null)
				{
					comfortNumber = ((Component)child).gameObject.AddComponent<ComfortNumber>();
				}
				comfortNumber.Set(Comfort.Gain(val));
			}
		}
	}
	[HarmonyPatch(typeof(BuildUi), "Update")]
	internal static class AdvancedUpdatePatch
	{
		[HarmonyPostfix]
		private static void Postfix(BuildUi __instance)
		{
			Icons.UpdateAdvanced(__instance);
		}
	}
	[HarmonyPatch(typeof(Hud), "UpdatePieceList")]
	internal static class LegacyUpdatePatch
	{
		[HarmonyPostfix]
		private static void Postfix(Hud __instance, Player player)
		{
			Icons.UpdateLegacy(__instance, player);
		}
	}
	[HarmonyPatch(typeof(Hud), "SetupPieceInfo")]
	internal static class RawComfortDescriptionPatch
	{
		[HarmonyPostfix]
		private static void Postfix(Hud __instance, Piece piece)
		{
			if (!((Object)(object)piece == (Object)null) && piece.m_comfort > 0 && !((Object)(object)__instance.m_pieceDescription == (Object)null))
			{
				TMP_Text pieceDescription = __instance.m_pieceDescription;
				pieceDescription.text = pieceDescription.text + "\nComfort: " + piece.m_comfort;
			}
		}
	}
}