Decompiled source of TidyStatusEffects v1.0.1

TidyStatusEffects.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
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.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("TidyStatusEffects")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("TidyStatusEffects")]
[assembly: AssemblyTitle("TidyStatusEffects")]
[assembly: AssemblyVersion("1.0.1.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 TidyStatusEffects
{
	[BepInPlugin("com.dreich.tidystatuseffects", "Tidy Status Effects", "1.0.1")]
	public class TidyStatusEffectsPlugin : BaseUnityPlugin
	{
		public enum Direction
		{
			Horizontal,
			Vertical
		}

		public const string Guid = "com.dreich.tidystatuseffects";

		public const string Name = "Tidy Status Effects";

		public const string Version = "1.0.1";

		internal static ConfigEntry<bool> MoveWithoutMinimap;

		internal static ConfigEntry<float> MarginX;

		internal static ConfigEntry<float> MarginY;

		internal static ConfigEntry<float> IconScale;

		internal static ConfigEntry<bool> UntimedOnRight;

		internal static ConfigEntry<Direction> Layout;

		private void Awake()
		{
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Expected O, but got Unknown
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Expected O, but got Unknown
			MoveWithoutMinimap = ((BaseUnityPlugin)this).Config.Bind<bool>("Layout", "MoveToCornerWithoutMinimap", true, "When the minimap isn't shown, move the status effects up to the top right corner of the screen.");
			MarginX = ((BaseUnityPlugin)this).Config.Bind<float>("Layout", "CornerMarginX", 20f, "Gap from the right edge of the screen when the status effects are in the corner.");
			MarginY = ((BaseUnityPlugin)this).Config.Bind<float>("Layout", "CornerMarginY", 20f, "Gap from the top edge of the screen when the status effects are in the corner.");
			IconScale = ((BaseUnityPlugin)this).Config.Bind<float>("Layout", "IconScale", 0.8f, new ConfigDescription("Size of the status effect icons. 1 = the game's normal size.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.3f, 2f), Array.Empty<object>()));
			Layout = ((BaseUnityPlugin)this).Config.Bind<Direction>("Layout", "Direction", Direction.Horizontal, "Horizontal lines the icons up in a row going left. Vertical stacks them in a column going down.");
			UntimedOnRight = ((BaseUnityPlugin)this).Config.Bind<bool>("Order", "UntimedOnRight", true, "Status effects without a timer, like Shelter or Cold, always come first: on the far right in a row, or at the top in a column.");
			Harmony val = new Harmony("com.dreich.tidystatuseffects");
			val.PatchAll();
			if (Chainloader.PluginInfos.ContainsKey("jatify.ui"))
			{
				JatifyBuffs.Apply(val, ((BaseUnityPlugin)this).Logger);
			}
		}
	}
	[HarmonyPatch(typeof(SEMan), "GetHUDStatusEffects")]
	internal static class StatusEffectOrderPatch
	{
		private static readonly List<StatusEffect> untimed = new List<StatusEffect>();

		private static readonly List<StatusEffect> timed = new List<StatusEffect>();

		private static void Postfix(List<StatusEffect> effects)
		{
			if (!TidyStatusEffectsPlugin.UntimedOnRight.Value || effects == null || effects.Count < 2)
			{
				return;
			}
			untimed.Clear();
			timed.Clear();
			foreach (StatusEffect effect in effects)
			{
				if (!((Object)(object)effect == (Object)null))
				{
					if (effect.m_ttl > 0f)
					{
						timed.Add(effect);
					}
					else
					{
						untimed.Add(effect);
					}
				}
			}
			effects.Clear();
			effects.AddRange(untimed);
			effects.AddRange(timed);
		}
	}
	[HarmonyPatch(typeof(Hud), "UpdateStatusEffects")]
	internal static class StatusEffectDirectionPatch
	{
		private static void Postfix(Hud __instance, List<RectTransform> ___m_statusEffects)
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			if (___m_statusEffects == null)
			{
				return;
			}
			bool flag = TidyStatusEffectsPlugin.Layout.Value == TidyStatusEffectsPlugin.Direction.Vertical;
			int num = Mathf.Max(1, __instance.m_effectsPerRow);
			float statusEffectSpacing = __instance.m_statusEffectSpacing;
			for (int i = 0; i < ___m_statusEffects.Count; i++)
			{
				RectTransform val = ___m_statusEffects[i];
				if (!((Object)(object)val == (Object)null))
				{
					int num2 = i / num;
					int num3 = i - num2 * num;
					Vector2 val2 = (flag ? new Vector2(-4f - (float)num2 * statusEffectSpacing, (float)(-num3) * statusEffectSpacing) : new Vector2(-4f - (float)num3 * statusEffectSpacing, (float)(-num2) * statusEffectSpacing));
					if (val.anchoredPosition != val2)
					{
						val.anchoredPosition = val2;
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(Hud), "Update")]
	internal static class StatusEffectPlacementPatch
	{
		private static RectTransform root;

		private static Vector3 originalPosition;

		private static Vector3 originalScale;

		private static bool moved;

		private static readonly Vector3[] corners = (Vector3[])(object)new Vector3[4];

		private static void Postfix(Hud __instance)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: 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_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			RectTransform statusEffectListRoot = __instance.m_statusEffectListRoot;
			if ((Object)(object)statusEffectListRoot == (Object)null)
			{
				return;
			}
			if ((Object)(object)statusEffectListRoot != (Object)(object)root)
			{
				root = statusEffectListRoot;
				originalPosition = ((Transform)statusEffectListRoot).localPosition;
				originalScale = ((Transform)statusEffectListRoot).localScale;
				moved = false;
			}
			Vector3 val = originalScale * TidyStatusEffectsPlugin.IconScale.Value;
			if (((Transform)statusEffectListRoot).localScale != val)
			{
				((Transform)statusEffectListRoot).localScale = val;
			}
			if (!TidyStatusEffectsPlugin.MoveWithoutMinimap.Value || MinimapShown())
			{
				if (moved)
				{
					((Transform)statusEffectListRoot).localPosition = originalPosition;
					moved = false;
				}
				return;
			}
			Canvas componentInParent = ((Component)statusEffectListRoot).GetComponentInParent<Canvas>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				return;
			}
			Transform transform = ((Component)componentInParent.rootCanvas).transform;
			RectTransform val2 = (RectTransform)(object)((transform is RectTransform) ? transform : null);
			if (!((Object)(object)val2 == (Object)null))
			{
				val2.GetWorldCorners(corners);
				Vector3 val3 = corners[2] + ((Transform)val2).TransformVector(new Vector3(0f - TidyStatusEffectsPlugin.MarginX.Value, 0f - TidyStatusEffectsPlugin.MarginY.Value, 0f));
				Vector3 val4 = ((Transform)statusEffectListRoot).position - val3;
				if (((Vector3)(ref val4)).sqrMagnitude > 0.01f)
				{
					((Transform)statusEffectListRoot).position = val3;
				}
				moved = true;
			}
		}

		internal static bool MinimapShown()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Invalid comparison between Unknown and I4
			Minimap instance = Minimap.instance;
			if ((Object)(object)instance != (Object)null)
			{
				return (int)instance.m_mode > 0;
			}
			return false;
		}
	}
	internal static class JatifyBuffs
	{
		internal const string JatifyGuid = "jatify.ui";

		private const float Step = 54f;

		private const float RowStep = 72f;

		private const float Right = 372f;

		private const int PerColumn = 7;

		private static FieldInfo buffsField;

		private static FieldInfo poolField;

		private static FieldInfo effectsField;

		private static FieldInfo rootField;

		private static Func<StatusEffect, bool> isDebuff;

		private static RectTransform buffs;

		private static Vector2 originalPosition;

		private static Vector3 originalScale;

		private static bool moved;

		internal static void Apply(Harmony harmony, ManualLogSource log)
		{
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Expected O, but got Unknown
			try
			{
				Type type = AccessTools.TypeByName("Jatify.UI.HudView");
				Type type2 = type?.GetNestedType("Buff", BindingFlags.Public | BindingFlags.NonPublic);
				MethodInfo methodInfo = ((type == null) ? null : AccessTools.Method(type, "TickBuffs", (Type[])null, (Type[])null));
				MethodInfo methodInfo2 = ((type == null) ? null : AccessTools.Method(type, "IsDebuff", (Type[])null, (Type[])null));
				buffsField = ((type == null) ? null : AccessTools.Field(type, "_buffs"));
				poolField = ((type == null) ? null : AccessTools.Field(type, "_buffPool"));
				effectsField = ((type == null) ? null : AccessTools.Field(type, "_effects"));
				rootField = ((type2 == null) ? null : AccessTools.Field(type2, "Root"));
				if (methodInfo == null || methodInfo2 == null || buffsField == null || poolField == null || effectsField == null || rootField == null)
				{
					log.LogWarning((object)"JatifyUI buffs not found, Jatify support is off.");
					return;
				}
				isDebuff = (Func<StatusEffect, bool>)Delegate.CreateDelegate(typeof(Func<StatusEffect, bool>), methodInfo2);
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(JatifyBuffs), "Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("JatifyUI support failed: " + ex.Message));
			}
		}

		private static void Postfix(object __instance)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: 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_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			object? value = buffsField.GetValue(__instance);
			RectTransform val = (RectTransform)((value is RectTransform) ? value : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			if ((Object)(object)val != (Object)(object)buffs)
			{
				buffs = val;
				originalPosition = val.anchoredPosition;
				originalScale = ((Transform)val).localScale;
				moved = false;
			}
			Vector3 val2 = originalScale * TidyStatusEffectsPlugin.IconScale.Value;
			if (((Transform)val).localScale != val2)
			{
				((Transform)val).localScale = val2;
			}
			if (TidyStatusEffectsPlugin.MoveWithoutMinimap.Value && !StatusEffectPlacementPatch.MinimapShown())
			{
				Vector2 val3 = default(Vector2);
				((Vector2)(ref val3))..ctor(0f - TidyStatusEffectsPlugin.MarginX.Value, 0f - TidyStatusEffectsPlugin.MarginY.Value);
				if (val.anchoredPosition != val3)
				{
					val.anchoredPosition = val3;
				}
				moved = true;
			}
			else if (moved)
			{
				val.anchoredPosition = originalPosition;
				moved = false;
			}
			if (TidyStatusEffectsPlugin.Layout.Value == TidyStatusEffectsPlugin.Direction.Vertical)
			{
				Stack(__instance);
			}
		}

		private static void Stack(object view)
		{
			IList list = poolField.GetValue(view) as IList;
			List<StatusEffect> list2 = effectsField.GetValue(view) as List<StatusEffect>;
			if (list == null || list2 == null)
			{
				return;
			}
			int num = 0;
			foreach (StatusEffect item in list2)
			{
				if ((Object)(object)item != (Object)null && (Object)(object)item.m_icon != (Object)null && isDebuff(item))
				{
					num++;
				}
			}
			int num2 = 0;
			for (int i = 0; i < list.Count; i++)
			{
				object? value = rootField.GetValue(list[i]);
				RectTransform val = (RectTransform)((value is RectTransform) ? value : null);
				if ((Object)(object)val == (Object)null || !((Component)val).gameObject.activeSelf)
				{
					break;
				}
				num2++;
			}
			num = Mathf.Min(num, num2);
			int num3 = 0;
			int num4 = 0;
			for (int j = num; j < num2; j++)
			{
				Place(list[j], num3, num4);
				if (++num4 >= 7)
				{
					num4 = 0;
					num3++;
				}
			}
			if (num4 > 0)
			{
				num3++;
			}
			num4 = 0;
			for (int k = 0; k < num; k++)
			{
				Place(list[k], num3, num4);
				if (++num4 >= 7)
				{
					num4 = 0;
					num3++;
				}
			}
		}

		private static void Place(object buff, int column, int row)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			object? value = rootField.GetValue(buff);
			RectTransform val = (RectTransform)((value is RectTransform) ? value : null);
			if (!((Object)(object)val == (Object)null))
			{
				Vector2 val2 = default(Vector2);
				((Vector2)(ref val2))..ctor(372f - (float)column * 54f, (float)(-row) * 72f);
				if (val.anchoredPosition != val2)
				{
					val.anchoredPosition = val2;
				}
			}
		}
	}
}