Decompiled source of WhatAreThose v1.0.1

WhatAreThose.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("WhatAreThose")]
[assembly: AssemblyProduct("WhatAreThose")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: ComVisible(false)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[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 WhatAreThose
{
	internal static class IconManager
	{
		private static readonly Dictionary<string, string> _iconResources = new Dictionary<string, string>
		{
			["IceShoes"] = "WhatAreThose.Assets.iceshoes.png",
			["IceSkates"] = "WhatAreThose.Assets.iceskates.png"
		};

		private static readonly Dictionary<string, Sprite> _sprites = new Dictionary<string, Sprite>();

		private static readonly MethodInfo _loadImageMethod = GetLoadImageMethod();

		public static void ReplaceIcons(ObjectDB objectDB)
		{
			foreach (KeyValuePair<string, string> iconResource in _iconResources)
			{
				ReplaceIcon(objectDB, iconResource.Key, iconResource.Value);
			}
		}

		private static void ReplaceIcon(ObjectDB objectDB, string prefabName, string resourceName)
		{
			GameObject itemPrefab = objectDB.GetItemPrefab(prefabName);
			if (!Object.op_Implicit((Object)(object)itemPrefab))
			{
				WhatAreThose.LogWarning("Could not find prefab '" + prefabName + "'.");
				return;
			}
			ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
			if (!Object.op_Implicit((Object)(object)component) || component.m_itemData == null || component.m_itemData.m_shared == null)
			{
				WhatAreThose.LogWarning("Prefab '" + prefabName + "' has no item data.");
				return;
			}
			Sprite[] icons = component.m_itemData.m_shared.m_icons;
			if (icons == null || icons.Length == 0)
			{
				WhatAreThose.LogWarning("Prefab '" + prefabName + "' has no icon slot.");
				return;
			}
			Sprite orLoadSprite = GetOrLoadSprite(prefabName, resourceName, icons[0]);
			if (Object.op_Implicit((Object)(object)orLoadSprite))
			{
				Sprite[] array = (Sprite[])(object)new Sprite[icons.Length];
				for (int i = 0; i < array.Length; i++)
				{
					array[i] = orLoadSprite;
				}
				component.m_itemData.m_shared.m_icons = array;
				WhatAreThose.LogInfo("Replaced icon for '" + prefabName + "'.");
			}
		}

		private static Sprite GetOrLoadSprite(string prefabName, string resourceName, Sprite source)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			if (_sprites.TryGetValue(prefabName, out var value))
			{
				return value;
			}
			using Stream stream = typeof(IconManager).Assembly.GetManifestResourceStream(resourceName);
			if (stream == null)
			{
				WhatAreThose.LogWarning("Embedded icon '" + resourceName + "' is missing.");
				return null;
			}
			using MemoryStream memoryStream = new MemoryStream();
			stream.CopyTo(memoryStream);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			if (!LoadPng(val, memoryStream.ToArray()))
			{
				Object.Destroy((Object)(object)val);
				WhatAreThose.LogWarning("Could not decode embedded icon '" + resourceName + "'.");
				return null;
			}
			((Object)val).name = prefabName + "_Icon";
			((Texture)val).filterMode = (FilterMode)1;
			((Texture)val).wrapMode = (TextureWrapMode)1;
			float num = (Object.op_Implicit((Object)(object)source) ? source.pixelsPerUnit : 100f);
			value = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), num, 0u, (SpriteMeshType)0);
			((Object)value).name = prefabName + "_Icon";
			_sprites[prefabName] = value;
			return value;
		}

		private static bool LoadPng(Texture2D texture, byte[] pngBytes)
		{
			if (_loadImageMethod == null)
			{
				WhatAreThose.LogWarning("Could not find UnityEngine.ImageConversion.LoadImage().");
				return false;
			}
			try
			{
				return (bool)_loadImageMethod.Invoke(null, new object[3] { texture, pngBytes, true });
			}
			catch (Exception ex)
			{
				WhatAreThose.LogWarning("Could not decode the embedded icon: " + ex.Message);
				return false;
			}
		}

		private static MethodInfo GetLoadImageMethod()
		{
			try
			{
				Assembly assembly = Array.Find(AppDomain.CurrentDomain.GetAssemblies(), (Assembly assembly2) => assembly2.GetName().Name == "UnityEngine.ImageConversionModule");
				if (assembly == null)
				{
					assembly = Assembly.Load("UnityEngine.ImageConversionModule");
				}
				return assembly.GetType("UnityEngine.ImageConversion")?.GetMethod("LoadImage", BindingFlags.Static | BindingFlags.Public, null, new Type[3]
				{
					typeof(Texture2D),
					typeof(byte[]),
					typeof(bool)
				}, null);
			}
			catch (Exception ex)
			{
				WhatAreThose.LogWarning("Could not load the image conversion module: " + ex.Message);
				return null;
			}
		}
	}
	[HarmonyPatch(typeof(ObjectDB))]
	internal static class ObjectDBPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void AwakePostfix(ObjectDB __instance)
		{
			if (Object.op_Implicit((Object)(object)__instance))
			{
				IconManager.ReplaceIcons(__instance);
			}
		}
	}
	[BepInPlugin("miker.whatarethose", "WhatAreThose", "1.0.0")]
	public sealed class WhatAreThose : BaseUnityPlugin
	{
		public const string PluginGuid = "miker.whatarethose";

		public const string PluginName = "WhatAreThose";

		public const string PluginVersion = "1.0.0";

		private static ManualLogSource _logger;

		private void Awake()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "miker.whatarethose");
		}

		public static void LogInfo(object obj)
		{
			_logger.LogInfo((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}

		public static void LogWarning(object obj)
		{
			_logger.LogWarning((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}
	}
}