Decompiled source of DamageNerf v1.0.1

DamageNerf.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using DamageNerf.Core;
using DamageNerf.Core.Config;
using HarmonyLib;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Loader), "DamageNerf", "1.0.0", "notfishvr", null)]
[assembly: MelonGame("ReLUGames", "MIMESIS")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DamageNerf")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+7e4bf983e4fa5f187e2165ae73f1e26674954c8a")]
[assembly: AssemblyProduct("DamageNerf")]
[assembly: AssemblyTitle("DamageNerf")]
[assembly: AssemblyVersion("1.0.0.0")]
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 DamageNerf.Core
{
	public class Loader : MelonMod
	{
		private GameObject gui;

		private MainGUI mainGUI;

		public override void OnInitializeMelon()
		{
		}

		public override void OnSceneWasLoaded(int buildIndex, string sceneName)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			if ((Object)(object)gui == (Object)null)
			{
				gui = new GameObject("MainGUI");
				Object.DontDestroyOnLoad((Object)(object)gui);
				mainGUI = gui.AddComponent<MainGUI>();
			}
		}
	}
	public class MainGUI : MonoBehaviour
	{
		private ConfigManager configManager;

		private void Start()
		{
			configManager = new ConfigManager();
			Patches.ApplyPatches(configManager);
		}
	}
	public static class Patches
	{
		[HarmonyPatch(typeof(StatManager), "ApplyDamage")]
		public static class StatManagerApplyDamagePatch
		{
			public static void Prefix(ref ApplyDamageArgs args)
			{
				//IL_003f: Unknown result type (might be due to invalid IL or missing references)
				//IL_005c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0068: Unknown result type (might be due to invalid IL or missing references)
				//IL_006e: Expected O, but got Unknown
				if (enabled && args != null)
				{
					VActor attacker = args.Attacker;
					VMonster val = (VMonster)(object)((attacker is VMonster) ? attacker : null);
					if (val != null)
					{
						long damage = args.Damage;
						long num = (long)((float)damage * damageMultiplier);
						args = new ApplyDamageArgs(args.Attacker, args.Victim, args.MutableStatChangeCause, num, args.GrogyValue, args.SkillMasterID, args.SkillSequenceID, args.HitType, args.SkillName);
					}
				}
			}
		}

		private static ConfigManager configManager;

		private static float damageMultiplier = 0.5f;

		private static bool enabled = true;

		public static void ApplyPatches(ConfigManager config)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			try
			{
				configManager = config;
				LoadConfig();
				Harmony val = new Harmony("com.Mimesis.DamageNerf");
				val.PatchAll(typeof(Patches).Assembly);
				MelonLogger.Msg("Harmony patches applied successfully");
			}
			catch (Exception ex)
			{
				MelonLogger.Error("Error applying patches: " + ex.Message);
			}
		}

		private static void LoadConfig()
		{
			if (configManager == null)
			{
				MelonLogger.Error("ConfigManager is null! Using default settings");
				damageMultiplier = 0.5f;
				enabled = true;
				return;
			}
			enabled = configManager.GetValue("Enabled", defaultValue: true, "Enable or disable DamageNerf mod");
			damageMultiplier = configManager.GetValue("DamageMultiplier", 0.5f, "Monster damage multiplier (0.0 = no damage, 1.0 = full damage)");
			if (damageMultiplier < 0f)
			{
				MelonLogger.Warning($"DamageMultiplier {damageMultiplier} is below 0.0, clamping to 0.0");
				damageMultiplier = 0f;
			}
			if (damageMultiplier > 1f)
			{
				MelonLogger.Warning($"DamageMultiplier {damageMultiplier} is above 1.0, clamping to 1.0");
				damageMultiplier = 1f;
			}
		}
	}
}
namespace DamageNerf.Core.Config
{
	public class ConfigManager
	{
		private const string MAIN_CATEGORY = "DamageNerf";

		private const string HOTKEYS_CATEGORY = "DamageNerf Hotkeys";

		private MelonPreferences_Category mainCategory;

		private MelonPreferences_Category hotkeysCategory;

		private Dictionary<string, MelonPreferences_Entry<string>> stringEntries = new Dictionary<string, MelonPreferences_Entry<string>>();

		private Dictionary<string, MelonPreferences_Entry<bool>> boolEntries = new Dictionary<string, MelonPreferences_Entry<bool>>();

