Decompiled source of NoDurability v1.0.3

NoDurability.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
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: AssemblyMetadata("Notice", "Copyright (c) casualexpie. All rights reserved. Do NOT decompile, disassemble, or reverse-engineer this assembly. Report bug symptoms on the Nexus Bugs page; do not post decompiled or reconstructed source. Reverse-engineering this DLL is not authorized.")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace NoDurability;

[BepInPlugin("nodurability", "No Durability", "1.0.2")]
public class Plugin : BaseUnityPlugin
{
	public const string Version = "1.0.2";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<KeyCode> ToggleKey;

	internal static ConfigEntry<bool> Tools;

	internal static ConfigEntry<bool> Weapons;

	internal static ConfigEntry<bool> Wearables;

	internal static ConfigEntry<bool> Containers;

	internal static ConfigEntry<bool> Batteries;

	internal static ConfigEntry<bool> Fuel;

	internal static ConfigEntry<string> FuelItems;

	internal static ConfigEntry<string> ExtraItems;

	internal static ConfigEntry<string> ExcludedItems;

	private void Awake()
	{
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Expected O, but got Unknown
		//IL_024b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0251: Expected O, but got Unknown
		//IL_0292: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turn No Durability on or off. You can also use the toggle key in game.");
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ToggleKey", (KeyCode)288, "Key that turns No Durability on and off in game.");
		Tools = ((BaseUnityPlugin)this).Config.Bind<bool>("Items", "Tools", true, "Tools and melee weapons: pickaxes, shovels, wrenches, machetes, knives, drills, ...");
		Weapons = ((BaseUnityPlugin)this).Config.Bind<bool>("Items", "Guns", true, "Guns don't wear out when you shoot them.");
		Wearables = ((BaseUnityPlugin)this).Config.Bind<bool>("Items", "Wearables", true, "Armor, helmets, clothes, shoes, climbing claws, ...");
		Containers = ((BaseUnityPlugin)this).Config.Bind<bool>("Items", "Containers", true, "Bags, backpacks, boxes, toolboxes and other containers.");
		Batteries = ((BaseUnityPlugin)this).Config.Bind<bool>("Items", "Batteries", true, "Battery powered items never lose charge: flashlight, drills, headlamp, grappling hook, AED, gravity bag, ... Loose batteries too.");
		Fuel = ((BaseUnityPlugin)this).Config.Bind<bool>("Items", "Fuel", true, "Fuel and burn time: jetpack, lantern, lighter, torch, campfire, ...");
		FuelItems = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "FuelItems", "jetpack,lantern,lighter,torch,campfire,emergencylight,makeshiftheadlamp", "Item IDs that count as fuel items.");
		ExtraItems = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "ExtraItems", "tweezers,lockpickingkit,sleepingbag", "Extra item IDs that never lose condition, comma separated.");
		ExcludedItems = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "ExcludedItems", "", "Item IDs that always wear out normally, comma separated.");
		((BaseUnityPlugin)this).Config.SettingChanged += delegate
		{
			Guard.Reset();
		};
		Harmony h = new Harmony("nodurability");
		int num = 0;
		num += Hook(h, typeof(Item), "Update", "ItemUpdatePrefix", null);
		num += Hook(h, typeof(RecipeItem), "UseItem", null, "RecipeUsePostfix");
		num += Hook(h, typeof(Body), "CombineItems", null, "CombinePostfix");
		num += Hook(h, typeof(BatteryItem), "DrainCharge", "DrainPrefix", null);
		num += Hook(h, typeof(CustomItemBehaviour), "Update", "CustomUpdatePrefix", null);
		GameObject val = new GameObject("NoDurabilityRunner");
		((Object)val).hideFlags = (HideFlags)61;
		Object.DontDestroyOnLoad((Object)(object)val);
		val.AddComponent<Runner>();
		Log.LogInfo((object)string.Concat("No Durability 1.0.2 loaded (", num, "/5 hooks). ", ToggleKey.Value, " turns it on or off. Game version ", Application.version, "."));
	}

	private static int Hook(Harmony h, Type t, string method, string prefix, string postfix)
	{
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			MethodInfo methodInfo = AccessTools.Method(t, method, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogWarning((object)("Couldn't find " + t.Name + "." + method + ", skipping it."));
				return 0;
			}
			h.Patch((MethodBase)methodInfo, (prefix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(typeof(Patches), prefix, (Type[])null), (postfix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(typeof(Patches), postfix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			return 1;
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Couldn't patch " + t.Name + "." + method + ": " + ex.Message));
			return 0;
		}
	}
}
internal static class Patches
{
	public static void ItemUpdatePrefix(Item __instance)
	{
		Guard.Hold(__instance);
	}

	public static bool DrainPrefix(BatteryItem __instance, float change)
	{
		if (change <= 0f || !Plugin.Enabled.Value || (Object)(object)__instance == (Object)null)
		{
			return true;
		}
		Item component = ((Component)__instance).GetComponent<Item>();
		if (!((Object)(object)component == (Object)null))
		{
			return !Guard.Protected(component);
		}
		return true;
	}

	public static void CustomUpdatePrefix(Item ___item)
	{
		Guard.Hold(___item);
	}

	public static void RecipeUsePostfix(RecipeItem __instance, Item item)
	{
		if (__instance != null && __instance.destroyItem)
		{
			Guard.Accept(item);
		}
	}

	public static void CombinePostfix(Item it1, Item it2)
	{
		Guard.Accept(it1);
		Guard.Accept(it2);
	}
}
internal static class Guard
{
	private static readonly Dictionary<Item, float> held = new Dictionary<Item, float>();

	private static readonly Dictionary<string, bool> cache = new Dictionary<string, bool>();

	private static HashSet<string> fuel;

	private static HashSet<string> extra;

	private static HashSet<string> excluded;

	public static void Reset()
	{
		held.Clear();
		cache.Clear();
		fuel = null;
		extra = null;
		excluded = null;
	}

	private static HashSet<string> Parse(string s)
	{
		HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
		if (string.IsNullOrEmpty(s))
		{
			return hashSet;
		}
		string[] array = s.Split(new char[3] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
		foreach (string text in array)
		{
			hashSet.Add(text.Trim());
		}
		return hashSet;
	}

	public static bool Protected(Item it)
	{
		if (string.IsNullOrEmpty(it.id))
		{
			return false;
		}
		if (cache.TryGetValue(it.id, out var value))
		{
			return value;
		}
		value = Decide(it);
		cache[it.id] = value;
		return value;
	}

	private static bool Decide(Item it)
	{
		if (fuel == null)
		{
			fuel = Parse(Plugin.FuelItems.Value);
		}
		if (extra == null)
		{
			extra = Parse(Plugin.ExtraItems.Value);
		}
		if (excluded == null)
		{
			excluded = Parse(Plugin.ExcludedItems.Value);
		}
		if (excluded.Contains(it.id))
		{
			return false;
		}
		if (extra.Contains(it.id))
		{
			return true;
		}
		ItemInfo stats;
		try
		{
			stats = it.Stats;
		}
		catch
		{
			return false;
		}
		if (stats == null)
		{
			return false;
		}
		if (stats is LiquidItemInfo || (Object)(object)((Component)it).GetComponent<WaterContainerItem>() != (Object)null)
		{
			return false;
		}
		if (Plugin.Fuel.Value && fuel.Contains(it.id))
		{
			return true;
		}
		if (Plugin.Batteries.Value && (stats is BatteryInfo || (Object)(object)((Component)it).GetComponent<BatteryItem>() != (Object)null))
		{
			return true;
		}
		if (Plugin.Weapons.Value && (Object)(object)((Component)it).GetComponent<GunScript>() != (Object)null)
		{
			return true;
		}
		if (Plugin.Containers.Value && (stats.category == "container" || (Object)(object)((Component)it).GetComponent<Container>() != (Object)null))
		{
			return true;
		}
		if (Plugin.Tools.Value && (stats.category == "tool" || SafeTag(stats, "tool")))
		{
			return true;
		}
		if (Plugin.Wearables.Value && stats.wearable)
		{
			return true;
		}
		return false;
	}

	private static bool SafeTag(ItemInfo s, string tag)
	{
		try
		{
			return s.HasTag(tag);
		}
		catch
		{
			return false;
		}
	}

	public static void Hold(Item it)
	{
		if (Plugin.Enabled.Value && !((Object)(object)it == (Object)null) && Protected(it))
		{
			float condition = it.condition;
			float value;
			if ((Object)(object)it.battery != (Object)null && !it.battery.hasBattery)
			{
				held[it] = condition;
			}
			else if (!held.TryGetValue(it, out value) || condition >= value)
			{
				held[it] = condition;
			}
			else
			{
				it.condition = value;
			}
		}
	}

	public static void Accept(Item it)
	{
		if (!((Object)(object)it == (Object)null) && held.ContainsKey(it))
		{
			held[it] = it.condition;
		}
	}

	public static void Tick()
	{
		if (!Plugin.Enabled.Value)
		{
			return;
		}
		List<Item> allItems = Item.allItems;
		if (allItems == null)
		{
			return;
		}
		for (int i = 0; i < allItems.Count; i++)
		{
			Item val = allItems[i];
			if ((Object)(object)val != (Object)null)
			{
				Hold(val);
			}
		}
	}

	public static void Sweep()
	{
		if (held.Count == 0)
		{
			return;
		}
		List<Item> list = null;
		foreach (Item key in held.Keys)
		{
			if ((Object)(object)key == (Object)null)
			{
				if (list == null)
				{
					list = new List<Item>();
				}
				list.Add(key);
			}
		}
		if (list == null)
		{
			return;
		}
		foreach (Item item in list)
		{
			held.Remove(item);
		}
	}
}
internal class Runner : MonoBehaviour
{
	private string toast;

	private float toastUntil;

	private float nextSweep;

	private GUIStyle style;

	private void Update()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(Plugin.ToggleKey.Value))
		{
			Plugin.Enabled.Value = !Plugin.Enabled.Value;
			Guard.Reset();
			toast = "No Durability " + (Plugin.Enabled.Value ? "ON" : "OFF");
			toastUntil = Time.realtimeSinceStartup + 2.5f;
			Plugin.Log.LogInfo((object)toast);
		}
	}

	private void LateUpdate()
	{
		try
		{
			Guard.Tick();
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)("No Durability error: " + ex));
		}
		if (Time.realtimeSinceStartup > nextSweep)
		{
			nextSweep = Time.realtimeSinceStartup + 30f;
			Guard.Sweep();
		}
	}

	private void OnGUI()
	{
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		if (toast != null && !(Time.realtimeSinceStartup > toastUntil))
		{
			if (style == null)
			{
				style = new GUIStyle(GUI.skin.label);
				style.alignment = (TextAnchor)4;
				style.fontSize = 20;
				style.fontStyle = (FontStyle)1;
			}
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(0f, (float)Screen.height - 120f, (float)Screen.width, 40f);
			style.normal.textColor = new Color(0f, 0f, 0f, 0.8f);
			GUI.Label(new Rect(((Rect)(ref val)).x + 2f, ((Rect)(ref val)).y + 2f, ((Rect)(ref val)).width, ((Rect)(ref val)).height), toast, style);
			style.normal.textColor = (Plugin.Enabled.Value ? new Color(1f, 0.58f, 0.13f) : new Color(0.84f, 0.26f, 0.21f));
			GUI.Label(val, toast, style);
		}
	}
}