Decompiled source of ColorfulDamage v1.1.0

ColorfulDamage.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using ComfyLib;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ColorfulDamage")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ColorfulDamage")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("fbd74ee8-6f2b-49cc-84ec-aece295491e7")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
namespace ComfyLib
{
	public static class ConfigFileExtensions
	{
		internal sealed class ConfigurationManagerAttributes
		{
			public int? Order;
		}

		private static readonly Dictionary<string, int> _sectionToSettingOrder = new Dictionary<string, int>();

		private static int GetSettingOrder(string section)
		{
			if (!_sectionToSettingOrder.TryGetValue(section, out var value))
			{
				value = 0;
			}
			_sectionToSettingOrder[section] = value - 1;
			return value;
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, AcceptableValueBase acceptableValues)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Order = GetSettingOrder(section)
				}
			}));
		}
	}
	public static class FontCache
	{
		public static readonly string ValheimAveriaSansLibreFont = "Valheim-AveriaSansLibre";

		private static readonly Dictionary<string, TMP_FontAsset> _fontAssetCache = new Dictionary<string, TMP_FontAsset>();

		public static TMP_FontAsset ValheimAveriaSansLibreFontAsset => ((TMP_Text)UnifiedPopup.instance.bodyText).font;

		public static TMP_FontAsset GetFontAssetByName(string fontAssetName)
		{
			if (!_fontAssetCache.TryGetValue(fontAssetName, out var value))
			{
				value = (TMP_FontAsset)((fontAssetName == ValheimAveriaSansLibreFont) ? ((object)ValheimAveriaSansLibreFontAsset) : ((object)((IEnumerable<TMP_FontAsset>)Resources.FindObjectsOfTypeAll<TMP_FontAsset>()).FirstOrDefault((Func<TMP_FontAsset, bool>)((TMP_FontAsset f) => ((Object)f).name == fontAssetName))));
				_fontAssetCache[fontAssetName] = value;
			}
			return value;
		}
	}
}
namespace ColorfulDamage
{
	[BepInPlugin("redseiko.valheim.colorfuldamage", "ColorfulDamage", "1.1.0")]
	public class ColorfulDamage : BaseUnityPlugin
	{
		public const string PluginGuid = "redseiko.valheim.colorfuldamage";

		public const string PluginName = "ColorfulDamage";

		public const string PluginVersion = "1.1.0";

		private Harmony _harmony;

		private void Awake()
		{
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.colorfuldamage");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(DamageText))]
	internal static class DamageTextPatch
	{
		private static readonly Lazy<string> _msgTooHard = new Lazy<string>(() => Localization.m_instance.Localize("$msg_toohard"));

		private static readonly Lazy<string> _msgBlocked = new Lazy<string>(() => Localization.m_instance.Localize("$msg_blocked: "));

		[HarmonyPrefix]
		[HarmonyPatch("AddInworldText")]
		private static bool AddInworldText(ref DamageText __instance, TextType type, Vector3 pos, float distance, float dmg, bool mySelf)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected O, but got Unknown
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			if (!PluginConfig.IsModEnabled.Value)
			{
				return true;
			}
			WorldTextInstance val = new WorldTextInstance();
			__instance.m_worldTexts.Add(val);
			val.m_worldPos = pos + Random.insideUnitSphere * 0.5f;
			val.m_gui = Object.Instantiate<GameObject>(__instance.m_worldTextBase, ((Component)__instance).transform);
			val.m_textField = val.m_gui.GetComponent<TMP_Text>();
			val.m_textField.text = GetWorldTextText(type, dmg);
			((Graphic)val.m_textField).color = GetWorldTextColor(type, dmg, mySelf);
			val.m_textField.font = FontCache.GetFontAssetByName(PluginConfig.DamageTextMessageFont.Value);
			val.m_textField.fontSize = ((distance > PluginConfig.DamageTextSmallPopupDistance.Value) ? PluginConfig.DamageTextSmallFontSize.Value : PluginConfig.DamageTextLargeFontSize.Value);
			val.m_timer = 0f;
			return false;
		}

		private static Color GetWorldTextColor(TextType damageTextType, float damage, bool isPlayerDamage)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected I4, but got Unknown
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: 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_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: 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_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			if ((int)damageTextType == 4)
			{
				return PluginConfig.DamageTextHealColor.Value;
			}
			if (isPlayerDamage)
			{
				if (damage != 0f)
				{
					return PluginConfig.DamageTextPlayerDamageColor.Value;
				}
				return PluginConfig.DamageTextPlayerNoDamageColor.Value;
			}
			return (Color)((int)damageTextType switch
			{
				0 => PluginConfig.DamageTextNormalColor.Value, 
				1 => PluginConfig.DamageTextResistantColor.Value, 
				2 => PluginConfig.DamageTextWeakColor.Value, 
				3 => PluginConfig.DamageTextImmuneColor.Value, 
				4 => PluginConfig.DamageTextHealColor.Value, 
				5 => PluginConfig.DamageTextTooHardColor.Value, 
				6 => PluginConfig.DamageTextBlockedColor.Value, 
				_ => Color.white, 
			});
		}

