Decompiled source of Glowing Signs v1.0.0

BepInEx/plugins/GlowingSigns/GlowingSigns.dll

Decompiled 20 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Glowing Signs")]
[assembly: AssemblyDescription("Restores glowing text on Valheim signs.")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Glowing Signs")]
[assembly: AssemblyCopyright("")]
[assembly: ComVisible(false)]
[assembly: Guid("b1922e24-9d65-48ca-a8e8-a0e1c4b28ea1")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 GlowingSigns
{
	[BepInPlugin("com.robert.glowingsigns", "Glowing Signs", "1.0.0")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.robert.glowingsigns";

		public const string PluginName = "Glowing Signs";

		public const string PluginVersion = "1.0.0";

		private Harmony? _harmony;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("com.robert.glowingsigns");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Glowing Signs 1.0.0 loaded. Restoring legacy color-matched sign glow.");
		}

		private void OnDestroy()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			LegacySignGlow.DisposeMaterials();
		}
	}
	internal static class LegacySignGlow
	{
		private const string LegacyShaderName = "TextMeshPro/Distance Field";

		private static readonly int FaceColorId = Shader.PropertyToID("_FaceColor");

		private static readonly Dictionary<int, Material> MaterialsByFontMaterial = new Dictionary<int, Material>();

		private static Shader? _legacyShader;

		private static bool _loggedSuccess;

		private static bool _loggedMissingShader;

		private static bool _loggedFailure;

		internal static void Apply(Sign sign)
		{
			try
			{
				TextMeshProUGUI textWidget = sign.m_textWidget;
				if ((Object)(object)textWidget == (Object)null)
				{
					return;
				}
				Shader val = FindLegacyShader();
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				TMP_FontAsset font = ((TMP_Text)textWidget).font;
				Material val2 = ((font != null) ? ((TMP_Asset)font).material : null);
				if ((Object)(object)val2 == (Object)null)
				{
					return;
				}
				Material orCreateLegacyMaterial = GetOrCreateLegacyMaterial(val2, val);
				if (!((Object)(object)((TMP_Text)textWidget).fontSharedMaterial == (Object)(object)orCreateLegacyMaterial))
				{
					((TMP_Text)textWidget).fontSharedMaterial = orCreateLegacyMaterial;
					((TMP_Text)textWidget).UpdateMeshPadding();
					((Graphic)textWidget).SetMaterialDirty();
					if (!_loggedSuccess)
					{
						_loggedSuccess = true;
						Plugin.Log.LogInfo((object)"Legacy sign glow is active; glow colors follow the displayed text colors.");
					}
				}
			}
			catch (Exception arg)
			{
				if (!_loggedFailure)
				{
					_loggedFailure = true;
					Plugin.Log.LogError((object)$"Could not restore sign glow: {arg}");
				}
			}
		}

		internal static void DisposeMaterials()
		{
			foreach (Material value in MaterialsByFontMaterial.Values)
			{
				if ((Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)value);
				}
			}
			MaterialsByFontMaterial.Clear();
			_legacyShader = null;
		}

		private static Material GetOrCreateLegacyMaterial(Material source, Shader legacyShader)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			int instanceID = ((Object)source).GetInstanceID();
			if (MaterialsByFontMaterial.TryGetValue(instanceID, out Material value) && (Object)(object)value != (Object)null)
			{
				return value;
			}
			value = new Material(source)
			{
				name = ((Object)source).name + " (Glowing Signs)",
				hideFlags = (HideFlags)61,
				shader = legacyShader
			};
			if (value.HasProperty(FaceColorId))
			{
				value.SetColor(FaceColorId, Color.white);
			}
			MaterialsByFontMaterial[instanceID] = value;
			return value;
		}

		private static Shader? FindLegacyShader()
		{
			if ((Object)(object)_legacyShader != (Object)null)
			{
				return _legacyShader;
			}
			_legacyShader = Shader.Find("TextMeshPro/Distance Field");
			if ((Object)(object)_legacyShader == (Object)null && !_loggedMissingShader)
			{
				_loggedMissingShader = true;
				Plugin.Log.LogError((object)"Required shader 'TextMeshPro/Distance Field' was not found. Sign glow cannot be restored.");
			}
			return _legacyShader;
		}
	}
	[HarmonyPatch(typeof(Sign))]
	internal static class SignPatches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void AwakePostfix(Sign __instance)
		{
			LegacySignGlow.Apply(__instance);
		}

		[HarmonyPatch("OnCheckPermissionCompleted")]
		[HarmonyPostfix]
		private static void PermissionCompletedPostfix(Sign __instance)
		{
			LegacySignGlow.Apply(__instance);
		}
	}
}