Decompiled source of InstantRepairPlus v2.1.0

InstantRepairPlus.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("FreeBuild")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP")]
[assembly: AssemblyProduct("FreeBuild")]
[assembly: AssemblyCopyright("Copyright © HP 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("99c8ea2c-831f-4cbb-b4d6-211ad57757ca")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("YouDied.InstantRepairPlus", "Instant Repair Plus", "2.1.0")]
public class InstantRepairPlus : BaseUnityPlugin
{
	[HarmonyPatch(typeof(InventoryGui), "Show")]
	public static class InventoryGui_Show_Patch
	{
		private static void Postfix()
		{
			if ((Object)(object)Instance == (Object)null || !Instance.autoRepairOnOpen.Value)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			try
			{
				Instance.RepairAllItems(localPlayer);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)Instance).Logger.LogError((object)("Instant Repair Plus: auto-repair-on-open failed - " + ex));
			}
		}
	}

	public static InstantRepairPlus Instance;

	private static bool harmonyPatchesApplied;

	private ConfigEntry<KeyCode> repairKey;

	private ConfigEntry<bool> ignoreStation;

	private ConfigEntry<bool> repairEquippedOnly;

	private ConfigEntry<bool> repairHotbarOnly;

	private ConfigEntry<bool> autoRepairOnOpen;

	private ConfigEntry<float> staminaCost;

	private ConfigEntry<bool> playEffect;

	private ConfigEntry<bool> showMessage;

	private void Awake()
	{
		Instance = this;
		repairKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "RepairKey", (KeyCode)57, "Key to repair items");
		ignoreStation = ((BaseUnityPlugin)this).Config.Bind<bool>("Rules", "IgnoreStationRequirements", true, "Repair without needing a workbench/forge");
		repairEquippedOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("Filters", "RepairEquippedOnly", false, "Only repair equipped items");
		repairHotbarOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("Filters", "RepairHotbarOnly", false, "Only repair hotbar items (row 0)");
		autoRepairOnOpen = ((BaseUnityPlugin)this).Config.Bind<bool>("Automation", "AutoRepairOnInventoryOpen", false, "Automatically repair when inventory opens");
		staminaCost = ((BaseUnityPlugin)this).Config.Bind<float>("Cost", "StaminaCost", 0f, "Stamina cost per repair action");
		playEffect = ((BaseUnityPlugin)this).Config.Bind<bool>("Feedback", "PlayRepairEffect", true, "Play repair animation");
		showMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("Feedback", "ShowMessage", true, "Show on-screen message");
		try
		{
			Harmony.CreateAndPatchAll(typeof(InstantRepairPlus), (string)null);
			harmonyPatchesApplied = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Instant Repair Plus Loaded! (v2.1.0, patches applied OK)");
		}
		catch (Exception ex)
		{
			harmonyPatchesApplied = false;
			((BaseUnityPlugin)this).Logger.LogError((object)("Instant Repair Plus: Harmony patching failed, likely a Valheim 1.0 API change. AutoRepairOnInventoryOpen will not work until this is fixed. Manual repair (hotkey) is unaffected since it doesn't depend on the patch. Exception: " + ex));
		}
	}

	private void Update()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(repairKey.Value))
		{
			try
			{
				TryRepair();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Instant Repair Plus: TryRepair threw, likely an item field/method renamed in the current Valheim build. Nothing was written to inventory this pass. Exception: " + ex));
			}
		}
	}

	private void TryRepair()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return;
		}
		if (staminaCost.Value > 0f && localPlayer.GetStamina() < staminaCost.Value)
		{
			((Character)localPlayer).Message((MessageType)2, "Not enough stamina!", 0, (Sprite)null, false);
			return;
		}
		int num = RepairAllItems(localPlayer);
		if (num > 0)
		{
			if (staminaCost.Value > 0f)
			{
				((Character)localPlayer).UseStamina(staminaCost.Value);
			}
			if (playEffect.Value)
			{
				localPlayer.StartEmote("craft", true);
			}
			if (showMessage.Value)
			{
				((Character)localPlayer).Message((MessageType)2, $"Repaired {num} items", 0, (Sprite)null, false);
			}
		}
	}

	public int RepairAllItems(Player player)
	{
		List<ItemData> allItems = ((Humanoid)player).GetInventory().GetAllItems();
		int num = 0;
		foreach (ItemData item in allItems)
		{
			if (item == null)
			{
				continue;
			}
			try
			{
				if ((!repairEquippedOnly.Value || item.m_equipped) && (!repairHotbarOnly.Value || item.m_gridPos.y == 0) && (ignoreStation.Value || CanRepairVanilla(player, item)) && item.m_durability < item.GetMaxDurability())
				{
					item.m_durability = item.GetMaxDurability();
					num++;
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Instant Repair Plus: skipped an item due to an error (possible API change) - " + ex.Message));
			}
		}
		return num;
	}

	private bool CanRepairVanilla(Player player, ItemData item)
	{
		CraftingStation currentCraftingStation = player.GetCurrentCraftingStation();
		if ((Object)(object)currentCraftingStation == (Object)null)
		{
			return false;
		}
		Recipe recipe = ObjectDB.instance.GetRecipe(item);
		if ((Object)(object)recipe == (Object)null || (Object)(object)recipe.m_craftingStation == (Object)null)
		{
			return false;
		}
		if (currentCraftingStation.m_name != recipe.m_craftingStation.m_name)
		{
			return false;
		}
		return currentCraftingStation.GetLevel(true) >= recipe.m_minStationLevel;
	}
}