Decompiled source of ValheimInfiniteFire v1.1.0

plugins/ValheimInfiniteFire.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Managers;
using UnityEngine;
using ValheimInfiniteFire.common;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ValheimInfiniteFire")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValheimInfiniteFire")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
namespace ValheimInfiniteFire
{
	internal static class Patches
	{
		[HarmonyPatch(typeof(Smelter), "GetFuel")]
		internal static class SmelterGetFuel
		{
			[HarmonyPostfix]
			internal static void Postfix(Smelter __instance, ref float __result)
			{
				string prefabName = Utils.GetPrefabName(((Object)((Component)__instance).gameObject).name);
				ValConfig.NoFuelConfigs.TryGetValue(prefabName, out var value);
				if (value != null && value.Value)
				{
					__result = 1f;
				}
			}
		}

		[HarmonyPatch(typeof(CookingStation), "GetFuel")]
		internal static class CookerGetFuel
		{
			[HarmonyPostfix]
			internal static void Postfix(Smelter __instance, ref float __result)
			{
				string prefabName = Utils.GetPrefabName(((Object)((Component)__instance).gameObject).name);
				ValConfig.NoFuelConfigs.TryGetValue(prefabName, out var value);
				if (value != null && value.Value)
				{
					__result = 1f;
				}
			}
		}

		[HarmonyPatch(typeof(CookingStation), "OnHoverFuelSwitch")]
		internal static class OnHoverDisplayNoFuelRequirementCookingStation
		{
			[HarmonyAfter(new string[] { "shudnal.MyLittleUI" })]
			[HarmonyPostfix]
			internal static void Postfix(CookingStation __instance, ref string __result)
			{
				string prefabName = Utils.GetPrefabName(((Object)((Component)__instance).gameObject).name);
				ValConfig.NoFuelConfigs.TryGetValue(prefabName, out var value);
				if (value != null && value.Value)
				{
					__result = "No Fuel Needed.";
				}
			}
		}

		[HarmonyPatch(typeof(Smelter), "OnHoverAddFuel")]
		internal static class OnHoverDisplayNoFuelRequirementSmelter
		{
			[HarmonyAfter(new string[] { "shudnal.MyLittleUI" })]
			[HarmonyPostfix]
			internal static void Postfix(Smelter __instance, ref string __result)
			{
				string prefabName = Utils.GetPrefabName(((Object)((Component)__instance).gameObject).name);
				ValConfig.NoFuelConfigs.TryGetValue(prefabName, out var value);
				if (value != null && value.Value)
				{
					__result = "No Fuel Needed.";
				}
			}
		}
	}
	[BepInPlugin("MidnightsFX.InfiniteFire", "InfiniteFire", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	internal class ValheimInfiniteFire : BaseUnityPlugin
	{
		public const string PluginGUID = "MidnightsFX.InfiniteFire";

		public const string PluginName = "InfiniteFire";

		public const string PluginVersion = "1.1.0";

		public ValConfig cfg;

		public static ManualLogSource Log;

		public static Dictionary<string, SmokeSpawner> SmokeSpawners = new Dictionary<string, SmokeSpawner>();

		public static Harmony HarmonyInstance { get; private set; }

		public void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			cfg = new ValConfig(((BaseUnityPlugin)this).Config);
			HarmonyInstance = new Harmony("MidnightsFX.InfiniteFire");
			HarmonyInstance.PatchAll();
			PrefabManager.OnPrefabsRegistered += FindAllFireTypes;
			PrefabManager.OnPrefabsRegistered += FindAllSmelters;
			PrefabManager.OnPrefabsRegistered += FindAllCookingStation;
			Logger.LogDebug("Lets Light it up");
		}

		public static void FindAllCookingStation()
		{
			CookingStation[] array = Resources.FindObjectsOfTypeAll<CookingStation>();
			foreach (CookingStation val in array)
			{
				if (!((Object)(object)val.m_fuelItem == (Object)null))
				{
					string prefabName = Utils.GetPrefabName(((Object)((Component)val).gameObject).name);
					ConfigEntry<bool> val2 = ValConfig.BindServerConfig("InfiniteFuel", prefabName, value: true, "Enable infinite fuel for this cooking station.");
					ValConfig.NoFuelConfigs.Add(prefabName, val2);
					Logger.LogDebug($"Registering {prefabName} with InfiniteFuel {val2.Value}");
				}
			}
		}

		public static void FindAllFireTypes()
		{
			Fireplace[] array = Resources.FindObjectsOfTypeAll<Fireplace>();
			foreach (Fireplace val in array)
			{
				string prefabname = Utils.GetPrefabName(((Object)((Component)val).gameObject).name);
				ConfigEntry<bool> enableFire = ValConfig.BindServerConfig("InfiniteFire", prefabname, value: true, "Enable infinite fuel for this fire.");
				Logger.LogDebug($"Registering {prefabname} with infinitefire {enableFire.Value}");
				val.m_infiniteFuel = enableFire.Value;
				enableFire.SettingChanged += delegate
				{
					foreach (Fireplace item in from fp in Resources.FindObjectsOfTypeAll<Fireplace>()
						where ((Object)((Component)fp).gameObject).name.StartsWith(prefabname)
						select fp)
					{
						Logger.LogDebug($"Updating {((Object)item).name} to InfiniteFire:{enableFire.Value}");
						item.m_infiniteFuel = enableFire.Value;
					}
				};
				ValConfig.NoFuelConfigs.Add(prefabname, enableFire);
			}
		}

		public static void FindAllSmelters()
		{
			Smelter[] array = Resources.FindObjectsOfTypeAll<Smelter>();
			foreach (Smelter val in array)
			{
				string prefabName = Utils.GetPrefabName(((Object)((Component)val).gameObject).name);
				ConfigEntry<bool> val2 = ValConfig.BindServerConfig("InfiniteFuel", prefabName, value: false, "Enable infinite fuel for this smelter.");
				ValConfig.NoFuelConfigs.Add(prefabName, val2);
				Logger.LogDebug($"Registering {prefabName} with InfiniteFuel {val2.Value}");
			}
		}

		public static void SmokeLess()
		{
			Smelter[] array = Resources.FindObjectsOfTypeAll<Smelter>();
			foreach (Smelter smelter in array)
			{
				if ((Object)(object)smelter.m_smokeSpawner == (Object)null)
				{
					continue;
				}
				string prefabname2 = Utils.GetPrefabName(((Object)((Component)smelter).gameObject).name);
				ConfigEntry<bool> noSmoke2 = ValConfig.BindServerConfig("NoSmoke", prefabname2, value: false, "Enable or disable smoke for this smelter.");
				SmokeSpawners.Add(prefabname2, smelter.m_smokeSpawner);
				Logger.LogDebug($"Registering {prefabname2} for NoSmoke Options {noSmoke2.Value}");
				noSmoke2.SettingChanged += delegate
				{
					foreach (Smelter item in from fp in Resources.FindObjectsOfTypeAll<Smelter>()
						where ((Object)((Component)fp).gameObject).name.StartsWith(prefabname2)
						select fp)
					{
						Logger.LogDebug($"Updating {((Object)item).name} to NoSmoke:{noSmoke2.Value}");
						if (noSmoke2.Value)
						{
							if ((Object)(object)smelter.m_smokeSpawner != (Object)null)
							{
								Object.Destroy((Object)(object)smelter.m_smokeSpawner);
							}
							smelter.m_smokeSpawner = null;
						}
						else
						{
							SmokeSpawners.TryGetValue(prefabname2, out var value2);
							if ((Object)(object)value2 != (Object)null)
							{
								smelter.m_smokeSpawner = value2;
							}
						}
					}
				};
			}
			Fireplace[] array2 = Resources.FindObjectsOfTypeAll<Fireplace>();
			foreach (Fireplace smelter2 in array2)
			{
				if ((Object)(object)smelter2.m_smokeSpawner == (Object)null)
				{
					continue;
				}
				string prefabname = Utils.GetPrefabName(((Object)((Component)smelter2).gameObject).name);
				ConfigEntry<bool> noSmoke = ValConfig.BindServerConfig("NoSmoke", prefabname, value: false, "Enable or disable smoke for this Fireplace.");
				SmokeSpawners.Add(prefabname, smelter2.m_smokeSpawner);
				Logger.LogDebug($"Registering {prefabname} for NoSmoke Options {noSmoke.Value}");
				noSmoke.SettingChanged += delegate
				{
					foreach (Fireplace item2 in from fp in Resources.FindObjectsOfTypeAll<Fireplace>()
						where ((Object)((Component)fp).gameObject).name.StartsWith(prefabname)
						select fp)
					{
						Logger.LogDebug($"Updating {((Object)item2).name} to NoSmoke:{noSmoke.Value}");
						if (noSmoke.Value)
						{
							if ((Object)(object)smelter2.m_smokeSpawner != (Object)null)
							{
								Object.Destroy((Object)(object)smelter2.m_smokeSpawner);
							}
							smelter2.m_smokeSpawner = null;
						}
						else
						{
							SmokeSpawners.TryGetValue(prefabname, out var value);
							if ((Object)(object)value != (Object)null)
							{
								smelter2.m_smokeSpawner = value;
							}
						}
					}
				};
			}
		}
	}
}
namespace ValheimInfiniteFire.common
{
	internal static class Logger
	{
		public static LogLevel Level = (LogLevel)16;

