Decompiled source of InfiniteFoundationsAndSand v1.0.0

plugins/InfiniteFoundationsAndSand.dll

Decompiled 21 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("InfiniteFoundationsAndSand")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+affb140ad92fa34a526146957c95897f99919124")]
[assembly: AssemblyProduct("InfiniteFoundationsAndSand")]
[assembly: AssemblyTitle("InfiniteFoundationsAndSand")]
[assembly: AssemblyVersion("1.0.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 InfiniteFoundationsAndSand
{
	public static class DeliverySlotsCompat
	{
		public const string Guid = "starfi5h.plugin.DeliverySlotsTweaks";

		public static bool Active { get; private set; }

		internal static void Init(Harmony harmony, ManualLogSource log)
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Expected O, but got Unknown
			if (!Chainloader.PluginInfos.TryGetValue("starfi5h.plugin.DeliverySlotsTweaks", out var value) || (Object)(object)value.Instance == (Object)null)
			{
				return;
			}
			try
			{
				Type? type = ((object)value.Instance).GetType().Assembly.GetType("DeliverySlotsTweaks.DeliveryPackagePatch", throwOnError: true);
				MethodInfo methodInfo = AccessTools.Method(type, "GetItemCount", (Type[])null, (Type[])null);
				MethodInfo methodInfo2 = AccessTools.Method(type, "TakeTailItems", (Type[])null, (Type[])null);
				MethodInfo methodInfo3 = AccessTools.Method(type, "TakeItem", (Type[])null, (Type[])null);
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(DeliverySlotsCompat), "CountPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(DeliverySlotsCompat), "TakeTailPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				harmony.Patch((MethodBase)methodInfo3, new HarmonyMethod(typeof(DeliverySlotsCompat), "TakeItemPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Active = true;
				log.LogInfo((object)"DeliverySlotsTweaks found: its inventory count and take functions are patched too.");
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("DeliverySlotsTweaks found but could not be patched; laying foundations by hand may ask for foundations.\n" + ex));
			}
		}

		private static void CountPostfix(int itemId, ref int __result)
		{
			if (itemId == 1131 && Plugin.InfiniteFoundations.Value && __result < 99999)
			{
				__result = 99999;
			}
		}

		private static bool TakeTailPrefix(StorageComponent storage, ref int itemId, ref int count, out int inc)
		{
			inc = 0;
			if (count <= 0 || !FoundationPatches.Free(itemId, storage))
			{
				return true;
			}
			return false;
		}

		private static bool TakeItemPrefix(StorageComponent storage, int itemId, int count, out int inc, ref int __result)
		{
			inc = 0;
			if (count <= 0 || !FoundationPatches.Free(itemId, storage))
			{
				return true;
			}
			__result = count;
			return false;
		}
	}
	public static class FoundationPatches
	{
		public const int FoundationId = 1131;

		public const int ShownCount = 99999;

		private static readonly FieldRef<BuildTool, StorageComponent> TmpPackage = AccessTools.FieldRefAccess<BuildTool, StorageComponent>("tmpPackage");

		public static bool IsPlayerStore(StorageComponent storage)
		{
			if (storage == null)
			{
				return false;
			}
			Player mainPlayer = GameMain.mainPlayer;
			if (mainPlayer == null)
			{
				return false;
			}
			if (storage == mainPlayer.package)
			{
				return true;
			}
			BuildTool[] array = mainPlayer.controller?.actionBuild?.tools;
			if (array == null)
			{
				return false;
			}
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] != null && TmpPackage.Invoke(array[i]) == storage)
				{
					return true;
				}
			}
			return false;
		}

		internal static bool Free(int itemId, StorageComponent storage)
		{
			if (itemId == 1131 && Plugin.InfiniteFoundations.Value)
			{
				return IsPlayerStore(storage);
			}
			return false;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StorageComponent), "GetItemCount", new Type[] { typeof(int) })]
		private static void Count(StorageComponent __instance, int itemId, ref int __result)
		{
			if (__result < 99999 && Free(itemId, __instance))
			{
				__result = 99999;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void CountWithInc(StorageComponent __instance, int itemId, ref int __result)
		{
			if (__result < 99999 && Free(itemId, __instance))
			{
				__result = 99999;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static bool TakeTail(StorageComponent __instance, ref int itemId, ref int count, out int inc)
		{
			inc = 0;
			if (count <= 0 || !Free(itemId, __instance))
			{
				return true;
			}
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(StorageComponent), "TakeTailItemsWithIncTable")]
		private static bool TakeWithIncTable(StorageComponent __instance, int itemId, ref int count, out int inc, ref int[] incTable)
		{
			inc = 0;
			if (!Free(itemId, __instance))
			{
				return true;
			}
			if (incTable == null || incTable.Length <= 10)
			{
				incTable = new int[11];
			}
			else
			{
				Array.Clear(incTable, 0, incTable.Length);
			}
			if (count > 0)
			{
				incTable[0] = count;
			}
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(StorageComponent), "TakeTailItemsByIncTable")]
		private static bool TakeByIncTable(StorageComponent __instance, int itemId, out int count, ref int[] incTable, ref bool __result)
		{
			count = 0;
			if (incTable == null || !Free(itemId, __instance))
			{
				return true;
			}
			for (int i = 0; i < incTable.Length; i++)
			{
				count += incTable[i];
				incTable[i] = 0;
			}
			__result = true;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static bool TakeItem(StorageComponent __instance, int itemId, int count, out int inc, ref int __result)
		{
			inc = 0;
			if (count <= 0 || !Free(itemId, __instance))
			{
				return true;
			}
			__result = count;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Player), "UseHandItems")]
		private static bool UseHand(Player __instance, int useCnt, out int useInc, ref int __result)
		{
			useInc = 0;
			if (__instance.inhandItemId != 1131 || !Plugin.InfiniteFoundations.Value || __instance != GameMain.mainPlayer)
			{
				return true;
			}
			__result = useCnt;
			return false;
		}
	}
	[BepInPlugin("com.github.jiesiluo.infinitefoundationsandsand", "Infinite Foundations And Sand", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.github.jiesiluo.infinitefoundationsandsand";

		public const string Version = "1.0.0";

		public static ConfigEntry<bool> InfiniteFoundations;

		public static ConfigEntry<bool> InfiniteSand;

		public static ConfigEntry<long> SandAmount;

		private void Awake()
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Expected O, but got Unknown
			InfiniteFoundations = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "InfiniteFoundations", true, "Laying foundations (by hand, by blueprint, or when removing a Dark Fog base pit) never uses up foundations.");
			InfiniteSand = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "InfiniteSand", true, "Soil pile (sand) never runs out: it is kept at SandAmount and spending it does not lower it.");
			SandAmount = ((BaseUnityPlugin)this).Config.Bind<long>("General", "SandAmount", 1000000000L, "The soil pile amount kept while InfiniteSand is on. Note: this amount is stored in the save.");
			Harmony.CreateAndPatchAll(typeof(FoundationPatches), "com.github.jiesiluo.infinitefoundationsandsand");
			Harmony.CreateAndPatchAll(typeof(SandPatches), "com.github.jiesiluo.infinitefoundationsandsand");
			DeliverySlotsCompat.Init(new Harmony("com.github.jiesiluo.infinitefoundationsandsand"), ((BaseUnityPlugin)this).Logger);
		}

		private void Update()
		{
			SandPatches.TopUp();
		}
	}
	public static class SandPatches
	{
		private static readonly Action<Player, long> SetRaw = AccessTools.MethodDelegate<Action<Player, long>>(AccessTools.PropertySetter(typeof(Player), "sandCount"), (object)null, true);

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Player), "SetSandCount")]
		private static void KeepSand(Player __instance, ref long newSandCount)
		{
			if (Plugin.InfiniteSand.Value && __instance == GameMain.mainPlayer && newSandCount < Plugin.SandAmount.Value)
			{
				newSandCount = Plugin.SandAmount.Value;
			}
		}

		public static void TopUp()
		{
			if (!Plugin.InfiniteSand.Value)
			{
				return;
			}
			Player mainPlayer = GameMain.mainPlayer;
			if (mainPlayer != null && !GameMain.isLoading)
			{
				long value = Plugin.SandAmount.Value;
				if (mainPlayer.sandCount < value)
				{
					SetRaw(mainPlayer, value);
				}
			}
		}
	}
}