Decompiled source of HPBarheimPoVFix v1.0.0

HPBarheimPoVFix.dll

Decompiled 3 days 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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PassiveTree;
using PassiveTree.UI;
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("HPBarheimPoVFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HPBarheimPoVFix")]
[assembly: AssemblyTitle("HPBarheimPoVFix")]
[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 HPBarheimPoVFix
{
	[BepInPlugin("groshyr.hpbarheimpovfix", "HPBarheimPoVFix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "groshyr.hpbarheimpovfix";

		private const string HpBarheimGuid = "DarthVladi.HPBarheim";

		private const string PovGuid = "treextr.pathofvalheiman";

		private const string CompatType = "HPBarheim.Compat.PathOfValheimanModifiers";

		private const string OwnIconsType = "HPBarheim.Hud.ModifierIconSprite";

		private const int MaxIcons = 32;

		private static ManualLogSource Log;

		private static FieldRef<bool> _tried;

		private static FieldRef<bool> _ready;

		private static Func<string, Sprite> _ownIcon;

		private static Func<int> _iconSet;

		private static readonly Dictionary<long, int> Handles = new Dictionary<long, int> { { 0L, 0 } };

		private static readonly List<long> Masks = new List<long> { 0L };

		private static readonly Dictionary<(long, int), Sprite[]> Built = new Dictionary<(long, int), Sprite[]>();

		private static bool _failed;

		private void Awake()
		{
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Expected O, but got Unknown
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Expected O, but got Unknown
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Expected O, but got Unknown
			//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Type type = AccessTools.TypeByName("HPBarheim.Compat.PathOfValheimanModifiers");
			MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "Bind", (Type[])null, (Type[])null) : null);
			MethodInfo methodInfo2 = ((type != null) ? AccessTools.Method(type, "ReadMask", (Type[])null, (Type[])null) : null);
			MethodInfo methodInfo3 = ((type != null) ? AccessTools.Method(type, "Sprites", (Type[])null, (Type[])null) : null);
			MethodInfo methodInfo4 = ((type != null) ? AccessTools.Method(type, "ParseList", (Type[])null, (Type[])null) : null);
			MethodInfo methodInfo5 = AccessTools.Method(AccessTools.TypeByName("HPBarheim.Hud.ModifierIconSprite"), "GetPov", new Type[1] { typeof(string) }, (Type[])null);
			MethodInfo methodInfo6 = ((type != null) ? AccessTools.PropertyGetter(type, "IconSet") : null);
			if (methodInfo == null || methodInfo2 == null || methodInfo3 == null || methodInfo4 == null || methodInfo5 == null || methodInfo6 == null || AccessTools.Field(type, "_tried") == null || AccessTools.Field(type, "_ready") == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"HP Barheim's Path of Valheiman modifier code was not found (HP Barheim changed?); nothing patched.");
				return;
			}
			_tried = AccessTools.StaticFieldRefAccess<bool>(AccessTools.Field(type, "_tried"));
			_ready = AccessTools.StaticFieldRefAccess<bool>(AccessTools.Field(type, "_ready"));
			_ownIcon = AccessTools.MethodDelegate<Func<string, Sprite>>(methodInfo5, (object)null, true);
			_iconSet = AccessTools.MethodDelegate<Func<int>>(methodInfo6, (object)null, true);
			Harmony val = new Harmony("groshyr.hpbarheimpovfix");
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "BindPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(Plugin), "ReadMaskPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo3, new HarmonyMethod(typeof(Plugin), "SpritesPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo4, new HarmonyMethod(typeof(Plugin), "ParseListPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"HP Barheim reads Path of Valheiman's 64-bit creature modifiers through this fix.");
		}

		private static bool BindPrefix()
		{
			_tried.Invoke() = true;
			try
			{
				RarityMods.Each((Mod)0);
				RarityMods.Icon((Mod)1);
				_ = IconMap.ByGroup.Count;
				_ready.Invoke() = true;
				Log.LogInfo((object)"Path of Valheiman's creature modifiers found (64-bit): HP Barheim draws them as icons over its health bars.");
			}
			catch (Exception ex)
			{
				_ready.Invoke() = false;
				Log.LogWarning((object)("Path of Valheiman's creature modifiers could not be bound: " + ex.Message));
			}
			return false;
		}

		private static bool ReadMaskPrefix(Character c, ref int __result)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected I8, but got Unknown
			__result = 0;
			if ((Object)(object)c == (Object)null || !_ready.Invoke() || _failed)
			{
				return false;
			}
			try
			{
				__result = Handle((long)RarityMods.On(c));
			}
			catch (Exception e)
			{
				Fail(e);
			}
			return false;
		}

		private static bool SpritesPrefix(int mask, Sprite[] buf, ref int total, ref int __result)
		{
			total = 0;
			__result = 0;
			if (mask <= 0 || mask >= Masks.Count || !_ready.Invoke() || buf == null || _failed)
			{
				return false;
			}
			int num = _iconSet();
			long num2 = Masks[mask];
			if (!Built.TryGetValue((num2, num), out var value) || Array.Exists(value, (Sprite s) => (Object)(object)s == (Object)null))
			{
				value = Build(num2, num == 0);
				Built[(num2, num)] = value;
			}
			total = value.Length;
			int num3 = Mathf.Min(value.Length, buf.Length);
			Array.Copy(value, buf, num3);
			__result = num3;
			return false;
		}

		private static bool ParseListPrefix(string list, ref int mask, ref int __result)
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected I8, but got Unknown
			mask = 0;
			__result = 0;
			if (string.IsNullOrEmpty(list) || !_ready.Invoke())
			{
				return false;
			}
			long num = 0L;
			string[] array = list.Split(new char[3] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				try
				{
					long num2 = (long)(Mod)Enum.Parse(typeof(Mod), text.Trim(), ignoreCase: true);
					if (num2 != 0L && (num2 & (num2 - 1)) == 0L && (num & num2) == 0L)
					{
						num |= num2;
						__result++;
					}
				}
				catch (ArgumentException)
				{
				}
			}
			mask = Handle(num);
			return false;
		}

		private static int Handle(long bits)
		{
			if (!Handles.TryGetValue(bits, out var value))
			{
				value = Masks.Count;
				Masks.Add(bits);
				Handles[bits] = value;
			}
			return value;
		}

		private unsafe static Sprite[] Build(long bits, bool own)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			List<Sprite> list = new List<Sprite>(8);
			try
			{
				foreach (Mod item in RarityMods.Each((Mod)bits))
				{
					if (list.Count >= 32)
					{
						break;
					}
					Sprite val = (own ? _ownIcon(((object)(*(Mod*)(&item))/*cast due to .constrained prefix*/).ToString()) : null);
					if ((Object)(object)val == (Object)null)
					{
						string text = RarityMods.Icon(item);
						if (string.IsNullOrEmpty(text) || !IconMap.ByGroup.TryGetValue(text, out var value))
						{
							continue;
						}
						val = UiKit.Tile(value);
					}
					if ((Object)(object)val != (Object)null)
					{
						list.Add(val);
					}
				}
			}
			catch (Exception e)
			{
				Fail(e);
			}
			return list.ToArray();
		}

		private static void Fail(Exception e)
		{
			if (!_failed)
			{
				_failed = true;
				Exception ex = ((e is TargetInvocationException && e.InnerException != null) ? e.InnerException : e);
				Log.LogWarning((object)("Path of Valheiman's creature modifiers could not be read any more (" + ex.GetType().Name + ": " + ex.Message + "); HP Barheim stops drawing them."));
			}
		}
	}
}