Decompiled source of EmeraudeCoffres v1.2.0

plugins/EmeraudeCoffres.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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("EmeraudeCoffres")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0")]
[assembly: AssemblyProduct("EmeraudeCoffres")]
[assembly: AssemblyTitle("EmeraudeCoffres")]
[assembly: AssemblyVersion("1.2.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 EmeraudeCoffres
{
	[HarmonyPatch(typeof(Player), "HaveRequirementItems", new Type[]
	{
		typeof(Recipe),
		typeof(bool),
		typeof(int),
		typeof(int)
	})]
	internal static class HaveRequirementItemsPatch
	{
		[HarmonyPriority(0)]
		private static void Postfix(Player __instance, Recipe piece, bool discover, int qualityLevel, int amount, ref bool __result)
		{
			if (!(__result || discover) && piece.m_requireOnlyOneIngredient && Chests.Active())
			{
				__result = Chests.FindIngredient(__instance, piece, qualityLevel, amount) != null;
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
	internal static class DoCraftingPatch
	{
		private static void Prefix(Player player, Recipe ___m_craftRecipe, ItemData ___m_craftUpgradeItem, bool ___m_multiCrafting, int ___m_multiCraftAmount)
		{
			if ((Object)(object)___m_craftRecipe == (Object)null || !___m_craftRecipe.m_requireOnlyOneIngredient || ___m_craftUpgradeItem != null || !Chests.Active())
			{
				return;
			}
			int num = ((!___m_multiCrafting) ? 1 : ___m_multiCraftAmount);
			int num2 = default(int);
			int num3 = default(int);
			if (player.GetFirstRequiredItem(((Humanoid)player).GetInventory(), ___m_craftRecipe, 1, ref num2, ref num3, num) == null)
			{
				Ingredient ingredient = Chests.FindIngredient(player, ___m_craftRecipe, 1, num);
				if (ingredient != null)
				{
					Chests.Pull(player, ingredient);
				}
			}
		}
	}
	internal class Ingredient
	{
		internal string Name;

		internal int Quality;

		internal int Missing;

		internal List<Container> Containers;
	}
	internal static class Chests
	{
		private static ConfigEntry<bool> modEnabled;

		private static Func<bool> allowByKey;

		private static Func<Vector3, List<Container>> nearbyContainers;

		internal static bool Bind()
		{
			Type type = AccessTools.TypeByName("CraftFromContainers.BepInExPlugin");
			if (type == null)
			{
				return false;
			}
			modEnabled = (ConfigEntry<bool>)AccessTools.Field(type, "modEnabled").GetValue(null);
			allowByKey = AccessTools.MethodDelegate<Func<bool>>(AccessTools.Method(type, "AllowByKey", (Type[])null, (Type[])null), (object)null, true);
			nearbyContainers = AccessTools.MethodDelegate<Func<Vector3, List<Container>>>(AccessTools.Method(type, "GetNearbyContainers", (Type[])null, (Type[])null), (object)null, true);
			return true;
		}

		internal static bool Active()
		{
			if (modEnabled.Value)
			{
				return allowByKey();
			}
			return false;
		}

		internal static Ingredient FindIngredient(Player player, Recipe recipe, int qualityLevel, int amount)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			CraftingStation currentCraftingStation = player.GetCurrentCraftingStation();
			List<Container> list = nearbyContainers(((Component)player).transform.position);
			Requirement[] resources = recipe.m_resources;
			foreach (Requirement val in resources)
			{
				if (!Object.op_Implicit((Object)(object)val.m_resItem) || (((Object)(object)currentCraftingStation != (Object)null) ? (currentCraftingStation.m_upgrader != val.m_upgraderResource) : val.m_upgraderResource))
				{
					continue;
				}
				int num = val.GetAmount(qualityLevel) * amount;
				if (num <= 0)
				{
					continue;
				}
				SharedData shared = val.m_resItem.m_itemData.m_shared;
				int quality;
				for (quality = 1; quality <= shared.m_maxQuality; quality++)
				{
					int num2 = ((Humanoid)player).GetInventory().CountItems(shared.m_name, quality, true);
					if (num2 + list.Sum((Container c) => c.GetInventory().CountItems(shared.m_name, quality, true)) >= num)
					{
						return new Ingredient
						{
							Name = shared.m_name,
							Quality = quality,
							Missing = num - num2,
							Containers = list
						};
					}
				}
			}
			return null;
		}

		internal static void Pull(Player player, Ingredient ingredient)
		{
			Inventory inventory = ((Humanoid)player).GetInventory();
			int num = ingredient.Missing;
			foreach (Container container in ingredient.Containers)
			{
				foreach (ItemData item in (from i in container.GetInventory().GetAllItems()
					where i.m_shared.m_name == ingredient.Name && i.m_quality == ingredient.Quality && i.m_worldLevel >= Game.m_worldLevel
					select i).ToList())
				{
					int num2 = Math.Min(item.m_stack, num);
					if (!inventory.CanAddItem(item, num2))
					{
						((Character)player).Message((MessageType)2, L.T("Inventory full", "Inventaire plein"), 0, (Sprite)null, false);
						return;
					}
					ZNetView component = ((Component)container).GetComponent<ZNetView>();
					if (component != null)
					{
						component.ClaimOwnership();
					}
					ItemData val = item.Clone();
					val.m_stack = num2;
					inventory.AddItem(val);
					container.GetInventory().RemoveItem(item, num2);
					num -= num2;
					if (num <= 0)
					{
						return;
					}
				}
			}
		}
	}
	public enum Language
	{
		English,
		French
	}
	internal static class L
	{
		public static bool Fr
		{
			get
			{
				if (Plugin.Lang != null)
				{
					return Plugin.Lang.Value == Language.French;
				}
				return false;
			}
		}

		public static string T(string en, string fr)
		{
			if (!Fr)
			{
				return en;
			}
			return fr;
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "UpdateRecipe")]
	internal static class UpdateRecipePatch
	{
		private static void Prefix(InventoryGui __instance, Player player, ref int ___m_multiCraftAmount)
		{
			if (ZInput.GetButton("AltPlace") || ZInput.GetButton("JoyLStick"))
			{
				___m_multiCraftAmount = MultiCraft.Amount(player, MultiCraft.Selected(__instance));
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "OnCraftPressed")]
	internal static class OnCraftPressedPatch
	{
		private static void Prefix(InventoryGui __instance, ref int ___m_multiCraftAmount)
		{
			___m_multiCraftAmount = MultiCraft.Amount(Player.m_localPlayer, MultiCraft.Selected(__instance));
		}
	}
	internal static class MultiCraft
	{
		private const int Limit = 999;

		private const float CacheSeconds = 0.5f;

		private static Recipe cachedRecipe;

		private static float cachedTime;

		private static int cachedAmount;

		internal static Recipe Selected(InventoryGui gui)
		{
			return Traverse.Create((object)gui).Field("m_selectedRecipe").Property("Recipe", (object[])null)
				.GetValue<Recipe>();
		}

		internal static int Amount(Player player, Recipe recipe)
		{
			int value = Plugin.MultiCraftAmount.Value;
			if (value > 0)
			{
				return value;
			}
			if ((Object)(object)player == (Object)null || (Object)(object)recipe == (Object)null)
			{
				return 1;
			}
			if ((Object)(object)recipe == (Object)(object)cachedRecipe && Time.time - cachedTime < 0.5f)
			{
				return cachedAmount;
			}
			cachedRecipe = recipe;
			cachedTime = Time.time;
			cachedAmount = Max(player, recipe);
			return cachedAmount;
		}

		private static int Max(Player player, Recipe recipe)
		{
			if (!Possible(player, recipe, 1))
			{
				return 1;
			}
			int num = 1;
			int num2 = 2;
			while (num2 <= 999 && Possible(player, recipe, num2))
			{
				num = num2;
				num2 *= 2;
			}
			if (num2 > 999)
			{
				if (Possible(player, recipe, 999))
				{
					return 999;
				}
				num2 = 999;
			}
			while (num2 - num > 1)
			{
				int num3 = (num + num2) / 2;
				if (Possible(player, recipe, num3))
				{
					num = num3;
				}
				else
				{
					num2 = num3;
				}
			}
			return num;
		}

		private static bool Possible(Player player, Recipe recipe, int amount)
		{
			if (player.HaveRequirements(recipe, false, 1, amount))
			{
				return ((Humanoid)player).GetInventory().CanAddItem(((Component)recipe.m_item).gameObject, recipe.m_amount * amount);
			}
			return false;
		}
	}
	[BepInPlugin("emeraude.coffres", "Emeraude Coffres", "1.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "emeraude.coffres";

		public const string Name = "Emeraude Coffres";

		public const string Version = "1.2.0";

		private const string CraftFromContainersGuid = "aedenthorn.CraftFromContainers";

		internal static ConfigEntry<Language> Lang;

		internal static ConfigEntry<int> MultiCraftAmount;

		private void Awake()
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			Lang = ((BaseUnityPlugin)this).Config.Bind<Language>("General", "Language", Language.English, "Language of the in-game messages. / Langue des messages en jeu.");
			MultiCraftAmount = ((BaseUnityPlugin)this).Config.Bind<int>("Crafting", "MultiCraftAmount", 0, new ConfigDescription("How many items Shift + Craft makes at once (5 in the base game). 0 = as many as possible: ingredients, chests in range and free inventory space. / Nombre d'objets fabriqués d'un coup avec Maj + Fabriquer (5 dans le jeu de base). 0 = autant que possible : ingrédients, coffres à portée et place dans l'inventaire.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 999), Array.Empty<object>()));
			if (!Chests.Bind())
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Emeraude Coffres : Craft From Containers introuvable, rien à compléter");
				return;
			}
			new Harmony("emeraude.coffres").PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Emeraude Coffres 1.2.0 loaded");
		}
	}
}