Decompiled source of Interpretation v1.1.3

Interpretation.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: AssemblyMetadata("Notice", "Copyright (c) casualexpie. All rights reserved. Do NOT decompile, disassemble, or reverse-engineer this assembly. Report bug symptoms on the Nexus Bugs page; do not post decompiled or reconstructed source. Reverse-engineering this DLL is not authorized.")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Interpretation;

[BepInPlugin("interpretation", "Interpretation", "1.1.1")]
public class Plugin : BaseUnityPlugin
{
	public const string Version = "1.1.1";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<bool> Expie;

	internal static ConfigEntry<bool> Milky;

	internal static ConfigEntry<bool> Dune;

	internal static ConfigEntry<bool> PauseMenu;

	internal static string Folder;

	private void Awake()
	{
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Use the Interpretation sprites (by PageIsForbidden).");
		Expie = ((BaseUnityPlugin)this).Config.Bind<bool>("Characters", "Expie", true, "Use the reworked sprites for Expie (the Experiment).");
		Milky = ((BaseUnityPlugin)this).Config.Bind<bool>("Characters", "Milky", true, "Use the reworked sprites for Milky.");
		Dune = ((BaseUnityPlugin)this).Config.Bind<bool>("Characters", "Dune", true, "Use Dune sprites, if a Dune folder exists in plugins\\Interpretation (none are made yet).");
		PauseMenu = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PauseMenuSwitch", true, "Show the Interpretation switch on the pause screen.");
		Folder = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "Interpretation");
		Packs.Load();
		new Harmony("interpretation").PatchAll(typeof(Plugin).Assembly);
		GameObject val = new GameObject("InterpretationSwitch");
		Object.DontDestroyOnLoad((Object)(object)val);
		((Object)val).hideFlags = (HideFlags)61;
		val.AddComponent<Switch>();
		Log.LogInfo((object)"Interpretation 1.1.1 loaded. Expie and Milky sprites by PageIsForbidden.");
	}

	internal static bool On(int chr)
	{
		if (!Enabled.Value)
		{
			return false;
		}
		return chr switch
		{
			0 => Expie.Value, 
			1 => Milky.Value, 
			2 => Dune.Value, 
			_ => false, 
		};
	}
}
internal static class Packs
{
	private static readonly Dictionary<string, Texture2D>[] tex = new Dictionary<string, Texture2D>[3]
	{
		new Dictionary<string, Texture2D>(),
		new Dictionary<string, Texture2D>(),
		new Dictionary<string, Texture2D>()
	};

	private static readonly Dictionary<Sprite, Sprite>[] made = new Dictionary<Sprite, Sprite>[3]
	{
		new Dictionary<Sprite, Sprite>(),
		new Dictionary<Sprite, Sprite>(),
		new Dictionary<Sprite, Sprite>()
	};

	private static readonly Dictionary<Sprite, Sprite>[] back = new Dictionary<Sprite, Sprite>[3]
	{
		new Dictionary<Sprite, Sprite>(),
		new Dictionary<Sprite, Sprite>(),
		new Dictionary<Sprite, Sprite>()
	};

	private static readonly HashSet<Sprite>[] miss = new HashSet<Sprite>[3]
	{
		new HashSet<Sprite>(),
		new HashSet<Sprite>(),
		new HashSet<Sprite>()
	};

	public static int Count(int chr)
	{
		if (chr < 0 || chr >= 3)
		{
			return 0;
		}
		return tex[chr].Count;
	}

	private static int CharOf(string dir)
	{
		string text = dir.ToLowerInvariant();
		if (text.Contains("milky") || text.Contains("milk"))
		{
			return 1;
		}
		if (text.Contains("dune"))
		{
			return 2;
		}
		if (text.Contains("expie") || text.Contains("exp") || text.Contains("experiment"))
		{
			return 0;
		}
		return -1;
	}

