Decompiled source of AllTheOresMagic v1.0.3

BepInEx/plugins/AllTheOresMagic/AllTheOresMagic.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace AllTheOresMagic
{
	[BepInPlugin("com.alltheores.magic.valheim", "AllTheOres (Magic)", "1.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class AllTheOresMagicPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.alltheores.magic.valheim";

		public const string PluginName = "AllTheOres (Magic)";

		public const string PluginVersion = "1.0.3";

		private static readonly (string From, string To)[] BlastFurnaceRecipes = new(string, string)[3]
		{
			("IronScrap", "Iron"),
			("CopperOre", "Copper"),
			("TinOre", "Tin")
		};

		private AllTheOresMagicSettings settings;

		private bool blastFurnaceConversionsAdded;

		private void Awake()
		{
			settings = new AllTheOresMagicSettings(((BaseUnityPlugin)this).Config);
			settings.EnableBlastFurnaceCompatibility.SettingChanged += OnBlastFurnaceCompatibilityChanged;
			if (settings.EnableGaldrTableRecipes.Value)
			{
				AddGaldrTableRecipes();
			}
			if (settings.EnableBlastFurnaceCompatibility.Value)
			{
				EnableBlastFurnaceCompatibility();
			}
		}

		private void OnDestroy()
		{
			settings.EnableBlastFurnaceCompatibility.SettingChanged -= OnBlastFurnaceCompatibilityChanged;
			ItemManager.OnItemsRegistered -= VerifyBlastFurnaceConversions;
		}

		private void OnBlastFurnaceCompatibilityChanged(object sender, EventArgs e)
		{
			if (settings.EnableBlastFurnaceCompatibility.Value)
			{
				EnableBlastFurnaceCompatibility();
			}
		}

		private void EnableBlastFurnaceCompatibility()
		{
			if (!blastFurnaceConversionsAdded)
			{
				AddBlastFurnaceConversions();
				blastFurnaceConversionsAdded = true;
			}
			ItemManager.OnItemsRegistered -= VerifyBlastFurnaceConversions;
			ItemManager.OnItemsRegistered += VerifyBlastFurnaceConversions;
			if ((Object)(object)PrefabManager.Instance.GetPrefab("blastfurnace") != (Object)null)
			{
				VerifyBlastFurnaceConversions();
			}
		}

		private void AddGaldrTableRecipes()
		{
			AddRecipe("Iron", "BlackMetalToIron", settings.BlackMetalToIronCost.Value, settings.RefinedEitrToIronCost.Value, settings.IronOutputAmount.Value);
			AddRecipe("Copper", "BlackMetalToCopper", settings.BlackMetalToCopperCost.Value, settings.RefinedEitrToCopperCost.Value, settings.CopperOutputAmount.Value);
			AddRecipe("Tin", "BlackMetalToTin", settings.BlackMetalToTinCost.Value, settings.RefinedEitrToTinCost.Value, settings.TinOutputAmount.Value);
		}

		private void AddRecipe(string outputItem, string recipeId, int blackMetalCost, int refinedEitrCost, int outputAmount)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			RecipeConfig val = new RecipeConfig
			{
				Name = "AllTheOresMagic_" + recipeId,
				Item = outputItem,
				Amount = Math.Max(1, outputAmount),
				Enabled = true,
				CraftingStation = CraftingStations.GaldrTable,
				MinStationLevel = 1
			};
			val.AddRequirement("BlackMetal", Math.Max(1, blackMetalCost), 0);
			val.AddRequirement("Eitr", Math.Max(1, refinedEitrCost), 0);
			if (!ItemManager.Instance.AddRecipe(new CustomRecipe(val)))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not add Galdr Table recipe for " + outputItem + "."));
			}
		}

		private void AddBlastFurnaceConversions()
		{
			(string, string)[] blastFurnaceRecipes = BlastFurnaceRecipes;
			for (int i = 0; i < blastFurnaceRecipes.Length; i++)
			{
				var (inputItem, outputItem) = blastFurnaceRecipes[i];
				AddBlastFurnaceConversion(inputItem, outputItem);
			}
		}

		private void AddBlastFurnaceConversion(string inputItem, string outputItem)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			SmelterConversionConfig val = new SmelterConversionConfig
			{
				Station = "blastfurnace",
				FromItem = inputItem,
				ToItem = outputItem
			};
			if (!ItemManager.Instance.AddItemConversion(new CustomItemConversion((ConversionConfig)(object)val)))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not add Blast Furnace conversion: " + inputItem + " -> " + outputItem + "."));
			}
		}

		private void VerifyBlastFurnaceConversions()
		{
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Expected O, but got Unknown
			GameObject prefab = PrefabManager.Instance.GetPrefab("blastfurnace");
			Smelter val = (((Object)(object)prefab == (Object)null) ? null : prefab.GetComponent<Smelter>());
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Could not find the vanilla blastfurnace Smelter component.");
				return;
			}
			(string, string)[] blastFurnaceRecipes = BlastFurnaceRecipes;
			for (int i = 0; i < blastFurnaceRecipes.Length; i++)
			{
				var (inputItem, text) = blastFurnaceRecipes[i];
				if (!val.m_conversion.Any((ItemConversion conversion) => (Object)(object)conversion.m_from != (Object)null && ((Object)((Component)conversion.m_from).gameObject).name == inputItem))
				{
					GameObject prefab2 = PrefabManager.Instance.GetPrefab(inputItem);
					GameObject prefab3 = PrefabManager.Instance.GetPrefab(text);
					ItemDrop val2 = (((Object)(object)prefab2 == (Object)null) ? null : prefab2.GetComponent<ItemDrop>());
					ItemDrop val3 = (((Object)(object)prefab3 == (Object)null) ? null : prefab3.GetComponent<ItemDrop>());
					if ((Object)(object)val2 == (Object)null || (Object)(object)val3 == (Object)null)
					{
						((BaseUnityPlugin)this).Logger.LogError((object)("Could not resolve vanilla Blast Furnace assets: " + inputItem + " -> " + text + "."));
					}
					else
					{
						val.m_conversion.Add(new ItemConversion
						{
							m_from = val2,
							m_to = val3
						});
					}
				}
			}
		}
	}
	public sealed class AllTheOresMagicSettings
	{
		public readonly ConfigEntry<bool> EnableGaldrTableRecipes;

		public readonly ConfigEntry<bool> EnableBlastFurnaceCompatibility;

		public readonly ConfigEntry<int> BlackMetalToIronCost;

		public readonly ConfigEntry<int> RefinedEitrToIronCost;

		public readonly ConfigEntry<int> IronOutputAmount;

		public readonly ConfigEntry<int> BlackMetalToCopperCost;

		public readonly ConfigEntry<int> RefinedEitrToCopperCost;

		public readonly ConfigEntry<int> CopperOutputAmount;

		public readonly ConfigEntry<int> BlackMetalToTinCost;

		public readonly ConfigEntry<int> RefinedEitrToTinCost;

		public readonly ConfigEntry<int> TinOutputAmount;

		public AllTheOresMagicSettings(ConfigFile config)
		{
			EnableGaldrTableRecipes = config.Bind<bool>("General", "EnableGaldrTableRecipes", true, "Enable Galdr Table recipes.");
			EnableBlastFurnaceCompatibility = config.Bind<bool>("General", "EnableBlastFurnaceCompatibility", false, "Allows the Blast Furnace to smelt Iron Scrap, Copper Ore and Tin Ore.");
			BlackMetalToIronCost = BindAmount(config, "BlackMetalToIronCost", 30, "Black Metal for Iron.");
			RefinedEitrToIronCost = BindAmount(config, "RefinedEitrToIronCost", 1, "Refined Eitr for Iron.");
			IronOutputAmount = BindAmount(config, "IronOutputAmount", 15, "Iron output.");
			BlackMetalToCopperCost = BindAmount(config, "BlackMetalToCopperCost", 30, "Black Metal for Copper.");
			RefinedEitrToCopperCost = BindAmount(config, "RefinedEitrToCopperCost", 1, "Refined Eitr for Copper.");
			CopperOutputAmount = BindAmount(config, "CopperOutputAmount", 15, "Copper output.");
			BlackMetalToTinCost = BindAmount(config, "BlackMetalToTinCost", 30, "Black Metal for Tin.");
			RefinedEitrToTinCost = BindAmount(config, "RefinedEitrToTinCost", 1, "Refined Eitr for Tin.");
			TinOutputAmount = BindAmount(config, "TinOutputAmount", 15, "Tin output.");
		}

		private static ConfigEntry<int> BindAmount(ConfigFile config, string key, int defaultValue, string description)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			return config.Bind<int>("Galdr Table Recipes", key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 999), Array.Empty<object>()));
		}
	}
}