Decompiled source of HotbarRD v1.1.0

HotbarRD.dll

Decompiled 9 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using HotbarRD.Patches;
using HotbarRD.Utils;
using LethalConfig;
using LethalConfig.ConfigItems;
using LethalConfig.ConfigItems.Options;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("HotbarRD")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+7ac05e91f542d33d1aa006b856b0731d3f114d6c")]
[assembly: AssemblyProduct("HotbarRD")]
[assembly: AssemblyTitle("HotbarRD")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 HotbarRD
{
	[BepInPlugin("xuxiaolan.hotbarrd", "HotbarRD", "1.0.2")]
	internal class Plugin : BaseUnityPlugin
	{
		private static readonly Harmony harmony = new Harmony("xuxiaolan.hotbarrd");

		internal static ManualLogSource logger;

		internal static PluginConfig Config { get; set; }

		private void Awake()
		{
			logger = ((BaseUnityPlugin)this).Logger;
			new AssetsManager();
			Config = new PluginConfig(((BaseUnityPlugin)this).Config);
			logger.LogInfo((object)"Hotbar Redesign - Arts by Xu Xiaolan / Mod programmed by VELD-Dev.");
			harmony.PatchAll(typeof(HUDManager_Patches));
			logger.LogDebug((object)"Applied HUDManager patches.");
		}
	}
	public class PluginConfig
	{
		public static ConfigEntry<CustomFrames> SelectedFrameType;

		public static ConfigEntry<int> SelectedFrameVariant;

		private static EnumDropDownConfigItem<CustomFrames> LCFrameSelector { get; set; }

		private static IntSliderConfigItem LCFrameVariantSelector { get; set; }

		public PluginConfig(ConfigFile cfg)
		{
			//IL_0043: 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)
			//IL_0055: Expected O, but got Unknown
			//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_006c: Expected O, but got Unknown
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			SelectedFrameType = cfg.Bind<CustomFrames>("General", "Texture pack", CustomFrames.Default, "Choose among the hotbar texture packs.");
			SelectedFrameVariant = cfg.Bind<int>("General", "Texture variant", 0, "Select among the variants of the selected hotbar design.\n\nIf the variant is not found, the first existing variant will be used instead.");
			LCFrameSelector = new EnumDropDownConfigItem<CustomFrames>(SelectedFrameType, new EnumDropDownOptions
			{
				RequiresRestart = false
			});
			ConfigEntry<int> selectedFrameVariant = SelectedFrameVariant;
			IntSliderOptions val = new IntSliderOptions();
			((BaseRangeOptions<int>)val).Min = 0;
			((BaseRangeOptions<int>)val).Max = 2;
			((BaseOptions)val).RequiresRestart = false;
			LCFrameVariantSelector = new IntSliderConfigItem(selectedFrameVariant, val);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)LCFrameSelector);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)LCFrameVariantSelector);
			LethalConfigManager.SetModDescription("HotbarRD adds new hotbar designs.\n\nArts by Xu Xiaolan\nMod by VELD-Dev");
			if (AssetsManager.Singleton != null)
			{
				Texture2D[] array = AssetsManager.Singleton.SearchAssets(SelectedFrameType.Value);
				Texture2D val2 = (Texture2D)((array.Length >= 1) ? ((object)array[Math.Clamp(SelectedFrameVariant.Value, 0, array.Length - 1)]) : ((object)AssetsManager.Singleton.SearchAssets(CustomFrames.Holy)[0]));
				LethalConfigManager.SetModIcon(Sprite.Create(val2, new Rect(0f, 0f, (float)((Texture)val2).width, (float)((Texture)val2).height), new Vector2(0f, 0f)));
			}
			cfg.SettingChanged += OnChanged;
		}

		internal static void OnChanged(object sender, SettingChangedEventArgs args)
		{
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			Plugin.logger.LogDebug((object)$"Plugin config saved - Trying to set slot frames [{SelectedFrameType?.Value}:{SelectedFrameVariant?.Value}]");
			HotbarUtils.TrySetSlotFrames(SelectedFrameType.Value, SelectedFrameVariant.Value);
			Texture2D[] array = AssetsManager.Singleton.SearchAssets(SelectedFrameType.Value);
			Texture2D val = (Texture2D)((array.Length >= 1) ? ((object)array[Math.Clamp(SelectedFrameVariant.Value, 0, array.Length - 1)]) : ((object)AssetsManager.Singleton.SearchAssets(CustomFrames.Holy)[0]));
			LethalConfigManager.SetModIcon(Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0f, 0f)));
		}
	}
	public class PluginInfo
	{
		public const string PluginName = "HotbarRD";

		public const string PluginGUID = "xuxiaolan.hotbarrd";

		public const string PluginVers = "1.0.2";
	}
}
namespace HotbarRD.Utils
{
	internal class AssetsManager
	{
		private readonly Dictionary<string, Object> Assets;

