using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
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("SoDIconPack")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SoDIconPack")]
[assembly: AssemblyTitle("SoDIconPack")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 SoDIconPack
{
[BepInPlugin("sm3gm.sod.iconpack", "SoDIconPack", "1.0.0")]
public class Plugin : BasePlugin
{
public enum HideSlotBackgroundMode
{
None,
CustomOnly,
ItemSlots,
AllOccupied
}
public const string VER = "1.0.0";
public static string PluginDir;
private static readonly List<Object> Keep = new List<Object>();
public static readonly HashSet<int> Ours = new HashSet<int>();
public static ConfigEntry<HideSlotBackgroundMode> HideSlotBackground;
public static ConfigEntry<string> ReapplyKey;
internal static KeyCode ReapplyCode = (KeyCode)0;
public override void Load()
{
//IL_0102: Unknown result type (might be due to invalid IL or missing references)
HideSlotBackground = ((BasePlugin)this).Config.Bind<HideSlotBackgroundMode>("Display", "HideSlotBackground", HideSlotBackgroundMode.AllOccupied, "Hide the faint background shape behind inventory icons. None = vanilla. CustomOnly = only behind this pack's icons. ItemSlots = behind every held item. AllOccupied = also behind the fixed top slots (Coin, Fists, Watch, Print scanner). Empty slots always keep their cube.");
L("HideSlotBackground=" + HideSlotBackground.Value);
ReapplyKey = ((BasePlugin)this).Config.Bind<string>("Display", "ReapplyKey", "", "Optional key that loads the icons again without restarting, for example after adding PNG files (a Unity key name such as F8). Empty = off; the icons are applied automatically on start.");
if (!string.IsNullOrWhiteSpace(ReapplyKey.Value))
{
Enum.TryParse<KeyCode>(ReapplyKey.Value.Trim(), ignoreCase: true, out ReapplyCode);
}
PluginDir = Path.GetDirectoryName(typeof(Plugin).Assembly.Location);
try
{
Harmony.CreateAndPatchAll(typeof(BackgroundPatch), (string)null);
}
catch (Exception ex)
{
L("Harmony patch failed: " + ex);
}
((BasePlugin)this).AddComponent<PackWatcher>();
L("SoDIconPack 1.0.0 loaded. icons=" + Path.Combine(PluginDir, "icons") + (((int)ReapplyCode != 0) ? (" " + ((object)Unsafe.As<KeyCode, KeyCode>(ref ReapplyCode)/*cast due to .constrained prefix*/).ToString() + " reapplies.") : ""));
}
public static void ApplyIcons()
{
//IL_014a: Unknown result type (might be due to invalid IL or missing references)
//IL_0151: Expected O, but got Unknown
//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
int num = 0;
int num2 = 0;
int num3 = 0;
try
{
string text = Path.Combine(PluginDir, "icons");
if (!Directory.Exists(text))
{
L("icons folder missing: " + text);
L("summary applied=" + num + " missing=" + num2 + " failed=" + num3);
return;
}
Dictionary<string, InteractablePreset> dictionary = new Dictionary<string, InteractablePreset>();
Il2CppArrayBase<InteractablePreset> val = Resources.FindObjectsOfTypeAll<InteractablePreset>();
if (val != null)
{
foreach (InteractablePreset item in val)
{
if (!((SoCustomComparison)(object)item == (SoCustomComparison)null) && ((Object)item).name != null && !dictionary.ContainsKey(((Object)item).name))
{
dictionary[((Object)item).name] = item;
}
}
}
string[] files = Directory.GetFiles(text, "*.png");
foreach (string path in files)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
try
{
if (!dictionary.TryGetValue(fileNameWithoutExtension, out var value) || (SoCustomComparison)(object)value == (SoCustomComparison)null)
{
num2++;
L("missing preset=" + fileNameWithoutExtension);
continue;
}
byte[] array = File.ReadAllBytes(path);
Texture2D val2 = new Texture2D(2, 2, (TextureFormat)4, false);
Il2CppStructArray<byte> val3 = Il2CppStructArray<byte>.op_Implicit(array);
if (!ImageConversion.LoadImage(val2, val3))
{
Object.Destroy((Object)(object)val2);
throw new Exception("LoadImage returned false");
}
((Texture)val2).filterMode = (FilterMode)1;
((Texture)val2).wrapMode = (TextureWrapMode)1;
((Object)val2).hideFlags = (HideFlags)32;
int width = ((Texture)val2).width;
int height = ((Texture)val2).height;
float num4 = 100f * (float)width / 48f;
Sprite val4 = Sprite.Create(val2, new Rect(0f, 0f, (float)width, (float)height), new Vector2(0.5f, 0.5f), num4);
((Object)val4).hideFlags = (HideFlags)32;
Keep.Add((Object)(object)val2);
Keep.Add((Object)(object)val4);
Ours.Add(((Object)val4).GetInstanceID());
value.iconOverride = val4;
num++;
L("ok preset=" + fileNameWithoutExtension + " size=" + width + "x" + height + " pixelsPerUnit=" + num4);
}
catch (Exception ex)
{
num3++;
L("failed name=" + fileNameWithoutExtension + ": " + ex);
}
}
L("summary applied=" + num + " missing=" + num2 + " failed=" + num3);
}
catch (Exception ex2)
{
L("apply failed: " + ex2);
}
}
internal static void L(string msg)
{
Logger.CreateLogSource("IconPack").LogInfo((object)("[IconPack] " + msg));
}
}
public class PackWatcher : MonoBehaviour
{
private float Acc;
private bool Applied;
public PackWatcher(IntPtr ptr)
: base(ptr)
{
}
private void Update()
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
try
{
if ((int)Plugin.ReapplyCode != 0 && Input.GetKeyDown(Plugin.ReapplyCode))
{
Plugin.ApplyIcons();
}
}
catch (Exception ex)
{
Logger.CreateLogSource("IconPack").LogInfo((object)("[IconPack] reapply key: " + ex));
}
if (Applied)
{
return;
}
Acc += Time.unscaledDeltaTime;
if (Acc < 2f)
{
return;
}
Acc = 0f;
try
{
Il2CppArrayBase<InteractablePreset> val = Resources.FindObjectsOfTypeAll<InteractablePreset>();
int num = 0;
if (val != null)
{
foreach (InteractablePreset item in val)
{
if ((SoCustomComparison)(object)item != (SoCustomComparison)null)
{
num++;
}
}
}
if (num > 0 && !((Object)(object)InterfaceControls.Instance == (Object)null))
{
Applied = true;
Plugin.ApplyIcons();
}
}
catch (Exception ex2)
{
Logger.CreateLogSource("IconPack").LogInfo((object)("[IconPack] poll: " + ex2));
}
}
}
[HarmonyPatch(typeof(InventorySquareController), "OnUpdateContent")]
public static class BackgroundPatch
{
private static readonly HashSet<string> LoggedTypes = new HashSet<string>();
private static void Postfix(InventorySquareController __instance)
{
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
try
{
if ((Object)(object)__instance == (Object)null || ((Object)(object)((Component)__instance).gameObject != (Object)null && ((Object)((Component)__instance).gameObject).name == "DeselectButton"))
{
return;
}
Image background = ((ButtonController)__instance).background;
if (!((Object)(object)background == (Object)null))
{
Image icon = ((ButtonController)__instance).icon;
bool flag = false;
if ((Object)(object)icon != (Object)null && (Object)(object)icon.sprite != (Object)null)
{
flag = Plugin.Ours.Contains(((Object)icon.sprite).GetInstanceID());
}
bool flag2 = false;
bool flag3 = false;
InventorySlot slot = __instance.slot;
if (slot != null)
{
flag2 = slot.GetInteractable() != null;
flag3 = (int)slot.isStatic != 0 && (Object)(object)icon != (Object)null && (Object)(object)icon.sprite != (Object)null && ((Behaviour)icon).enabled;
}
if (!flag2 && !flag3)
{
((Behaviour)background).enabled = true;
}
else if (Plugin.HideSlotBackground != null)
{
Plugin.HideSlotBackgroundMode value = Plugin.HideSlotBackground.Value;
bool flag4 = (value == Plugin.HideSlotBackgroundMode.CustomOnly && flag) || (value == Plugin.HideSlotBackgroundMode.ItemSlots && flag2) || (value == Plugin.HideSlotBackgroundMode.AllOccupied && (flag2 || flag3));
((Behaviour)background).enabled = !flag4;
}
}
}
catch (Exception ex)
{
string name = ex.GetType().Name;
if (LoggedTypes.Add(name))
{
Plugin.L("BackgroundPatch " + name + ": " + ex.Message);
}
}
}
}
}