Decompiled source of ChargingStationTuning v1.0.0

plugins/ChargingStationTuning/ChargingStationTuning.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ChargingStationTuning.Configuration;
using ChargingStationTuning.Shop;
using ChargingStationTuning.Station;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.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 ChargingStationTuning
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "DarkSpider90.ChargingStationTuning";

		public const string PLUGIN_NAME = "ChargingStationTuning";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	[BepInPlugin("DarkSpider90.ChargingStationTuning", "ChargingStationTuning", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			TuningSettings.Bind(((BaseUnityPlugin)this).Config);
			ApplyPatches();
			ChargingStationMembers.ReportAvailability();
			Log.LogInfo((object)"ChargingStationTuning v1.0.0 loaded.");
		}

		private void ApplyPatches()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("DarkSpider90.ChargingStationTuning");
			Type[] typesFromAssembly = AccessTools.GetTypesFromAssembly(Assembly.GetExecutingAssembly());
			foreach (Type type in typesFromAssembly)
			{
				try
				{
					List<MethodInfo> list = _harmony.CreateClassProcessor(type).Patch();
					if ((list == null || list.Count <= 0) && DeclaresHarmonyPatchMethods(type))
					{
						Log.LogError((object)("[Plugin] " + type.FullName + " declares Harmony patch methods but carries no [HarmonyPatch] attribute on the class itself, so none of them were applied."));
					}
				}
				catch (Exception arg)
				{
					Log.LogError((object)$"[Plugin] Failed to apply patches from {type.FullName}; continuing. {arg}");
				}
			}
		}

		private static bool DeclaresHarmonyPatchMethods(Type type)
		{
			foreach (MethodInfo declaredMethod in AccessTools.GetDeclaredMethods(type))
			{
				if (((MemberInfo)declaredMethod).GetCustomAttribute<HarmonyPrefix>() != null || ((MemberInfo)declaredMethod).GetCustomAttribute<HarmonyPostfix>() != null || ((MemberInfo)declaredMethod).GetCustomAttribute<HarmonyTranspiler>() != null || ((MemberInfo)declaredMethod).GetCustomAttribute<HarmonyFinalizer>() != null)
				{
					return true;
				}
			}
			return false;
		}
	}
}
namespace ChargingStationTuning.Station
{
	internal static class ChargeCostTuner
	{
		private const float MinimumChargeCost = 1E-06f;

		internal static void Apply(ChargingStation station)
		{
			if (TuningSettings.WantsChargingChange && !((Object)(object)station == (Object)null) && ChargingStationMembers.TryReadChargeCost(station, out var chargeCost))
			{
				float num = Mathf.Max(1E-06f, chargeCost * TuningSettings.EnergyCostMultiplier.Value);
				if (ChargingStationMembers.TryWriteChargeCost(station, num) && TuningSettings.DebugLogs.Value)
				{
					Plugin.Log.LogInfo((object)($"[Charging] {((Object)station).name}: energy per unit of charge {chargeCost:G6} -> {num:G6} " + $"(x{TuningSettings.EnergyCostMultiplier.Value:0.##})."));
				}
			}
		}
	}
	internal static class ChargingStationMembers
	{
		private const string BatteryChargeCostField = "batteryChargeCost";

		private static FieldInfo batteryChargeCost;

		private static bool resolved;

		internal static bool IsAvailable
		{
			get
			{
				Resolve();
				return batteryChargeCost != null;
			}
		}

		internal static bool TryReadChargeCost(ChargingStation station, out float chargeCost)
		{
			chargeCost = 0f;
			if (!IsAvailable || (Object)(object)station == (Object)null)
			{
				return false;
			}
			if (!(batteryChargeCost.GetValue(station) is float num))
			{
				return false;
			}
			chargeCost = num;
			return true;
		}

		internal static bool TryWriteChargeCost(ChargingStation station, float chargeCost)
		{
			if (!IsAvailable || (Object)(object)station == (Object)null)
			{
				return false;
			}
			batteryChargeCost.SetValue(station, chargeCost);
			return true;
		}