		private Dictionary<string, MelonPreferences_Entry<float>> floatEntries = new Dictionary<string, MelonPreferences_Entry<float>>();

		private Dictionary<string, MelonPreferences_Entry<string>> hotkeyEntries = new Dictionary<string, MelonPreferences_Entry<string>>();

		public ConfigManager()
		{
			mainCategory = MelonPreferences.CreateCategory("DamageNerf", "DamageNerf Configuration");
			hotkeysCategory = MelonPreferences.CreateCategory("DamageNerf Hotkeys", "DamageNerf Hotkey Configuration");
		}

		public void LoadAllConfigs()
		{
		}

		public T GetValue<T>(string key, T defaultValue, string description = "")
		{
			try
			{
				if (typeof(T) == typeof(string))
				{
					if (!stringEntries.TryGetValue(key, out MelonPreferences_Entry<string> value))
					{
						value = mainCategory.CreateEntry<string>(key, (string)(object)defaultValue, key, description, false, false, (ValueValidator)null, (string)null);
						stringEntries[key] = value;
					}
					return (T)(object)value.Value;
				}
				if (typeof(T) == typeof(bool))
				{
					if (!boolEntries.TryGetValue(key, out MelonPreferences_Entry<bool> value2))
					{
						value2 = mainCategory.CreateEntry<bool>(key, (bool)(object)defaultValue, key, description, false, false, (ValueValidator)null, (string)null);
						boolEntries[key] = value2;
					}
					return (T)(object)value2.Value;
				}
				if (typeof(T) == typeof(float))
				{
					if (!floatEntries.TryGetValue(key, out MelonPreferences_Entry<float> value3))
					{
						value3 = mainCategory.CreateEntry<float>(key, (float)(object)defaultValue, key, description, false, false, (ValueValidator)null, (string)null);
						floatEntries[key] = value3;
					}
					return (T)(object)value3.Value;
				}
				MelonLogger.Warning("Unsupported type " + typeof(T).Name + " for key " + key);
				return defaultValue;
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Error getting " + typeof(T).Name + " " + key + ": " + ex.Message);
				return defaultValue;
			}
		}

		public void SetValue<T>(string key, T value, string description = "")
		{
			try
			{
				if (typeof(T) == typeof(string))
				{
					if (!stringEntries.TryGetValue(key, out MelonPreferences_Entry<string> value2))
					{
						value2 = mainCategory.CreateEntry<string>(key, (string)(object)value, key, description, false, false, (ValueValidator)null, (string)null);
						stringEntries[key] = value2;
					}
					else
					{
						value2.Value = (string)(object)value;
					}
				}
				else if (typeof(T) == typeof(bool))
				{
					if (!boolEntries.TryGetValue(key, out MelonPreferences_Entry<bool> value3))
					{
						value3 = mainCategory.CreateEntry<bool>(key, (bool)(object)value, key, description, false, false, (ValueValidator)null, (string)null);
						boolEntries[key] = value3;
					}
					else
					{
						value3.Value = (bool)(object)value;
					}
				}
				else
				{
					if (!(typeof(T) == typeof(float)))
					{
						MelonLogger.Warning("Unsupported type " + typeof(T).Name + " for key " + key);
						return;
					}
					if (!floatEntries.TryGetValue(key, out MelonPreferences_Entry<float> value4))
					{
						value4 = mainCategory.CreateEntry<float>(key, (float)(object)value, key, description, false, false, (ValueValidator)null, (string)null);
						floatEntries[key] = value4;
					}
					else
					{
						value4.Value = (float)(object)value;
					}
				}
				MelonPreferences.Save();
			}
			catch (Exception ex)
			{
				MelonLogger.Error("Error setting " + typeof(T).Name + " " + key + ": " + ex.Message);
			}
		}

		public HotkeyConfig GetHotkey(string feature)
		{
			try
			{
				if (!hotkeyEntries.TryGetValue(feature, out MelonPreferences_Entry<string> value))
				{
					value = hotkeysCategory.CreateEntry<string>(feature, "None", feature, "", false, false, (ValueValidator)null, (string)null);
					hotkeyEntries[feature] = value;
				}
				if (value != null)
				{
					return HotkeyConfig.Parse(value.Value);
				}
				return new HotkeyConfig((KeyCode)0);
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Error getting hotkey " + feature + ": " + ex.Message);
				return new HotkeyConfig((KeyCode)0);
			}
		}

