Decompiled source of Lareins AutoSmelters v1.0.1

plugins\Lareins_AutoSmelters\Lareins_AutoSmelters.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.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Lareins_Core;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Lareins_AutoSmelters")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Lareins_AutoSmelters")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("05875f51-bbd6-405c-9a31-2e0de280a741")]
[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_AutoSmelters;

[BepInPlugin("com.lareins.autosmelters", "Lareins_AutoSmelters", "4.4.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class AutoSmeltersPlugin : BaseUnityPlugin
{
	public const string ModGUID = "com.lareins.autosmelters";

	public const string ModName = "Lareins_AutoSmelters";

	public const string ModVersion = "4.4.0";

	public static ConfigEntry<bool> ModAktif;

	public static ConfigEntry<bool> BildirimGoster;

	public static ConfigEntry<KeyCode> AcKapatTusu;

	public static ConfigEntry<float> FirinMenzili;

	public static ConfigEntry<int> KomurStokLimiti;

	public static ConfigEntry<int> KulceStokLimiti;

	public static ConfigEntry<int> MinimumOdunKorumasi;

	private static float genelSayac;

	private void Awake()
	{
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		ModAktif = ((BaseUnityPlugin)this).Config.Bind<bool>("Genel", "ModAktif", true, "Firin ve komur ocagi otomasyonunu acar/kapatir");
		BildirimGoster = ((BaseUnityPlugin)this).Config.Bind<bool>("Genel", "BildirimGoster", true, "Firinlar malzeme cekip cikti koydugunda sol ustte bilgi gosterir");
		AcKapatTusu = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Kontroller", "AcKapatTusu", (KeyCode)111, "Baktiginiz firin/ocak otomasyonunu acip kapatma tusu");
		FirinMenzili = ((BaseUnityPlugin)this).Config.Bind<float>("Menziller", "FirinMenzili", 50f, "Firinlarin sandik tarama mesafesi (10m - 75m)");
		KomurStokLimiti = ((BaseUnityPlugin)this).Config.Bind<int>("Stok Limitleri", "KomurStokLimiti", 1000, "Sandiklarda bu kadar komur birikince komur ocagi durur (50 - 3000)");
		KulceStokLimiti = ((BaseUnityPlugin)this).Config.Bind<int>("Stok Limitleri", "KulceStokLimiti", 1000, "Sandiklarda bir kulceden bu kadar birikince o cevher eritilmez (50 - 3000)");
		MinimumOdunKorumasi = ((BaseUnityPlugin)this).Config.Bind<int>("Stok Limitleri", "MinimumOdunKorumasi", 50, "Odun sandiginda en az bu kadar odun kalirsa komur ocagi cekmez (0 - 1000)");
		CorePlugin.AyarEkle("Otonom Firinlar", "Mod Aktif", "Firin ve ocaklarin sandiktan beslenip bosalmasini acar.", ModAktif);
		CorePlugin.AyarEkle("Otonom Firinlar", "Islem Bildirimleri", "Firinlar malzeme alip cikti koydugunda sol ustte bilgi verir.", BildirimGoster);
		CorePlugin.AyarEkle("Otonom Firinlar", "Firin Ac/Kapat Tusu", "Baktiginiz firinin otomasyonunu acip kapatir.", AcKapatTusu);
		CorePlugin.AyarEkle("Otonom Firinlar", "Firin Tarama Menzili (m)", "Firinlarin sandiklara erisim mesafesi (10m - 75m).", FirinMenzili, 10f, 75f);
		CorePlugin.AyarEkle("Otonom Firinlar", "Komur Stok Limiti", "Sandiklarda hedef komur sayisina ulasilinca ocak durur.", KomurStokLimiti, 50, 3000);
		CorePlugin.AyarEkle("Otonom Firinlar", "Kulce Stok Limiti", "Sandiklarda hedef kulce sayisina ulasilinca firin durur.", KulceStokLimiti, 50, 3000);
		CorePlugin.AyarEkle("Otonom Firinlar", "Minimum Odun Korumasi", "Evdeki odun bu sayinin altina duserse komur yakilmaz.", MinimumOdunKorumasi, 0, 1000);
		new Harmony("com.lareins.autosmelters").PatchAll(typeof(AutoSmeltersPlugin));
	}

	private static void SandikZDOKaydet(ZNetView nv, Inventory inv)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		if (!((Object)(object)nv == (Object)null) && nv.IsValid() && inv != null)
		{
			nv.ClaimOwnership();
			ZPackage val = new ZPackage();
			inv.Save(val);
			nv.GetZDO().Set("items", val.GetBase64());
		}
	}

	[HarmonyPatch(typeof(Smelter), "OnHoverAddOre")]
	[HarmonyPostfix]
	private static void FirinEtiketi(Smelter __instance, ref string __result)
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		if (ModAktif.Value)
		{
			ZNetView componentInParent = ((Component)__instance).GetComponentInParent<ZNetView>();
			if ((Object)(object)componentInParent != (Object)null && componentInParent.IsValid())
			{
				string arg = (componentInParent.GetZDO().GetBool("Lareins_FirinOto", true) ? "<color=green>ACIK</color>" : "<color=red>KAPALI</color>");
				__result += $"\nOtomasyon: [{arg}] ([<color=orange>{AcKapatTusu.Value}</color>] Degistir)";
			}
		}
	}

	[HarmonyPatch(typeof(Smelter), "OnHoverAddFuel")]
	[HarmonyPostfix]
	private static void YakitEtiketi(Smelter __instance, ref string __result)
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		if (ModAktif.Value)
		{
			ZNetView componentInParent = ((Component)__instance).GetComponentInParent<ZNetView>();
			if ((Object)(object)componentInParent != (Object)null && componentInParent.IsValid())
			{
				string arg = (componentInParent.GetZDO().GetBool("Lareins_FirinOto", true) ? "<color=green>ACIK</color>" : "<color=red>KAPALI</color>");
				__result += $"\nOtomasyon: [{arg}] ([<color=orange>{AcKapatTusu.Value}</color>] Degistir)";
			}
		}
	}

	private static List<Container> MenzildekiSandiklar(Vector3 merkez)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		return (from c in Object.FindObjectsOfType<Container>()
			where (Object)(object)c != (Object)null && ((Behaviour)c).isActiveAndEnabled && !c.IsInUse() && c.GetInventory() != null
			where (Object)(object)((Component)c).GetComponentInParent<ZNetView>() != (Object)null && ((Component)c).GetComponentInParent<ZNetView>().IsValid()
			where Vector3.Distance(merkez, ((Component)c).transform.position) <= FirinMenzili.Value
			select c).ToList();
	}

	private void Update()
	{
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_047a: Unknown result type (might be due to invalid IL or missing references)
		if (!ModAktif.Value || (Object)(object)Player.m_localPlayer == (Object)null)
		{
			return;
		}
		if (!CorePlugin.HerhangiPencereAcikMi() && !TextInput.IsVisible() && !Minimap.IsOpen() && !InventoryGui.IsVisible() && Input.GetKeyDown(AcKapatTusu.Value))
		{
			GameObject hoverObject = ((Humanoid)Player.m_localPlayer).GetHoverObject();
			Smelter val = ((hoverObject != null) ? hoverObject.GetComponentInParent<Smelter>() : null);
			ZNetView val2 = ((val != null) ? ((Component)val).GetComponentInParent<ZNetView>() : null);
			if ((Object)(object)val2 != (Object)null && val2.IsValid())
			{
				val2.ClaimOwnership();
				bool flag = !val2.GetZDO().GetBool("Lareins_FirinOto", true);
				val2.GetZDO().Set("Lareins_FirinOto", flag);
				((Character)Player.m_localPlayer).Message((MessageType)2, "Firin Otomasyonu: " + (flag ? "ACIK" : "KAPALI"), 0, (Sprite)null, false);
			}
		}
		genelSayac += Time.deltaTime;
		if (genelSayac < 1f)
		{
			return;
		}
		genelSayac = 0f;
		try
		{
			List<Container> list = (from c in Object.FindObjectsOfType<Container>()
				where (Object)(object)c != (Object)null && ((Behaviour)c).isActiveAndEnabled && !c.IsInUse() && c.GetInventory() != null
				where (Object)(object)((Component)c).GetComponentInParent<ZNetView>() != (Object)null && ((Component)c).GetComponentInParent<ZNetView>().IsValid()
				select c).ToList();
			List<Smelter> list2 = (from f in Object.FindObjectsOfType<Smelter>()
				where (Object)(object)f != (Object)null && ((Behaviour)f).isActiveAndEnabled
				select f).ToList();
			if (list.Count == 0 || list2.Count == 0)
			{
				return;
			}
			foreach (Smelter firin in list2)
			{
				ZNetView componentInParent = ((Component)firin).GetComponentInParent<ZNetView>();
				if ((Object)(object)componentInParent == (Object)null || !componentInParent.IsValid() || !componentInParent.GetZDO().GetBool("Lareins_FirinOto", true))
				{
					continue;
				}
				List<Container> list3 = list.Where((Container c) => Vector3.Distance(((Component)firin).transform.position, ((Component)c).transform.position) <= FirinMenzili.Value).ToList();
				if (list3.Count == 0)
				{
					continue;
				}
				componentInParent.ClaimOwnership();
				float num = componentInParent.GetZDO().GetFloat("fuel", 0f);
				if ((Object)(object)firin.m_fuelItem != (Object)null && num <= (float)firin.m_maxFuel - 1f)
				{
					int sandikNo = EsyaKategorileri.KategoriBul(firin.m_fuelItem.m_itemData);
					if (SandiktanAl(((Component)firin).transform.position, list3, sandikNo, firin.m_fuelItem, out var _, out var _))
					{
						componentInParent.InvokeRPC("RPC_AddFuel", Array.Empty<object>());
					}
				}
				int num2 = componentInParent.GetZDO().GetInt("queued", 0);
				if (num2 >= firin.m_maxOre)
				{
					continue;
				}
				foreach (ItemConversion item in firin.m_conversion)
				{
					if ((Object)(object)item.m_from == (Object)null || (Object)(object)item.m_to == (Object)null)
					{
						continue;
					}
					int num3 = SandiklardaToplamSay(list3, item.m_to);
					int num4 = SandiklardaToplamSay(list3, item.m_from);
					if (num4 <= 0)
					{
						continue;
					}
					if (((Object)item.m_to).name == "Coal")
					{
						if (num3 >= KomurStokLimiti.Value || num4 <= MinimumOdunKorumasi.Value)
						{
							continue;
						}
					}
					else if (num3 >= KulceStokLimiti.Value)
					{
						continue;
					}
					int sandikNo2 = EsyaKategorileri.KategoriBul(item.m_from.m_itemData);
					if (!SandiktanAl(((Component)firin).transform.position, list3, sandikNo2, item.m_from, out var kaynakNo2, out var mesafe2))
					{
						continue;
					}
					componentInParent.InvokeRPC("RPC_AddOre", new object[2]
					{
						((Object)item.m_from).name,
						false
					});
					if (BildirimGoster.Value)
					{
						((Character)Player.m_localPlayer).Message((MessageType)1, $"Otomasyon: -1 {((Object)item.m_from).name} (Sandik #{kaynakNo2}, {mesafe2:F0}m)", 0, (Sprite)null, false);
					}
					break;
				}
			}
		}
		catch (Exception)
		{
		}
	}

	[HarmonyPatch(typeof(Smelter), "Spawn")]
	[HarmonyPrefix]
	private static bool CiktiyiSandigaKoy(Smelter __instance, string ore, int stack)
	{
		//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0300: Unknown result type (might be due to invalid IL or missing references)
		//IL_030c: Unknown result type (might be due to invalid IL or missing references)
		if (!ModAktif.Value || string.IsNullOrEmpty(ore) || stack <= 0)
		{
			return true;
		}
		ZNetView componentInParent = ((Component)__instance).GetComponentInParent<ZNetView>();
		if ((Object)(object)componentInParent != (Object)null && componentInParent.IsValid() && !componentInParent.GetZDO().GetBool("Lareins_FirinOto", true))
		{
			return true;
		}
		ItemDrop val = null;
		if (__instance.m_conversion != null && __instance.m_conversion.Count > 0)
		{
			ItemConversion val2 = ((IEnumerable<ItemConversion>)__instance.m_conversion).FirstOrDefault((Func<ItemConversion, bool>)((ItemConversion c) => (Object)(object)c.m_from != (Object)null && ((Object)c.m_from).name.Equals(ore, StringComparison.OrdinalIgnoreCase)));
			if (val2 == null && ((Object)__instance).name.IndexOf("kiln", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				val2 = __instance.m_conversion[0];
			}
			if (val2 != null && (Object)(object)val2.m_to != (Object)null)
			{
				val = val2.m_to;
			}
		}
		if ((Object)(object)val == (Object)null && (Object)(object)ObjectDB.instance != (Object)null)
		{
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(ore);
			if ((Object)(object)itemPrefab != (Object)null)
			{
				val = itemPrefab.GetComponent<ItemDrop>();
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			return true;
		}
		ZNetScene instance = ZNetScene.instance;
		GameObject dropPrefab = ((instance != null) ? instance.GetPrefab(((Object)val).name) : null) ?? ((Component)val).gameObject;
		int hedefNo = EsyaKategorileri.KategoriBul(val.m_itemData);
		List<Container> list = MenzildekiSandiklar(((Component)__instance).transform.position);
		if (list.Count == 0)
		{
			return true;
		}
		List<Container> list2 = list.OrderByDescending(delegate(Container s)
		{
			ZNetView componentInParent3 = ((Component)s).GetComponentInParent<ZNetView>();
			int? obj;
			if (componentInParent3 == null)
			{
				obj = null;
			}
			else
			{
				ZDO zDO = componentInParent3.GetZDO();
				obj = ((zDO != null) ? new int?(zDO.GetInt("Lareins_SandikNo", 0)) : ((int?)null));
			}
			int? num4 = obj;
			int valueOrDefault = num4.GetValueOrDefault();
			return (valueOrDefault == hedefNo && hedefNo != 0) ? 4 : (valueOrDefault switch
			{
				3 => 3, 
				0 => 2, 
				_ => 1, 
			});
		}).ThenBy((Container s) => Vector3.Distance(((Component)__instance).transform.position, ((Component)s).transform.position)).ToList();
		int num = stack;
		int num2 = 0;
		float num3 = 0f;
		foreach (Container item in list2)
		{
			if (num <= 0)
			{
				break;
			}
			ZNetView componentInParent2 = ((Component)item).GetComponentInParent<ZNetView>();
			if ((Object)(object)componentInParent2 == (Object)null || !componentInParent2.IsValid())
			{
				continue;
			}
			Inventory inventory = item.GetInventory();
			if (inventory != null)
			{
				ItemData val3 = val.m_itemData.Clone();
				val3.m_dropPrefab = dropPrefab;
				val3.m_stack = num;
				if (inventory.CanAddItem(val3, num) && inventory.AddItem(val3))
				{
					SandikZDOKaydet(componentInParent2, inventory);
					num2 = componentInParent2.GetZDO().GetInt("Lareins_SandikNo", 0);
					num3 = Vector3.Distance(((Component)__instance).transform.position, ((Component)item).transform.position);
					num = 0;
					break;
				}
			}
		}
		if (num == 0 && BildirimGoster.Value && (Object)(object)Player.m_localPlayer != (Object)null)
		{
			((Character)Player.m_localPlayer).Message((MessageType)1, $"Otomasyon: +{stack} {((Object)val).name} -> Sandik #{num2} ({num3:F0}m)", 0, (Sprite)null, false);
		}
		return num > 0;
	}

	private static bool EsyaEslesirMi(ItemData sandikEsyasi, ItemDrop hedefPrefab)
	{
		if (sandikEsyasi == null || (Object)(object)hedefPrefab == (Object)null)
		{
			return false;
		}
		if ((Object)(object)sandikEsyasi.m_dropPrefab != (Object)null && ((Object)sandikEsyasi.m_dropPrefab).name == ((Object)hedefPrefab).name)
		{
			return true;
		}
		if (sandikEsyasi.m_shared != null && hedefPrefab.m_itemData?.m_shared != null && sandikEsyasi.m_shared.m_name == hedefPrefab.m_itemData.m_shared.m_name)
		{
			return true;
		}
		return false;
	}

	private static int SandiklardaToplamSay(List<Container> sandiklar, ItemDrop hedefPrefab)
	{
		int num = 0;
		foreach (Container item in sandiklar)
		{
			Inventory inventory = item.GetInventory();
			if (inventory == null)
			{
				continue;
			}
			List<ItemData> allItems = inventory.GetAllItems();
			if (allItems == null)
			{
				continue;
			}
			foreach (ItemData item2 in allItems)
			{
				if (EsyaEslesirMi(item2, hedefPrefab))
				{
					num += item2.m_stack;
				}
			}
		}
		return num;
	}

	private static bool SandiktanAl(Vector3 merkez, List<Container> sandiklar, int sandikNo, ItemDrop hedefPrefab, out int kaynakNo, out float mesafe)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_012c: Unknown result type (might be due to invalid IL or missing references)
		kaynakNo = 0;
		mesafe = 0f;
		List<Container> list = sandiklar.OrderByDescending(delegate(Container s)
		{
			ZNetView componentInParent2 = ((Component)s).GetComponentInParent<ZNetView>();
			int? obj;
			if (componentInParent2 == null)
			{
				obj = null;
			}
			else
			{
				ZDO zDO = componentInParent2.GetZDO();
				obj = ((zDO != null) ? new int?(zDO.GetInt("Lareins_SandikNo", 0)) : ((int?)null));
			}
			int? num = obj;
			int valueOrDefault = num.GetValueOrDefault();
			if (valueOrDefault == sandikNo && sandikNo != 0)
			{
				return 3;
			}
			return (valueOrDefault != 0) ? 1 : 2;
		}).ThenBy((Container s) => Vector3.Distance(merkez, ((Component)s).transform.position)).ToList();
		foreach (Container item in list)
		{
			ZNetView componentInParent = ((Component)item).GetComponentInParent<ZNetView>();
			if ((Object)(object)componentInParent == (Object)null || !componentInParent.IsValid())
			{
				continue;
			}
			Inventory inventory = item.GetInventory();
			if (inventory == null)
			{
				continue;
			}
			List<ItemData> allItems = inventory.GetAllItems();
			if (allItems != null)
			{
				ItemData val = ((IEnumerable<ItemData>)allItems).FirstOrDefault((Func<ItemData, bool>)((ItemData i) => EsyaEslesirMi(i, hedefPrefab)));
				if (val != null)
				{
					inventory.RemoveOneItem(val);
					SandikZDOKaydet(componentInParent, inventory);
					kaynakNo = componentInParent.GetZDO().GetInt("Lareins_SandikNo", 0);
					mesafe = Vector3.Distance(merkez, ((Component)item).transform.position);
					return true;
				}
			}
		}
		return false;
	}
}