Decompiled source of DowngradeableIdols v1.0.0

BepInEx/plugins/DowngradeableIdols.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DowngradeableIdols")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0830ee782f97241a59e752fe8bf9dd3544691110")]
[assembly: AssemblyProduct("DowngradeableIdols")]
[assembly: AssemblyTitle("DowngradeableIdols")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DowngradeableIdols;

[BepInPlugin("com.valheimmods.downgradeableidols", "Downgradeable Idols", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	internal static class ObjectDBAwakePatch
	{
		[HarmonyPostfix]
		private static void Postfix(ObjectDB __instance)
		{
			Apply(__instance);
		}
	}

	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	internal static class ObjectDBCopyPatch
	{
		[HarmonyPrepare]
		private static bool Prepare()
		{
			return AccessTools.Method(typeof(ObjectDB), "CopyOtherDB", (Type[])null, (Type[])null) != null;
		}

		[HarmonyPostfix]
		private static void Postfix(ObjectDB __instance)
		{
			Apply(__instance);
		}
	}

	public const string PluginGuid = "com.valheimmods.downgradeableidols";

	public const string PluginName = "Downgradeable Idols";

	public const string PluginVersion = "1.0.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<bool> DoWeapons;

	internal static ConfigEntry<bool> DoArmor;

	internal static ConfigEntry<string> WorkbenchPrefab;

	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
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Master switch for the mod.");
		DoWeapons = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Recipes", "DowngradeBattleIdols", true, "Add workbench recipes that turn a Battle Idol into the Battle Idol of the tier below (1:1).");
		DoArmor = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Recipes", "DowngradeProtectionIdols", true, "Add workbench recipes that turn a Protection Idol into the Protection Idol of the tier below (1:1).");
		WorkbenchPrefab = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Advanced", "WorkbenchPrefab", "piece_workbench", "Prefab name of the crafting station the downgrade recipes are crafted at.");
		_harmony = new Harmony("com.valheimmods.downgradeableidols");
		_harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Downgradeable Idols v1.0.0 loaded.");
	}

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

	internal static void Apply(ObjectDB db)
	{
		if ((Object)(object)db == (Object)null || db.m_items == null || db.m_items.Count == 0 || !Enabled.Value)
		{
			return;
		}
		Dictionary<string, ItemDrop> dictionary = CollectItems(db);
		if (dictionary.Count == 0)
		{
			return;
		}
		CraftingStation val = FindStation(db);
		if ((Object)(object)val == (Object)null)
		{
			Log.LogWarning((object)("Could not find crafting station '" + WorkbenchPrefab.Value + "'. No downgrade recipes were added."));
			return;
		}
		if (db.m_recipes == null)
		{
			db.m_recipes = new List<Recipe>();
		}
		int num = 0;
		if (DoWeapons.Value)
		{
			num += AddDowngrades(db, dictionary, val, "Weapon", "Battle Idols");
		}
		if (DoArmor.Value)
		{
			num += AddDowngrades(db, dictionary, val, "Armor", "Protection Idols");
		}
		if (num > 0)
		{
			Log.LogInfo((object)("Added " + num + " idol downgrade recipe(s) to the " + val.m_name + "."));
		}
	}

	private static Dictionary<string, ItemDrop> CollectItems(ObjectDB db)
	{
		Dictionary<string, ItemDrop> dictionary = new Dictionary<string, ItemDrop>(StringComparer.Ordinal);
		foreach (GameObject item in db.m_items)
		{
			if (!((Object)(object)item == (Object)null))
			{
				ItemDrop component = item.GetComponent<ItemDrop>();
				if (!((Object)(object)component == (Object)null) && component.m_itemData != null && component.m_itemData.m_shared != null && !dictionary.ContainsKey(((Object)item).name))
				{
					dictionary.Add(((Object)item).name, component);
				}
			}
		}
		return dictionary;
	}

	private static CraftingStation FindStation(ObjectDB db)
	{
		string value = WorkbenchPrefab.Value;
		GameObject itemPrefab = db.GetItemPrefab(value);
		if ((Object)(object)itemPrefab != (Object)null)
		{
			CraftingStation component = itemPrefab.GetComponent<CraftingStation>();
			if ((Object)(object)component != (Object)null)
			{
				return component;
			}
		}
		if (db.m_recipes != null)
		{
			foreach (Recipe recipe in db.m_recipes)
			{
				if (!((Object)(object)recipe == (Object)null) && !((Object)(object)recipe.m_craftingStation == (Object)null) && recipe.m_craftingStation.m_name != null && recipe.m_craftingStation.m_name.IndexOf("workbench", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return recipe.m_craftingStation;
				}
			}
		}
		return null;
	}

	private static int AddDowngrades(ObjectDB db, Dictionary<string, ItemDrop> items, CraftingStation station, string suffix, string label)
	{
		int num = 0;
		for (int i = 1; i <= 64; i++)
		{
			string key = "Upgrader" + i + suffix;
			string key2 = "Upgrader" + (i - 1) + suffix;
			ItemDrop value;
			bool flag = items.TryGetValue(key, out value);
			ItemDrop value2;
			bool flag2 = items.TryGetValue(key2, out value2);
			if (!flag && !flag2)
			{
				break;
			}
			if (flag && flag2 && AddRecipe(db, station, value, value2))
			{
				num++;
			}
		}
		if (num > 0)
		{
			Log.LogDebug((object)("Added " + num + " " + label + " downgrade recipe(s)."));
		}
		return num;
	}

	private static bool AddRecipe(ObjectDB db, CraftingStation station, ItemDrop higher, ItemDrop lower)
	{
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Expected O, but got Unknown
		string text = "Recipe_Downgrade_" + ((Object)higher).name + "_To_" + ((Object)lower).name;
		foreach (Recipe recipe in db.m_recipes)
		{
			if ((Object)(object)recipe != (Object)null && ((Object)recipe).name == text)
			{
				return false;
			}
		}
		Recipe val = ScriptableObject.CreateInstance<Recipe>();
		((Object)val).name = text;
		val.m_item = lower;
		val.m_amount = 1;
		val.m_enabled = true;
		val.m_qualityResultAmountMultiplier = 1f;
		val.m_listSortWeight = 0;
		val.m_noCraftOnlyUpgrade = false;
		val.m_craftingStation = station;
		val.m_repairStation = null;
		val.m_minStationLevel = 1;
		val.m_requireOnlyOneIngredient = false;
		val.m_resources = (Requirement[])(object)new Requirement[1]
		{
			new Requirement
			{
				m_resItem = higher,
				m_amount = 1,
				m_extraAmountOnlyOneIngredient = 0,
				m_amountPerLevel = 0,
				m_upgraderResource = false,
				m_recover = true
			}
		};
		db.m_recipes.Add(val);
		Log.LogDebug((object)("Recipe added: 1x " + higher.m_itemData.m_shared.m_name + " -> 1x " + lower.m_itemData.m_shared.m_name));
		return true;
	}
}