Decompiled source of EffortlessRepair v1.0.0

plugins/EffortlessRepair.dll

Decompiled a week ago
using System;
using System.Collections;
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("EffortlessRepair")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+5d51ccf09bf1a7710878a31879c7c5f9c7c0d815")]
[assembly: AssemblyProduct("EffortlessRepair")]
[assembly: AssemblyTitle("EffortlessRepair")]
[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 PlayToDate.EffortlessRepair
{
	[BepInPlugin("it.playtodate.effortlessrepair", "Effortless Repair", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "it.playtodate.effortlessrepair";

		public const string Name = "Effortless Repair";

		public const string Version = "1.0.0";

		private static ConfigEntry<bool> _enabled;

		private static ConfigEntry<bool> _message;

		private static Plugin _self;

		private static FieldInfo _localPlayer;

		private static FieldInfo _inventory;

		private static MethodInfo _canRepair;

		private static MethodInfo _getAllItems;

		private static MethodInfo _getMaxDur;

		private static MethodInfo _currentStation;

		private static MethodInfo _message0;

		private static MethodInfo _getLanguage;

		private static Type _tLocalization;

		private void Awake()
		{
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Expected O, but got Unknown
			_self = this;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "One click on Repair fixes every item the station can repair. / Un clic su Ripara sistema tutto quello che la stazione sa riparare.");
			_message = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Show message", true, "Show how many items were repaired. / Mostra quanti oggetti sono stati riparati.");
			Type type = AccessTools.TypeByName("InventoryGui");
			Type type2 = AccessTools.TypeByName("Player");
			Type type3 = AccessTools.TypeByName("Humanoid");
			if (type == null || type2 == null || type3 == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Effortless Repair: game types not found, mod disabled");
				return;
			}
			_localPlayer = AccessTools.Field(type2, "m_localPlayer");
			_inventory = AccessTools.Field(type3, "m_inventory");
			_currentStation = AccessTools.Method(type2, "GetCurrentCraftingStation", (Type[])null, (Type[])null);
			MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo in methods)
			{
				if (methodInfo.Name == "CanRepair" && methodInfo.GetParameters().Length == 1)
				{
					_canRepair = methodInfo;
					break;
				}
			}
			MethodInfo methodInfo2 = AccessTools.Method(type, "RepairOneItem", (Type[])null, (Type[])null);
			if (_localPlayer == null || _inventory == null || _canRepair == null || methodInfo2 == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Effortless Repair: the game changed (missing m_localPlayer, m_inventory, CanRepair or RepairOneItem), mod disabled");
				return;
			}
			_tLocalization = AccessTools.TypeByName("Localization");
			new Harmony("it.playtodate.effortlessrepair").Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(Plugin).GetMethod("RepairPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Effortless Repair 1.0.0 ready");
		}

		private static bool RepairPrefix(object __instance)
		{
			if (!_enabled.Value)
			{
				return true;
			}
			try
			{
				object value = _localPlayer.GetValue(null);
				if (value == null)
				{
					return true;
				}
				object value2 = _inventory.GetValue(value);
				if (value2 == null)
				{
					return true;
				}
				if (_getAllItems == null)
				{
					_getAllItems = AccessTools.Method(value2.GetType(), "GetAllItems", Type.EmptyTypes, (Type[])null);
				}
				if (!(_getAllItems?.Invoke(value2, null) is IEnumerable enumerable))
				{
					return true;
				}
				int num = 0;
				foreach (object item in enumerable)
				{
					if (item == null)
					{
						continue;
					}
					Type type = item.GetType();
					FieldInfo fieldInfo = AccessTools.Field(type, "m_durability");
					if (fieldInfo == null)
					{
						continue;
					}
					if (_getMaxDur == null)
					{
						_getMaxDur = AccessTools.Method(type, "GetMaxDurability", Type.EmptyTypes, (Type[])null);
					}
					if (_getMaxDur == null)
					{
						return true;
					}
					float num2 = Convert.ToSingle(_getMaxDur.Invoke(item, null));
					if (!(Convert.ToSingle(fieldInfo.GetValue(item)) >= num2))
					{
						object obj = _canRepair.Invoke(_canRepair.IsStatic ? null : __instance, new object[1] { item });
						if (obj is bool && (bool)obj)
						{
							fieldInfo.SetValue(item, num2);
							num++;
						}
					}
				}
				if (num == 0)
				{
					return true;
				}
				PlayEffect(value);
				if (_message.Value)
				{
					Say(value, (!Italian()) ? ((num == 1) ? "Repaired 1 item" : ("Repaired " + num + " items")) : ((num == 1) ? "Riparato 1 oggetto" : ("Riparati " + num + " oggetti")));
				}
				return false;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)_self).Logger.LogWarning((object)("Effortless Repair: repair all failed, the game repairs one item (" + ex.Message + ")"));
				return true;
			}
		}

		private static void PlayEffect(object player)
		{
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				object? obj = _currentStation?.Invoke(player, null);
				Component val = (Component)((obj is Component) ? obj : null);
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				object obj2 = AccessTools.Field(((object)val).GetType(), "m_repairItemDoneEffects")?.GetValue(val);
				if (obj2 == null)
				{
					return;
				}
				MethodInfo[] methods = obj2.GetType().GetMethods();
				foreach (MethodInfo methodInfo in methods)
				{
					if (methodInfo.Name != "Create")
					{
						continue;
					}
					ParameterInfo[] parameters = methodInfo.GetParameters();
					if (parameters.Length >= 2 && !(parameters[0].ParameterType != typeof(Vector3)) && !(parameters[1].ParameterType != typeof(Quaternion)))
					{
						object[] array = new object[parameters.Length];
						array[0] = val.transform.position;
						array[1] = Quaternion.identity;
						for (int j = 2; j < parameters.Length; j++)
						{
							array[j] = (parameters[j].HasDefaultValue ? parameters[j].DefaultValue : (parameters[j].ParameterType.IsValueType ? Activator.CreateInstance(parameters[j].ParameterType) : null));
						}
						methodInfo.Invoke(obj2, array);
						break;
					}
				}
			}
			catch
			{
			}
		}

		private static bool Italian()
		{
			try
			{
				object obj = _tLocalization?.GetProperty("instance", BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
				if (obj == null)
				{
					return false;
				}
				if (_getLanguage == null)
				{
					_getLanguage = AccessTools.Method(_tLocalization, "GetSelectedLanguage", (Type[])null, (Type[])null);
				}
				return string.Equals(_getLanguage?.Invoke(obj, null) as string, "Italian", StringComparison.OrdinalIgnoreCase);
			}
			catch
			{
				return false;
			}
		}

		private static void Say(object player, string text)
		{
			try
			{
				if (_message0 == null)
				{
					MethodInfo[] methods = AccessTools.TypeByName("Character").GetMethods(BindingFlags.Instance | BindingFlags.Public);
					foreach (MethodInfo methodInfo in methods)
					{
						if (methodInfo.Name == "Message" && methodInfo.GetParameters().Length >= 2)
						{
							_message0 = methodInfo;
							break;
						}
					}
				}
				if (!(_message0 == null))
				{
					ParameterInfo[] parameters = _message0.GetParameters();
					object[] array = new object[parameters.Length];
					array[0] = Enum.ToObject(parameters[0].ParameterType, 1);
					array[1] = text;
					for (int j = 2; j < parameters.Length; j++)
					{
						array[j] = (parameters[j].HasDefaultValue ? parameters[j].DefaultValue : (parameters[j].ParameterType.IsValueType ? Activator.CreateInstance(parameters[j].ParameterType) : null));
					}
					_message0.Invoke(player, array);
				}
			}
			catch
			{
			}
		}
	}
}