		private AssetBundle assetBundle;

		internal static AssetsManager? Singleton { get; private set; }

		internal AssetsManager()
		{
			Singleton = this;
			Assets = new Dictionary<string, Object>();
			LoadAllAssets();
		}

		internal void LoadAllAssets()
		{
			string[] manifestResourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
			string name = manifestResourceNames.FirstOrDefault((string n) => n.EndsWith("hotbarrd.assets"));
			Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
			assetBundle = AssetBundle.LoadFromStream(manifestResourceStream);
			manifestResourceStream.Close();
			Object[] array = assetBundle.LoadAllAssets();
			Object[] array2 = array;
			foreach (Object val in array2)
			{
				string text = ((object)val).GetType().Name + "." + val.name;
				Assets.TryAdd(text, val);
				Plugin.logger.LogDebug((object)("Loaded and cached asset '" + text + "' (" + val.name + ") of type " + ((object)val).GetType().FullName + "."));
			}
		}

		internal bool TryGetAsset<T>(string name, out T result) where T : Object
		{
			Object value;
			bool result2 = Assets.TryGetValue(typeof(T).Name + "." + name, out value);
			result = (T)(object)value;
			return result2;
		}

		internal Object[] SearchAssets(string searchArgument)
		{
			Plugin.logger.LogDebug((object)("Searching resource " + searchArgument));
			Regex regex = new Regex(searchArgument ?? "", RegexOptions.IgnoreCase);
			List<Object> list = new List<Object>();
			foreach (KeyValuePair<string, Object> asset in Assets)
			{
				if (regex.IsMatch(asset.Key))
				{
					list.Add(asset.Value);
				}
			}
			Plugin.logger.LogDebug((object)$"Resources found: {list.Count}/{Assets.Count}");
			return list.ToArray();
		}

		internal T[] SearchAssets<T>(string searchArgument) where T : Object
		{
			Object[] array = (from o in SearchAssets(searchArgument ?? "")
				where ((object)o).GetType() == typeof(T)
				select o).ToArray();
			List<T> list = new List<T>();
			Object[] array2 = array;
			foreach (Object val in array2)
			{
				list.Add((T)(object)val);
			}
			T[] array3 = list.ToArray();
			Plugin.logger.LogDebug((object)$"Resources filtered: {array3.Length}/{Assets.Count}");
			if (array3.Length < 1 || array3 == null)
			{
				return Array.Empty<T>();
			}
			return array3;
		}

		internal Texture2D[] SearchAssets(CustomFrames frameType)
		{
			return this.SearchAssets<Texture2D>(frameType.ToString());
		}
	}
	public enum CustomFrames
	{
		Default,
		Cursed,
		Earth,
		Electric,
		Fairy,
		Fire,
		Holy,
		Neutral,
		Plant,
		Water
	}
	internal static class HotbarUtils
	{
		private static RuntimeAnimatorController? _defaultHUDRuntimeAnimatorController;

