Decompiled source of Open Dyslexic Font v1.1.0

OpenDyslexic.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using PluginConfig.API;
using PluginConfig.API.Fields;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TextCore;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("OpenDyslexic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OpenDyslexic")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("efb942a6-245b-4845-b951-fae8d9739db7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace OpenDyslexic;

public class FontTracker : MonoBehaviour
{
	public TextMeshProUGUI target;

	public TMP_FontAsset original;

	public void StartTracingEnabledStatus()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		OpenDyslexicPlugin.isEnabled.onValueChange += (BoolValueChangeEventDelegate)delegate(BoolValueChangeEvent bvce)
		{
			OpenDyslexicPlugin.isEnabled.value = bvce.value;
			if (!OpenDyslexicPlugin.isEnabled.value)
			{
				Revert();
			}
		};
	}

	public void Revert()
	{
		((TMP_Text)target).font = original;
		target.UpdateFontAsset();
		Object.DestroyImmediate((Object)(object)this);
	}

	public static FontTracker FindFor(TextMeshProUGUI text)
	{
		FontTracker[] components = ((Component)text).gameObject.GetComponents<FontTracker>();
		foreach (FontTracker fontTracker in components)
		{
			if ((Object)(object)fontTracker.target == (Object)(object)text)
			{
				return fontTracker;
			}
		}
		return null;
	}
}
[BepInPlugin("triggeredidiot.OpenDyslexic", "Open Dyslexic Font", "1.0.0")]
public class OpenDyslexicPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(TextMeshProUGUI))]
	public class OpenDyslexicPluginPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("Awake")]
		public static void Prefix_Awake(TextMeshProUGUI __instance)
		{
			PatchFont(__instance);
		}
	}

	private const string modGUID = "triggeredidiot.OpenDyslexic";

	private const string modName = "Open Dyslexic Font";

	private const string modVersion = "1.0.0";

	private static readonly Harmony Harmony = new Harmony("triggeredidiot.OpenDyslexic");

	private static AssetBundle ass;

	public static TMP_FontAsset OpenDyslexicFont;

	private PluginConfigurator config;

	public static BoolField isEnabled;

	public static FloatSliderField fontSize;

	public static AssetBundle LoadBundleAt(string path)
	{
		string text = path.Replace('/', '.').Replace('\\', '.');
		try
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string name = "OpenDyslexic." + text;
			using Stream stream = executingAssembly.GetManifestResourceStream(name);
			if (stream == null)
			{
				throw new Exception("Resource 'OpenDyslexic." + text + "' not found in embedded resources.");
			}
			byte[] array = new byte[stream.Length];
			stream.Read(array, 0, array.Length);
			return AssetBundle.LoadFromMemory(array);
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("Error loading OpenDyslexic." + text + ": " + ex.Message));
			try
			{
				Assembly executingAssembly2 = Assembly.GetExecutingAssembly();
				string name2 = "ULTRAKILLBepInExTemplate." + text;
				using Stream stream2 = executingAssembly2.GetManifestResourceStream(name2);
				if (stream2 == null)
				{
					Debug.LogError((object)("Resource 'ULTRAKILLBepInExTemplate." + text + "' not found in embedded resources."));
					return null;
				}
				byte[] array2 = new byte[stream2.Length];
				stream2.Read(array2, 0, array2.Length);
				return AssetBundle.LoadFromMemory(array2);
			}
			catch (Exception)
			{
				Debug.LogError((object)("Error loading ULTRAKILLBepInExTemplate." + text + ": " + ex.Message));
				return null;
			}
		}
	}

	public void ScanAndApplyFont()
	{
		TextMeshProUGUI[] array = Object.FindObjectsOfTypeAll(typeof(TextMeshProUGUI)) as TextMeshProUGUI[];
		for (int i = 0; i < array.Length; i++)
		{
			PatchFont(array[i]);
		}
	}

	public void ScanAndRefreshFont()
	{
		TextMeshProUGUI[] array = Object.FindObjectsOfTypeAll(typeof(TextMeshProUGUI)) as TextMeshProUGUI[];
		foreach (TextMeshProUGUI obj in array)
		{
			obj.UpdateFontAsset();
			((Graphic)obj).SetAllDirty();
		}
	}

	private void Awake()
	{
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Expected O, but got Unknown
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Expected O, but got Unknown
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Expected O, but got Unknown
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Expected O, but got Unknown
		Debug.Log((object)"Mod Open Dyslexic Font version 1.0.0 is loading...");
		ass = LoadBundleAt("opendyslexic.bundle");
		Debug.Log((object)"Loading OpenDyslexicFont");
		Object obj = ass.LoadAsset("OpenDyslexic.asset");
		OpenDyslexicFont = (TMP_FontAsset)(object)((obj is TMP_FontAsset) ? obj : null);
		Debug.Log((object)$"Got {OpenDyslexicFont} for our font asset!");
		SceneManager.sceneLoaded += delegate
		{
			ScanAndApplyFont();
		};
		Debug.Log((object)"Loading our PluginConfigurator...");
		config = PluginConfigurator.Create("Open Dyslexic Font", "triggeredidiot.OpenDyslexic");
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		config.SetIconWithURL("file://" + Path.Combine(directoryName, "icon.png"));
		isEnabled = new BoolField(config.rootPanel, "Enabled", "triggeredidiot.OpenDyslexic.enabled", true);
		fontSize = new FloatSliderField(config.rootPanel, "Font Size", "triggeredidiot.OpenDyslexic.fontsize", new Tuple<float, float>(0.25f, 2.25f), 1f);
		Debug.Log((object)"Loaded our PluginConfigurator...");
		Debug.Log((object)"Final loading stage...");
		isEnabled.onValueChange += (BoolValueChangeEventDelegate)delegate(BoolValueChangeEvent bvce)
		{
			isEnabled.value = bvce.value;
			if ((Object)(object)OpenDyslexicFont != (Object)null && isEnabled.value)
			{
				ScanAndApplyFont();
			}
		};
		fontSize.onValueChange += (OnValueChangeEventDelegate)delegate(FloatSliderValueChangeEvent changed)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			FieldInfo? field = typeof(TMP_FontAsset).GetField("m_FaceInfo", BindingFlags.Instance | BindingFlags.NonPublic);
			FaceInfo val = (FaceInfo)field.GetValue(OpenDyslexicFont);
			((FaceInfo)(ref val)).scale = changed.newValue * 0.66f;
			field.SetValue(OpenDyslexicFont, val);
			ScanAndRefreshFont();
		};
		Harmony.PatchAll();
		Debug.Log((object)"Mod Open Dyslexic Font version 1.0.0 is loaded!");
	}

	public static void PatchFont(TextMeshProUGUI UGUI)
	{
		if ((Object)(object)OpenDyslexicFont != (Object)null && isEnabled.value)
		{
			FontTracker fontTracker = FontTracker.FindFor(UGUI);
			if ((Object)(object)fontTracker != (Object)null)
			{
				fontTracker.Revert();
			}
			fontTracker = ((Component)UGUI).gameObject.AddComponent<FontTracker>();
			fontTracker.target = UGUI;
			fontTracker.original = ((TMP_Text)UGUI).font;
			((TMP_Text)UGUI).font = OpenDyslexicFont;
			UGUI.UpdateFontAsset();
			fontTracker.StartTracingEnabledStatus();
		}
	}
}