		private static string GetWorldTextText(TextType damageTextType, float damage)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected I4, but got Unknown
			return (damageTextType - 4) switch
			{
				0 => "+" + damage.ToString("0.#", CultureInfo.InvariantCulture), 
				1 => _msgTooHard.Value, 
				2 => _msgBlocked.Value + damage.ToString("0.#", CultureInfo.InvariantCulture), 
				_ => damage.ToString("0.#", CultureInfo.InvariantCulture), 
			};
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateWorldTexts")]
		private static bool UpdateWorldTextsPrefix(ref DamageText __instance, float dt)
		{
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			if (!PluginConfig.IsModEnabled.Value)
			{
				return true;
			}
			Camera mainCamera = Utils.GetMainCamera();
			float num = Screen.width;
			float num2 = Screen.height;
			for (int num3 = __instance.m_worldTexts.Count - 1; num3 >= 0; num3--)
			{
				WorldTextInstance val = __instance.m_worldTexts[num3];
				val.m_timer += dt;
				if (val.m_timer > PluginConfig.DamageTextPopupDuration.Value)
				{
					Object.Destroy((Object)(object)val.m_gui);
					__instance.m_worldTexts[num3] = __instance.m_worldTexts[__instance.m_worldTexts.Count - 1];
					__instance.m_worldTexts.RemoveAt(__instance.m_worldTexts.Count - 1);
				}
				else
				{
					float num4 = val.m_timer / PluginConfig.DamageTextPopupDuration.Value;
					Vector3 val2 = Vector3.Lerp(val.m_worldPos, val.m_worldPos + PluginConfig.DamageTextPopupLerpPosition.Value, num4);
					Vector3 val3 = mainCamera.WorldToScreenPoint(val2);
					if (val3.x < 0f || val3.x > num || val3.y < 0f || val3.y > num2 || val3.z < 0f)
					{
						val.m_gui.SetActive(false);
					}
					else
					{
						Color color = ((Graphic)val.m_textField).color;
						color.a = 1f - num4 * num4 * num4;
						((Graphic)val.m_textField).color = color;
						val.m_gui.SetActive(true);
						val.m_gui.transform.position = val3;
					}
				}
			}
			return false;
		}

