Decompiled source of CrusherPerformanceFix v1.0.0

plugins/CrusherPerformanceFix/CrusherPerformanceFix.dll

Decompiled 4 days ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("StackLimits")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StackLimits")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e1eed1dd-765c-465b-956d-6bb006ca326f")]
[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 StackLimits;

[BepInPlugin("CrusherPerformanceFix", "Crusher Performance Fix", "1.0.0")]
public class CrusherPerformanceFix : BaseUnityPlugin
{
	[HarmonyPatch(typeof(CrusherInstance), "UpdateCrafting")]
	private static class CrusherUpdateCraftingDelay
	{
		private static bool Prefix(float dt, ref CrusherInstance __instance)
		{
			if (!__instance.isCrafting)
			{
				__instance.progress += dt * notCraftingDelayCrusher;
				if (__instance.progress <= 1f)
				{
					return false;
				}
				__instance.progress = 0f;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(ThresherInstance), "UpdateCrafting")]
	private static class ThresherUpdateCraftingDelay
	{
		private static bool Prefix(float dt, ref ThresherInstance __instance)
		{
			if (!__instance.isCrafting)
			{
				__instance.progress += dt * notCraftingDelayThresher;
				if (__instance.progress <= 1f)
				{
					return false;
				}
				__instance.progress = 0f;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(AssemblerInstance), "UpdateCrafting")]
	private static class AssemblerUpdateCraftingDelay
	{
		private static bool Prefix(float dt, ref AssemblerInstance __instance)
		{
			if (!__instance.isCrafting)
			{
				__instance.timeCrafting += dt * notCraftingDelayAssembler;
				if (__instance.timeCrafting <= 1f)
				{
					return false;
				}
				__instance.timeCrafting = 0f;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(GridManager), "GetChunkArrayIndexFromId")]
	private static class GetChunkArrayIndexFromIdWarningRemoval
	{
		public static void Prefix(in int chunkId, ref bool warnOutOfBounds)
		{
			warnOutOfBounds = false;
		}
	}

	private const string PluginName = "Crusher Performance Fix";

	private const string pluginGuid = "CrusherPerformanceFix";

	private const string VersionString = "1.0.0";

	private Harmony harmony = new Harmony("Crusher Performance Fix");

	public static float notCraftingDelayCrusher;

	public static float notCraftingDelayAssembler;

	public static float notCraftingDelayThresher;

	private void Awake()
	{
		if (((BaseUnityPlugin)this).Config.Bind<bool>("General", "RemoveLogSpamNearEdge", true, "Prevent the game from spamming 'Chunk out of range: (90, 90, 330)@0 out of range with offsets (3, 3, 11)@0/(11, 9, 11)' when you try to build near the edge of a level. ").Value)
		{
			harmony.PatchAll(typeof(GetChunkArrayIndexFromIdWarningRemoval));
		}
		if (((BaseUnityPlugin)this).Config.Bind<bool>("Crusher", "DelayActive", true, "Apply the delay for crushers. Strongly recommended to keep this on true as this is the main performance improvement of the mod.").Value)
		{
			harmony.PatchAll(typeof(CrusherUpdateCraftingDelay));
		}
		if (((BaseUnityPlugin)this).Config.Bind<bool>("Assembler", "DelayActive", false, "Apply the delay for assemblers.").Value)
		{
			harmony.PatchAll(typeof(AssemblerUpdateCraftingDelay));
		}
		if (((BaseUnityPlugin)this).Config.Bind<bool>("Thresher", "DelayActive", false, "Apply the delay for threshers.").Value)
		{
			harmony.PatchAll(typeof(ThresherUpdateCraftingDelay));
		}
		notCraftingDelayCrusher = 1f / ((BaseUnityPlugin)this).Config.Bind<float>("Crusher", "CheckDelay", 1f, "Time until next check is performed when machine stopped due to lack of input. Number needs to be larger than 0.02 for this mod to make any sense. Only applied is DelayActive is set for this machine.").Value;
		notCraftingDelayAssembler = 1f / ((BaseUnityPlugin)this).Config.Bind<float>("Assembler", "CheckDelay", 1f, "Time until next check is performed when machine stopped due to lack of input. Number needs to be larger than 0.02 for this mod to make any sense. Only applied is DelayActive is set for this machine.").Value;
		notCraftingDelayThresher = 1f / ((BaseUnityPlugin)this).Config.Bind<float>("Thresher", "CheckDelay", 1f, "Time until next check is performed when machine stopped due to lack of input. Number needs to be larger than 0.02 for this mod to make any sense. Only applied is DelayActive is set for this machine.").Value;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Crusher Performance Fix Version 1.0.0 has loaded.");
	}
}