	private static void Add(int chr, string key, byte[] data)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Expected O, but got Unknown
		Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
		if (ImageConversion.LoadImage(val, data, false))
		{
			((Texture)val).filterMode = (FilterMode)0;
			((Texture)val).wrapMode = (TextureWrapMode)1;
			((Object)val).name = key;
			Object.DontDestroyOnLoad((Object)(object)val);
			tex[chr][key] = val;
		}
	}

	public static void Load()
	{
		//IL_0205: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: Expected O, but got Unknown
		Assembly assembly = typeof(Packs).Assembly;
		string[] manifestResourceNames = assembly.GetManifestResourceNames();
		foreach (string text in manifestResourceNames)
		{
			if (!text.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
			{
				continue;
			}
			string[] array = text.Split('.');
			if (array.Length < 4)
			{
				continue;
			}
			int num = CharOf(array[1]);
			if (num < 0)
			{
				continue;
			}
			try
			{
				using Stream stream = assembly.GetManifestResourceStream(text);
				byte[] array2 = new byte[stream.Length];
				int num2;
				for (int j = 0; j < array2.Length; j += num2)
				{
					num2 = stream.Read(array2, j, array2.Length - j);
					if (num2 <= 0)
					{
						break;
					}
				}
				Add(num, string.Join(".", array, 2, array.Length - 3).ToLowerInvariant(), array2);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Couldn't load built-in sprite " + text + ": " + ex.Message));
			}
		}
		for (int k = 0; k < 2; k++)
		{
			Plugin.Log.LogInfo((object)("Built-in sprites for " + ((k == 0) ? "Expie" : "Milky") + ": " + tex[k].Count + "."));
		}
		if (!Directory.Exists(Plugin.Folder))
		{
			return;
		}
		string[] directories = Directory.GetDirectories(Plugin.Folder);
		foreach (string path in directories)
		{
			int num3 = CharOf(Path.GetFileName(path));
			if (num3 < 0)
			{
				Plugin.Log.LogWarning((object)("Skipped folder " + Path.GetFileName(path) + ": name it Expie, Milky or Dune."));
				continue;
			}
			string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
			foreach (string text2 in files)
			{
				if (!text2.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
				{
					continue;
				}
				try
				{
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					if (ImageConversion.LoadImage(val, File.ReadAllBytes(text2), false))
					{
						((Texture)val).filterMode = (FilterMode)0;
						((Texture)val).wrapMode = (TextureWrapMode)1;
						string key = (((Object)val).name = Path.GetFileNameWithoutExtension(text2).ToLowerInvariant());
						Object.DontDestroyOnLoad((Object)(object)val);
						tex[num3][key] = val;
					}
				}
				catch (Exception ex2)
				{
					Plugin.Log.LogWarning((object)("Couldn't load " + text2 + ": " + ex2.Message));
				}
			}
			Plugin.Log.LogInfo((object)string.Concat("Sprites for ", num3 switch
			{
				1 => "Milky", 
				0 => "Expie", 
				_ => "Dune", 
			}, " with the plugins\\Interpretation folder added: ", tex[num3].Count, "."));
		}
	}

	public static Sprite Swap(int chr, Sprite s)
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: 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_00a0: 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_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)s == (Object)null || chr < 0 || chr > 2)
		{
			return null;
		}
		if (made[chr].TryGetValue(s, out var value))
		{
			return value;
		}
		if (back[chr].ContainsKey(s) || miss[chr].Contains(s))
		{
			return null;
		}
		if (!tex[chr].TryGetValue(((Object)s).name.ToLowerInvariant(), out var value2))
		{
			miss[chr].Add(s);
			return null;
		}
		Rect rect = s.rect;
		float num;
		if (!(((Rect)(ref rect)).width > 0f))
		{
			num = 0.5f;
		}
		else
		{
			float x = s.pivot.x;
			Rect rect2 = s.rect;
			num = x / ((Rect)(ref rect2)).width;
		}
		Rect rect3 = s.rect;
		float num2;
		if (!(((Rect)(ref rect3)).height > 0f))
		{
			num2 = 0.5f;
		}
		else
		{
			float y = s.pivot.y;
			Rect rect4 = s.rect;
			num2 = y / ((Rect)(ref rect4)).height;
		}
		Vector2 val = default(Vector2);
		((Vector2)(ref val))..ctor(num, num2);
		value = Sprite.Create(value2, new Rect(0f, 0f, (float)((Texture)value2).width, (float)((Texture)value2).height), val, s.pixelsPerUnit, 0u, (SpriteMeshType)0);
		((Object)value).name = ((Object)s).name;
		Object.DontDestroyOnLoad((Object)(object)value);
		made[chr][s] = value;
		back[chr][value] = s;
		return value;
	}

	public static Sprite Undo(int chr, Sprite s)
	{
		if ((Object)(object)s == (Object)null || chr < 0 || chr > 2)
		{
			return null;
		}
		if (!back[chr].TryGetValue(s, out var value))
		{
			return null;
		}
		return value;
	}

	public static Sprite Convert(int chr, Sprite s, bool fwd)
	{
		if (!fwd)
		{
			return Undo(chr, s);
		}
		return Swap(chr, s);
	}
}
internal class Reworker : MonoBehaviour
{
	public Body body;

