Decompiled source of Selective Pet Protection v0.1.1

SelectivePetProtection.dll

Decompiled 14 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace SelectivePetProtection
{
	public enum ShieldState
	{
		None,
		Stun,
		Immortal
	}
	public static class ShieldColors
	{
		public static Dictionary<ShieldState, string> Colors = new Dictionary<ShieldState, string>
		{
			{
				ShieldState.None,
				"#FFFFFF"
			},
			{
				ShieldState.Stun,
				"#95c9da"
			},
			{
				ShieldState.Immortal,
				"#d8bf58"
			}
		};

		public static void UpdateColors(string stunColor, string immortalColor)
		{
			Colors[ShieldState.Stun] = stunColor;
			Colors[ShieldState.Immortal] = immortalColor;
		}
	}
	[BepInPlugin("magnus.SelectivePetProtection", "SelectivePetProtection", "0.1.1")]
	public class SelectivePetProtection : BaseUnityPlugin
	{
		internal const string ModName = "SelectivePetProtection";

		internal const string ModVersion = "0.1.1";

		internal const string Author = "magnus";

		private const string ModGUID = "magnus.SelectivePetProtection";

		internal static ManualLogSource Log;

		public static Harmony harmony = new Harmony("mod.selective_pet_protection");

		private ConfigEntry<int> stunRecoveryTimeConfig;

		private ConfigEntry<string> stunShieldColorConfig;

		private ConfigEntry<string> immortalShieldColorConfig;

		public static float stunRecoveryTime { get; internal set; } = 120f;


		public static string stunShieldColor { get; internal set; } = "#95c9da";


		public static string immortalShieldColor { get; internal set; } = "#d8bf58";


		private void Awake()
		{
			stunRecoveryTimeConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", "StunRecoveryTime", 120, "Time in seconds it takes for a pet to recover from being stunned after receiving a deadly hit");
			stunRecoveryTime = stunRecoveryTimeConfig.Value;
			stunShieldColorConfig = ((BaseUnityPlugin)this).Config.Bind<string>("General", "StunShieldColor", "#95c9da", "Color as hex code of the shield icon that appears when a pet is protected and gets stunned when receiving a deadly hit (equivalent to essential in V+/PetProtection)");
			stunShieldColor = stunShieldColorConfig.Value;
			immortalShieldColorConfig = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ImmortalShieldColor", "#d8bf58", "Color as hex code of the shield icon that appears when a pet is immortal (equivalent to immortal in V+/PetProtection)");
			ShieldColors.UpdateColors(stunShieldColorConfig.Value, immortalShieldColorConfig.Value);
			harmony.PatchAll();
		}
	}
	[HarmonyPatch(typeof(Tameable), "RPC_SetName")]
	public static class Tameable_RPC_SetName_Patch
	{
		public static bool Prefix(ref Tameable __instance, ref string name)
		{
			ZDO zDO = __instance.m_character.m_nview.GetZDO();
			string @string = zDO.GetString(ZDOVars.s_tamedName, "");
			string text = StringExtensionMethods.RemoveRichTextTags(@string).Replace("\ud83d\udee1\ufe0f", "");
			if (name.Equals("!stun"))
			{
				zDO.Set(ZDOVars.s_tamedName, text + "<color=" + ShieldColors.Colors[ShieldState.Stun] + ">\ud83d\udee1\ufe0f</color>");
				return false;
			}
			if (name.Equals("!god"))
			{
				zDO.Set(ZDOVars.s_tamedName, text + "<color=" + ShieldColors.Colors[ShieldState.Immortal] + ">\ud83d\udee1\ufe0f</color>");
				return false;
			}
			if (name.Equals("!none"))
			{
				zDO.Set(ZDOVars.s_tamedName, text);
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Tameable), "SetName")]
	public static class Tameable_SetName_Patch
	{
		public static bool Prefix(ref Tameable __instance)
		{
			ZDO zDO = __instance.m_character.m_nview.GetZDO();
			string @string = zDO.GetString(ZDOVars.s_tamedName, "");
			if (@string.Contains("\ud83d\udee1\ufe0f"))
			{
				TextInput.instance.m_queuedSign = (TextReceiver)(object)__instance;
				TextInput.instance.Show("$hud_rename", StringExtensionMethods.RemoveRichTextTags(@string).Replace("\ud83d\udee1\ufe0f", ""), 10);
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Tameable), "GetHoverName")]
	public static class Tameable_GetHoverName_Patch
	{
		public static void Postfix(ref Tameable __instance, ref string __result)
		{
			ZDO zDO = __instance.m_character.m_nview.GetZDO();
			string @string = zDO.GetString(ZDOVars.s_tamedName, "");
			if (@string.Contains("\ud83d\udee1\ufe0f</color>"))
			{
				__result = @string;
			}
		}
	}
	[HarmonyPatch(typeof(Character), "ApplyDamage")]
	public static class Character_Damage_Patch
	{
		public static void Postfix(ref Character __instance, ref HitData hit, ref bool showDamageText, ref bool triggerEffects, ref DamageModifier mod)
		{
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			ZDO zDO = __instance.m_nview.GetZDO();
			Tameable component = ((Component)__instance).GetComponent<Tameable>();
			string @string = zDO.GetString(ZDOVars.s_tamedName, "");
			if (!__instance.IsTamed() || zDO == null || (Object)(object)component == (Object)null || !@string.Contains("\ud83d\udee1\ufe0f") || !(__instance.GetHealth() <= 5f) || !ShouldIgnoreDamage(__instance, hit, zDO))
			{
				return;
			}
			if (@string.Contains(ShieldColors.Colors[ShieldState.Stun]))
			{
				__instance.SetHealth(__instance.GetMaxHealth());
				__instance.m_animator.SetBool("sleeping", true);
				zDO.Set("sleeping", true);
				zDO.Set("isRecoveringFromStun", true);
				((Component)__instance).GetComponent<MonsterAI>().m_sleeping = true;
				((BaseAI)((Component)__instance).GetComponent<MonsterAI>()).SetAlerted(false);
				__instance.m_disableWhileSleeping = true;
				((BaseAI)((Component)__instance).GetComponent<MonsterAI>()).m_nview.GetZDO().Set(ZDOVars.s_sleeping, true);
				if (((BaseAI)((Component)__instance).GetComponent<MonsterAI>()).m_character.m_moveDir != Vector3.zero)
				{
					((BaseAI)((Component)__instance).GetComponent<MonsterAI>()).StopMoving();
				}
			}
			else
			{
				__instance.SetHealth(__instance.GetMaxHealth());
			}
		}

		private static bool ShouldIgnoreDamage(Character __instance, HitData hit, ZDO zdo)
		{
			if (hit == null)
			{
				return true;
			}
			Character attacker = hit.GetAttacker();
			if ((Object)(object)attacker == (Object)null)
			{
				return true;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(MonsterAI), "UpdateSleep")]
	public static class MonsterAI_UpdateSleep_Patch
	{
		public static bool Prefix(MonsterAI __instance, ref float dt)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			Tameable component = ((Component)__instance).GetComponent<Tameable>();
			if ((Object)(object)component == (Object)null)
			{
				return true;
			}
			ZDO zDO = ((BaseAI)__instance).m_nview.GetZDO();
			if (zDO == null || !zDO.GetBool("isRecoveringFromStun", false))
			{
				return true;
			}
			if (((BaseAI)__instance).m_character.m_moveDir != Vector3.zero)
			{
				((BaseAI)__instance).StopMoving();
			}
			if (__instance.m_sleepTimer != 0f)
			{
				__instance.m_sleepTimer = 0f;
			}
			float num = zDO.GetFloat("timeSinceStun", 0f) + dt;
			zDO.Set("timeSinceStun", num);
			if (num >= SelectivePetProtection.stunRecoveryTime)
			{
				zDO.Set("timeSinceStun", 0f);
				__instance.m_sleepTimer = 0.5f;
				((BaseAI)__instance).m_character.m_animator.SetBool("sleeping", false);
				zDO.Set("sleeping", false);
				zDO.Set("isRecoveringFromStun", false);
				__instance.Wakeup();
			}
			dt = 0f;
			return false;
		}
	}
	[HarmonyPatch(typeof(Tameable), "GetHoverText")]
	public static class Tameable_GetHoverText_Patch
	{
		public static void Postfix(Tameable __instance, ref string __result)
		{
			if (__instance.m_character.m_nview.GetZDO().GetBool("isRecoveringFromStun", false))
			{
				__result = __result.Insert(__result.IndexOf(" )"), ", Recovering");
			}
		}
	}
}