Decompiled source of NoTorchLeftBehind v1.0.0

NoTorchLeftBehind.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
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("NoTorchLeftBehind")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NoTorchLeftBehind")]
[assembly: AssemblyTitle("NoTorchLeftBehind")]
[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.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]
	[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;
		}
	}
	[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 NoTorchLeftBehind
{
	[BepInPlugin("com.lokas.valheim.notorchleftbehind", "NoTorchLeftBehind", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
		public static class ObjectDB_CopyOtherDB_Patch
		{
			[HarmonyPostfix]
			public static void Postfix(ObjectDB __instance)
			{
				UpdateTorchSettings(__instance);
			}
		}

		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		public static class ZNetScene_Awake_Patch
		{
			[HarmonyPostfix]
			public static void Postfix()
			{
				if ((Object)(object)ObjectDB.instance != (Object)null)
				{
					UpdateTorchSettings(ObjectDB.instance);
				}
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "CanRepair", new Type[] { typeof(ItemData) })]
		public static class InventoryGui_CanRepair_Patch
		{
			[HarmonyPostfix]
			public static void Postfix(ItemData item, ref bool __result)
			{
				if (__result || item == null || (Object)(object)item.m_dropPrefab == (Object)null || ((Object)item.m_dropPrefab).name != "Torch")
				{
					return;
				}
				Player localPlayer = Player.m_localPlayer;
				if (!((Object)(object)localPlayer == (Object)null))
				{
					CraftingStation currentCraftingStation = localPlayer.GetCurrentCraftingStation();
					if (!((Object)(object)currentCraftingStation == (Object)null) && (currentCraftingStation.m_name == "$piece_workbench" || currentCraftingStation.m_name == "$piece_forge" || ((Object)currentCraftingStation).name.StartsWith("piece_workbench") || ((Object)currentCraftingStation).name.StartsWith("forge")))
					{
						__result = true;
					}
				}
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "RepairOneItem")]
		public static class InventoryGui_RepairOneItem_Patch
		{
			[HarmonyPrefix]
			public static bool Prefix(InventoryGui __instance)
			{
				//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null)
				{
					return true;
				}
				CraftingStation currentCraftingStation = localPlayer.GetCurrentCraftingStation();
				if ((Object)(object)currentCraftingStation == (Object)null)
				{
					return true;
				}
				if (!(currentCraftingStation.m_name == "$piece_workbench") && !(currentCraftingStation.m_name == "$piece_forge") && !((Object)currentCraftingStation).name.StartsWith("piece_workbench") && !((Object)currentCraftingStation).name.StartsWith("forge"))
				{
					return true;
				}
				foreach (ItemData allItem in ((Humanoid)localPlayer).GetInventory().GetAllItems())
				{
					if ((Object)(object)allItem.m_dropPrefab != (Object)null && ((Object)allItem.m_dropPrefab).name == "Torch" && allItem.m_durability < allItem.GetMaxDurability())
					{
						allItem.m_durability = allItem.GetMaxDurability();
						EffectList repairItemDoneEffects = currentCraftingStation.m_repairItemDoneEffects;
						if (repairItemDoneEffects != null)
						{
							repairItemDoneEffects.Create(((Component)localPlayer).transform.position, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
						}
						((Character)localPlayer).Message((MessageType)2, allItem.m_shared.m_name + " repaired", 0, (Sprite)null, false);
						return false;
					}
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(Humanoid), "UpdateDepletion")]
		public static class Humanoid_UpdateDepletion_Patch
		{
			private static readonly FieldInfo? RightItemField = AccessTools.Field(typeof(Humanoid), "m_rightItem");

			private static readonly FieldInfo? LeftItemField = AccessTools.Field(typeof(Humanoid), "m_leftItem");

			[HarmonyPostfix]
			public static void Postfix(Humanoid __instance)
			{
				if (!((Object)(object)__instance == (Object)null))
				{
					object? obj = RightItemField?.GetValue(__instance);
					ItemData val = (ItemData)((obj is ItemData) ? obj : null);
					if (val != null && (Object)(object)val.m_dropPrefab != (Object)null && ((Object)val.m_dropPrefab).name == "Torch" && val.m_durability <= 0f)
					{
						__instance.UnequipItem(val, true);
					}
					object? obj2 = LeftItemField?.GetValue(__instance);
					ItemData val2 = (ItemData)((obj2 is ItemData) ? obj2 : null);
					if (val2 != null && (Object)(object)val2.m_dropPrefab != (Object)null && ((Object)val2.m_dropPrefab).name == "Torch" && val2.m_durability <= 0f)
					{
						__instance.UnequipItem(val2, true);
					}
				}
			}
		}

		public const string PluginGUID = "com.lokas.valheim.notorchleftbehind";

		public const string PluginName = "NoTorchLeftBehind";

		public const string PluginVersion = "1.0.0";

		private Harmony? _harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("com.lokas.valheim.notorchleftbehind");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"NoTorchLeftBehind v1.0.0 loaded successfully.");
		}

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

		private static void UpdateTorchSettings(ObjectDB db)
		{
			if ((Object)(object)db == (Object)null || db.m_items == null || db.m_items.Count == 0)
			{
				return;
			}
			GameObject itemPrefab = db.GetItemPrefab("Torch");
			if (!((Object)(object)itemPrefab == (Object)null))
			{
				ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
				if (!((Object)(object)component == (Object)null) && component.m_itemData?.m_shared != null)
				{
					component.m_itemData.m_shared.m_destroyBroken = false;
					component.m_itemData.m_shared.m_canBeReparied = true;
				}
			}
		}
	}
}