	private int chr = -1;

	private SpriteRenderer[] renderers = (SpriteRenderer[])(object)new SpriteRenderer[0];

	private float rescan;

	private bool applied;

	private void Start()
	{
		if ((Object)(object)body == (Object)null)
		{
			body = ((Component)this).GetComponent<Body>();
		}
		if ((Object)(object)body == (Object)null)
		{
			Object.Destroy((Object)(object)this);
			return;
		}
		chr = body.charType;
		if (Packs.Count(chr) == 0)
		{
			Object.Destroy((Object)(object)this);
			return;
		}
		applied = Plugin.On(chr);
		Scan(applied ? 1 : 0);
	}

	private Transform Root()
	{
		Transform val = ((Component)body).transform;
		while ((Object)(object)val.parent != (Object)null && ((Component)val.parent).GetComponentsInChildren<Body>(true).Length == 1 && ((Component)val.parent).GetComponentsInChildren<SpriteRenderer>(true).Length <= 150)
		{
			val = val.parent;
		}
		return val;
	}

	private void Scan(int fields)
	{
		Transform val = Root();
		renderers = ((Component)val).GetComponentsInChildren<SpriteRenderer>(true);
		if (fields < 0)
		{
			return;
		}
		bool flag = fields == 1;
		int num = 0;
		MonoBehaviour[] componentsInChildren = ((Component)val).GetComponentsInChildren<MonoBehaviour>(true);
		foreach (MonoBehaviour val2 in componentsInChildren)
		{
			if (!((Object)(object)val2 == (Object)null) && !(val2 is Reworker))
			{
				Type type = ((object)val2).GetType();
				if (!(type.Assembly != typeof(Body).Assembly) && !((Object)(object)((Component)val2).GetComponentInParent<Item>() != (Object)null))
				{
					num += Fix(val2, type, 0, flag);
				}
			}
		}
		if (num > 0)
		{
			Plugin.Log.LogInfo((object)((flag ? "Interpretation" : "Vanilla") + " sprites on " + ((Object)val).name + " (" + num + " fields)."));
		}
	}

