Decompiled source of Supermans Smelters v1.1.0

SupermansSmelters.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_utils")]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 Smelters
{
	[BepInPlugin("supermans.smelters", "Supermans Smelters", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal class Station
		{
			public ConfigEntry<float> Input;

			public ConfigEntry<float> Fuel;
		}

		public const string Guid = "supermans.smelters";

		public const string Name = "Supermans Smelters";

		public const string Version = "1.1.0";

		internal static ManualLogSource Log;

		internal static ConfigFile Cfg;

		internal static ConfigEntry<bool> ModEnabled;

		internal static readonly Dictionary<string, Station> Stations = new Dictionary<string, Station>();

		private void Awake()
		{
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Cfg = ((BaseUnityPlugin)this).Config;
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Master switch. When off, every station goes back to its normal capacity.");
			Known("$piece_smelter", "2 - Smelter", "OreMultiplier", "ore", "CoalMultiplier", "coal");
			Known("$piece_blastfurnace", "3 - Blast Furnace", "OreMultiplier", "ore", "CoalMultiplier", "coal");
			Known("$piece_charcoalkiln", "4 - Charcoal Kiln", "WoodMultiplier", "wood");
			Known("$piece_eitrrefinery", "5 - Eitr Refinery", "SoftTissueMultiplier", "soft tissue", "SapMultiplier", "sap");
			Known("$piece_windmill", "6 - Windmill", "BarleyMultiplier", "barley");
			Known("$piece_spinningwheel", "7 - Spinning Wheel", "FlaxMultiplier", "flax");
			((BaseUnityPlugin)this).Config.SettingChanged += delegate
			{
				StationPatches.ApplyAll();
			};
			new Harmony("supermans.smelters").PatchAll();
			Log.LogInfo((object)"Supermans Smelters 1.1.0 loaded");
		}

		private static void Known(string token, string section, string inputKey, string inputItem, string fuelKey = null, string fuelItem = null)
		{
			string text = section.Substring(section.IndexOf('-') + 2).ToLowerInvariant();
			Stations[token] = new Station
			{
				Input = ((inputKey == null) ? null : Multiplier(section, inputKey, "How much " + inputItem + " the " + text + " holds")),
				Fuel = ((fuelKey == null) ? null : Multiplier(section, fuelKey, "How much " + fuelItem + " the " + text + " holds"))
			};
		}

		internal static void Discover(string token, bool hasInput, bool hasFuel, string inputItem, string fuelItem)
		{
			if (!string.IsNullOrEmpty(token) && !Stations.ContainsKey(token) && (hasInput || hasFuel))
			{
				string text = ((Localization.instance != null) ? Localization.instance.Localize(token) : token);
				string section = "9 - Other: " + Regex.Replace(text, "[\\[\\]=\\n\\r\\t\"'\\\\]", "").Trim();
				Stations[token] = new Station
				{
					Input = (hasInput ? Multiplier(section, "InputMultiplier", "How much " + Item(inputItem, "input") + " it holds") : null),
					Fuel = (hasFuel ? Multiplier(section, "FuelMultiplier", "How much " + Item(fuelItem, "fuel") + " it holds") : null)
				};
				Log.LogInfo((object)("Found another processing station: " + text + " (" + token + ")"));
			}
		}

		private static string Item(string token, string fallback)
		{
			if (!string.IsNullOrEmpty(token) && Localization.instance != null)
			{
				return Localization.instance.Localize(token).ToLowerInvariant();
			}
			return fallback;
		}

		private static ConfigEntry<float> Multiplier(string section, string key, string description)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			return Cfg.Bind<float>(section, key, 1f, new ConfigDescription(description + ", times its normal amount. 1 = normal, 3 = triple.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 3f), Array.Empty<object>()));
		}

		internal static float InputMultiplier(string token)
		{
			return Get(token, input: true);
		}

		internal static float FuelMultiplier(string token)
		{
			return Get(token, input: false);
		}

		private static float Get(string token, bool input)
		{
			if (!ModEnabled.Value || !Stations.TryGetValue(token, out var value))
			{
				return 1f;
			}
			ConfigEntry<float> val = (input ? value.Input : value.Fuel);
			if (val != null)
			{
				return Mathf.Clamp(val.Value, 1f, 3f);
			}
			return 1f;
		}
	}
	[HarmonyPatch]
	internal static class StationPatches
	{
		private struct Limits
		{
			public int input;

			public int fuel;
		}

		private static readonly Dictionary<string, Limits> Originals = new Dictionary<string, Limits>();

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		private static void FindStations(ZNetScene __instance)
		{
			foreach (GameObject prefab in __instance.m_prefabs)
			{
				if (!((Object)(object)prefab == (Object)null))
				{
					Smelter componentInChildren = prefab.GetComponentInChildren<Smelter>(true);
					if ((Object)(object)componentInChildren != (Object)null)
					{
						Plugin.Discover(componentInChildren.m_name, componentInChildren.m_maxOre > 0, componentInChildren.m_maxFuel > 0, FirstInput(componentInChildren), ((Object)(object)componentInChildren.m_fuelItem != (Object)null) ? componentInChildren.m_fuelItem.m_itemData.m_shared.m_name : null);
					}
				}
			}
		}

		private static string FirstInput(Smelter smelter)
		{
			foreach (ItemConversion item in smelter.m_conversion)
			{
				if ((Object)(object)item?.m_from != (Object)null)
				{
					return item.m_from.m_itemData.m_shared.m_name;
				}
			}
			return null;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Smelter), "Awake")]
		private static void SmelterAwake(Smelter __instance)
		{
			string key = PrefabName(__instance);
			if (!Originals.ContainsKey(key))
			{
				Originals[key] = new Limits
				{
					input = __instance.m_maxOre,
					fuel = __instance.m_maxFuel
				};
			}
			Apply(__instance);
		}

		internal static void ApplyAll()
		{
			Smelter[] array = Object.FindObjectsByType<Smelter>((FindObjectsSortMode)0);
			for (int i = 0; i < array.Length; i++)
			{
				Apply(array[i]);
			}
		}

		private static void Apply(Smelter smelter)
		{
			if (Originals.TryGetValue(PrefabName(smelter), out var value))
			{
				smelter.m_maxOre = Scale(value.input, Plugin.InputMultiplier(smelter.m_name));
				smelter.m_maxFuel = Scale(value.fuel, Plugin.FuelMultiplier(smelter.m_name));
			}
		}

		private static string PrefabName(Smelter station)
		{
			ZNetView componentInParent = ((Component)station).GetComponentInParent<ZNetView>();
			return Utils.GetPrefabName(((Object)(object)componentInParent != (Object)null) ? ((Component)componentInParent).gameObject : ((Component)station).gameObject);
		}

		private static int Scale(int original, float multiplier)
		{
			if (original > 0)
			{
				return Mathf.Max(1, Mathf.RoundToInt((float)original * multiplier));
			}
			return original;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}