Decompiled source of AutoStore v0.6.0

AutoStore.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			int result;
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				result = ((instance != null && instance.HasFocus()) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
		int result2;
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				TextViewer instance3 = TextViewer.instance;
				result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
				goto IL_00d2;
			}
		}
		result2 = 1;
		goto IL_00d2;
		IL_00d2:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			foreach (string text in array)
			{
				if (Input.GetKeyDown(text.ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool CheckKeyUp(string value)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			foreach (string text in array)
			{
				if (Input.GetKeyUp(text.ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			foreach (string text in array)
			{
				if (Input.GetKey(text.ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return !req;
	}

	public static void ShuffleList<T>(List<T> list)
	{
		int num = list.Count;
		while (num > 1)
		{
			num--;
			int index = Random.Range(0, num);
			T value = list[index];
			list[index] = list[num];
			list[num] = value;
		}
	}

	public static string GetAssetPath(object obj, bool create = false)
	{
		return GetAssetPath(obj.GetType().Namespace, create);
	}

	public static string GetAssetPath(string name, bool create = false)
	{
		string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), name);
		if (create && !Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		return text;
	}

	public static string GetTransformPath(Transform t)
	{
		if (!Object.op_Implicit((Object)(object)t.parent))
		{
			return ((Object)t).name;
		}
		return GetTransformPath(t.parent) + "/" + ((Object)t).name;
	}

	public static byte[] EncodeToPNG(Texture2D texture)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Expected O, but got Unknown
		RenderTexture temporary = RenderTexture.GetTemporary(((Texture)texture).width, ((Texture)texture).height, 0, (RenderTextureFormat)7, (RenderTextureReadWrite)0);
		Graphics.Blit((Texture)(object)texture, temporary);
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = temporary;
		Texture2D val = new Texture2D(((Texture)texture).width, ((Texture)texture).height, (TextureFormat)4, true, false);
		val.ReadPixels(new Rect(0f, 0f, (float)((Texture)temporary).width, (float)((Texture)temporary).height), 0, 0);
		val.Apply();
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
		Texture2D val2 = new Texture2D(((Texture)texture).width, ((Texture)texture).height);
		val2.SetPixels(val.GetPixels());
		val2.Apply();
		return ImageConversion.EncodeToPNG(val2);
	}
}
namespace AutoStore;

[BepInPlugin("aedenthorn.AutoStore", "Auto Store", "0.6.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Container), "CheckForChanges")]
	private static class Container_CheckForChanges_Patch
	{
		private static void Postfix(Container __instance, ZNetView ___m_nview)
		{
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: 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)
			if (!isOn.Value || (Object)(object)___m_nview == (Object)null || ___m_nview.GetZDO() == null)
			{
				return;
			}
			if (!pullWhileBuilding.Value)
			{
				ItemData val = (ItemData)AccessTools.Method(typeof(Player), "GetLeftItem", (Type[])null, (Type[])null).Invoke(Player.m_localPlayer, new object[0]);
				if ((int)val != 0 && val.m_shared?.m_buildPieces.m_pieces.Count > 0)
				{
					return;
				}
				ItemData val2 = (ItemData)AccessTools.Method(typeof(Player), "GetRightItem", (Type[])null, (Type[])null).Invoke(Player.m_localPlayer, new object[0]);
				if ((int)val2 != 0 && val2.m_shared?.m_buildPieces.m_pieces.Count > 0)
				{
					return;
				}
			}
			Vector3 val3 = ((Component)__instance).transform.position + Vector3.up;
			Collider[] array = Physics.OverlapSphere(val3, ContainerRange(__instance), LayerMask.GetMask(new string[1] { "item" }));
			foreach (Collider val4 in array)
			{
				if (!Object.op_Implicit((Object)(object)((val4 != null) ? val4.attachedRigidbody : null)))
				{
					continue;
				}
				ItemDrop component = ((Component)val4.attachedRigidbody).GetComponent<ItemDrop>();
				if (component == null)
				{
					continue;
				}
				ZNetView component2 = ((Component)component).GetComponent<ZNetView>();
				if (((component2 != null) ? new bool?(component2.IsValid()) : null) == true && ((Component)component).GetComponent<ZNetView>().IsOwner() && TryStoreItem(__instance, ref component.m_itemData))
				{
					AccessTools.Method(typeof(ItemDrop), "Save", (Type[])null, (Type[])null).Invoke(component, new object[0]);
					if ((Object)(object)((Component)component).GetComponent<ZNetView>() == (Object)null)
					{
						Object.DestroyImmediate((Object)(object)((Component)component).gameObject);
					}
					else
					{
						ZNetScene.instance.Destroy(((Component)component).gameObject);
					}
				}
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				__instance.AddString(text);
				__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
				return false;
			}
			return true;
		}
	}

	private static readonly bool isDebug = true;

	public static ConfigEntry<float> dropRangeChests;

	public static ConfigEntry<float> dropRangePersonalChests;

	public static ConfigEntry<float> dropRangeReinforcedChests;

	public static ConfigEntry<float> dropRangeCarts;

	public static ConfigEntry<float> dropRangeShips;

	public static ConfigEntry<string> itemDisallowTypes;

	public static ConfigEntry<string> itemAllowTypes;

	public static ConfigEntry<string> itemDisallowTypesChests;

	public static ConfigEntry<string> itemAllowTypesChests;

	public static ConfigEntry<string> itemDisallowTypesPersonalChests;

	public static ConfigEntry<string> itemAllowTypesPersonalChests;

	public static ConfigEntry<string> itemDisallowTypesReinforcedChests;

	public static ConfigEntry<string> itemAllowTypesReinforcedChests;

	public static ConfigEntry<string> itemDisallowTypesBlackMetalChests;

	public static ConfigEntry<string> itemAllowTypesBlackMetalChests;

	public static ConfigEntry<string> itemDisallowTypesCarts;

	public static ConfigEntry<string> itemAllowTypesCarts;

	public static ConfigEntry<string> itemDisallowTypesShips;

	public static ConfigEntry<string> itemAllowTypesShips;

	public static ConfigEntry<string> toggleKey;

	public static ConfigEntry<string> toggleString;

	public static ConfigEntry<bool> mustHaveItemToPull;

	public static ConfigEntry<bool> pullWhileBuilding;

	public static ConfigEntry<bool> isOn;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	private static BepInExPlugin context;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		context = this;
		dropRangeChests = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DropRangeChests", 5f, "The maximum range to pull dropped items");
		dropRangePersonalChests = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DropRangePersonalChests", 5f, "The maximum range to pull dropped items");
		dropRangeReinforcedChests = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DropRangeReinforcedChests", 5f, "The maximum range to pull dropped items");
		dropRangeCarts = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DropRangeCarts", 5f, "The maximum range to pull dropped items");
		dropRangeShips = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DropRangeShips", 5f, "The maximum range to pull dropped items");
		itemDisallowTypes = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypes", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypes = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypes", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypes");
		itemDisallowTypesChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypesChests", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypesChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypesChests", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypesChests");
		itemDisallowTypesPersonalChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypesPersonalChests", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypesPersonalChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypesPersonalChests", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypesPersonalChests");
		itemDisallowTypesReinforcedChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypesReinforcedChests", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypesReinforcedChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypesReinforcedChests", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypesReinforcedChests");
		itemDisallowTypesBlackMetalChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypesBlackMetalChests", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypesBlackMetalChests = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypesBlackMetalChests", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypesBlackMetalChests");
		itemDisallowTypesCarts = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypesCarts", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypesCarts = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypesCarts", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypesCarts");
		itemDisallowTypesShips = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemDisallowTypesShips", "", "Types of item to disallow pulling for, comma-separated.");
		itemAllowTypesShips = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ItemAllowTypesShips", "", "Types of item to only allow pulling for, comma-separated. Overrides ItemDisallowTypesShips");
		toggleString = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ToggleString", "Auto Pull: {0}", "Text to show on toggle. {0} is replaced with true/false");
		toggleKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ToggleKey", "", "Key to toggle behaviour. Leave blank to disable the toggle key. Use https://docs.unity3d.com/Manual/class-InputManager.html syntax.");
		mustHaveItemToPull = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "MustHaveItemToPull", false, "If true, a container must already have at least one of the item to pull.");
		pullWhileBuilding = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PullWhileBuilding", true, "If false, containers won't pull while the player is holding the hammer.");
		isOn = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IsOn", true, "Behaviour is currently on or not");
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 174, "Nexus mod ID for updates");
		if (modEnabled.Value)
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	private void Update()
	{
		if (modEnabled.Value && !AedenthornUtils.IgnoreKeyPresses() && AedenthornUtils.CheckKeyDown(toggleKey.Value))
		{
			isOn.Value = !isOn.Value;
			((BaseUnityPlugin)this).Config.Save();
			((Character)Player.m_localPlayer).Message((MessageType)2, string.Format(toggleString.Value, isOn.Value), 0, (Sprite)null);
		}
	}

	private static bool DisallowItem(Container container, ItemData item)
	{
		string name = ((Object)item.m_dropPrefab).name;
		if (itemAllowTypes.Value != null && itemAllowTypes.Value.Length > 0 && !itemAllowTypes.Value.Split(new char[1] { ',' }).Contains(name))
		{
			return true;
		}
		if (itemDisallowTypes.Value.Split(new char[1] { ',' }).Contains(name))
		{
			return true;
		}
		if (mustHaveItemToPull.Value && !container.GetInventory().HaveItem(item.m_shared.m_name, true))
		{
			return true;
		}
		Transform parent = ((Component)container).gameObject.transform.parent;
		Ship val = ((parent != null) ? ((Component)parent).GetComponent<Ship>() : null);
		if ((Object)(object)val != (Object)null)
		{
			if (itemAllowTypesShips.Value != null && itemAllowTypesShips.Value.Length > 0 && !itemAllowTypesShips.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			if (itemDisallowTypesShips.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			return false;
		}
		if (Object.op_Implicit((Object)(object)container.m_wagon))
		{
			if (itemAllowTypesCarts.Value != null && itemAllowTypesCarts.Value.Length > 0 && !itemAllowTypesCarts.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			if (itemDisallowTypesCarts.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			return false;
		}
		if (((Object)container).name.StartsWith("piece_chest_wood"))
		{
			if (itemAllowTypesChests.Value != null && itemAllowTypesChests.Value.Length > 0 && !itemAllowTypesChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			if (itemDisallowTypesChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			return false;
		}
		if (((Object)container).name.StartsWith("piece_chest_private"))
		{
			if (itemAllowTypesPersonalChests.Value != null && itemAllowTypesPersonalChests.Value.Length > 0 && !itemAllowTypesPersonalChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			if (itemDisallowTypesPersonalChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			return false;
		}
		if (((Object)container).name.StartsWith("piece_chest_blackmetal"))
		{
			if (itemAllowTypesBlackMetalChests.Value != null && itemAllowTypesBlackMetalChests.Value.Length > 0 && !itemAllowTypesBlackMetalChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			if (itemDisallowTypesBlackMetalChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			return false;
		}
		if (((Object)container).name.StartsWith("piece_chest"))
		{
			if (itemAllowTypesReinforcedChests.Value != null && itemAllowTypesReinforcedChests.Value.Length > 0 && !itemAllowTypesReinforcedChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			if (itemDisallowTypesReinforcedChests.Value.Split(new char[1] { ',' }).Contains(name))
			{
				return true;
			}
			return false;
		}
		return true;
	}

	private static float ContainerRange(Container container)
	{
		if (container.GetInventory() == null)
		{
			return -1f;
		}
		Transform parent = ((Component)container).gameObject.transform.parent;
		Ship val = ((parent != null) ? ((Component)parent).GetComponent<Ship>() : null);
		if ((Object)(object)val != (Object)null)
		{
			return dropRangeShips.Value;
		}
		if (Object.op_Implicit((Object)(object)container.m_wagon))
		{
			return dropRangeCarts.Value;
		}
		if (((Object)container).name.StartsWith("piece_chest_wood"))
		{
			return dropRangeChests.Value;
		}
		if (((Object)container).name.StartsWith("piece_chest_private"))
		{
			return dropRangePersonalChests.Value;
		}
		if (((Object)container).name.StartsWith("piece_chest"))
		{
			return dropRangeReinforcedChests.Value;
		}
		return -1f;
	}

	private static bool TryStoreItem(Container __instance, ref ItemData item)
	{
		if (DisallowItem(__instance, item))
		{
			return false;
		}
		bool flag = false;
		while (item.m_stack > 1 && __instance.GetInventory().CanAddItem(item, 1))
		{
			flag = true;
			ItemData obj = item;
			obj.m_stack--;
			ItemData val = item.Clone();
			val.m_stack = 1;
			__instance.GetInventory().AddItem(val);
		}
		if (item.m_stack == 1 && __instance.GetInventory().CanAddItem(item, 1))
		{
			ItemData val2 = item.Clone();
			item.m_stack = 0;
			__instance.GetInventory().AddItem(val2);
			flag = true;
		}
		if (flag)
		{
			AccessTools.Method(typeof(Container), "Save", (Type[])null, (Type[])null).Invoke(__instance, new object[0]);
		}
		return flag;
	}
}