	private int Fix(object o, Type ty, int depth, bool fwd)
	{
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Expected O, but got Unknown
		int num = 0;
		Type type = ty;
		while (type != null && type != typeof(MonoBehaviour) && type != typeof(object) && type != typeof(ValueType))
		{
			FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo in fields)
			{
				Type fieldType = fieldInfo.FieldType;
				try
				{
					if (fieldType == typeof(Sprite))
					{
						int num2 = chr;
						object? value = fieldInfo.GetValue(o);
						Sprite val = Packs.Convert(num2, (Sprite)((value is Sprite) ? value : null), fwd);
						if ((Object)(object)val != (Object)null)
						{
							fieldInfo.SetValue(o, val);
							num++;
						}
					}
					else
					{
						if (fieldType == typeof(Sprite[]))
						{
							if (!(fieldInfo.GetValue(o) is Sprite[] array))
							{
								continue;
							}
							for (int j = 0; j < array.Length; j++)
							{
								Sprite val2 = Packs.Convert(chr, array[j], fwd);
								if ((Object)(object)val2 != (Object)null)
								{
									array[j] = val2;
									num++;
								}
							}
							continue;
						}
						if (depth != 0 || !typeof(IList).IsAssignableFrom(fieldType) || !(fieldInfo.GetValue(o) is IList list))
						{
							continue;
						}
						for (int k = 0; k < list.Count; k++)
						{
							object obj = list[k];
							if (obj == null)
							{
								continue;
							}
							if (obj is Sprite)
							{
								Sprite val3 = Packs.Convert(chr, (Sprite)obj, fwd);
								if ((Object)(object)val3 != (Object)null)
								{
									list[k] = val3;
									num++;
								}
								continue;
							}
							Type type2 = obj.GetType();
							if (!type2.IsPrimitive && !(obj is Object) && !(obj is string))
							{
								int num3 = Fix(obj, type2, 1, fwd);
								if (num3 > 0)
								{
									list[k] = obj;
									num += num3;
								}
								continue;
							}
							break;
						}
						continue;
					}
				}
				catch (Exception)
				{
				}
			}
			type = type.BaseType;
		}
		return num;
	}

	private void LateUpdate()
	{
		if (chr < 0)
		{
			return;
		}
		bool flag = Plugin.On(chr);
		if (flag != applied)
		{
			applied = flag;
			Scan(flag ? 1 : 0);
			rescan = 2f;
		}
		rescan -= Time.unscaledDeltaTime;
		if (rescan <= 0f)
		{
			rescan = 2f;
			Scan(-1);
		}
		for (int i = 0; i < renderers.Length; i++)
		{
			SpriteRenderer val = renderers[i];
			if (!((Object)(object)val == (Object)null))
			{
				Sprite val2 = Packs.Convert(chr, val.sprite, applied);
				if ((Object)(object)val2 != (Object)null)
				{
					val.sprite = val2;
				}
			}
		}
	}
}
[HarmonyPatch(typeof(Body), "Start")]
internal static class BodyStartPatch
{
	private static void Postfix(Body __instance)
	{
		try
		{
			if (Packs.Count(__instance.charType) != 0 && (Object)(object)((Component)__instance).GetComponent<Reworker>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<Reworker>().body = __instance;
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Couldn't add the sprite rework: " + ex.Message));
		}
	}
}
internal class Switch : MonoBehaviour
{
	private class Extra
	{
		public string Label;

		public ConfigEntry<bool> Entry;
	}

	private readonly List<Extra> extras = new List<Extra>();

	private bool looked;

	private GUIStyle box;

	private GUIStyle title;

	private GUIStyle button;

	private GUIStyle small;

	private static FieldInfo slotOpen;

	private static bool slotLooked;

	private void Look()
	{
		looked = true;
		Add("hatshatshats", "Fit hats to the reskin");
		Add("iregretnothing", "Fit the grenade to the reskin");
	}

	private void Add(string guid, string label)
	{
		ConfigEntry<bool> entry = default(ConfigEntry<bool>);
		if (Chainloader.PluginInfos.TryGetValue(guid, out var value) && value != null && !((Object)(object)value.Instance == (Object)null) && value.Instance.Config.TryGetEntry<bool>("Interpretation", "FitToReskin", ref entry))
		{
			Extra extra = new Extra();
			extra.Label = label;
			extra.Entry = entry;
			extras.Add(extra);
		}
	}