		public void SetHotkey(string feature, HotkeyConfig hotkey)
		{
			try
			{
				if (!hotkeyEntries.TryGetValue(feature, out MelonPreferences_Entry<string> value))
				{
					value = hotkeysCategory.CreateEntry<string>(feature, hotkey.ToString(), feature, "", false, false, (ValueValidator)null, (string)null);
					hotkeyEntries[feature] = value;
				}
				else
				{
					value.Value = hotkey.ToString();
				}
				MelonPreferences.Save();
			}
			catch (Exception ex)
			{
				MelonLogger.Error("Error setting hotkey " + feature + ": " + ex.Message);
			}
		}

		public bool IsHotkeyPressed(string feature)
		{
			return GetHotkey(feature).IsPressed();
		}

		public Dictionary<string, HotkeyConfig> GetAllHotkeys()
		{
			Dictionary<string, HotkeyConfig> dictionary = new Dictionary<string, HotkeyConfig>();
			try
			{
				foreach (MelonPreferences_Entry entry in hotkeysCategory.Entries)
				{
					if (entry is MelonPreferences_Entry<string> val)
					{
						dictionary[entry.Identifier] = HotkeyConfig.Parse(val.Value);
						hotkeyEntries[entry.Identifier] = val;
					}
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Error getting all hotkeys: " + ex.Message);
			}
			return dictionary;
		}
	}
	public class HotkeyConfig
	{
		public KeyCode Key { get; set; }

		public bool Shift { get; set; }

		public bool Ctrl { get; set; }

		public bool Alt { get; set; }

		public HotkeyConfig(KeyCode key = 0, bool shift = false, bool ctrl = false, bool alt = false)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			Key = key;
			Shift = shift;
			Ctrl = ctrl;
			Alt = alt;
		}

		public bool IsPressed()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((int)Key == 0)
				{
					return false;
				}
				Keyboard current = Keyboard.current;
				if (current == null)
				{
					return false;
				}
				KeyCode key = Key;
				KeyControl val = current.FindKeyOnCurrentKeyboardLayout(((object)(KeyCode)(ref key)).ToString());
				if (val == null || !((ButtonControl)val).wasPressedThisFrame)
				{
					return false;
				}
				return CheckModifiers(current);
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("IsPressed error: " + ex.Message);
				return false;
			}
		}

		private bool CheckModifiers(Keyboard keyboard)
		{
			bool flag = ((ButtonControl)keyboard.leftShiftKey).isPressed || ((ButtonControl)keyboard.rightShiftKey).isPressed;
			bool flag2 = ((ButtonControl)keyboard.leftCtrlKey).isPressed || ((ButtonControl)keyboard.rightCtrlKey).isPressed;
			bool flag3 = ((ButtonControl)keyboard.leftAltKey).isPressed || ((ButtonControl)keyboard.rightAltKey).isPressed;
			if (Shift == flag && Ctrl == flag2)
			{
				return Alt == flag3;
			}
			return false;
		}

		public override string ToString()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				KeyCode key = Key;
				string text = ((object)(KeyCode)(ref key)).ToString();
				if (Ctrl)
				{
					text = "Ctrl+" + text;
				}
				if (Shift)
				{
					text = "Shift+" + text;
				}
				if (Alt)
				{
					text = "Alt+" + text;
				}
				return text;
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("ToString error: " + ex.Message);
				return "None";
			}
		}

		public static HotkeyConfig Parse(string input)
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (string.IsNullOrEmpty(input) || input.Equals("None", StringComparison.OrdinalIgnoreCase))
				{
					return new HotkeyConfig((KeyCode)0);
				}
				bool ctrl = input.Contains("Ctrl+");
				bool shift = input.Contains("Shift+");
				bool alt = input.Contains("Alt+");
				string value = input.Replace("Ctrl+", "").Replace("Shift+", "").Replace("Alt+", "")
					.Trim();
				if (Enum.TryParse<KeyCode>(value, ignoreCase: true, out KeyCode result))
				{
					return new HotkeyConfig(result, shift, ctrl, alt);
				}
				MelonLogger.Warning("Failed to parse hotkey: " + input);
				return new HotkeyConfig((KeyCode)0);
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Parse error: " + ex.Message);
				return new HotkeyConfig((KeyCode)0);
			}
		}
	}
}