Decompiled source of Lareins CraftFromContainers v1.0.0

plugins\Lareins_CraftFromContainers\Lareins_CraftFromContainers.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Lareins_Core;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Lareins_CraftFromContainers")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Lareins_CraftFromContainers")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("66f365f6-db71-4dea-9bd9-e992d4274b67")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Lareins_CraftFromContainers;

[BepInPlugin("com.lareins.craftfromcontainers", "Lareins_CraftFromContainers", "1.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class CraftFromContainersPlugin : BaseUnityPlugin
{
	public const string ModGUID = "com.lareins.craftfromcontainers";

	public const string ModName = "Lareins_CraftFromContainers";

	public const string ModVersion = "1.1.0";

	public static CraftFromContainersPlugin Instance;

	private Harmony _harmony;

	public static ConfigEntry<bool> ModAktif;

	public static ConfigEntry<float> TaramaMenzili;

	public static ConfigEntry<bool> MesajGoster;

	private static readonly List<Container> _onbellekSandiklar = new List<Container>();

	private static float _sonTaramaZamani = -10f;

	private static readonly MethodInfo _miContainerSave = AccessTools.Method(typeof(Container), "Save", (Type[])null, (Type[])null);

	private static readonly MethodInfo _miInvChanged = AccessTools.Method(typeof(Inventory), "Changed", (Type[])null, (Type[])null);

	private void Awake()
	{
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Expected O, but got Unknown
		Instance = this;
		ModAktif = ((BaseUnityPlugin)this).Config.Bind<bool>("Genel", "ModAktif", true, "Tezgah, ocak ve el uretiminde yakin sandiklardaki esyalari kullanir.");
		TaramaMenzili = ((BaseUnityPlugin)this).Config.Bind<float>("Genel", "TaramaMenzili", 25f, "Sandiklarin taranacagi maksimum mesafe (metre).");
		MesajGoster = ((BaseUnityPlugin)this).Config.Bind<bool>("Genel", "MesajGoster", false, "Sandiktan malzeme cekildiginde sol ustte bilgi mesaji gosterir.");
		string text = "Sandiktan Uretim";
		CorePlugin.AyarEkle(text, "Sandiktan Uretim Aktif", "Cevredeki sandiklari uretim ve yukseltme masalarina baglar.", ModAktif);
		CorePlugin.AyarEkle(text, "Tarama Menzili (m)", "Sandiklarin algilanacagi mesafe (Varsayilan: 25m).", TaramaMenzili, 5f, 60f);
		CorePlugin.AyarEkle(text, "Malzeme Bilgi Mesaji", "Sandiktan malzeme kullanildiginda bildirim gosterir.", MesajGoster);
		_harmony = new Harmony("com.lareins.craftfromcontainers");
		YamalariUygula();
	}

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

	private void YamalariUygula()
	{
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Expected O, but got Unknown
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Expected O, but got Unknown
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Expected O, but got Unknown
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Expected O, but got Unknown
		MethodInfo[] methods = typeof(Player).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		foreach (MethodInfo methodInfo in methods)
		{
			if (methodInfo.Name != "HaveRequirements" || methodInfo.ReturnType != typeof(bool))
			{
				continue;
			}
			ParameterInfo[] parameters = methodInfo.GetParameters();
			if (parameters.Length >= 1 && parameters[0].ParameterType == typeof(Recipe))
			{
				try
				{
					_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(CraftFromContainersPlugin), "Postfix_HaveRequirements_Recipe", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				catch
				{
				}
			}
			else if (parameters.Length >= 1 && parameters[0].ParameterType == typeof(Requirement[]))
			{
				try
				{
					_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(CraftFromContainersPlugin), "Postfix_HaveRequirements_Array", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				catch
				{
				}
			}
		}
		MethodInfo methodInfo2 = AccessTools.Method(typeof(Player), "ConsumeResources", (Type[])null, (Type[])null);
		if (methodInfo2 != null)
		{
			try
			{
				_harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(CraftFromContainersPlugin), "Prefix_ConsumeResources", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch
			{
			}
		}
		MethodInfo methodInfo3 = AccessTools.Method(typeof(InventoryGui), "SetupRequirement", (Type[])null, (Type[])null);
		if (methodInfo3 != null)
		{
			try
			{
				_harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(CraftFromContainersPlugin), "Postfix_SetupRequirement", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch
			{
			}
		}
	}

	public static List<Container> YakinSandiklariGetir(Player player, bool zorlaYenile = false)
	{
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null || !ModAktif.Value)
		{
			_onbellekSandiklar.Clear();
			return _onbellekSandiklar;
		}
		if (!zorlaYenile && Time.unscaledTime - _sonTaramaZamani < 0.5f)
		{
			return _onbellekSandiklar;
		}
		_sonTaramaZamani = Time.unscaledTime;
		_onbellekSandiklar.Clear();
		Vector3 position = ((Component)player).transform.position;
		float value = TaramaMenzili.Value;
		float menzilKare = value * value;
		Container[] array = Object.FindObjectsByType<Container>((FindObjectsSortMode)0);
		foreach (Container val in array)
		{
			if (SandikGecerliMi(val, position, menzilKare))
			{
				_onbellekSandiklar.Add(val);
			}
		}
		return _onbellekSandiklar;
	}

	private static bool SandikGecerliMi(Container c, Vector3 oyuncuKonum, float menzilKare)
	{
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)c == (Object)null || c.GetInventory() == null)
		{
			return false;
		}
		ZNetView component = ((Component)c).GetComponent<ZNetView>();
		if ((Object)(object)component == (Object)null || !component.IsValid())
		{
			return false;
		}
		Vector3 val = ((Component)c).transform.position - oyuncuKonum;
		if (((Vector3)(ref val)).sqrMagnitude > menzilKare)
		{
			return false;
		}
		if (c.IsInUse())
		{
			return false;
		}
		return true;
	}

	public static int SandiklardaEsyaSay(Player player, string esyaAdi, int kalite = -1)
	{
		if ((Object)(object)player == (Object)null || string.IsNullOrEmpty(esyaAdi) || !ModAktif.Value)
		{
			return 0;
		}
		int num = 0;
		List<Container> list = YakinSandiklariGetir(player);
		for (int i = 0; i < list.Count; i++)
		{
			Inventory inventory = list[i].GetInventory();
			if (inventory != null)
			{
				num += inventory.CountItems(esyaAdi, kalite, true);
			}
		}
		return num;
	}

	public static void SandiklardanEsyaDus(Player player, string esyaAdi, int miktar, int kalite = -1)
	{
		if ((Object)(object)player == (Object)null || miktar <= 0 || string.IsNullOrEmpty(esyaAdi))
		{
			return;
		}
		int num = miktar;
		List<Container> list = YakinSandiklariGetir(player, zorlaYenile: true);
		for (int i = 0; i < list.Count; i++)
		{
			if (num <= 0)
			{
				break;
			}
			Container val = list[i];
			Inventory inventory = val.GetInventory();
			if (inventory != null)
			{
				int num2 = inventory.CountItems(esyaAdi, kalite, true);
				if (num2 > 0)
				{
					int num3 = Mathf.Min(num, num2);
					inventory.RemoveItem(esyaAdi, num3, kalite, true);
					num -= num3;
					SandikKaydet(val, inventory);
				}
			}
		}
		if (!MesajGoster.Value || miktar <= num)
		{
			return;
		}
		string arg = esyaAdi;
		Type type = AccessTools.TypeByName("Localization");
		if (type != null)
		{
			object obj = AccessTools.Property(type, "instance")?.GetValue(null, null);
			MethodInfo methodInfo = AccessTools.Method(type, "Localize", new Type[1] { typeof(string) }, (Type[])null);
			if (obj != null && methodInfo != null)
			{
				arg = (methodInfo.Invoke(obj, new object[1] { esyaAdi }) as string) ?? esyaAdi;
			}
		}
		((Character)player).Message((MessageType)1, $"Sandıktan kullanıldı: {miktar - num}x {arg}", 0, (Sprite)null, false);
	}

	private static void SandikKaydet(Container c, Inventory inv)
	{
		if ((Object)(object)c == (Object)null)
		{
			return;
		}
		try
		{
			if (_miInvChanged != null && inv != null)
			{
				_miInvChanged.Invoke(inv, null);
			}
			else if (AccessTools.Field(typeof(Inventory), "m_onChanged")?.GetValue(inv) is Action action)
			{
				action();
			}
			if (_miContainerSave != null)
			{
				_miContainerSave.Invoke(c, null);
			}
		}
		catch
		{
		}
	}

	private static bool GereksinimlerKarsilaniyorMu(Player player, Requirement[] reqs, bool discover, int qualityLevel)
	{
		if (discover || reqs == null || (Object)(object)player == (Object)null)
		{
			return false;
		}
		Inventory inventory = ((Humanoid)player).GetInventory();
		if (inventory == null)
		{
			return false;
		}
		foreach (Requirement val in reqs)
		{
			if (val == null || (Object)(object)val.m_resItem == (Object)null)
			{
				continue;
			}
			int amount = val.GetAmount(qualityLevel);
			if (amount <= 0)
			{
				continue;
			}
			string name = val.m_resItem.m_itemData.m_shared.m_name;
			int num = inventory.CountItems(name, -1, true);
			if (num < amount)
			{
				int num2 = SandiklardaEsyaSay(player, name);
				if (num + num2 < amount)
				{
					return false;
				}
			}
		}
		return true;
	}

	private static void Postfix_HaveRequirements_Recipe(Player __instance, Recipe recipe, bool discover, int qualityLevel, ref bool __result)
	{
		if (!__result && ModAktif.Value && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && !((Object)(object)recipe == (Object)null) && GereksinimlerKarsilaniyorMu(__instance, recipe.m_resources, discover, qualityLevel))
		{
			__result = true;
		}
	}

	private static void Postfix_HaveRequirements_Array(Player __instance, Requirement[] requirements, bool discover, int qualityLevel, ref bool __result)
	{
		if (!__result && ModAktif.Value && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && GereksinimlerKarsilaniyorMu(__instance, requirements, discover, qualityLevel))
		{
			__result = true;
		}
	}

	private static bool Prefix_ConsumeResources(Player __instance, Requirement[] requirements, int qualityLevel, int itemQuality)
	{
		if (!ModAktif.Value || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || requirements == null)
		{
			return true;
		}
		Inventory inventory = ((Humanoid)__instance).GetInventory();
		if (inventory == null)
		{
			return true;
		}
		foreach (Requirement val in requirements)
		{
			if (val == null || (Object)(object)val.m_resItem == (Object)null)
			{
				continue;
			}
			int amount = val.GetAmount(qualityLevel);
			if (amount <= 0)
			{
				continue;
			}
			string name = val.m_resItem.m_itemData.m_shared.m_name;
			int num = inventory.CountItems(name, itemQuality, true);
			if (num >= amount)
			{
				inventory.RemoveItem(name, amount, itemQuality, true);
				continue;
			}
			if (num > 0)
			{
				inventory.RemoveItem(name, num, itemQuality, true);
			}
			int miktar = amount - num;
			SandiklardanEsyaDus(__instance, name, miktar, itemQuality);
		}
		return false;
	}

	private static void Postfix_SetupRequirement(Transform elementRoot, Requirement req, Player player, bool craft, int quality, int craftMultiplier)
	{
		//IL_0214: Unknown result type (might be due to invalid IL or missing references)
		//IL_020d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_0241: Unknown result type (might be due to invalid IL or missing references)
		if (!ModAktif.Value || (Object)(object)elementRoot == (Object)null || req == null || (Object)(object)req.m_resItem == (Object)null || (Object)(object)player == (Object)null)
		{
			return;
		}
		int num = req.GetAmount(quality) * Mathf.Max(1, craftMultiplier);
		if (num <= 0)
		{
			return;
		}
		string name = req.m_resItem.m_itemData.m_shared.m_name;
		int num2 = ((((Humanoid)player).GetInventory() != null) ? ((Humanoid)player).GetInventory().CountItems(name, -1, true) : 0);
		int num3 = SandiklardaEsyaSay(player, name);
		int num4 = num2 + num3;
		MonoBehaviour[] componentsInChildren = ((Component)elementRoot).GetComponentsInChildren<MonoBehaviour>(true);
		foreach (MonoBehaviour val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null))
			{
				string text = ((object)val).GetType().Name.ToLowerInvariant();
				if (text.Contains("blink") || text.Contains("flash"))
				{
					((Behaviour)val).enabled = false;
				}
			}
		}
		Component[] componentsInChildren2 = ((Component)elementRoot).GetComponentsInChildren<Component>(true);
		foreach (Component val2 in componentsInChildren2)
		{
			if ((Object)(object)val2 == (Object)null)
			{
				continue;
			}
			PropertyInfo property = ((object)val2).GetType().GetProperty("text");
			if (property == null)
			{
				continue;
			}
			string text2 = ((Object)val2.gameObject).name.ToLowerInvariant();
			string text3 = property.GetValue(val2, null) as string;
			if (text2.Contains("amount") || (!string.IsNullOrEmpty(text3) && text3.Contains("/")))
			{
				property.SetValue(val2, $"{num4}/{num}", null);
				PropertyInfo property2 = ((object)val2).GetType().GetProperty("color");
				if (property2 != null)
				{
					property2.SetValue(val2, (num4 >= num) ? Color.white : Color.red, null);
				}
				Graphic val3 = (Graphic)(object)((val2 is Graphic) ? val2 : null);
				if (val3 != null)
				{
					val3.CrossFadeColor((num4 >= num) ? Color.white : Color.red, 0f, true, true);
				}
				break;
			}
		}
	}
}