Decompiled source of WiresComfortInfo v0.1.4

WiresComfortInfo.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
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.4")]
	[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.4";

		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");
			Type[] typesFromAssembly = AccessTools.GetTypesFromAssembly(typeof(Plugin).Assembly);
			foreach (Type type in typesFromAssembly)
			{
				try
				{
					_harmony.CreateClassProcessor(type).Patch();
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Patch failed for " + type.FullName + ": " + ex.Message));
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Wire's Comfort Info 0.1.4 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_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: 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)
			{
				Fireplace component = ((Component)hypothetical).GetComponent<Fireplace>();
				if ((Object)(object)component == (Object)null || component.m_startFuel <= 0f)
				{
					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;

		private int _shown;

		private float _retryFontAt;

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

		private TMP_Text FindStyleSource()
		{
			Hud instance = Hud.instance;
			if ((Object)(object)instance != (Object)null)
			{
				TMP_Text val = Valid(instance.m_buildSelection);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
				val = Valid(instance.m_pieceDescription);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
				val = Valid((TMP_Text)(object)instance.m_hoverName);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
			}
			TMP_Text[] array = (((Object)(object)((Component)this).transform.parent != (Object)null) ? ((Component)((Component)this).transform.parent).GetComponentsInChildren<TMP_Text>(true) : ((Component)((Component)this).transform).GetComponentsInChildren<TMP_Text>(true));
			for (int i = 0; i < array.Length; i++)
			{
				TMP_Text val2 = Valid(array[i]);
				if ((Object)(object)val2 != (Object)null)
				{
					return val2;
				}
			}
			return null;
		}

		private static TMP_Text Valid(TMP_Text text)
		{
			if (!((Object)(object)text != (Object)null) || !((Object)(object)text.font != (Object)null) || !((Object)(object)text.fontSharedMaterial != (Object)null))
			{
				return null;
			}
			return text;
		}

		private void Create()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: 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)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			TMP_Text val = FindStyleSource();
			if ((Object)(object)val == (Object)null)
			{
				_retryFontAt = Time.unscaledTime + 5f;
				return;
			}
			GameObject val2 = new GameObject("Wire Comfort Addition", new Type[1] { typeof(RectTransform) });
			val2.SetActive(false);
			val2.transform.SetParent(((Component)this).transform, false);
			RectTransform val3 = (RectTransform)val2.transform;
			val3.anchorMin = Vector2.one;
			val3.anchorMax = Vector2.one;
			val3.pivot = Vector2.one;
			val3.anchoredPosition = new Vector2(-3f, -2f);
			val3.sizeDelta = new Vector2(34f, 22f);
			((Transform)val3).SetAsLastSibling();
			_number = val2.AddComponent<TextMeshProUGUI>();
			((TMP_Text)_number).font = val.font;
			((TMP_Text)_number).fontSharedMaterial = val.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).text = "";
		}
	}
	internal static class Icons
	{
		private static readonly FieldRef<BuildUi, List<BuildUiPieceButton>> PieceButtons = AccessTools.FieldRefAccess<BuildUi, List<BuildUiPieceButton>>("m_pieceButtons");

		internal static void Refresh(BuildUi ui)
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)ui == (Object)null || (Object)(object)localPlayer == (Object)null || !((Character)localPlayer).InPlaceMode())
			{
				return;
			}
			List<BuildUiPieceButton> list = PieceButtons.Invoke(ui);
			if (list == null)
			{
				return;
			}
			Comfort.Begin(localPlayer);
			foreach (BuildUiPieceButton item in list)
			{
				RefreshButton(item);
			}
		}

		private 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>();
			}
			int value = Comfort.Gain(piece);
			comfortNumber.Set(value);
		}
	}
	[HarmonyPatch(typeof(BuildUi), "OpenBuildMenu")]
	internal static class OpenPatch
	{
		[HarmonyPostfix]
		private static void Postfix(BuildUi __instance)
		{
			Icons.Refresh(__instance);
		}
	}
	[HarmonyPatch(typeof(BuildUi), "UpdatePieceButtons")]
	internal static class PieceButtonsPatch
	{
		[HarmonyPostfix]
		private static void Postfix(BuildUi __instance)
		{
			if (((Component)__instance).gameObject.activeInHierarchy)
			{
				Icons.Refresh(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Hud), "SetupPieceInfo")]
	internal static class RawComfortDescriptionPatch
	{
		private static Piece _piece;

		private static string _raw;

		private static string _text;

		private static Localization _localization;

		private static string _language;

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> input)
		{
			FieldInfo description = AccessTools.Field(typeof(Piece), "m_description");
			MethodInfo localize = AccessTools.Method(typeof(Localization), "Localize", new Type[1] { typeof(string) }, (Type[])null);
			MethodInfo describe = AccessTools.Method(typeof(RawComfortDescriptionPatch), "Describe", (Type[])null, (Type[])null);
			bool afterDescription = false;
			bool done = false;
			foreach (CodeInstruction code in input)
			{
				if (!done && afterDescription && CodeInstructionExtensions.Calls(code, localize))
				{
					done = true;
					yield return new CodeInstruction(OpCodes.Ldarg_1, (object)null);
					yield return new CodeInstruction(OpCodes.Call, (object)describe)
					{
						labels = code.labels,
						blocks = code.blocks
					};
				}
				else
				{
					afterDescription = !done && CodeInstructionExtensions.LoadsField(code, description, false);
					yield return code;
				}
			}
			if (!done)
			{
				Debug.LogWarning((object)"[Comfort Info] Piece description hook not found; the comfort line won't show.");
			}
		}

		private static string Describe(Localization localization, string raw, Piece piece)
		{
			string selectedLanguage = localization.GetSelectedLanguage();
			if ((Object)(object)piece != (Object)(object)_piece || raw != _raw || localization != _localization || selectedLanguage != _language || _text == null)
			{
				_language = selectedLanguage;
				_piece = piece;
				_raw = raw;
				_localization = localization;
				string text = localization.Localize(raw);
				if ((Object)(object)piece != (Object)null && piece.m_comfort > 0)
				{
					text = text + "\nComfort: <color=orange>" + piece.m_comfort + "</color>";
				}
				_text = text;
			}
			return _text;
		}
	}
	internal static class Prof
	{
		internal const int Open = 0;

		internal const int Refresh = 1;

		internal const int ComfortScan = 2;

		internal const int Gain = 3;

		internal const int NewNumber = 4;

		internal const int Description = 5;

		internal const int FrameAfterOpen = 6;

		internal const int Count = 7;

		[Conditional("WIRE_PROFILE")]
		internal static void Begin(int section)
		{
		}

		[Conditional("WIRE_PROFILE")]
		internal static void End(int section)
		{
		}
	}
}