		public static void enableDebugLogging(object sender, EventArgs e)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if (ValConfig.EnableDebugMode.Value)
			{
				Level = (LogLevel)32;
			}
			else
			{
				Level = (LogLevel)16;
			}
		}

		public static void CheckEnableDebugLogging()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if (ValConfig.EnableDebugMode.Value)
			{
				Level = (LogLevel)32;
			}
			else
			{
				Level = (LogLevel)16;
			}
		}

		public static void LogDebug(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)Level >= 32)
			{
				ValheimInfiniteFire.Log.LogInfo((object)message);
			}
		}

		public static void LogInfo(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)Level >= 16)
			{
				ValheimInfiniteFire.Log.LogInfo((object)message);
			}
		}

		public static void LogWarning(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)Level >= 4)
			{
				ValheimInfiniteFire.Log.LogWarning((object)message);
			}
		}

		public static void LogError(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)Level >= 2)
			{
				ValheimInfiniteFire.Log.LogError((object)message);
			}
		}
	}
	internal class ValConfig
	{
		public static ConfigFile cfg;

		public static Dictionary<string, ConfigEntry<bool>> NoFuelConfigs = new Dictionary<string, ConfigEntry<bool>>();

		public static ConfigEntry<bool> EnableDebugMode;

		public ValConfig(ConfigFile cf)
		{
			cfg = cf;
			cfg.SaveOnConfigSet = true;
			CreateConfigValues(cf);
		}

		private void CreateConfigValues(ConfigFile Config)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			EnableDebugMode = Config.Bind<bool>("Client config", "EnableDebugMode", false, new ConfigDescription("Enables Debug logging.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdvanced = true
			} }));
			EnableDebugMode.SettingChanged += Logger.enableDebugLogging;
			Logger.CheckEnableDebugLogging();
		}

		public static ConfigEntry<bool> BindServerConfig(string catagory, string key, bool value, string description, AcceptableValueBase acceptableValues = null, bool advanced = false)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			return cfg.Bind<bool>(catagory, key, value, new ConfigDescription(description, acceptableValues, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}
	}
}