Decompiled source of Hard Difficulty Shield Rebalance v1.0.0

HardDifficultyShieldRebalance.dll

Decompiled 5 hours ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace HardDifficultyShieldRebalance;

[BepInPlugin("ucross.harddifficultyshieldrebalance", "Hard Difficulty Shield Rebalance", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "ucross.harddifficultyshieldrebalance";

	public const string PluginName = "Hard Difficulty Shield Rebalance";

	public const string Version = "1.0.0";

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<float> Strength;

	internal static ConfigEntry<bool> IncludeWeapons;

	internal static ConfigEntry<bool> LogBlocks;

	internal static ManualLogSource Log;

	internal static MethodInfo GetCurrentBlocker;

	internal static FieldInfo BlockTimer;

	private Harmony harmony;

	private void Awake()
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch.");
		Strength = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Strength (%)", 100f, new ConfigDescription("How much of the difficulty's extra enemy damage is added to block power. 100 = block power rises by exactly the extra damage (enemies +50% damage -> block power +50%), which makes blocking behave like Normal apart from the damage that gets through. 0 = vanilla.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 300f), new object[0]));
		IncludeWeapons = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Include weapon blocks", true, "On: blocking or parrying with a weapon (no shield) gets the bonus too. Off: shields only.");
		LogBlocks = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Log blocks", false, "Write each boosted block or parry to the BepInEx log (for testing).");
		GetCurrentBlocker = AccessTools.Method(typeof(Humanoid), "GetCurrentBlocker", (Type[])null, (Type[])null);
		BlockTimer = AccessTools.Field(typeof(Humanoid), "m_blockTimer");
		if (GetCurrentBlocker == null)
		{
			Log.LogError((object)"Could not find Humanoid.GetCurrentBlocker; this Valheim version changed blocking. Block scaling is OFF.");
			return;
		}
		harmony = new Harmony("ucross.harddifficultyshieldrebalance");
		harmony.PatchAll(typeof(BlockAttackPatch));
		Log.LogInfo((object)"Hard Difficulty Shield Rebalance 1.0.0 loaded.");
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
internal sealed class BlockState
{
	public SharedData Shared;

	public float BlockPower;

	public float BlockPowerPerLevel;
}
[HarmonyPatch(typeof(Humanoid), "BlockAttack")]
internal static class BlockAttackPatch
{
	[HarmonyPriority(800)]
	private static void Prefix(Humanoid __instance, Character attacker, out BlockState __state)
	{
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Invalid comparison between Unknown and I4
		__state = null;
		try
		{
			if (!Plugin.Enabled.Value || (Object)(object)__instance == (Object)null || !((Character)__instance).IsPlayer() || (Object)(object)attacker == (Object)null || attacker.IsPlayer())
			{
				return;
			}
			float enemyDamageRate = Game.m_enemyDamageRate;
			if (enemyDamageRate <= 1f)
			{
				return;
			}
			object? obj = Plugin.GetCurrentBlocker.Invoke(__instance, null);
			ItemData val = (ItemData)((obj is ItemData) ? obj : null);
			if (val == null || val.m_shared == null)
			{
				return;
			}
			SharedData shared = val.m_shared;
			if ((int)shared.m_itemType != 5 && !Plugin.IncludeWeapons.Value)
			{
				return;
			}
			float num = 1f + (enemyDamageRate - 1f) * (Plugin.Strength.Value / 100f);
			if (num <= 1f)
			{
				return;
			}
			BlockState blockState = new BlockState();
			blockState.Shared = shared;
			blockState.BlockPower = shared.m_blockPower;
			blockState.BlockPowerPerLevel = shared.m_blockPowerPerLevel;
			shared.m_blockPower = blockState.BlockPower * num;
			shared.m_blockPowerPerLevel = blockState.BlockPowerPerLevel * num;
			__state = blockState;
			if (Plugin.LogBlocks.Value)
			{
				bool flag = false;
				if (Plugin.BlockTimer != null && shared.m_timedBlockBonus > 1f)
				{
					float num2 = (float)Plugin.BlockTimer.GetValue(__instance);
					flag = num2 != -1f && num2 < 0.25f;
				}
				Plugin.Log.LogInfo((object)string.Format("{0} with {1} vs {2}: enemy damage x{3:0.##}, block power x{4:0.##}", flag ? "Parry" : "Block", shared.m_name, attacker.m_name, enemyDamageRate, num));
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Block scaling skipped for this hit: " + ex.Message));
		}
	}

	private static Exception Finalizer(Exception __exception, BlockState __state)
	{
		if (__state != null)
		{
			__state.Shared.m_blockPower = __state.BlockPower;
			__state.Shared.m_blockPowerPerLevel = __state.BlockPowerPerLevel;
		}
		return __exception;
	}
}