		internal static bool TrySetSlotFrames(CustomFrames frameType, int frameVariant)
		{
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			if (HUDManager.Instance == null)
			{
				return false;
			}
			if (HUDManager.Instance.itemSlotIconFrames.Length < 1)
			{
				return false;
			}
			if (_defaultHUDRuntimeAnimatorController == null)
			{
				Animator val = ((Component)HUDManager.Instance.itemSlotIconFrames[0]).gameObject.GetComponent<Animator>() ?? throw new NullReferenceException("The animator was not found and then cannot be processed!");
				_defaultHUDRuntimeAnimatorController = val.runtimeAnimatorController;
				Plugin.logger.LogDebug((object)("Default HUD Rntime Animator Controller was null. Filled it with " + ((Object)val.runtimeAnimatorController).name));
			}
			Image[] itemSlotIconFrames = HUDManager.Instance.itemSlotIconFrames;
			if (frameType == CustomFrames.Default)
			{
				Image[] array = itemSlotIconFrames;
				foreach (Image val2 in array)
				{
					val2.overrideSprite = val2.sprite;
					Animator val3 = ((Component)val2).gameObject.GetComponent<Animator>() ?? throw new NullReferenceException("The animator was not found and then cannot be processed!");
					string name = ((Object)val3.runtimeAnimatorController).name;
					val3.runtimeAnimatorController = _defaultHUDRuntimeAnimatorController;
					Plugin.logger.LogDebug((object)("Swapped Runtime Animator Controller " + name + " with " + ((Object)_defaultHUDRuntimeAnimatorController).name + " (back to default)"));
				}
				return true;
			}
			Texture2D[] array2 = AssetsManager.Singleton.SearchAssets(frameType);
			Plugin.logger.LogDebug((object)$"Redesigned frames: {array2.Length} variants.");
			if (array2.Length < 1)
			{
				return false;
			}
			Texture2D val4 = array2[Math.Clamp(frameVariant, 0, array2.Length - 1)];
			Image[] array3 = itemSlotIconFrames;
			foreach (Image val5 in array3)
			{
				Plugin.logger.LogDebug((object)$"Updating slot: {((Object)val5).name} with {((Texture)val4).width}x{((Texture)val4).height} frame. (Col:{((Graphic)val5).color})");
				Sprite overrideSprite = Sprite.Create(val4, new Rect(0f, 0f, (float)((Texture)val4).width, (float)((Texture)val4).height), Vector2.zero);
				val5.overrideSprite = overrideSprite;
				Animator val6 = ((Component)val5).gameObject.GetComponent<Animator>() ?? throw new NullReferenceException("The animator was not found and then cannot be processed!");
				if (!AssetsManager.Singleton.TryGetAsset<AnimatorOverrideController>("SlotsAnimatorOverride", out AnimatorOverrideController result))
				{
					Plugin.logger.LogError((object)"The slot animator override was not found. Consider making a report for this error.");
					continue;
				}
				val6.runtimeAnimatorController = (RuntimeAnimatorController)(object)result;
				Plugin.logger.LogDebug((object)("Swapped Runtime Animator Controller " + ((Object)_defaultHUDRuntimeAnimatorController).name + " with " + ((Object)result).name + "."));
			}
			return true;
		}

		internal static byte[] ReadAllBytes(this Stream stream)
		{
			byte[] array = new byte[stream.Length];
			stream.Read(array, 0, (int)stream.Length);
			return array;
		}
	}
}
namespace HotbarRD.Patches
{
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDManager_Patches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(HUDManager), "Start")]
		private static void Start(HUDManager __instance)
		{
			Plugin.logger.LogDebug((object)$"HUDManager started - Trying to set slot frames [{PluginConfig.SelectedFrameType?.Value}:{PluginConfig.SelectedFrameVariant?.Value}]");
			if (!HotbarUtils.TrySetSlotFrames(PluginConfig.SelectedFrameType.Value, PluginConfig.SelectedFrameVariant.Value))
			{
				Plugin.logger.LogError((object)"Could change the slot frames!");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HUDManager), "OnEnable")]
		private static void OnEnable(HUDManager __instance)
		{
			Plugin.logger.LogDebug((object)$"HUDManager enabled - Trying to set slot frames [{PluginConfig.SelectedFrameType?.Value}:{PluginConfig.SelectedFrameVariant?.Value}]");
			if (!HotbarUtils.TrySetSlotFrames(PluginConfig.SelectedFrameType.Value, PluginConfig.SelectedFrameVariant.Value))
			{
				Plugin.logger.LogError((object)"Could change the slot frames!");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HUDManager), "DisplayNewScrapFound")]
		private static void DisplayNewScrapFound(HUDManager __instance)
		{
			Plugin.logger.LogDebug((object)$"HUDManager display new scrap found - Trying to set slot frames [{PluginConfig.SelectedFrameType?.Value}:{PluginConfig.SelectedFrameVariant?.Value}]");
			if (!HotbarUtils.TrySetSlotFrames(PluginConfig.SelectedFrameType.Value, PluginConfig.SelectedFrameVariant.Value))
			{
				Plugin.logger.LogError((object)"Could change the slot frames!");
			}
		}
	}
}