Decompiled source of BetterEnergyExchanger v1.1.0

BetterEnergyExchanger.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BetterEnergyExchanger")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2faf58d16ee54159f2821059a859a33db1628d3a")]
[assembly: AssemblyProduct("BetterEnergyExchanger")]
[assembly: AssemblyTitle("BetterEnergyExchanger")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BetterEnergyExchanger
{
	[BepInPlugin("kelso4321.plugin.BetterEnergyExchanger", "BetterEnergyExchanger", "1.1.0")]
	public class BetterEnergyExchanger : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(PowerSystem), "NewExchangerComponent")]
		public static class ExchangerCreatePatch
		{
			private static void Postfix(PowerSystem __instance, int __result)
			{
				if (__result > 0)
				{
					PowerExchangerComponent[] excPool = __instance.excPool;
					if (excPool != null && __result < excPool.Length)
					{
						ref PowerExchangerComponent reference = ref excPool[__result];
						int value = ExchangerMultiplier.Value;
						reference.maxPoolEnergy *= value;
						reference.energyPerTick *= value;
						Logger.LogDebug((object)$"Scaled exchanger #{__result} x{value}");
					}
				}
			}
		}

		[HarmonyPatch(typeof(PowerSystem), "Import")]
		public static class ExchangerImportPatch
		{
			private static void Postfix(PowerSystem __instance)
			{
				PowerExchangerComponent[] excPool = __instance.excPool;
				if (excPool == null)
				{
					return;
				}
				int value = ExchangerMultiplier.Value;
				for (int i = 1; i < excPool.Length; i++)
				{
					if (excPool[i].id != 0)
					{
						ref PowerExchangerComponent reference = ref excPool[i];
						reference.maxPoolEnergy *= value;
						reference.energyPerTick *= value;
					}
				}
				Logger.LogInfo((object)$"Rescaled loaded exchangers x{value}");
			}
		}

		[HarmonyPatch(typeof(GameData), "Import")]
		public static class GameDataImportPatch
		{
			private static void Postfix()
			{
				Logger.LogInfo((object)"Applying accumulator capacity modification...");
				int value = ExchangerMultiplier.Value;
				ModifyAccumulator(2206, value);
				ModifyAccumulator(2207, value);
			}
		}

		public const string GUID = "kelso4321.plugin.BetterEnergyExchanger";

		public const string NAME = "BetterEnergyExchanger";

		public const string VERSION = "1.1.0";

		internal static BetterEnergyExchanger Instance;

		internal static ManualLogSource Logger;

		internal static Harmony harmony;

		private static ConfigEntry<int> ExchangerMultiplier;

		private void Awake()
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"BetterEnergyExchanger v1.1.0 loaded!");
			InitConfig();
			harmony = new Harmony("kelso4321.plugin.BetterEnergyExchanger");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
		}

		private static void InitConfig()
		{
			Logger.LogInfo((object)("Initializing config from " + ((BaseUnityPlugin)Instance).Config.ConfigFilePath + "..."));
			ExchangerMultiplier = CreateConfigEntry("ExchangerMultiplier", "Exchanger multiplier", "Multiplier for energy exchanger charge / discharge rate and battery capacity");
			Logger.LogInfo((object)$"Config initialized with ExchangerMultiplier: {ExchangerMultiplier.Value}");
		}

		private static ConfigEntry<int> CreateConfigEntry(string key, string name, string description, int defaultValue = 5, int min = 1, int max = 10)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			return ((BaseUnityPlugin)Instance).Config.Bind<int>("General", key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<int>(min, max), Array.Empty<object>()));
		}

		private static void ModifyAccumulator(int itemId, int mult)
		{
			ItemProto val = ((ProtoSet<ItemProto>)(object)LDB.items).Select(itemId);
			if (val?.prefabDesc == null)
			{
				Logger.LogWarning((object)$"Accumulator {itemId} prefab missing!");
				return;
			}
			long maxAcuEnergy = val.prefabDesc.maxAcuEnergy;
			PrefabDesc prefabDesc = val.prefabDesc;
			prefabDesc.maxAcuEnergy *= mult;
			Logger.LogInfo((object)$"Accumulator {itemId}: {maxAcuEnergy} -> {val.prefabDesc.maxAcuEnergy}");
		}
	}
}