Decompiled source of ComfyAutoRepair v1.0.0

ComfyAutoRepair.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ComfyAutoRepair")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ComfyAutoRepair")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("baa1b1fe-aa04-4786-8726-415268475c32")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ComfyAutoRepair
{
	[BepInPlugin("redseiko.valheim.comfyautorepair", "ComfyAutoRepair", "1.0.0")]
	public sealed class ComfyAutoRepair : BaseUnityPlugin
	{
		public const string PluginGuid = "redseiko.valheim.comfyautorepair";

		public const string PluginName = "ComfyAutoRepair";

		public const string PluginVersion = "1.0.0";

		private static ManualLogSource _logger;

		private void Awake()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.comfyautorepair");
		}

		public static void LogInfo(object obj)
		{
			_logger.LogInfo((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}
	}
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<bool> ShowVanillaRepairMessage { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			IsModEnabled = config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod.");
			ShowVanillaRepairMessage = config.Bind<bool>("Message", "showVanillaRepairMessage", false, "If set, shows the vanilla repair message for each item repaired.");
		}
	}
	public static class RecipeManager
	{
		private static readonly Dictionary<string, Recipe> _recipeByItemNameCache = new Dictionary<string, Recipe>();

		public static void Reset()
		{
			_recipeByItemNameCache.Clear();
		}

		public static bool TryGetRecipe(ItemData item, out Recipe recipe)
		{
			string name = item.m_shared.m_name;
			if (!_recipeByItemNameCache.TryGetValue(name, out recipe))
			{
				recipe = GetRecipeByItemName(name);
				_recipeByItemNameCache[name] = recipe;
			}
			return Object.op_Implicit((Object)(object)recipe);
		}

		public static Recipe GetRecipeByItemName(string itemName)
		{
			foreach (Recipe recipe in ObjectDB.m_instance.m_recipes)
			{
				if (Object.op_Implicit((Object)(object)recipe.m_item) && recipe.m_item.m_itemData.m_shared.m_name == itemName)
				{
					return recipe;
				}
			}
			return null;
		}
	}
	public static class RepairManager
	{
		public static bool RepairAllItems(Player player, CraftingStation craftingStation, bool checkUsable = true)
		{
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)player))
			{
				return false;
			}
			bool flag = player.NoCostCheat();
			if ((!flag && !Object.op_Implicit((Object)(object)craftingStation)) || (checkUsable && !craftingStation.CheckUsable(player, false)))
			{
				return false;
			}
			string name = craftingStation.m_name;
			int craftingStationLevel = Mathf.Min(craftingStation.GetLevel(true), 4);
			int num = 0;
			bool value = PluginConfig.ShowVanillaRepairMessage.Value;
			foreach (ItemData allItem in ((Humanoid)player).GetInventory().GetAllItems())
			{
				if (!allItem.m_shared.m_useDurability || !allItem.m_shared.m_canBeReparied)
				{
					continue;
				}
				float maxDurability = allItem.GetMaxDurability(allItem.m_quality);
				if (!(allItem.m_durability >= maxDurability) && (flag || CanRepairItem(allItem, name, craftingStationLevel)))
				{
					((Character)player).RaiseSkill((SkillType)107, 1f - allItem.m_durability / maxDurability);
					allItem.m_durability = maxDurability;
					if (value)
					{
						((Character)player).Message((MessageType)2, Localization.instance.Localize("$msg_repaired", new string[1] { allItem.m_shared.m_name }), 0, (Sprite)null);
					}
					num++;
				}
			}
			if (num > 0)
			{
				ComfyAutoRepair.LogInfo($"Repaired {num} items.");
				if (Object.op_Implicit((Object)(object)craftingStation))
				{
					EffectList repairItemDoneEffects = craftingStation.m_repairItemDoneEffects;
					if (repairItemDoneEffects != null)
					{
						repairItemDoneEffects.Create(((Component)craftingStation).transform.position, Quaternion.identity, (Transform)null, 1f, -1);
					}
				}
			}
			return true;
		}

		public static bool CanRepairItem(ItemData item, string craftingStationName, int craftingStationLevel)
		{
			if (RecipeManager.TryGetRecipe(item, out var recipe) && (Object.op_Implicit((Object)(object)recipe.m_craftingStation) || Object.op_Implicit((Object)(object)recipe.m_repairStation)) && ((Object.op_Implicit((Object)(object)recipe.m_repairStation) && recipe.m_repairStation.m_name == craftingStationName) || (Object.op_Implicit((Object)(object)recipe.m_craftingStation) && recipe.m_craftingStation.m_name == craftingStationName) || item.m_worldLevel < Game.m_worldLevel))
			{
				return craftingStationLevel >= recipe.m_minStationLevel;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(CraftingStation))]
	internal static class CraftingStationPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("Interact")]
		private static void InteractPostfix(CraftingStation __instance, Humanoid user, bool repeat)
		{
			if (!repeat && PluginConfig.IsModEnabled.Value && (Object)(object)user == (Object)(object)Player.m_localPlayer && (Object)(object)__instance == (Object)(object)Player.m_localPlayer.GetCurrentCraftingStation())
			{
				RepairManager.RepairAllItems(Player.m_localPlayer, __instance, checkUsable: false);
			}
		}
	}
	[HarmonyPatch(typeof(ObjectDB))]
	internal static class ObjectDBPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("CopyOtherDB")]
		private static void CopyOtherDBPostfix()
		{
			RecipeManager.Reset();
		}

		[HarmonyPrefix]
		[HarmonyPatch("GetRecipe")]
		private static bool GetRecipePrefix(InventoryGui __instance, ItemData item, ref Recipe __result)
		{
			if (PluginConfig.IsModEnabled.Value)
			{
				RecipeManager.TryGetRecipe(item, out __result);
				return false;
			}
			return true;
		}
	}
}