		internal static void ReportAvailability()
		{
			if (IsAvailable)
			{
				if (TuningSettings.DebugLogs.Value)
				{
					Plugin.Log.LogInfo((object)"[Station] ChargingStation.batteryChargeCost resolved; charging changes are active.");
				}
			}
			else
			{
				Plugin.Log.LogError((object)"[Station] ChargingStation has no \"batteryChargeCost\" field in this version of R.E.P.O., so the charging cost cannot be changed. The mod needs an update; nothing else it does has been touched.");
			}
		}

		private static void Resolve()
		{
			if (!resolved)
			{
				resolved = true;
				batteryChargeCost = AccessTools.Field(typeof(ChargingStation), "batteryChargeCost");
			}
		}
	}
}
namespace ChargingStationTuning.Shop
{
	internal static class CrystalPriceTuner
	{
		private const float MinimumPrice = 1f;

		internal static float Apply(float vanillaPrice)
		{
			if (!TuningSettings.WantsPriceChange)
			{
				return vanillaPrice;
			}
			float num = Mathf.Max(1f, Mathf.Ceil(vanillaPrice * TuningSettings.CrystalPriceMultiplier.Value));
			if (TuningSettings.DebugLogs.Value)
			{
				Plugin.Log.LogInfo((object)($"[Shop] Energy crystal price {vanillaPrice:0} -> {num:0} " + $"(x{TuningSettings.CrystalPriceMultiplier.Value:0.##})."));
			}
			return num;
		}
	}
}
namespace ChargingStationTuning.Patches
{
	[HarmonyPatch(typeof(ChargingStation))]
	internal static class ChargingStationPatches
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPostfix(ChargingStation __instance)
		{
			ChargeCostTuner.Apply(__instance);
		}
	}
	[HarmonyPatch(typeof(ShopManager))]
	internal static class ShopManagerPatches
	{
		[HarmonyPatch("CrystalValueGet")]
		[HarmonyPostfix]
		private static void CrystalValueGetPostfix(ref float __result)
		{
			__result = CrystalPriceTuner.Apply(__result);
		}
	}
}
namespace ChargingStationTuning.Configuration
{
	internal static class TuningSettings
	{
		private const string GeneralSection = "General";

		private const string ChargingSection = "Charging";

		private const string ShopSection = "Shop";

		internal static ConfigEntry<bool> EnableMod { get; private set; }

		internal static ConfigEntry<bool> DebugLogs { get; private set; }

		internal static ConfigEntry<float> EnergyCostMultiplier { get; private set; }

		internal static ConfigEntry<float> CrystalPriceMultiplier { get; private set; }

		internal static bool WantsChargingChange
		{
			get
			{
				if (EnableMod.Value)
				{
					return !Mathf.Approximately(EnergyCostMultiplier.Value, 1f);
				}
				return false;
			}
		}

		internal static bool WantsPriceChange
		{
			get
			{
				if (EnableMod.Value)
				{
					return !Mathf.Approximately(CrystalPriceMultiplier.Value, 1f);
				}
				return false;
			}
		}

		internal static void Bind(ConfigFile config)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Expected O, but got Unknown
			EnableMod = config.Bind<bool>("General", "Enable Mod", true, "Turn the whole mod off without uninstalling it.");
			DebugLogs = config.Bind<bool>("General", "Debug Logs", false, "Write a line to the log for every change the mod makes.");
			EnergyCostMultiplier = config.Bind<float>("Charging", "Energy Cost Multiplier", 0.5f, new ConfigDescription("How much truck energy charging your gear costs, as a share of what the game charges. 1.00 is exactly vanilla, 0.50 is half the energy for the same charge, 2.00 is twice as much. This is the setting the mod exists for.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 20f), Array.Empty<object>()));
			CrystalPriceMultiplier = config.Bind<float>("Shop", "Crystal Price Multiplier", 1f, new ConfigDescription("What an energy crystal costs in the shop, as a share of the normal price. 1.00 leaves the price alone. Raise it if you want to pay for the cheaper charging rather than get it free.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
		}
	}
}