	private void Styles()
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Expected O, but got Unknown
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Expected O, but got Unknown
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0114: Expected O, but got Unknown
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Expected O, but got Unknown
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		if (box == null)
		{
			Texture2D val = new Texture2D(1, 1);
			val.SetPixel(0, 0, new Color(0.05f, 0.05f, 0.06f, 0.82f));
			val.Apply();
			((Object)val).hideFlags = (HideFlags)61;
			box = new GUIStyle(GUI.skin.box);
			box.normal.background = val;
			box.padding = new RectOffset(14, 14, 10, 12);
			title = new GUIStyle(GUI.skin.label);
			title.fontSize = 20;
			title.fontStyle = (FontStyle)1;
			title.normal.textColor = new Color(1f, 0.62f, 0.25f);
			button = new GUIStyle(GUI.skin.button);
			button.fontSize = 17;
			button.alignment = (TextAnchor)3;
			button.padding = new RectOffset(10, 10, 6, 6);
			small = new GUIStyle(GUI.skin.label);
			small.fontSize = 13;
			small.normal.textColor = new Color(0.75f, 0.75f, 0.78f);
		}
	}

	private static bool Covered()
	{
		if (!slotLooked)
		{
			slotLooked = true;
			if (Chainloader.PluginInfos.TryGetValue("saveanywhere", out var value) && value != null && (Object)(object)value.Instance != (Object)null)
			{
				Type type = ((object)value.Instance).GetType().Assembly.GetType("SaveAnywhere.SlotScreen");
				if (type != null)
				{
					slotOpen = type.GetField("Open", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				}
			}
		}
		try
		{
			return slotOpen != null && (bool)slotOpen.GetValue(null);
		}
		catch (Exception)
		{
			return false;
		}
	}

	private static string Mark(bool on)
	{
		if (!on)
		{
			return "[OFF] ";
		}
		return "[ON]  ";
	}

	private void OnGUI()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//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_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
		if (!Plugin.PauseMenu.Value || !PauseHandler.paused || Covered())
		{
			return;
		}
		if (!looked)
		{
			Look();
		}
		Styles();
		Matrix4x4 matrix = GUI.matrix;
		float num = (float)Screen.height / 1080f;
		GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(num, num, 1f));
		float num2 = 360f;
		float num3 = 152f + (float)extras.Count * 40f + ((extras.Count > 0) ? 8f : 0f);
		float num4 = 1080f - num3 - 24f;
		GUILayout.BeginArea(new Rect(24f, num4, num2, num3), box);
		GUILayout.Label("INTERPRETATION", title, (GUILayoutOption[])(object)new GUILayoutOption[0]);
		if (GUILayout.Button(Plugin.Enabled.Value ? "Sprites: Interpretation" : "Sprites: Vanilla", button, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(36f) }))
		{
			Plugin.Enabled.Value = !Plugin.Enabled.Value;
		}
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUI.enabled = Plugin.Enabled.Value;
		if (GUILayout.Button(Mark(Plugin.Expie.Value) + "Expie", button, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(34f) }))
		{
			Plugin.Expie.Value = !Plugin.Expie.Value;
		}
		if (GUILayout.Button(Mark(Plugin.Milky.Value) + "Milky", button, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(34f) }))
		{
			Plugin.Milky.Value = !Plugin.Milky.Value;
		}
		GUI.enabled = true;
		GUILayout.EndHorizontal();
		if (extras.Count > 0)
		{
			GUILayout.Space(8f);
		}
		foreach (Extra extra in extras)
		{
			if (GUILayout.Button(Mark(extra.Entry.Value) + extra.Label, button, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(34f) }))
			{
				extra.Entry.Value = !extra.Entry.Value;
			}
		}
		GUILayout.Label("Sprites by PageIsForbidden", small, (GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.EndArea();
		GUI.matrix = matrix;
	}
}