Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of Forgekeeper v0.1.0
plugins/Forgekeeper/Forgekeeper.dll
Decompiled 16 hours agousing System; using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: AssemblyVersion("0.0.0.0")] namespace Forgekeeper; [BepInPlugin("felih.Forgekeeper", "Forgekeeper", "0.1.0")] public class Plugin : BaseUnityPlugin { public const string GUID = "felih.Forgekeeper"; public const string NAME = "Forgekeeper"; public const string VERSION = "0.1.0"; internal static ManualLogSource Log; internal static ConfigEntry<bool> Enabled; internal static ConfigEntry<bool> Fuel; internal static ConfigEntry<bool> Input; internal static ConfigEntry<bool> Output; internal static ConfigEntry<bool> FuelOnly; internal static ConfigEntry<bool> DetailedLog; internal static ConfigEntry<float> ChestRange; internal static ConfigEntry<float> StationRange; internal static ConfigEntry<float> Interval; internal static ConfigEntry<string> NeverFeed; internal static ConfigEntry<string> SkipStations; internal static ConfigEntry<string> KeepInChests; private void Awake() { //IL_005a: Unknown result type (might be due to invalid IL or missing references) //IL_0064: Expected O, but got Unknown //IL_0098: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Expected O, but got Unknown //IL_00d6: Unknown result type (might be due to invalid IL or missing references) //IL_00e0: Expected O, but got Unknown //IL_01f6: Unknown result type (might be due to invalid IL or missing references) Log = ((BaseUnityPlugin)this).Logger; Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Stations near you feed themselves from nearby chests and put what they make into them."); StationRange = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "StationRange", 40f, new ConfigDescription("Stations within this distance of you (metres) are looked after.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 100f), new object[0])); ChestRange = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "ChestRange", 30f, new ConfigDescription("How far from a station (metres) its chests may stand.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 50f), new object[0])); Interval = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "Interval", 5f, new ConfigDescription("Seconds between two checks.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 30f), new object[0])); Fuel = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - What it does", "AddFuel", true, "Fuel (coal, wood, soft tissue...) is taken from the chests and put in."); Input = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - What it does", "AddInput", true, "What a station turns into something else (ore, scrap, wood, sap, flax, barley...) is taken from the chests and put in."); Output = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - What it does", "StoreOutput", true, "What the station made (ingots, coal, eitr, linen, flour...) is collected and stored in the chests instead of piling up or lying on the ground."); KeepInChests = ((BaseUnityPlugin)this).Config.Bind<string>("2 - What it does", "KeepInChests", "Wood:100,Stone:100,Flax:40,Barley:40,Sap:0,Softtissue:40", "Least of an item (prefab name:amount, comma separated) that always stays in the chests around a station: it only takes what is above it. Wood:100 = the kiln makes coal only while the chests hold more than 100 wood. Items not listed (ores, scrap, coal) may be used up to the last one."); NeverFeed = ((BaseUnityPlugin)this).Config.Bind<string>("2 - What it does", "NeverFeed", "FineWood,RoundLog,ElderBark,YggdrasilWood,Blackwood", "Items (prefab names, comma separated) never taken from the chests to be turned into something else: so the kiln doesn't burn your fine wood or core wood. Fuel is not affected."); FuelOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - What it does", "FuelOnlyStations", false, "Also keep fueled the stations that only burn fuel and make nothing: the hot tub (wood) and the frost kiln (ice). Off by default."); SkipStations = ((BaseUnityPlugin)this).Config.Bind<string>("2 - What it does", "SkipStations", "", "Stations (prefab names, comma separated) left alone, e.g. charcoal_kiln,windmill."); DetailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "DetailedLog", false, "Writes details to the BepInEx log."); new Harmony("felih.Forgekeeper").PatchAll(typeof(Plugin).Assembly); } private void Update() { if (Enabled.Value) { Stations.ListOnce(); Stations.Update(); } } internal static void Debug(string s) { if (DetailedLog.Value) { Log.LogInfo((object)s); } } } [HarmonyPatch] internal static class Stations { private static readonly List<Smelter> All = new List<Smelter>(); private static readonly List<Container> Chests = new List<Container>(); private static float _next; private static readonly MethodInfo QueueSizeM = AccessTools.Method(typeof(Smelter), "GetQueueSize", (Type[])null, (Type[])null); private static readonly MethodInfo FuelM = AccessTools.Method(typeof(Smelter), "GetFuel", (Type[])null, (Type[])null); private static readonly MethodInfo AccessM = AccessTools.Method(typeof(Container), "CheckAccess", new Type[1] { typeof(long) }, (Type[])null); private static readonly MethodInfo ConversionM = AccessTools.Method(typeof(Smelter), "GetItemConversion", new Type[1] { typeof(string) }, (Type[])null); private static readonly Dictionary<Smelter, string> State = new Dictionary<Smelter, string>(); private static bool _listed; [HarmonyPostfix] [HarmonyPatch(typeof(Smelter), "Awake")] private static void SeenStation(Smelter __instance) { All.Add(__instance); } [HarmonyPostfix] [HarmonyPatch(typeof(Container), "Awake")] private static void SeenChest(Container __instance) { Chests.Add(__instance); } internal static void Update() { //IL_00cd: Unknown result type (might be due to invalid IL or missing references) //IL_00d2: Unknown result type (might be due to invalid IL or missing references) //IL_00d3: Unknown result type (might be due to invalid IL or missing references) //IL_00d8: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Unknown result type (might be due to invalid IL or missing references) if (Time.time < _next) { return; } _next = Time.time + Plugin.Interval.Value; Player localPlayer = Player.m_localPlayer; if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead() || QueueSizeM == null || FuelM == null) { return; } Chests.RemoveAll((Container c) => (Object)(object)c == (Object)null); float value = Plugin.StationRange.Value; Vector3 position = ((Component)localPlayer).transform.position; for (int num = All.Count - 1; num >= 0; num--) { Smelter val = All[num]; if ((Object)(object)val == (Object)null) { All.RemoveAt(num); } else { Vector3 val2 = ((Component)val).transform.position - position; if (!(((Vector3)(ref val2)).sqrMagnitude > value * value)) { try { Look(localPlayer, val); } catch (Exception ex) { Plugin.Log.LogWarning((object)("[forge] " + ((Object)val).name + ": " + ex.Message)); } } } } } private static void Say(Smelter s, string name, string state) { //IL_0063: Unknown result type (might be due to invalid IL or missing references) //IL_0068: Unknown result type (might be due to invalid IL or missing references) if (!State.TryGetValue(s, out var value) || !(value == state)) { State[s] = state; if (State.Count > 300) { State.Clear(); } string[] array = new string[6] { "[forge] ", name, " at ", null, null, null }; Vector3 position = ((Component)s).transform.position; array[3] = ((Vector3)(ref position)).ToString("0"); array[4] = ": "; array[5] = state; Plugin.Debug(string.Concat(array)); } } private static void Look(Player p, Smelter s) { //IL_00b7: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)((Component)s).GetComponent<Piece>() == (Object)null) { return; } string prefabName = Utils.GetPrefabName(((Component)s).gameObject); ZNetView component = ((Component)s).GetComponent<ZNetView>(); if ((Object)(object)component == (Object)null || !component.IsValid()) { return; } if (!component.IsOwner()) { Say(s, prefabName, "left alone: another player's game runs it now (owner " + component.GetZDO().GetOwner() + ")"); return; } if (InList(Plugin.SkipStations.Value, prefabName)) { Say(s, prefabName, "left alone: in SkipStations"); return; } if (!Plugin.FuelOnly.Value && (s.m_conversion == null || s.m_conversion.Count == 0)) { Say(s, prefabName, "left alone: it only burns fuel (FuelOnlyStations is off)"); return; } int near; int others; int locked; List<Container> list = Near(p, ((Component)s).transform.position, out near, out others, out locked); if (list.Count == 0) { Say(s, prefabName, "no chest to use within " + Plugin.ChestRange.Value.ToString("0") + " m (" + near + " chests near: " + others + " run by another player's game, " + locked + " private or warded)"); return; } Say(s, prefabName, "looked after, with " + list.Count + " chest(s)" + ((others + locked > 0) ? (" (" + others + " run by another game, " + locked + " private or warded, left out)") : "")); if (Plugin.Output.Value) { StoreOutput(s, component, list, prefabName); } if (Plugin.Fuel.Value) { AddFuel(s, component, list, prefabName); } if (Plugin.Input.Value) { AddInput(s, component, list, prefabName); } } private static List<Container> Near(Player p, Vector3 at, out int near, out int others, out int locked) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_0064: Unknown result type (might be due to invalid IL or missing references) //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0075: Unknown result type (might be due to invalid IL or missing references) //IL_0146: Unknown result type (might be due to invalid IL or missing references) near = (others = (locked = 0)); float value = Plugin.ChestRange.Value; long playerID = p.GetPlayerID(); List<Container> list = new List<Container>(); foreach (Container chest in Chests) { if ((Object)(object)chest == (Object)null) { continue; } Vector3 val = ((Component)chest).transform.position - at; if (!(((Vector3)(ref val)).sqrMagnitude > value * value) && !((Object)(object)chest.m_wagon != (Object)null) && !((Object)(object)((Component)chest).GetComponentInParent<Ship>() != (Object)null) && !((Object)(object)((Component)chest).GetComponent<Piece>() == (Object)null) && !((Object)(object)((Component)chest).GetComponent<TombStone>() != (Object)null) && !((Object)(object)((Component)chest).GetComponentInParent<Character>() != (Object)null)) { near++; ZNetView component = ((Component)chest).GetComponent<ZNetView>(); if ((Object)(object)component == (Object)null || !component.IsValid() || !component.IsOwner()) { others++; } else if ((AccessM != null && !(bool)AccessM.Invoke(chest, new object[1] { playerID })) || !PrivateArea.CheckAccess(((Component)chest).transform.position, 0f, false, false)) { locked++; } else if (chest.GetInventory() != null) { list.Add(chest); } } } list.Sort(delegate(Container a, Container b) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) Vector3 val2 = ((Component)a).transform.position - at; float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude; Vector3 val3 = ((Component)b).transform.position - at; return sqrMagnitude.CompareTo(((Vector3)(ref val3)).sqrMagnitude); }); return list; } private static void Check(string name, bool ok, string what) { if (ok) { Plugin.Debug("[forge][check] OK " + name + ": " + what); } else { Plugin.Log.LogWarning((object)("[forge][check] PROBLEM " + name + ": " + what)); } } private static void AddFuel(Smelter s, ZNetView nv, List<Container> chests, string name) { if ((Object)(object)s.m_fuelItem == (Object)null || s.m_maxFuel <= 0) { return; } float num = (float)FuelM.Invoke(s, null); int num2 = Mathf.FloorToInt((float)s.m_maxFuel - num); if (num2 <= 0) { return; } string name2 = ((Object)((Component)s.m_fuelItem).gameObject).name; int num3 = Take(chests, name2, num2, name); if (num3 > 0) { for (int i = 0; i < num3; i++) { nv.InvokeRPC("RPC_AddFuel", new object[0]); } float num4 = (float)FuelM.Invoke(s, null); int num5 = Mathf.RoundToInt(num4 - num); Check(name, num5 == num3, num3 + " " + name2 + " as fuel: fuel " + num.ToString("0.#") + " -> " + num4.ToString("0.#") + " (max " + s.m_maxFuel + ")"); if (num5 < num3) { GiveBack(chests, ((Component)s.m_fuelItem).gameObject, num3 - Mathf.Max(0, num5), name); } } } private static void AddInput(Smelter s, ZNetView nv, List<Container> chests, string name) { if (s.m_conversion == null || s.m_maxOre <= 0) { return; } int num = s.m_maxOre - (int)QueueSizeM.Invoke(s, null); foreach (ItemConversion item in s.m_conversion) { if (num <= 0) { break; } if (item == null || (Object)(object)item.m_from == (Object)null) { continue; } string name2 = ((Object)((Component)item.m_from).gameObject).name; if (InList(Plugin.NeverFeed.Value, name2)) { continue; } int num2 = (int)QueueSizeM.Invoke(s, null); int num3 = Take(chests, name2, num, name); if (num3 > 0) { for (int i = 0; i < num3; i++) { nv.InvokeRPC("RPC_AddOre", new object[2] { name2, false }); } int num4 = (int)QueueSizeM.Invoke(s, null); Check(name, num4 - num2 == num3, num3 + " " + name2 + " put in: queue " + num2 + " -> " + num4 + " (holds " + s.m_maxOre + ")"); if (num4 - num2 < num3) { GiveBack(chests, ((Component)item.m_from).gameObject, num3 - Mathf.Max(0, num4 - num2), name); } num -= num3; } } } private static int Keep(string prefab) { string value = Plugin.KeepInChests.Value; if (string.IsNullOrEmpty(value)) { return 0; } string[] array = value.Split(','); foreach (string text in array) { string[] array2 = text.Split(':'); if (array2.Length == 2 && string.Equals(array2[0].Trim(), prefab, StringComparison.OrdinalIgnoreCase) && int.TryParse(array2[1].Trim(), out var result)) { return Mathf.Max(0, result); } } return 0; } private static int Count(List<Container> chests, string prefab) { int num = 0; foreach (Container chest in chests) { foreach (ItemData allItem in chest.GetInventory().GetAllItems()) { if (allItem != null && (Object)(object)allItem.m_dropPrefab != (Object)null && ((Object)allItem.m_dropPrefab).name == prefab) { num += allItem.m_stack; } } } return num; } private static void GiveBack(List<Container> chests, GameObject prefab, int n, string name) { int num = Store(chests, prefab, n, name); Check(name, num == n, "gave back " + num + " of " + n + " " + ((Object)prefab).name + " the station didn't take"); } private static int Take(List<Container> chests, string prefab, int max, string name) { int num = Count(chests, prefab); int num2 = Keep(prefab); if (num2 > 0) { max = Mathf.Min(max, num - num2); } if (max <= 0 || num <= 0) { return 0; } int num3 = Remove(chests, prefab, max); int num4 = Count(chests, prefab); Check(name, num - num4 == num3, "took " + num3 + " " + prefab + " from the chests: " + num + " -> " + num4 + ((num2 > 0) ? (" (keeps " + num2 + ")") : "")); if (num - num4 != num3) { return Mathf.Max(0, num - num4); } return num3; } private static int Remove(List<Container> chests, string prefab, int max) { int num = 0; foreach (Container chest in chests) { if (num >= max) { break; } Inventory inventory = chest.GetInventory(); foreach (ItemData item in new List<ItemData>(inventory.GetAllItems())) { if (num >= max) { break; } if (item != null && !((Object)(object)item.m_dropPrefab == (Object)null) && !(((Object)item.m_dropPrefab).name != prefab) && item.m_stack > 0) { int num2 = Mathf.Min(item.m_stack, max - num); if (inventory.RemoveItem(item, num2)) { num += num2; } } } } return num; } private static void StoreOutput(Smelter s, ZNetView nv, List<Container> chests, string name) { //IL_01df: Unknown result type (might be due to invalid IL or missing references) if (s.m_spawnStack) { ZDO zDO = nv.GetZDO(); string text = zDO.GetString(ZDOVars.s_spawnOre, ""); int num = zDO.GetInt(ZDOVars.s_spawnAmount, 0); if (num <= 0 || text == "" || ConversionM == null) { return; } object? obj = ConversionM.Invoke(s, new object[1] { text }); ItemConversion val = (ItemConversion)((obj is ItemConversion) ? obj : null); if (val == null || (Object)(object)val.m_to == (Object)null) { return; } int num2 = Store(chests, ((Component)val.m_to).gameObject, num, name); if (num2 > 0) { if (num2 >= num) { zDO.Set(ZDOVars.s_spawnOre, ""); zDO.Set(ZDOVars.s_spawnAmount, 0, false); } else { zDO.Set(ZDOVars.s_spawnAmount, num - num2, false); } int num3 = zDO.GetInt(ZDOVars.s_spawnAmount, 0); Check(name, num3 == num - num2, "its stack of " + num + " " + ((Object)((Component)val.m_to).gameObject).name + ": " + num2 + " into the chests, " + num3 + " left in the station"); } } else { if ((Object)(object)s.m_outputPoint == (Object)null || s.m_conversion == null) { return; } HashSet<string> hashSet = new HashSet<string>(); foreach (ItemConversion item in s.m_conversion) { if (item != null && (Object)(object)item.m_to != (Object)null) { hashSet.Add(((Object)((Component)item.m_to).gameObject).name); } } Collider[] array = Physics.OverlapSphere(s.m_outputPoint.position, 2f, LayerMask.GetMask(new string[1] { "item" }), (QueryTriggerInteraction)2); foreach (Collider val2 in array) { ItemDrop val3 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponentInParent<ItemDrop>() : null); if ((Object)(object)val3 == (Object)null || val3.m_itemData == null) { continue; } ZNetView component = ((Component)val3).GetComponent<ZNetView>(); if ((Object)(object)component == (Object)null || !component.IsValid() || !component.IsOwner()) { continue; } string prefabName = Utils.GetPrefabName(((Component)val3).gameObject); if (hashSet.Contains(prefabName)) { GameObject val4 = (((Object)(object)ZNetScene.instance != (Object)null) ? ZNetScene.instance.GetPrefab(prefabName) : null); int stack = val3.m_itemData.m_stack; if (!((Object)(object)val4 == (Object)null) && stack > 0 && Fits(chests, val4, stack) && Store(chests, val4, stack, name) == stack) { ZNetScene.instance.Destroy(((Component)val3).gameObject); Check(name, !component.IsValid() || component.GetZDO() == null, "picked up " + stack + " " + prefabName + " from the ground into the chests" + ((component.IsValid() && component.GetZDO() != null) ? ", but it is still on the ground" : "")); } } } } } private static bool Fits(List<Container> chests, GameObject prefab, int amount) { foreach (Container chest in chests) { if (chest.GetInventory().CanAddItem(prefab, amount)) { return true; } } return false; } private static int Store(List<Container> chests, GameObject prefab, int amount, string name) { ItemDrop component = prefab.GetComponent<ItemDrop>(); if ((Object)(object)component == (Object)null) { return 0; } int num = Count(chests, ((Object)prefab).name); int num2 = Put(chests, prefab, component, amount); int num3 = Count(chests, ((Object)prefab).name); Check(name, num3 - num == num2, "stored " + num2 + " of " + amount + " " + ((Object)prefab).name + " in the chests: " + num + " -> " + num3); if (num3 - num != num2) { return Mathf.Max(0, num3 - num); } return num2; } private static int Put(List<Container> chests, GameObject prefab, ItemDrop d, int amount) { string name = d.m_itemData.m_shared.m_name; int num = amount; for (int i = 0; i < 2; i++) { if (num <= 0) { break; } foreach (Container chest in chests) { if (num <= 0) { break; } Inventory inventory = chest.GetInventory(); if (i != 0 || inventory.CountItems(name, -1, true) != 0) { int num2 = num; while (num2 > 0 && !inventory.CanAddItem(prefab, num2)) { num2--; } if (num2 > 0 && inventory.AddItem(prefab, num2)) { num -= num2; } } } } return amount - num; } internal static void ListOnce() { if (_listed || (Object)(object)ZNetScene.instance == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null) { return; } _listed = true; foreach (GameObject prefab in ZNetScene.instance.m_prefabs) { Smelter val = (((Object)(object)prefab != (Object)null) ? prefab.GetComponent<Smelter>() : null); if ((Object)(object)val == (Object)null) { continue; } List<string> list = new List<string>(); if (val.m_conversion != null) { foreach (ItemConversion item in val.m_conversion) { if (item != null && (Object)(object)item.m_from != (Object)null && (Object)(object)item.m_to != (Object)null) { list.Add(((Object)((Component)item.m_from).gameObject).name + ">" + ((Object)((Component)item.m_to).gameObject).name); } } } Plugin.Log.LogInfo((object)("[forge] station " + ((Object)prefab).name + ": fuel " + (((Object)(object)val.m_fuelItem != (Object)null) ? (((Object)((Component)val.m_fuelItem).gameObject).name + " (max " + val.m_maxFuel + ", " + val.m_fuelPerProduct + " per item)") : "none") + ", holds " + val.m_maxOre + ", " + val.m_secPerProduct + " s each, " + (val.m_spawnStack ? "keeps a stack" : "drops each") + ": " + string.Join(", ", list.ToArray()))); } } private static bool InList(string list, string name) { if (string.IsNullOrEmpty(list)) { return false; } string[] array = list.Split(','); foreach (string text in array) { if (string.Equals(text.Trim(), name, StringComparison.OrdinalIgnoreCase)) { return true; } } return false; } }