Decompiled source of No Damage Flash v0.2.0

Plugins/VentureValheim.NoDamageFlash.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("VentureValheim.NoDamageFlash")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VentureValheim.NoDamageFlash")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("9296555D-4935-4C29-8E6F-1AE15530EE6B")]
[assembly: AssemblyFileVersion("0.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.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 VentureValheim.NoDamageFlash
{
	[BepInPlugin("com.orianaventure.mod.NoDamageFlash", "NoDamageFlash", "0.2.0")]
	public class NoDamageFlashPlugin : BaseUnityPlugin
	{
		private const string ModName = "NoDamageFlash";

		private const string ModVersion = "0.2.0";

		private const string Author = "com.orianaventure.mod";

		private const string ModGUID = "com.orianaventure.mod.NoDamageFlash";

		private static string ConfigFileName = "com.orianaventure.mod.NoDamageFlash.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony HarmonyInstance = new Harmony("com.orianaventure.mod.NoDamageFlash");

		public static readonly ManualLogSource NoDamageFlashLogger;

		private static ConfigEntry<bool> CE_RemoveAllFlash;

		private static ConfigEntry<bool> CE_RemoveDamageFlash;

		private static ConfigEntry<bool> CE_RemovePukeFlash;

		private static ConfigEntry<bool> CE_RemoveUseHealthFlash;

		public static bool GetRemoveAllFlash()
		{
			return CE_RemoveAllFlash.Value;
		}

		public static bool GetRemoveDamageFlash()
		{
			return CE_RemoveDamageFlash.Value;
		}

		public static bool GetRemovePukeFlash()
		{
			return CE_RemovePukeFlash.Value;
		}

		public static bool GetRemoveUseHealthFlash()
		{
			return CE_RemoveUseHealthFlash.Value;
		}

		private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
		{
			string extendedDescription = GetExtendedDescription(description, synced);
			configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, extendedDescription);
		}

		public string GetExtendedDescription(string description, bool synchronizedSetting)
		{
			return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]");
		}

		public void Awake()
		{
			AddConfig("RemoveAllFlash", "General", "True to disable all damage flash, set to false to use other configs (boolean).", synced: false, value: true, ref CE_RemoveAllFlash);
			AddConfig("RemoveDamageFlash", "General", "True to disable damage flash from recieving damage (boolean).", synced: false, value: false, ref CE_RemoveDamageFlash);
			AddConfig("RemovePukeFlash", "General", "True to disable damage flash from puking (boolean).", synced: false, value: false, ref CE_RemovePukeFlash);
			AddConfig("RemoveUseHealthFlash", "General", "True to disable damage flash from using blood magic weapons (boolean).", synced: false, value: false, ref CE_RemoveUseHealthFlash);
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				NoDamageFlashLogger.LogDebug((object)"Attempting to reload configuration...");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				NoDamageFlashLogger.LogError((object)("There was an issue loading " + ConfigFileName));
			}
		}

		static NoDamageFlashPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			NoDamageFlashLogger = Logger.CreateLogSource("NoDamageFlash");
			CE_RemoveAllFlash = null;
			CE_RemoveDamageFlash = null;
			CE_RemovePukeFlash = null;
			CE_RemoveUseHealthFlash = null;
		}
	}
	public class NoDamageFlash
	{
		[HarmonyPatch(typeof(Hud), "DamageFlash")]
		public static class Patch_Hud_DamageFlash
		{
			private static bool Prefix()
			{
				return !NoDamageFlashPlugin.GetRemoveAllFlash();
			}
		}

		[HarmonyPatch(typeof(Character), "UseHealth")]
		public static class Patch_Character_UseHealth
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Unknown result type (might be due to invalid IL or missing references)
				//IL_0037: Expected O, but got Unknown
				return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.Method(typeof(Character), "IsPlayer", (Type[])null, (Type[])null), (string)null)
				}).ThrowIfInvalid("Could not patch Player.UseHealth").Advance(1)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<bool, bool>>((Func<bool, bool>)FlashEnabledDelegate) })
					.InstructionEnumeration();
			}

			private static bool FlashEnabledDelegate(bool isPlayerResult)
			{
				if (NoDamageFlashPlugin.GetRemoveUseHealthFlash())
				{
					return false;
				}
				return isPlayerResult;
			}
		}

		[HarmonyPatch(typeof(Player), "OnDamaged")]
		public static class Patch_Player_OnDamaged
		{
			private static IEnumerable<CodeInstruction> Transpiler(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_0045: Unknown result type (might be due to invalid IL or missing references)
				//IL_004b: Expected O, but got Unknown
				//IL_0059: Unknown result type (might be due to invalid IL or missing references)
				//IL_005f: Expected O, but got Unknown
				//IL_0082: Unknown result type (might be due to invalid IL or missing references)
				//IL_0088: Expected O, but got Unknown
				//IL_0096: Unknown result type (might be due to invalid IL or missing references)
				//IL_009c: Expected O, but got Unknown
				//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b0: Expected O, but got Unknown
				return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_1, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.Method(typeof(HitData), "GetTotalDamage", (Type[])null, (Type[])null), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Call, (object)AccessTools.Method(typeof(Character), "GetMaxHealth", (Type[])null, (Type[])null), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Div, (object)null, (string)null)
				}).ThrowIfInvalid("Could not patch Player.OnDamaged").Advance(6)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<float, float, bool>>((Func<float, float, bool>)FlashEnabledDelegate) })
					.SetOpcodeAndAdvance(OpCodes.Brfalse_S)
					.InstructionEnumeration();
			}

			private static bool FlashEnabledDelegate(float totalDamage, float maxHealthTenth)
			{
				if (!NoDamageFlashPlugin.GetRemoveDamageFlash())
				{
					return totalDamage > maxHealthTenth;
				}
				return false;
			}
		}

		[HarmonyPatch(typeof(SE_Puke), "UpdateStatusEffect")]
		public static class Patch_SE_Puke_UpdateStatusEffect
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Unknown result type (might be due to invalid IL or missing references)
				//IL_0037: Expected O, but got Unknown
				return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.Method(typeof(Player), "RemoveOneFood", (Type[])null, (Type[])null), (string)null)
				}).ThrowIfInvalid("Could not patch SE_Puke.UpdateStatusEffect").Advance(1)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<bool, bool>>((Func<bool, bool>)FlashEnabledDelegate) })
					.InstructionEnumeration();
			}

			private static bool FlashEnabledDelegate(bool consumedFood)
			{
				if (NoDamageFlashPlugin.GetRemovePukeFlash())
				{
					return false;
				}
				return consumedFood;
			}
		}
	}
}