Decompiled source of RepairAll v1.0.0

BepInEx/plugins/RepairAll.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Dedryx")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © Dedryx 2026")]
[assembly: AssemblyDescription("Automatically repairs all eligible equipment when interacting with the appropriate crafting station.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RepairAll")]
[assembly: AssemblyTitle("RepairAll")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace RepairAll
{
	[BepInPlugin("dedryx.repairall", "RepairAll", "1.0.0")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(CraftingStation), "Interact", new Type[]
		{
			typeof(Humanoid),
			typeof(bool),
			typeof(bool)
		})]
		private static class StationInteractionPatch
		{
			private static void Postfix(CraftingStation __instance, Humanoid user, bool repeat)
			{
				Player localPlayer = Player.m_localPlayer;
				InventoryGui instance = InventoryGui.instance;
				if (!enableRepairAll.Value || repeat || (Object)(object)localPlayer == (Object)null || (Object)(object)user != (Object)(object)localPlayer || (Object)(object)instance == (Object)null || (Object)(object)localPlayer.GetCurrentCraftingStation() != (Object)(object)__instance || !__instance.m_canRepair || localPlayer.NoCostCheat() || !__instance.InUseDistance((Humanoid)(object)localPlayer) || !__instance.CheckUsable(localPlayer, false))
				{
					return;
				}
				List<ItemData> list = new List<ItemData>();
				((Humanoid)localPlayer).GetInventory().GetWornItems(list);
				bool flag = false;
				foreach (ItemData item in list)
				{
					if (canRepair(instance, item))
					{
						((Character)localPlayer).RaiseSkill((SkillType)107, 1f - item.m_durability / item.GetMaxDurability());
						item.m_durability = item.GetMaxDurability();
						flag = true;
					}
				}
				if (flag)
				{
					PlayRepairSound(__instance);
				}
			}
		}

		private static ConfigEntry<bool> enableRepairAll;

		private static Func<InventoryGui, ItemData, bool> canRepair;

		private void Awake()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			enableRepairAll = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableRepairAll", true, "Automatically repair eligible equipment when using a crafting station.");
			canRepair = AccessTools.MethodDelegate<Func<InventoryGui, ItemData, bool>>(AccessTools.DeclaredMethod(typeof(InventoryGui), "CanRepair", new Type[1] { typeof(ItemData) }, (Type[])null), (object)null, true);
			new Harmony("dedryx.repairall").PatchAll(typeof(Plugin).Assembly);
		}

		private static void PlayRepairSound(CraftingStation station)
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Expected O, but got Unknown
			EffectData[] effectPrefabs = station.m_repairItemDoneEffects.m_effectPrefabs;
			foreach (EffectData val in effectPrefabs)
			{
				if (!val.m_enabled || (Object)(object)val.m_prefab == (Object)null)
				{
					continue;
				}
				ZSFX[] componentsInChildren = val.m_prefab.GetComponentsInChildren<ZSFX>();
				foreach (ZSFX val2 in componentsInChildren)
				{
					if (val2.m_audioClips.Length != 0)
					{
						AudioClip val3 = val2.m_audioClips[Random.Range(0, val2.m_audioClips.Length)];
						if (!((Object)(object)val3 == (Object)null))
						{
							AudioSource component = ((Component)val2).GetComponent<AudioSource>();
							GameObject val4 = new GameObject("RepairAll sound");
							AudioSource val5 = val4.AddComponent<AudioSource>();
							val5.playOnAwake = false;
							val5.clip = val3;
							val5.outputAudioMixerGroup = (((Object)(object)component != (Object)null) ? component.outputAudioMixerGroup : null);
							val5.volume = Random.Range(val2.m_minVol, val2.m_maxVol);
							val5.pitch = Mathf.Max(0.1f, Random.Range(val2.m_minPitch, val2.m_maxPitch));
							val5.spatialBlend = 0f;
							val5.loop = false;
							val5.Play();
							Object.Destroy((Object)val4, val3.length / val5.pitch + 0.5f);
							return;
						}
					}
				}
			}
		}
	}
}