Decompiled source of PotatoRunes v1.0.1

plugins/PotatoRunes.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PotatoRunes;

[BepInPlugin("potato.runes", "Potato Runes", "1.0.1")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string Id = "potato.runes";

	public const string Version = "1.0.1";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<bool> Bold;

	internal static ConfigEntry<float> TextSize;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Font", "Runic hover text", true, "Draw the text that shows when you look at something in the font the game uses on signs.");
		TextSize = ((BaseUnityPlugin)this).Config.Bind<float>("1 - Font", "Text size", 1.15f, new ConfigDescription("Size of the hover text compared with the game's own. Raise it if the runic letters are hard to read.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.8f, 1.6f), Array.Empty<object>()));
		Bold = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Font", "Bold", false, "Draw the runic hover text thicker.");
		harmony = new Harmony("potato.runes");
		try
		{
			harmony.CreateClassProcessor(typeof(HoverFont)).Patch();
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Runic hover text is disabled: " + ex.GetBaseException().Message));
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}
[HarmonyPatch(typeof(Hud), "UpdateCrosshair")]
internal static class HoverFont
{
	private static bool failed;

	private static bool sameFont;

	private static TMP_FontAsset signFont;

	private static Material signMaterial;

	private static string tag;

	private static string tagFor;

	[HarmonyPriority(100)]
	private static void Postfix(Hud __instance)
	{
		if (failed || !Plugin.Enabled.Value)
		{
			return;
		}
		try
		{
			Apply(__instance);
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)("Runic hover text is disabled: " + ex.GetBaseException().Message));
			failed = true;
		}
	}

	private static void Apply(Hud hud)
	{
		TextMeshProUGUI hoverName = hud.m_hoverName;
		if (!Object.op_Implicit((Object)(object)hoverName) || !Object.op_Implicit((Object)(object)ZNetScene.instance))
		{
			return;
		}
		if (!Object.op_Implicit((Object)(object)signFont))
		{
			if (!FindSignFont())
			{
				failed = true;
				return;
			}
			sameFont = (Object)(object)signFont == (Object)(object)((TMP_Text)hoverName).font;
			if (sameFont)
			{
				Plugin.Log.LogInfo((object)("The hover text already uses the sign's font (" + ((Object)signFont).name + "), so nothing changes."));
			}
		}
		if (sameFont)
		{
			return;
		}
		string text = ((TMP_Text)hoverName).text;
		if (!string.IsNullOrEmpty(text))
		{
			string text2 = Tag();
			if (!text.StartsWith(text2, StringComparison.Ordinal))
			{
				((TMP_Text)hoverName).text = text2 + text;
			}
		}
	}

	private static string Tag()
	{
		int num = Mathf.RoundToInt(Plugin.TextSize.Value * 100f);
		bool value = Plugin.Bold.Value;
		Material val = ((Object.op_Implicit((Object)(object)signMaterial) && Object.op_Implicit((Object)(object)signFont.atlasTexture) && (Object)(object)signMaterial.mainTexture == (Object)(object)signFont.atlasTexture) ? signMaterial : null);
		string text = $"{num}|{value}|{(Object.op_Implicit((Object)(object)val) ? ((Object)val).GetInstanceID() : 0)}";
		if (tag != null && text == tagFor)
		{
			return tag;
		}
		string arg = "";
		if (Object.op_Implicit((Object)(object)val))
		{
			string text2 = "PotatoRunes" + ((Object)val).GetInstanceID();
			int hashCode = TMP_TextUtilities.GetHashCode(text2);
			Material val2 = default(Material);
			if (!MaterialReferenceManager.TryGetMaterial(hashCode, ref val2))
			{
				MaterialReferenceManager.AddFontMaterial(hashCode, val);
			}
			arg = " material=\"" + text2 + "\"";
		}
		tag = $"<font=\"{((Object)signFont).name}\"{arg}><size={num}%>" + (value ? "<b>" : "");
		tagFor = text;
		return tag;
	}

	private static bool FindSignFont()
	{
		GameObject prefab = ZNetScene.instance.GetPrefab("sign");
		Sign val = (Object.op_Implicit((Object)(object)prefab) ? prefab.GetComponent<Sign>() : null);
		if (!Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)val.m_textWidget) || !Object.op_Implicit((Object)(object)((TMP_Text)val.m_textWidget).font))
		{
			Plugin.Log.LogWarning((object)"Couldn't find the game's sign or its font, so the hover text keeps the game's own.");
			return false;
		}
		signFont = ((TMP_Text)val.m_textWidget).font;
		TMP_FontAsset val2 = default(TMP_FontAsset);
		if (!MaterialReferenceManager.TryGetFontAsset(((TMP_Asset)signFont).hashCode, ref val2))
		{
			try
			{
				MaterialReferenceManager.AddFontAsset(signFont);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Registering the sign's font: " + ex.GetBaseException().Message));
			}
		}
		TMP_Text.OnFontAssetRequest += (int hash, string name) => (!(name == ((Object)signFont).name)) ? null : signFont;
		List<Material> source = (from m in Resources.FindObjectsOfTypeAll<Material>()
			where Object.op_Implicit((Object)(object)m) && (Object)(object)m.mainTexture == (Object)(object)((TMP_Asset)signFont).material.mainTexture
			select m).ToList();
		signMaterial = ((IEnumerable<Material>)source).FirstOrDefault((Func<Material, bool>)((Material m) => ((Object)m).name == ((Object)signFont).name + " - Outline")) ?? ((IEnumerable<Material>)source).FirstOrDefault((Func<Material, bool>)((Material m) => ((Object)m).name.IndexOf("Outline", StringComparison.OrdinalIgnoreCase) >= 0));
		Plugin.Log.LogInfo((object)("Hover text font: " + ((Object)signFont).name + ", look: " + (Object.op_Implicit((Object)(object)signMaterial) ? ((Object)signMaterial).name : ((Object)((TMP_Asset)signFont).material).name) + "."));
		return true;
	}
}