Decompiled source of ArcanumWindmill v1.0.0

BepInEx/plugins/ArcanumWindmill/ArcanumWindmill.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("ArcanumWindmill")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ArcanumWindmill")]
[assembly: AssemblyTitle("ArcanumWindmill")]
[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 ArcanumWindmill
{
	[BepInPlugin("arcanum.windmill", "ArcanumWindmill", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "arcanum.windmill";

		public const string PluginName = "ArcanumWindmill";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<bool> IgnoreWindRestrictions;

		internal static ConfigEntry<float> ProcessingSpeed;

		private Harmony harmony;

		private void Awake()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			IgnoreWindRestrictions = ((BaseUnityPlugin)this).Config.Bind<bool>("Windmill", "IgnoreWindRestrictions", true, "Ignore wind intensity and shelter for production. True uses vanilla maximum wind power. Visuals still follow weather.");
			ProcessingSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Windmill", "ProcessingSpeed", 1f, new ConfigDescription("Independent production multiplier: 1 = normal, 2 = double, 0.5 = half. Range 0.01 to 10. Restart after editing the config.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 10f), Array.Empty<object>()));
			if (float.IsNaN(ProcessingSpeed.Value) || float.IsInfinity(ProcessingSpeed.Value))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"ProcessingSpeed must be finite; resetting to 1.");
				ProcessingSpeed.Value = 1f;
			}
			harmony = new Harmony("arcanum.windmill");
			try
			{
				harmony.PatchAll(typeof(Plugin).Assembly);
				((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1} loaded. IgnoreWindRestrictions={2}, ProcessingSpeed={3}", "ArcanumWindmill", "1.0.0", IgnoreWindRestrictions.Value, ProcessingSpeed.Value));
			}
			catch (Exception arg)
			{
				harmony.UnpatchSelf();
				((BaseUnityPlugin)this).Logger.LogError((object)$"Windmill patch could not be applied; vanilla behavior retained. {arg}");
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	internal static class ProductionMath
	{
		internal static float Scale(float basePower, float speed)
		{
			if (float.IsNaN(speed) || float.IsInfinity(speed))
			{
				speed = 1f;
			}
			if (speed < 0.01f)
			{
				speed = 0.01f;
			}
			if (speed > 10f)
			{
				speed = 10f;
			}
			return basePower * speed;
		}
	}
	[HarmonyPatch(typeof(Smelter), "UpdateSmelter")]
	internal static class WindmillProductionPatch
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = instructions.ToList();
			MethodInfo original = AccessTools.Method(typeof(Windmill), "GetPowerOutput", Type.EmptyTypes, (Type[])null);
			MethodInfo operand = AccessTools.Method(typeof(WindmillProductionPatch), "GetProductionPower", (Type[])null, (Type[])null);
			List<CodeInstruction> list2 = list.Where((CodeInstruction instruction) => CodeInstructionExtensions.Calls(instruction, original)).ToList();
			if (list2.Count != 1)
			{
				throw new InvalidOperationException($"Expected one Windmill.GetPowerOutput call in Smelter.UpdateSmelter; found {list2.Count}. Check compatibility with this game version.");
			}
			list2[0].opcode = OpCodes.Call;
			list2[0].operand = operand;
			return list;
		}

		private static float GetProductionPower(Windmill windmill)
		{
			return ProductionMath.Scale(Plugin.IgnoreWindRestrictions.Value ? 1f : windmill.GetPowerOutput(), Plugin.ProcessingSpeed.Value);
		}
	}
}