Decompiled source of CaspersSandConfig v1.0.0

plugins/CaspersSandConfig/CaspersSandConfig.dll

Decompiled 6 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CaspersSandConfig.Patches;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CaspersSandConfig")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CaspersSandConfig")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ff677bc5-9ac6-409d-b1cc-f9b75df6fd33")]
[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 CaspersSandConfig
{
	[BepInPlugin("com.casper.sandconfig", "CaspersSandConfig", "1.0.0")]
	public class CaspersSandConfigPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.casper.sandconfig";

		private const string PluginName = "CaspersSandConfig";

		private const string VersionString = "1.0.0";

		private const string MaxLevelReductionKey = "MaxLevelReductionPercentage";

		private const string FillRateReductionKey = "FillRateReductionPercentage";

		private const string DefaultDigRateMultiplierKey = "DefaultDigRateMultiplier";

		public static ConfigEntry<float> maxLevelReductionPercentage;

		public static ConfigEntry<float> fillRateReductionPercentage;

		public static ConfigEntry<float> defaultDigRateMultiplier;

		private static readonly Harmony Harmony = new Harmony("com.casper.sandconfig");

		public static ManualLogSource Log = new ManualLogSource("CaspersSandConfig");

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			maxLevelReductionPercentage = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MaxLevelReductionPercentage", 0f, new ConfigDescription("Percentage to reduce max sand levels (default 0%, no reduction).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			fillRateReductionPercentage = ((BaseUnityPlugin)this).Config.Bind<float>("General", "FillRateReductionPercentage", 0f, new ConfigDescription("Percentage to reduce fill rates (default 0%, no reduction).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			defaultDigRateMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DefaultDigRateMultiplier", 1f, new ConfigDescription("Default dig rate multiplier for all sand volumes (default 1.0, no change).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 100f), Array.Empty<object>()));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"CaspersSandConfig v1.0.0 is loading...");
			Harmony.PatchAll();
			Harmony.CreateAndPatchAll(typeof(SandVolumePatch), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"CaspersSandConfig v1.0.0 loaded successfully.");
			Log = ((BaseUnityPlugin)this).Logger;
		}
	}
}
namespace CaspersSandConfig.Patches
{
	internal class SandVolumePatch
	{
		private static readonly FieldRef<SandVolume, float> getMaxSandLevel = AccessTools.FieldRefAccess<SandVolume, float>("maxSandLevel");

		private static readonly FieldRef<SandVolume, float> getStrataSandFillRate = AccessTools.FieldRefAccess<SandVolume, float>("strataSandFillRate");

		private static readonly FieldRef<SandVolume, float> getStrataSandDigRateMultiplier = AccessTools.FieldRefAccess<SandVolume, float>("strataSandDigRateMultiplier");

		private static readonly FieldRef<SandVolume, float> setDigRateThisFrame = AccessTools.FieldRefAccess<SandVolume, float>("digRateThisFrame");

		private static readonly FieldRef<GameDefines, float> getSandPumpMaxRemovalRate = AccessTools.FieldRefAccess<GameDefines, float>("SandPumpMaxRemovalRate");

		[HarmonyPatch(typeof(SandVolume), "Initialize", new Type[] { typeof(LevelDefinition) })]
		[HarmonyPostfix]
		public static void AdjustSandMaxLevelAndRates()
		{
			float num = 1f - CaspersSandConfigPlugin.maxLevelReductionPercentage.Value / 100f;
			float num2 = 1f - CaspersSandConfigPlugin.fillRateReductionPercentage.Value / 100f;
			foreach (SandVolume allSand in SandVolume.allSands)
			{
				float num3 = getMaxSandLevel.Invoke(allSand) * num;
				getMaxSandLevel.Invoke(allSand) = num3;
				allSand.state.linearSandLevel = num3;
				float num4 = getStrataSandFillRate.Invoke(allSand) * num2;
				getStrataSandFillRate.Invoke(allSand) = num4;
				getStrataSandDigRateMultiplier.Invoke(allSand) = CaspersSandConfigPlugin.defaultDigRateMultiplier.Value;
			}
			getSandPumpMaxRemovalRate.Invoke(GameDefines.instance) = float.MaxValue;
		}

		[HarmonyPatch(typeof(SandVolume), "Update")]
		[HarmonyPrefix]
		public static void OverrideDigRate(SandVolume __instance)
		{
			float value = Traverse.Create((object)__instance).Field("num2").GetValue<float>();
			float cHEAT_DigAmountModifier = SandVolume.CHEAT_DigAmountModifier;
			float num = getStrataSandDigRateMultiplier.Invoke(__instance);
			setDigRateThisFrame.Invoke(__instance) = value * cHEAT_DigAmountModifier * num;
		}
	}
}