Decompiled source of SlotMachineSkinNames v1.0.0

plugins/SlotMachineSkinNames/SlotMachineSkinNames.dll

Decompiled 13 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SlotMachineSkinNames")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+67b8d95d288a6a44ecfa1a6033cf4695fd7100ad")]
[assembly: AssemblyProduct("SlotMachineSkinNames")]
[assembly: AssemblyTitle("SlotMachineSkinNames")]
[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 SlotMachineSkinNames
{
	internal static class GameFields
	{
		public static FieldRef<SlotMachine, TextMeshProUGUI> RollerText { get; private set; }

		public static FieldRef<SlotMachine, Vector2> DefaultSlotSize { get; private set; }

		public static FieldRef<SlotMachine, MachineSlot[]> MachineSlots { get; private set; }

		public static Func<byte> RolledIndex { get; private set; }

		public static void Resolve()
		{
			RollerText = Instance<TextMeshProUGUI>("_rollerText", "skin names can't be shown");
			DefaultSlotSize = Instance<Vector2>("_defaultSlotSize", "label size falls back to the slot's current size");
			MachineSlots = Instance<MachineSlot[]>("_machineSlots", "losing slots will keep their names after a roll");
			FieldInfo rolled = AccessTools.Field(typeof(SlotMachine), "_rolledIndex");
			if (rolled != null && rolled.IsStatic && rolled.FieldType == typeof(byte))
			{
				RolledIndex = () => (byte)rolled.GetValue(null);
			}
			else
			{
				Warn("_rolledIndex", typeof(byte), "losing slots will keep their names after a roll");
			}
		}

		private static FieldRef<SlotMachine, T> Instance<T>(string name, string effect)
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(SlotMachine), name);
			if (fieldInfo != null && !fieldInfo.IsStatic && fieldInfo.FieldType == typeof(T))
			{
				return AccessTools.FieldRefAccess<SlotMachine, T>(fieldInfo);
			}
			Warn(name, typeof(T), effect);
			return null;
		}

		private static void Warn(string name, Type type, string effect)
		{
			Plugin.Log.LogWarning((object)("SlotMachine." + name + " (" + type.Name + ") not found, the game was probably updated: " + effect + "."));
		}
	}
	[HarmonyPatch(typeof(MachineSlot), "SetIndex")]
	internal static class MachineSlotSetIndexPatch
	{
		private static void Postfix(MachineSlot __instance, byte itemIndex, byte skinIndex)
		{
			//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_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!TryGetSkin(itemIndex, skinIndex, out var skin))
				{
					return;
				}
				TextMeshProUGUI val = SkinLabels.Get(__instance);
				if (!((Object)(object)val == (Object)null))
				{
					((TMP_Text)val).text = ((ItemSkin)(ref skin)).Name;
					((Graphic)val).color = SkinLabels.RarityColor(((ItemSkin)(ref skin)).Rarity);
					((Component)val).gameObject.SetActive(true);
					if (Plugin.LogSlotText.Value)
					{
						ManualLogSource log = Plugin.Log;
						string text = $"{((Object)__instance).name}: \"{((ItemSkin)(ref skin)).Name}\" ({((ItemSkin)(ref skin)).Rarity}), item {itemIndex}, skin {skinIndex}, ";
						object arg = ((Graphic)val).color;
						Rect rect = ((TMP_Text)val).rectTransform.rect;
						log.LogInfo((object)(text + $"color {arg}, size {((Rect)(ref rect)).size}, font {((TMP_Text)val).fontSize}"));
					}
				}
			}
			catch (Exception arg2)
			{
				Plugin.Log.LogError((object)$"Could not label {((__instance != null) ? ((Object)__instance).name : null)}: {arg2}");
			}
		}

		private static bool TryGetSkin(byte itemIndex, byte skinIndex, out ItemSkin skin)
		{
			//IL_0001: 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)
			skin = default(ItemSkin);
			SkinPreset val = null;
			if (itemIndex == byte.MaxValue)
			{
				if (Object.op_Implicit((Object)(object)BoatManager.Boat))
				{
					val = BoatManager.Boat.SkinPreset;
				}
			}
			else
			{
				Item val2 = GameInfo.IDToItem(itemIndex);
				if (Object.op_Implicit((Object)(object)val2))
				{
					val = val2.SkinPreset;
				}
			}
			if (!Object.op_Implicit((Object)(object)val) || skinIndex >= val.Skins.Count)
			{
				return false;
			}
			skin = val.Skins[skinIndex];
			return true;
		}
	}
	[HarmonyPatch(typeof(SlotMachine), "OnRollFinished")]
	internal static class SlotMachineOnRollFinishedPatch
	{
		private static void Postfix(SlotMachine __instance)
		{
			if (GameFields.MachineSlots == null || GameFields.RolledIndex == null)
			{
				return;
			}
			try
			{
				MachineSlot[] array = GameFields.MachineSlots.Invoke(__instance);
				byte b = GameFields.RolledIndex();
				for (int i = 0; i < array.Length; i++)
				{
					if (i != b && Object.op_Implicit((Object)(object)array[i]))
					{
						TextMeshProUGUI val = SkinLabels.Find(array[i]);
						if ((Object)(object)val != (Object)null)
						{
							((Component)val).gameObject.SetActive(false);
						}
					}
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Could not hide the losing labels: {arg}");
			}
		}
	}
	internal static class SkinLabels
	{
		private const string LabelName = "SkinNameLabel";

		public static TextMeshProUGUI Find(MachineSlot slot)
		{
			Transform val = ((Component)slot).transform.Find("SkinNameLabel");
			if (!Object.op_Implicit((Object)(object)val))
			{
				return null;
			}
			return ((Component)val).GetComponent<TextMeshProUGUI>();
		}

		public static TextMeshProUGUI Get(MachineSlot slot)
		{
			//IL_0096: 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_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI val = Find(slot);
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
			SlotMachine componentInParent = ((Component)slot).GetComponentInParent<SlotMachine>();
			if (!Object.op_Implicit((Object)(object)componentInParent) || GameFields.RollerText == null)
			{
				return null;
			}
			TextMeshProUGUI val2 = GameFields.RollerText.Invoke(componentInParent);
			if (!Object.op_Implicit((Object)(object)val2))
			{
				return null;
			}
			GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, ((Component)slot).transform, false);
			((Object)val3).name = "SkinNameLabel";
			val3.layer = ((Component)val2).gameObject.layer;
			val = val3.GetComponent<TextMeshProUGUI>();
			Vector2 val4 = ((GameFields.DefaultSlotSize != null) ? GameFields.DefaultSlotSize.Invoke(componentInParent) : slot.Rect.sizeDelta);
			if (val4.x <= 0f || val4.y <= 0f)
			{
				val4 = slot.Rect.sizeDelta;
			}
			Rect rect = ((TMP_Text)val2).rectTransform.rect;
			float num = ((Rect)(ref rect)).height;
			if (num <= 0f)
			{
				num = val4.y / 3f;
			}
			RectTransform rectTransform = ((TMP_Text)val).rectTransform;
			Vector2 val5 = default(Vector2);
			((Vector2)(ref val5))..ctor(0.5f, 0.5f);
			rectTransform.anchorMax = val5;
			rectTransform.anchorMin = val5;
			rectTransform.pivot = new Vector2(0.5f, 1f);
			rectTransform.sizeDelta = new Vector2(val4.x * 1.15f, num);
			rectTransform.anchoredPosition = new Vector2(0f, (0f - val4.y) * 0.5f);
			((Transform)rectTransform).localScale = Vector3.one;
			((Transform)rectTransform).localRotation = Quaternion.identity;
			((TMP_Text)val).text = "";
			((Graphic)val).raycastTarget = false;
			((TMP_Text)val).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)val).alignment = (TextAlignmentOptions)514;
			((TMP_Text)val).enableAutoSizing = true;
			((TMP_Text)val).fontSizeMax = ((TMP_Text)val2).fontSize * 0.8f;
			((TMP_Text)val).fontSizeMin = ((TMP_Text)val).fontSizeMax * 0.4f;
			if (Plugin.LogSlotText.Value)
			{
				Plugin.Log.LogInfo((object)(string.Format("Created {0} on {1}: layer {2}, slot {3}, height {4}, ", "SkinNameLabel", ((Object)slot).name, val3.layer, val4, num) + $"font {((TMP_Text)val).fontSizeMin}-{((TMP_Text)val).fontSizeMax}; rarity colors common {GameInfo.CommonColor}, " + $"rare {GameInfo.RareColor}, legendary {GameInfo.LegendaryColor}"));
			}
			return val;
		}

		public static Color RarityColor(Rarity rarity)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Invalid comparison between Unknown and I4
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			Color result;
			if ((int)rarity != 2)
			{
				if ((int)rarity != 3)
				{
					return Color.white;
				}
				result = GameInfo.LegendaryColor;
			}
			else
			{
				result = GameInfo.RareColor;
			}
			result.a = 1f;
			return result;
		}
	}
	[BepInPlugin("avidkaos.howtofish.slotmachineskinnames", "Slot Machine Skin Names", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "avidkaos.howtofish.slotmachineskinnames";

		public const string PluginName = "Slot Machine Skin Names";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> LogSlotText;

		private void Awake()
		{
			//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_004f: Expected O, but got Unknown
			//IL_0063: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			LogSlotText = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "LogSlotText", false, "Write each slot's skin name and text layout (size, font size, overflow) to the log when a roll starts.");
			GameFields.Resolve();
			Harmony val = new Harmony("avidkaos.howtofish.slotmachineskinnames");
			Apply(val, typeof(MachineSlotSetIndexPatch), "skin names can't be shown");
			Apply(val, typeof(SlotMachineOnRollFinishedPatch), "losing slots will keep their names after a roll");
			Log.LogInfo((object)"Slot Machine Skin Names 1.0.0 loaded");
		}

		private static void Apply(Harmony harmony, Type patch, string effect)
		{
			try
			{
				harmony.CreateClassProcessor(patch).Patch();
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Could not apply " + patch.Name + ", the game was probably updated: " + effect + ". " + ex.GetBaseException().Message));
			}
		}
	}
	internal static class PluginInfo
	{
		public const string Version = "1.0.0";
	}
}