Decompiled source of AdvancedSigns v0.4.0

plugins/AdvancedSigns.dll

Decompiled 6 months ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("0.4.0.0")]
namespace AdvancedSigns;

[BepInPlugin("cjayride.AdvancedSigns", "Advanced Signs", "0.4.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Sign), "Awake")]
	private static class Sign_Awake_Patch
	{
		private static void Postfix(Sign __instance)
		{
			FixSign(ref __instance);
		}
	}

	[HarmonyPatch(typeof(Sign), "UpdateText")]
	private static class Sign_UpdateText_Patch
	{
		private static bool Prefix(Sign __instance, ZNetView ___m_nview)
		{
			string @string = ___m_nview.GetZDO().GetString(ZDOVars.s_text, __instance.m_defaultText);
			if (!string.IsNullOrEmpty(@string))
			{
				if (removeRichText.Value)
				{
					if (@string.Contains("<"))
					{
						@string = StringExtensionMethods.RemoveRichTextTags(@string);
						___m_nview.GetZDO().Set(ZDOVars.s_text, @string);
					}
				}
				else if (!@string.Contains("<") && !string.IsNullOrEmpty(defaultColor.Value))
				{
					@string = "<color=" + defaultColor.Value + ">" + @string;
					___m_nview.GetZDO().Set(ZDOVars.s_text, @string);
				}
			}
			return true;
		}

		private static void Postfix(Sign __instance)
		{
			FixSign(ref __instance);
		}
	}

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> useRichText;

	public static ConfigEntry<string> fontName;

	public static ConfigEntry<string> defaultColor;

	public static ConfigEntry<bool> removeRichText;

	public static ConfigEntry<Vector2> textPositionOffset;

	public static ConfigEntry<Vector3> signScale;

	public static TMP_FontAsset currentFont;

	public static string lastFontName;

	private void Awake()
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		signScale = ((BaseUnityPlugin)this).Config.Bind<Vector3>("Signs", "SignScale", new Vector3(1f, 1f, 1f), "Sign scale (w,h,d)");
		textPositionOffset = ((BaseUnityPlugin)this).Config.Bind<Vector2>("Signs", "TextPositionOffset", new Vector2(0f, 0f), "Default font size");
		useRichText = ((BaseUnityPlugin)this).Config.Bind<bool>("Signs", "UseRichText", true, "Enable rich text");
		fontName = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "FontName", "AveriaSerifLibre-Bold", "Font name");
		defaultColor = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "DefaultColor", "#00ffffff", "Default color");
		removeRichText = ((BaseUnityPlugin)this).Config.Bind<bool>("Signs", "RemoveRichText", false, "Remove rich text");
		if (modEnabled.Value)
		{
			currentFont = GetFont(fontName.Value, 20);
			TMP_FontAsset obj = currentFont;
			lastFontName = ((obj != null) ? ((Object)obj).name : null);
			harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
			harmony.PatchAll();
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchAll((string)null);
		}
	}

	private static void FixSign(ref Sign sign)
	{
		//IL_0019: 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)
		if (!modEnabled.Value)
		{
			return;
		}
		((Component)sign).transform.localScale = signScale.Value;
		((TMP_Text)sign.m_textWidget).richText = useRichText.Value;
		sign.m_characterLimit = 0;
		((Graphic)sign.m_textWidget).material = null;
		((Component)sign.m_textWidget).gameObject.GetComponent<RectTransform>().anchoredPosition = textPositionOffset.Value;
		if (lastFontName != fontName.Value)
		{
			lastFontName = fontName.Value;
			TMP_FontAsset font = GetFont(fontName.Value, 20);
			if ((Object)(object)font != (Object)null)
			{
				currentFont = font;
			}
		}
		if ((Object)(object)currentFont != (Object)null)
		{
			TMP_FontAsset font2 = ((TMP_Text)sign.m_textWidget).font;
			if (((font2 != null) ? ((Object)font2).name : null) != ((Object)currentFont).name)
			{
				((TMP_Text)sign.m_textWidget).font = currentFont;
			}
		}
	}

	private static TMP_FontAsset GetFont(string fontName, int fontSize)
	{
		TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
		foreach (TMP_FontAsset val in array)
		{
			if (((Object)val).name == fontName)
			{
				return val;
			}
		}
		return null;
	}
}