Decompiled source of CustomSprite v0.1.0

CustomSprite.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("CustomSprite")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+3fc55069f1219188464896238e5b9dc0c2183772")]
[assembly: AssemblyProduct("CustomSprite")]
[assembly: AssemblyTitle("CustomSprite")]
[assembly: AssemblyVersion("0.1.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 CustomSprite
{
	public class AssetLoader
	{
		public static string basePath = Path.Combine(Paths.ConfigPath, "CustomSprite");

		public static string assetFolder;

		public static string defaultFolder;

		public static void Init()
		{
			assetFolder = Path.Combine(basePath, CustomSprite.currSkinFolder ?? "Default");
			defaultFolder = Path.Combine(basePath, "Default");
			CustomSprite.Instance.Log("Load Directory:" + assetFolder);
			Directory.Exists(assetFolder);
		}

		public static Texture2D GetTexture(string FileName)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			Texture2D val = null;
			try
			{
				((Object)val).name = FileName;
				string text = Path.Combine(assetFolder, FileName + ".png");
				CustomSprite.Instance.Log(text);
				byte[] array = File.ReadAllBytes(text);
				val = new Texture2D(2, 2, (TextureFormat)4, false);
				ImageConversion.LoadImage(val, array);
			}
			catch (Exception ex)
			{
				CustomSprite.Instance.Log(ex.ToString());
			}
			return val;
		}

		public static string[] GetAllDirectories(string directory)
		{
			try
			{
				return Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly);
			}
			catch (Exception ex)
			{
				CustomSprite.Instance.Log("Failed to access directory " + directory + ": " + ex.Message);
				return Array.Empty<string>();
			}
		}
	}
	[BepInPlugin("Yukikaco.CustomSprite", "CustomSprite", "0.1.0")]
	public class CustomSprite : BaseUnityPlugin
	{
		private Harmony harmony;

		public ConfigEntry<bool> openFolder;

		private ConfigEntry<string?> skins;

		public static string currSkinFolder = "";

		public static CustomSprite Instance { get; private set; }

		private void Awake()
		{
			harmony = Harmony.CreateAndPatchAll(typeof(CustomSprite).Assembly, (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Yukikaco.CustomSprite has loaded!");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Paths.Config: " + Paths.ConfigPath));
			SetupConfig();
			Instance = this;
		}

		private void SetupConfig()
		{
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			openFolder = ((BaseUnityPlugin)this).Config.Bind<bool>("Folder", "Open CustomSprite Folder", false, "");
			skins = ((BaseUnityPlugin)this).Config.Bind<string>("Skin List", "Select Skin", (string)null, new ConfigDescription("", (AcceptableValueBase)(object)new AcceptableValueList<string>(AssetLoader.GetAllDirectories(Path.Combine(Paths.ConfigPath, "CustomSprite")).Select(Path.GetFileName).ToArray()), Array.Empty<object>()));
			currSkinFolder = skins.Value ?? "Default";
			skins.SettingChanged += delegate
			{
				currSkinFolder = skins.Value;
				InitializeAssets();
			};
			openFolder.SettingChanged += delegate
			{
				Process.Start(AssetLoader.basePath);
			};
		}

		private void Start()
		{
			AssetLoader.Init();
		}

		public static void HeroSprite()
		{
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Expected O, but got Unknown
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Expected O, but got Unknown
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Material[] materials = ((tk2dBaseSprite)((Component)HeroController.instance).GetComponent<tk2dSprite>()).Collection.materials;
			foreach (Material val in materials)
			{
				string name = ((Object)val.mainTexture).name;
				string path = Path.Combine(AssetLoader.defaultFolder, "Knight_" + name + ".png");
				if (!File.Exists(path))
				{
					Texture2D val2 = (Texture2D)((!val.mainTexture.isReadable) ? ((object)TextureUtils.Duplicate((Texture2D)val.mainTexture)) : ((object)(Texture2D)val.mainTexture));
					byte[] bytes = ImageConversion.EncodeToPNG(val2);
					if ((Object)(object)val2 != (Object)(object)val.mainTexture)
					{
						Object.Destroy((Object)(object)val2);
					}
					try
					{
						File.WriteAllBytes(path, bytes);
					}
					catch (IOException ex)
					{
						Instance.Log(ex.ToString());
					}
				}
				Texture2D val3 = new Texture2D(2, 2);
				string path2 = Path.Combine(AssetLoader.assetFolder, "Knight_" + name + ".png");
				if (File.Exists(path2))
				{
					byte[] array = File.ReadAllBytes(path2);
					val3 = new Texture2D(2, 2, (TextureFormat)4, false);
					((Object)val3).name = name;
					ImageConversion.LoadImage(val3, array);
					val.mainTexture = (Texture)(object)val3;
				}
			}
		}

		private void InitializeAssets()
		{
			AssetLoader.Init();
			HeroSprite();
		}

		public void Log(string msg)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)msg);
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
		}
	}
	[HarmonyPatch]
	public class Patches
	{
		[HarmonyPatch(typeof(HeroController), "Start")]
		[HarmonyPostfix]
		private static void HookHeroStart(ref HeroController __instance)
		{
			CustomSprite.HeroSprite();
		}
	}
	internal static class TextureUtils
	{
		public static Texture2D Duplicate(this Texture2D texture)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			RenderTexture temporary = RenderTexture.GetTemporary(((Texture)texture).width, ((Texture)texture).height, 0, (RenderTextureFormat)7, (RenderTextureReadWrite)1);
			Graphics.Blit((Texture)(object)texture, temporary);
			RenderTexture active = RenderTexture.active;
			RenderTexture.active = temporary;
			Texture2D val = new Texture2D(((Texture)texture).width, ((Texture)texture).height);
			val.ReadPixels(new Rect(0f, 0f, (float)((Texture)temporary).width, (float)((Texture)temporary).height), 0, 0);
			val.Apply();
			RenderTexture.active = active;
			RenderTexture.ReleaseTemporary(temporary);
			return val;
		}

		public static void SaveToFile(this Texture2D texture, string path)
		{
			byte[] buffer = ImageConversion.EncodeToPNG(texture);
			Directory.CreateDirectory(Path.GetDirectoryName(path));
			using FileStream output = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
			using BinaryWriter binaryWriter = new BinaryWriter(output);
			binaryWriter.Write(buffer);
			binaryWriter.Close();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "Yukikaco.CustomSprite";

		public const string PLUGIN_NAME = "CustomSprite";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}