Decompiled source of SurtlingFuel v1.4.0

plugins/SurtlingFuel.dll

Decompiled 2 weeks 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;
using UnityEngine;

[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("SurtlingFuel")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d19d78a2b790c63d93d05838b611289362d8503b")]
[assembly: AssemblyProduct("SurtlingFuel")]
[assembly: AssemblyTitle("SurtlingFuel")]
[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 SurtlingFuel
{
	[BepInPlugin("com.dejan.surtlingfuel", "SurtlingFuel", "1.4.0")]
	public class SurtlingFuelPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.dejan.surtlingfuel";

		public const string PluginName = "SurtlingFuel";

		public const string PluginVersion = "1.4.0";

		public static ConfigEntry<int> SmelterFuelPerCore;

		public static ConfigEntry<int> BlastFurnaceFuelPerCore;

		public static ConfigEntry<float> SpeedMultiplier;

		public static ConfigEntry<bool> PreferCoresOverCoal;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Expected O, but got Unknown
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Expected O, but got Unknown
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Expected O, but got Unknown
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Expected O, but got Unknown
			//IL_022c: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			SmelterFuelPerCore = ((BaseUnityPlugin)this).Config.Bind<int>("Balance", "SmelterFuelPerCore", 4, "How many coal-equivalent fuel units 1 surtling core provides in the smelter. Vanilla smelter uses 2 coal per ore, so 4 = 2 ores per core.");
			BlastFurnaceFuelPerCore = ((BaseUnityPlugin)this).Config.Bind<int>("Balance", "BlastFurnaceFuelPerCore", 4, "How many coal-equivalent fuel units 1 surtling core provides in the blast furnace. Vanilla blast furnace uses 2 coal per ore, so 4 = 2 ores per core.");
			SpeedMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Balance", "SpeedMultiplier", 2f, "Smelting speed multiplier while burning surtling cores. 2.0 = twice as fast.");
			PreferCoresOverCoal = ((BaseUnityPlugin)this).Config.Bind<bool>("Preference", "PreferCoresOverCoal", true, "When true, surtling cores are used before coal when pressing E on the fuel switch. When false, coal is used first and cores are only used when you have no coal. This is a client-side setting — each player controls their own preference.");
			_harmony = new Harmony("com.dejan.surtlingfuel");
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(Smelter), "Awake", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(SmelterPatches), "Awake_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find Smelter.Awake method!");
				}
				MethodInfo methodInfo2 = AccessTools.Method(typeof(Smelter), "OnAddFuel", new Type[3]
				{
					typeof(Switch),
					typeof(Humanoid),
					typeof(ItemData)
				}, (Type[])null);
				if (methodInfo2 != null)
				{
					_harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(SmelterPatches), "OnAddFuel_Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find Smelter.OnAddFuel method!");
				}
				MethodInfo methodInfo3 = AccessTools.Method(typeof(Smelter), "OnHoverAddFuel", (Type[])null, (Type[])null);
				if (methodInfo3 != null)
				{
					_harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(SmelterPatches), "OnHoverAddFuel_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find Smelter.OnHoverAddFuel method!");
				}
				MethodInfo methodInfo4 = AccessTools.Method(typeof(Smelter), "UpdateSmelter", (Type[])null, (Type[])null);
				if (methodInfo4 != null)
				{
					_harmony.Patch((MethodBase)methodInfo4, new HarmonyMethod(typeof(SmelterPatches), "UpdateSmelter_Prefix", (Type[])null), new HarmonyMethod(typeof(SmelterPatches), "UpdateSmelter_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find Smelter.UpdateSmelter method!");
				}
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to apply patches: {arg}");
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SurtlingFuel v1.4.0 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		public static int GetFuelPerCore(Smelter smelter)
		{
			if ((Object)(object)smelter == (Object)null)
			{
				return SmelterFuelPerCore.Value;
			}
			if (smelter.m_name.ToLowerInvariant().Contains("blast"))
			{
				return BlastFurnaceFuelPerCore.Value;
			}
			return SmelterFuelPerCore.Value;
		}
	}
	public static class SmelterPatches
	{
		private const string SurtlingCorePrefab = "SurtlingCore";

		private const string RPC_AddCoreFuel = "RPC_SurtlingFuel_AddCoreFuel";

		private static readonly int CoreFuelKey = StringExtensionMethods.GetStableHashCode("SurtlingFuel_CoreFuel");

		private static readonly FieldInfo NViewField = AccessTools.Field(typeof(Smelter), "m_nview");

		private static ZNetView GetNView(Smelter smelter)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			return (ZNetView)NViewField.GetValue(smelter);
		}

		private static ItemData FindSurtlingCore(Inventory inventory)
		{
			if (inventory == null)
			{
				return null;
			}
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if ((Object)(object)allItem.m_dropPrefab != (Object)null && ((Object)allItem.m_dropPrefab).name == "SurtlingCore")
				{
					return allItem;
				}
				if (allItem.m_shared != null && allItem.m_shared.m_name == "$item_surtlingcore")
				{
					return allItem;
				}
			}
			return null;
		}

		public static void Awake_Postfix(Smelter __instance)
		{
			ZNetView nview = GetNView(__instance);
			if ((Object)(object)nview == (Object)null || nview.GetZDO() == null)
			{
				return;
			}
			nview.Register<int>("RPC_SurtlingFuel_AddCoreFuel", (Action<long, int>)delegate(long sender, int fuelToAdd)
			{
				//IL_0097: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
				if (nview.IsOwner())
				{
					ZDO zDO = nview.GetZDO();
					if (zDO != null)
					{
						float @float = zDO.GetFloat(ZDOVars.s_fuel, 0f);
						int num = __instance.m_maxFuel - Mathf.CeilToInt(@float);
						if (num > 0)
						{
							int num2 = Mathf.Min(fuelToAdd, num);
							zDO.Set(ZDOVars.s_fuel, @float + (float)num2);
							int @int = zDO.GetInt(CoreFuelKey, 0);
							zDO.Set(CoreFuelKey, @int + num2, false);
							EffectList fuelAddedEffects = __instance.m_fuelAddedEffects;
							if (fuelAddedEffects != null)
							{
								fuelAddedEffects.Create(((Component)__instance).transform.position, ((Component)__instance).transform.rotation, ((Component)__instance).transform, 1f, -1);
							}
						}
					}
				}
			});
		}

		public static bool OnAddFuel_Prefix(Smelter __instance, ref bool __result, Switch sw, Humanoid user, ItemData item)
		{
			if ((Object)(object)user == (Object)null || user.GetInventory() == null)
			{
				return true;
			}
			ZNetView nView = GetNView(__instance);
			if ((Object)(object)nView == (Object)null || !nView.IsValid())
			{
				return true;
			}
			ZDO zDO = nView.GetZDO();
			if (zDO == null)
			{
				return true;
			}
			float @float = zDO.GetFloat(ZDOVars.s_fuel, 0f);
			bool flag = zDO.GetInt(CoreFuelKey, 0) > 0;
			bool flag2 = @float > 0.01f && !flag;
			ItemData val = null;
			bool flag3 = false;
			if (item != null)
			{
				if (((Object)(object)item.m_dropPrefab != (Object)null && ((Object)item.m_dropPrefab).name == "SurtlingCore") || (item.m_shared != null && item.m_shared.m_name == "$item_surtlingcore"))
				{
					val = item;
					flag3 = true;
				}
			}
			else
			{
				if (!SurtlingFuelPlugin.PreferCoresOverCoal.Value && (Object)(object)__instance.m_fuelItem != (Object)null && user.GetInventory().HaveItem(__instance.m_fuelItem.m_itemData.m_shared.m_name, true) && !flag)
				{
					return true;
				}
				val = FindSurtlingCore(user.GetInventory());
				if (val != null)
				{
					flag3 = true;
				}
			}
			if (!flag3 && flag)
			{
				((Character)user).Message((MessageType)2, "Smelter is burning Surtling Cores", 0, (Sprite)null);
				__result = false;
				return false;
			}
			if (flag3 && flag2)
			{
				((Character)user).Message((MessageType)2, "Smelter is burning Coal", 0, (Sprite)null);
				__result = false;
				return false;
			}
			if (!flag3)
			{
				return true;
			}
			if (@float > (float)(__instance.m_maxFuel - 1))
			{
				((Character)user).Message((MessageType)2, "$msg_itsfull", 0, (Sprite)null);
				__result = false;
				return false;
			}
			int num = Mathf.Min(SurtlingFuelPlugin.GetFuelPerCore(__instance), __instance.m_maxFuel - Mathf.CeilToInt(@float));
			user.GetInventory().RemoveItem(val, 1);
			nView.InvokeRPC("RPC_SurtlingFuel_AddCoreFuel", new object[1] { num });
			((Character)user).Message((MessageType)2, $"Surtling Core added (+{num} fuel)", 0, (Sprite)null);
			__result = true;
			return false;
		}

		public static void OnHoverAddFuel_Postfix(Smelter __instance, ref string __result)
		{
			if (__result != null && (Object)(object)__instance.m_fuelItem != (Object)null)
			{
				string name = __instance.m_fuelItem.m_itemData.m_shared.m_name;
				if (__result.Contains(name))
				{
					__result = __result.Replace(name, name + " / Surtling Core");
				}
			}
		}

		public static void UpdateSmelter_Prefix(Smelter __instance, out float __state)
		{
			__state = __instance.m_secPerProduct;
			ZNetView nView = GetNView(__instance);
			if ((Object)(object)nView == (Object)null || !nView.IsValid())
			{
				return;
			}
			ZDO zDO = nView.GetZDO();
			if (zDO != null && zDO.GetInt(CoreFuelKey, 0) > 0)
			{
				float value = SurtlingFuelPlugin.SpeedMultiplier.Value;
				if (value > 0f)
				{
					__instance.m_secPerProduct /= value;
				}
			}
		}

		public static void UpdateSmelter_Postfix(Smelter __instance, float __state)
		{
			__instance.m_secPerProduct = __state;
			ZNetView nView = GetNView(__instance);
			if ((Object)(object)nView == (Object)null || !nView.IsValid())
			{
				return;
			}
			ZDO zDO = nView.GetZDO();
			if (zDO == null)
			{
				return;
			}
			int @int = zDO.GetInt(CoreFuelKey, 0);
			if (@int > 0)
			{
				int num = Mathf.CeilToInt(zDO.GetFloat(ZDOVars.s_fuel, 0f));
				if (num < @int)
				{
					zDO.Set(CoreFuelKey, Mathf.Max(0, num), false);
				}
				if (num <= 0)
				{
					zDO.Set(CoreFuelKey, 0, false);
				}
			}
		}
	}
}