		[HarmonyTranspiler]
		[HarmonyPatch("RPC_DamageText")]
		private static IEnumerable<CodeInstruction> RpcDamageTextTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(DamageText), "m_maxTextDistance"), (string)null)
			}).Advance(2).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<float, float>>((Func<float, float>)MaxTextDistanceDelegate) })
				.InstructionEnumeration();
		}

		private static float MaxTextDistanceDelegate(float maxTextDistance)
		{
			if (!PluginConfig.IsModEnabled.Value)
			{
				return maxTextDistance;
			}
			return PluginConfig.DamageTextMaxPopupDistance.Value;
		}
	}
	public static class PluginConfig
	{
		[HarmonyPatch(typeof(FejdStartup))]
		private static class FejdStartupPatch
		{
			[HarmonyPostfix]
			[HarmonyPatch("Awake")]
			private static void AwakePostfix()
			{
				while (_fejdStartupBindConfigQueue.Count > 0)
				{
					_fejdStartupBindConfigQueue.Dequeue()?.Invoke();
				}
			}
		}

		private static readonly Queue<Action> _fejdStartupBindConfigQueue = new Queue<Action>();

		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<float> DamageTextPopupDuration { get; private set; }

		public static ConfigEntry<Vector3> DamageTextPopupLerpPosition { get; private set; }

		public static ConfigEntry<float> DamageTextMaxPopupDistance { get; private set; }

		public static ConfigEntry<float> DamageTextSmallPopupDistance { get; private set; }

		public static ConfigEntry<Color> DamageTextPlayerDamageColor { get; private set; }

		public static ConfigEntry<Color> DamageTextPlayerNoDamageColor { get; private set; }

		public static ConfigEntry<Color> DamageTextNormalColor { get; private set; }

		public static ConfigEntry<Color> DamageTextResistantColor { get; private set; }

		public static ConfigEntry<Color> DamageTextWeakColor { get; private set; }

		public static ConfigEntry<Color> DamageTextImmuneColor { get; private set; }

		public static ConfigEntry<Color> DamageTextHealColor { get; private set; }

		public static ConfigEntry<Color> DamageTextTooHardColor { get; private set; }

		public static ConfigEntry<Color> DamageTextBlockedColor { get; private set; }

		public static ConfigEntry<string> DamageTextMessageFont { get; private set; }

		public static ConfigEntry<int> DamageTextSmallFontSize { get; private set; }

		public static ConfigEntry<int> DamageTextLargeFontSize { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			IsModEnabled = config.BindInOrder("_Global", "isModEnabled", defaultValue: true, "Globally enable or disable this mod.");
			BindDamageTextPopupConfig(config);
			BindDamageTextColorConfig(config);
			_fejdStartupBindConfigQueue.Clear();
			_fejdStartupBindConfigQueue.Enqueue(delegate
			{
				BindDamageTextFontConfig(config);
			});
		}

		private static void BindDamageTextPopupConfig(ConfigFile config)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			DamageTextPopupDuration = config.BindInOrder("DamageText.Popup", "popupDuration", 1.5f, "Duration (in seconds) to show DamageText messages.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f));
			DamageTextPopupLerpPosition = config.BindInOrder<Vector3>("DamageText.Popup", "popupLerpPosition", new Vector3(0f, 1.5f, 0f), "Position (Vector3) offset to lerp the DamageText message to.");
			DamageTextMaxPopupDistance = config.BindInOrder("DamageText.Popup", "maxPopupDistance", 30f, "Maximum distance to popup ANY (small/ or large) DamageText messages.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 60f));
			DamageTextSmallPopupDistance = config.BindInOrder("DamageText.Popup", "smallPopupDistance", 10f, "Distance to popup DamageText messages using small (far-away) font size.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 60f));
		}

		private static void BindDamageTextColorConfig(ConfigFile config)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			DamageTextPlayerDamageColor = config.BindInOrder<Color>("DamageText.Color", "playerDamageColor", Color.red, "DamageText.color for damage to player > 0.");
			DamageTextPlayerNoDamageColor = config.BindInOrder<Color>("DamageText.Color", "playerNoDamageColor", Color.gray, "DamageText.color for damage to player = 0.");
			DamageTextNormalColor = config.BindInOrder<Color>("DamageText.Color", "normalColor", Color.white, "DamageText.color for TextType.Normal damage.");
			DamageTextResistantColor = config.BindInOrder<Color>("DamageText.Color", "resistantColor", new Color(0.6f, 0.6f, 0.6f, 1f), "DamageText.color for TextType.Resistant damage.");
			DamageTextWeakColor = config.BindInOrder<Color>("DamageText.Color", "weakColor", new Color(1f, 1f, 0f, 1f), "DamageText.color for TextType.Weak damage.");
			DamageTextImmuneColor = config.BindInOrder<Color>("DamageText.Color", "immuneColor", new Color(0.6f, 0.6f, 0.6f, 1f), "DamageText.color for TextType.Immune damage.");
			DamageTextHealColor = config.BindInOrder<Color>("DamageText.Color", "healColor", new Color(0.5f, 1f, 0.5f, 0.7f), "DamageText.color for TextType.Heal damage.");
			DamageTextTooHardColor = config.BindInOrder<Color>("DamageText.Color", "tooHardColor", new Color(0.8f, 0.7f, 0.7f, 1f), "DamageText.color for TextType.TooHard damage.");
			DamageTextBlockedColor = config.BindInOrder<Color>("DamageText.Color", "blockedColor", Color.white, "DamageText.color for TextType.Blocked damage.");
		}

		private static void BindDamageTextFontConfig(ConfigFile config)
		{
			string[] array = (from f in Resources.FindObjectsOfTypeAll<TMP_FontAsset>()
				select ((Object)f).name into f
				orderby f
				select f).ToArray();
			DamageTextMessageFont = config.BindInOrder("DamageText.Font", "messageFont", FontCache.ValheimAveriaSansLibreFont, "DamageText.font for all damage messages.", (AcceptableValueBase)(object)new AcceptableValueList<string>(array));
			if (DamageTextSmallFontSize == null)
			{
				DamageTextSmallFontSize = config.BindInOrder("DamageText.Font", "smallFontSize", 14, "DamageText.fontSize for small (far-away) damage messages.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 32));
			}
			DamageTextLargeFontSize = config.BindInOrder("DamageText.Font", "largeFontSize", 18, "DamageText.fontSize for large (nearby) damage messages.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 32));
		}
	}
}