Decompiled source of PerPlayerDifficulty v1.1.0

plugins/PerPlayerDifficulty.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("PerPlayerDifficulty")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PerPlayerDifficulty")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("587ed786-ece7-436c-8607-3fbe1b40ea86")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace PerPlayerDifficulty;

public static class Configs
{
	public static ConfigEntry<bool> enable_debug_logs;

	public static ConfigEntry<bool> player_damage_inflicted_after_reductions;

	public static ConfigEntry<bool> player_damage_taken_after_reductions;

	public static ConfigEntry<string> player_difficulty_settings;

	public static void SetupConfigs(ConfigFile config)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Expected O, but got Unknown
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Expected O, but got Unknown
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		//IL_00b0: 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_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Expected O, but got Unknown
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Expected O, but got Unknown
		ConfigurationManagerAttributes val = new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};
		enable_debug_logs = config.Bind<bool>("General", GenerateConfigKey("enable_debug_logs"), false, "Enables debug logs.");
		player_damage_inflicted_after_reductions = config.Bind<bool>("General", GenerateConfigKey("player_damage_inflicted_after_reductions"), true, new ConfigDescription("Applies the player's damage inflicted modifier after resistances, armor and blocking.\nWhen disabled, applies it before these reductions.", (AcceptableValueBase)null, new object[1] { val }));
		player_damage_taken_after_reductions = config.Bind<bool>("General", GenerateConfigKey("player_damage_taken_after_reductions"), true, new ConfigDescription("Applies the player's damage taken modifier after resistances, armor and blocking.\nWhen disabled, applies it before these reductions.", (AcceptableValueBase)null, new object[1] { val }));
		player_difficulty_settings = config.Bind<string>("General", GenerateConfigKey("player_difficulty_settings"), "", new ConfigDescription("Determines the damage inflicted and damage taken modifiers for each player.\nThe Player Damage Inflicted After Reductions and Player Damage Taken After Reductions settings control whether each modifier applies before or after resistances, armor and blocking.\nFormat: player_name_1:damage_inflicted_in_percentage:damage_taken_in_percentage,player_name_2:damage_inflicted_in_percentage:damage_taken_in_percentage\nExample: Alpus:75:150,Friend:100:50\nAlpus will deal 75% of normal damage to enemies and take 150% of normal damage from enemies; Friend will deal normal damage but take only half the normal damage from enemies.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true,
			CustomDrawer = TextAreaDrawer
		} }));
		player_difficulty_settings.SettingChanged += Player_difficulty_settings_SettingChanged;
	}

	public static string GenerateConfigKey(string key)
	{
		return GenerateConfigKey(key, null);
	}

	public static string GenerateConfigKey(string key, string unit)
	{
		key = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(key.Replace('_', ' ').ToLower());
		if (unit != null)
		{
			key += $" ({unit})";
		}
		return key;
	}

	private static void Player_difficulty_settings_SettingChanged(object sender, EventArgs e)
	{
		PerPlayerDifficulty.UpdatePlayerDifficultySettings();
	}

	private static void TextAreaDrawer(ConfigEntryBase entry)
	{
		GUILayout.ExpandHeight(true);
		GUILayout.ExpandWidth(true);
		entry.BoxedValue = GUILayout.TextArea((string)entry.BoxedValue, (GUILayoutOption[])(object)new GUILayoutOption[2]
		{
			GUILayout.ExpandWidth(true),
			GUILayout.ExpandHeight(true)
		});
	}
}
[BepInPlugin("PerPlayerDifficulty", "Per Player Difficulty", "1.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class PerPlayerDifficulty : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Character), "Damage")]
	[HarmonyPriority(0)]
	public class Character_Damage_Patch
	{
		public static void Prefix(Character __instance, ref HitData hit)
		{
			if (!Configs.player_damage_taken_after_reductions.Value)
			{
				ApplyDamageTakenModifier(__instance, ref hit);
			}
			if (!Configs.player_damage_inflicted_after_reductions.Value)
			{
				Character_ApplyDamage_Patch.ApplyDamageInflictedModifier(ref hit);
			}
		}

		public static void ApplyDamageTakenModifier(Character character, ref HitData hit)
		{
			Player val = (Player)(object)((character is Player) ? character : null);
			if (val == null)
			{
				return;
			}
			if (Configs.enable_debug_logs.Value)
			{
				ZLog.Log((object)("PerPlayerDifficulty: Player Damage Taken - Name: " + val.GetPlayerName()));
			}
			if (PlayerDifficultySettings.ContainsKey(val.GetPlayerName()))
			{
				bool flag = ApplyModifier(ref hit, PlayerDifficultySettings[val.GetPlayerName()].DamageTakenModifier);
				if (Configs.enable_debug_logs.Value)
				{
					ZLog.Log((object)("PerPlayerDifficulty: Player Damage Taken - Modifier: " + PlayerDifficultySettings[val.GetPlayerName()].DamageTakenModifier.ToString("0.00") + (flag ? (", Modified Damage: " + hit.GetTotalDamage().ToString("0.00")) : " (Skipping Modifier)")));
				}
			}
		}
	}

	[HarmonyPatch(typeof(Character), "ApplyDamage")]
	[HarmonyPriority(0)]
	public class Character_ApplyDamage_Patch
	{
		public static void Prefix(Character __instance, ref HitData hit)
		{
			if (Configs.player_damage_taken_after_reductions.Value)
			{
				Character_Damage_Patch.ApplyDamageTakenModifier(__instance, ref hit);
			}
			if (Configs.player_damage_inflicted_after_reductions.Value)
			{
				ApplyDamageInflictedModifier(ref hit);
			}
		}

		public static void ApplyDamageInflictedModifier(ref HitData hit)
		{
			if (!((Object)(object)hit.GetAttacker() != (Object)null))
			{
				return;
			}
			Character attacker = hit.GetAttacker();
			Player val = (Player)(object)((attacker is Player) ? attacker : null);
			if (val == null)
			{
				return;
			}
			if (Configs.enable_debug_logs.Value)
			{
				ZLog.Log((object)("PerPlayerDifficulty: Player Damage Inflicted - Name: " + val.GetPlayerName()));
			}
			if (PlayerDifficultySettings.ContainsKey(val.GetPlayerName()))
			{
				bool flag = ApplyModifier(ref hit, PlayerDifficultySettings[val.GetPlayerName()].DamageInflictedModifier);
				if (Configs.enable_debug_logs.Value)
				{
					ZLog.Log((object)("PerPlayerDifficulty: Player Damage Inflicted - Modifier: " + PlayerDifficultySettings[val.GetPlayerName()].DamageInflictedModifier.ToString("0.00") + (flag ? (", Modified Damage: " + hit.GetTotalDamage().ToString("0.00")) : " (Skipping Modifier)")));
				}
			}
		}
	}

	public const string PluginVersion = "1.1.0";

	private Harmony harmony = new Harmony("PerPlayerDifficulty");

	public static Dictionary<string, PlayerDifficultyModifier> PlayerDifficultySettings = new Dictionary<string, PlayerDifficultyModifier>();

	private void Awake()
	{
		Configs.SetupConfigs(((BaseUnityPlugin)this).Config);
		harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "HeadLamp");
		UpdatePlayerDifficultySettings();
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}

	public static void UpdatePlayerDifficultySettings()
	{
		if (PlayerDifficultySettings.Count > 0)
		{
			PlayerDifficultySettings.Clear();
		}
		if (string.IsNullOrEmpty(Configs.player_difficulty_settings.Value))
		{
			return;
		}
		string[] array = Configs.player_difficulty_settings.Value.Split(new char[1] { ',' });
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Trim().Split(new char[1] { ':' });
			if (array2.Length < 3)
			{
				ZLog.LogError((object)"PerPlayerDifficulty: Player Difficulty Settings not in the expected format. Please see the documentation and adhere to the correct format.");
				PlayerDifficultySettings.Clear();
				return;
			}
			string text = array2[0].Trim();
			int result = 0;
			int result2 = 0;
			bool flag = int.TryParse(array2[1].Trim(), out result) && int.TryParse(array2[2].Trim(), out result2);
			if (text.Length == 0 || !flag)
			{
				ZLog.LogError((object)"PerPlayerDifficulty: Player Difficulty Settings not in the expected format. Please see the documentation and adhere to the correct format.");
				PlayerDifficultySettings.Clear();
				return;
			}
			if (result <= 0 || result2 <= 0)
			{
				ZLog.LogError((object)"PerPlayerDifficulty: Damage inflicted and damage taken modifiers in Player Difficulty Settings cannot be equal or lower than 0.");
				PlayerDifficultySettings.Clear();
				return;
			}
			PlayerDifficultySettings.Add(text, new PlayerDifficultyModifier((float)result / 100f, (float)result2 / 100f));
		}
		ZLog.Log((object)"PerPlayerDifficulty: Player Difficulty Settings updated successfully.");
	}

	public static bool ApplyModifier(ref HitData hit, float modifier)
	{
		if (modifier != 1f)
		{
			hit.m_damage.m_blunt *= modifier;
			hit.m_damage.m_pierce *= modifier;
			hit.m_damage.m_slash *= modifier;
			hit.m_damage.m_fire *= modifier;
			hit.m_damage.m_frost *= modifier;
			hit.m_damage.m_lightning *= modifier;
			hit.m_damage.m_poison *= modifier;
			hit.m_damage.m_spirit *= modifier;
			return true;
		}
		return false;
	}
}
public struct PlayerDifficultyModifier
{
	public float DamageInflictedModifier;

	public float DamageTakenModifier;

	public PlayerDifficultyModifier(float damageInflictedModifier, float damageTakenModifier)
	{
		DamageInflictedModifier = damageInflictedModifier;
		DamageTakenModifier = damageTakenModifier;
	}
}