Decompiled source of AllTheOres v1.0.5

BepInEx/plugins/AllTheOres/AllTheOres.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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 AllTheOres
{
	[BepInPlugin("com.alltheores.valheim", "All the Ores", "1.0.5")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class AllTheOresPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.alltheores.valheim";

		public const string PluginName = "All the Ores";

		public const string PluginVersion = "1.0.5";

		internal static ManualLogSource Log = null;

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

		private Harmony harmony;

		private AllTheOresSettings settings;

		private 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;
			settings = new AllTheOresSettings(((BaseUnityPlugin)this).Config);
			harmony = new Harmony("com.alltheores.valheim");
			harmony.PatchAll(typeof(AllTheOresPlugin).Assembly);
			if (settings.EnableForgeRecipes.Value)
			{
				AddForgeRecipes();
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Forge recipes are disabled in the config.");
			}
			if (settings.EnableBlastFurnaceCompatibility.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Blast Furnace compatibility enabled: IronScrap, CopperOre and TinOre.");
				AddBlastFurnaceConversions();
				ItemManager.OnItemsRegistered -= VerifyBlastFurnaceConversions;
				ItemManager.OnItemsRegistered += VerifyBlastFurnaceConversions;
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Blast Furnace compatibility is disabled in the config.");
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"All the Ores 1.0.5 loaded.");
		}

		private void OnDestroy()
		{
			ItemManager.OnItemsRegistered -= VerifyBlastFurnaceConversions;
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}

		private void AddForgeRecipes()
		{
			AddForgeRecipe("Iron", "BlackmetalToIron", settings.BlackmetalToIronCost.Value);
			AddForgeRecipe("Copper", "BlackmetalToCopper", settings.BlackmetalToCopperCost.Value);
			AddForgeRecipe("Tin", "BlackmetalToTin", settings.BlackmetalToTinCost.Value);
		}

		private void AddForgeRecipe(string outputItem, string recipeId, int configuredCost)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			int num = ((configuredCost < 1) ? 1 : configuredCost);
			RecipeConfig val = new RecipeConfig
			{
				Name = "AllTheOres_" + recipeId,
				Item = outputItem,
				Amount = 1,
				Enabled = true,
				CraftingStation = CraftingStations.Forge,
				MinStationLevel = 1
			};
			val.AddRequirement("BlackMetal", num, 0);
			if (ItemManager.Instance.AddRecipe(new CustomRecipe(val)))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Added Forge recipe: " + num + " Black Metal -> 1 " + outputItem + "."));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not add Forge 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.LogInfo((object)("Added Blast Furnace conversion: " + inputItem + " -> " + outputItem + "."));
			}
			else
			{
				((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
						});
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Verified Blast Furnace conversion: " + inputItem + " -> " + text + "."));
					}
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class VanillaItemIconRepair
	{
		private static readonly HashSet<ItemData> Reported = new HashSet<ItemData>();

		private static IEnumerable<MethodBase> TargetMethods()
		{
			return from method in typeof(Inventory).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
				where method.Name == "Load"
				select method;
		}

		[HarmonyPostfix]
		private static void Postfix(Inventory __instance)
		{
			foreach (ItemData allItem in __instance.GetAllItems())
			{
				Sprite[] array = allItem.m_shared?.m_icons;
				if (array == null || array.Length == 0)
				{
					continue;
				}
				int variant = allItem.m_variant;
				if (variant < 0 || variant >= array.Length)
				{
					allItem.m_variant = 0;
					if (Reported.Add(allItem))
					{
						string text = (((Object)(object)allItem.m_dropPrefab != (Object)null) ? ((Object)allItem.m_dropPrefab).name : (allItem.m_shared?.m_name ?? "unknown"));
						AllTheOresPlugin.Log.LogWarning((object)("Repaired invalid icon variant " + variant + " on item " + text + " to vanilla icon 0."));
					}
				}
			}
		}
	}
	public sealed class AllTheOresSettings
	{
		public readonly ConfigEntry<bool> EnableForgeRecipes;

		public readonly ConfigEntry<bool> EnableBlastFurnaceCompatibility;

		public readonly ConfigEntry<int> BlackmetalToIronCost;

		public readonly ConfigEntry<int> BlackmetalToCopperCost;

		public readonly ConfigEntry<int> BlackmetalToTinCost;

		public AllTheOresSettings(ConfigFile config)
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			EnableForgeRecipes = config.Bind<bool>("General", "EnableForgeRecipes", true, "Adds the three Black Metal conversion recipes to the Forge.");
			EnableBlastFurnaceCompatibility = config.Bind<bool>("General", "EnableBlastFurnaceCompatibility", false, "Allows the Blast Furnace to smelt Iron Scrap, Copper Ore and Tin Ore.");
			BlackmetalToIronCost = config.Bind<int>("Forge Recipes", "BlackmetalToIronCost", 2, new ConfigDescription("Black Metal bars required for one Iron bar.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 99), Array.Empty<object>()));
			BlackmetalToCopperCost = config.Bind<int>("Forge Recipes", "BlackmetalToCopperCost", 2, new ConfigDescription("Black Metal bars required for one Copper bar.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 99), Array.Empty<object>()));
			BlackmetalToTinCost = config.Bind<int>("Forge Recipes", "BlackmetalToTinCost", 2, new ConfigDescription("Black Metal bars required for one Tin bar.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 99), Array.Empty<object>()));
		}
	}
}