Decompiled source of AnyWood v1.0.5

AnyWood.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using ConditionalConfigSync;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Any Wood")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Any Wood")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("b827ad55-2348-4519-89fe-968caec582f2")]
[assembly: AssemblyFileVersion("1.0.5")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.0")]
[module: UnverifiableCode]
namespace AnyWood;

[BepInPlugin("shudnal.AnyWood", "Any Wood", "1.0.5")]
[BepInDependency("_shudnal.ConditionalConfigSync", "1.0.5")]
[BepInIncompatibility("randyknapp.mods.itsjustwood")]
public class AnyWood : BaseUnityPlugin
{
	private sealed class SmelterCoalRule
	{
		internal readonly Dictionary<string, int> originalWoodConversionIndices = new Dictionary<string, int>();
	}

	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	private static class ObjectDB_CopyOtherDB_TransmuteRecipes
	{
		private static void Postfix(ObjectDB __instance)
		{
			if (modEnabled.Value && !((Object)(object)__instance.GetItemPrefab("Wood") == (Object)null))
			{
				InitializeItems(__instance);
				AddRecipes(__instance);
				CheckSmelterCoalConversions();
			}
		}
	}

	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	private static class ObjectDB_Awake_TransmuteRecipes
	{
		private static void Postfix(ObjectDB __instance)
		{
			if (modEnabled.Value && !((Object)(object)__instance.GetItemPrefab("Wood") == (Object)null))
			{
				InitializeItems(__instance);
				AddRecipes(__instance);
				CheckSmelterCoalConversions();
			}
		}
	}

	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	private static class ZNetScene_Awake_CoalConversions
	{
		private static void Postfix()
		{
			smelterCoalRules.Clear();
			CheckSmelterCoalConversions();
		}
	}

	[HarmonyPatch(typeof(Smelter), "Awake")]
	private static class Smelter_Awake_CoalConversions
	{
		[HarmonyPriority(800)]
		private static void Postfix(Smelter __instance)
		{
			SynchronizeSmelterCoalConversions(__instance);
		}
	}

	[HarmonyPatch(typeof(Smelter), "CanUseItems")]
	private static class Smelter_CanUseItems_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(Smelter __instance, Player player, Switch switchRef, ref ItemDrop __state)
		{
			if (!((Object)(object)switchRef != (Object)(object)__instance.m_addWoodSwitch))
			{
				ItemDrop replacementFuelItem = GetReplacementFuelItem(((Humanoid)player).GetInventory(), __instance.m_fuelItem);
				if (!((Object)(object)replacementFuelItem == (Object)null))
				{
					__state = __instance.m_fuelItem;
					__instance.m_fuelItem = replacementFuelItem;
				}
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(Smelter __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	[HarmonyPatch(typeof(Smelter), "TryGetItems")]
	private static class Smelter_TryGetItems_FuelOptions
	{
		private static void Postfix(Smelter __instance, Player player, Switch switchRef, ref List<string> items, bool __result)
		{
			if (__result && !((Object)(object)switchRef != (Object)(object)__instance.m_addWoodSwitch) && __instance.CanUseItems(player, switchRef, false))
			{
				items = GetFuelItemNames(__instance.m_fuelItem);
			}
		}
	}

	[HarmonyPatch(typeof(Smelter), "OnAddFuel")]
	private static class Smelter_OnAddFuel_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(Smelter __instance, Humanoid user, ItemData item, ref ItemDrop __state)
		{
			ItemDrop replacementFuelItem = GetReplacementFuelItem(user.GetInventory(), __instance.m_fuelItem, item);
			if (!((Object)(object)replacementFuelItem == (Object)null))
			{
				__state = __instance.m_fuelItem;
				__instance.m_fuelItem = replacementFuelItem;
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(Smelter __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	[HarmonyPatch(typeof(Fireplace), "CanUseItems")]
	private static class Fireplace_CanUseItems_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(Fireplace __instance, Player player, ref ItemDrop __state)
		{
			ItemDrop replacementFuelItem = GetReplacementFuelItem(((Humanoid)player).GetInventory(), __instance.m_fuelItem);
			if (!((Object)(object)replacementFuelItem == (Object)null))
			{
				__state = __instance.m_fuelItem;
				__instance.m_fuelItem = replacementFuelItem;
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(Fireplace __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	[HarmonyPatch(typeof(Fireplace), "TryGetItems")]
	private static class Fireplace_TryGetItems_FuelOptions
	{
		private static void Postfix(Fireplace __instance, Player player, ref List<string> items, bool __result)
		{
			if (__result && __instance.CanUseItems(player, false))
			{
				items = GetFuelItemNames(__instance.m_fuelItem);
			}
		}
	}

	[HarmonyPatch(typeof(Fireplace), "Interact")]
	public static class Fireplace_Interact_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(Fireplace __instance, Humanoid user, ref ItemDrop __state)
		{
			ItemDrop replacementFuelItem = GetReplacementFuelItem(user.GetInventory(), __instance.m_fuelItem);
			if (!((Object)(object)replacementFuelItem == (Object)null))
			{
				__state = __instance.m_fuelItem;
				__instance.m_fuelItem = replacementFuelItem;
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(Fireplace __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	[HarmonyPatch(typeof(Fireplace), "UseItem")]
	public static class Fireplace_UseItem_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(Fireplace __instance, Humanoid user, ItemData item, ref ItemDrop __state)
		{
			ItemDrop replacementFuelItem = GetReplacementFuelItem(user.GetInventory(), __instance.m_fuelItem, item);
			if (!((Object)(object)replacementFuelItem == (Object)null))
			{
				__state = __instance.m_fuelItem;
				__instance.m_fuelItem = replacementFuelItem;
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(Fireplace __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	[HarmonyPatch(typeof(CookingStation), "CanUseItems")]
	private static class CookingStation_CanUseItems_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(CookingStation __instance, Player player, Switch switchRef, ref ItemDrop __state)
		{
			if (!((Object)(object)switchRef != (Object)(object)__instance.m_addFuelSwitch))
			{
				ItemDrop replacementFuelItem = GetReplacementFuelItem(((Humanoid)player).GetInventory(), __instance.m_fuelItem);
				if (!((Object)(object)replacementFuelItem == (Object)null))
				{
					__state = __instance.m_fuelItem;
					__instance.m_fuelItem = replacementFuelItem;
				}
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(CookingStation __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	[HarmonyPatch(typeof(CookingStation), "TryGetItems")]
	private static class CookingStation_TryGetItems_FuelOptions
	{
		private static void Postfix(CookingStation __instance, Player player, Switch switchRef, ref List<string> items, bool __result)
		{
			if (__result && !((Object)(object)switchRef != (Object)(object)__instance.m_addFuelSwitch) && __instance.CanUseItems(player, switchRef, false))
			{
				items = GetFuelItemNames(__instance.m_fuelItem);
			}
		}
	}

	[HarmonyPatch(typeof(CookingStation), "OnAddFuelSwitch")]
	public static class CookingStation_OnAddFuelSwitch_FuelReplacement
	{
		[HarmonyPriority(0)]
		private static void Prefix(CookingStation __instance, Humanoid user, ItemData item, ref ItemDrop __state)
		{
			ItemDrop replacementFuelItem = GetReplacementFuelItem(user.GetInventory(), __instance.m_fuelItem, item);
			if (!((Object)(object)replacementFuelItem == (Object)null))
			{
				__state = __instance.m_fuelItem;
				__instance.m_fuelItem = replacementFuelItem;
			}
		}

		[HarmonyPriority(800)]
		private static Exception Finalizer(CookingStation __instance, ItemDrop __state, Exception __exception)
		{
			if ((Object)(object)__state != (Object)null)
			{
				__instance.m_fuelItem = __state;
			}
			return __exception;
		}
	}

	public const string pluginID = "shudnal.AnyWood";

	public const string pluginName = "Any Wood";

	public const string pluginVersion = "1.0.5";

	private Harmony _harmony;

	internal static readonly ConfigSync configSync = new ConfigSync("shudnal.AnyWood")
	{
		DisplayName = "Any Wood",
		CurrentVersion = "1.0.5",
		MinimumRequiredVersion = "1.0.5"
	};

	internal static AnyWood instance;

	private static ConfigEntry<bool> modEnabled;

	private static ConfigEntry<bool> configLocked;

	private static ConfigEntry<bool> loggingEnabled;

	private static ConfigEntry<Vector3> fineWoodToWood;

	private static ConfigEntry<Vector3> coreWoodToWood;

	private static ConfigEntry<Vector3> ancientBarkToWood;

	private static ConfigEntry<Vector3> yggdrasilWoodToWood;

	private static ConfigEntry<Vector3> ashWoodToWood;

	private static ConfigEntry<Vector3> timberWoodToWood;

	private static ConfigEntry<bool> fineWoodForFuel;

	private static ConfigEntry<bool> coreWoodForFuel;

	private static ConfigEntry<bool> ancientBarkForFuel;

	private static ConfigEntry<bool> yggdrasilWoodForFuel;

	private static ConfigEntry<bool> ashWoodForFuel;

	private static ConfigEntry<bool> timberWoodForFuel;

	private static ConfigEntry<bool> fineWoodForCoal;

	private static ConfigEntry<bool> coreWoodForCoal;

	private static ConfigEntry<bool> ancientBarkForCoal;

	private static ConfigEntry<bool> yggdrasilWoodForCoal;

	private static ConfigEntry<bool> ashWoodForCoal;

	private static ConfigEntry<bool> timberWoodForCoal;

	private static ItemDrop wood;

	private static ItemDrop fineWood;

	private static ItemDrop coreWood;

	private static ItemDrop ancientBark;

	private static ItemDrop yggdrasilWood;

	private static ItemDrop ashWood;

	private static ItemDrop timberWood;

	private static CraftingStation workbench;

	private static readonly Dictionary<string, SmelterCoalRule> smelterCoalRules = new Dictionary<string, SmelterCoalRule>();

	private void Awake()
	{
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "shudnal.AnyWood");
		instance = this;
		ConfigInit();
		((ConditionalConfigSync)configSync).AddLockingConfigEntry<bool>(configLocked);
	}

	private void ConfigInit()
	{
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		modEnabled = config("General", "Enabled", defaultValue: true, "Enable this mod. Relaunch the world to take effect.");
		configLocked = config("General", "Lock Configuration", defaultValue: true, "Configuration is locked and can be changed by server admins only.");
		loggingEnabled = config("General", "Enable logging", defaultValue: false, "Enable logging. [Not Synced with Server]", synchronizedSetting: false);
		fineWoodToWood = config<Vector3>("Transmute", "Fine wood", new Vector3(10f, 10f, 4f), "Fine wood to wood transmute recipe. First is fine wood used, second basic wood result, third - workbench level required.\nSet 0 to wood to disable recipe. Set 0 to station to disable station requirement.");
		coreWoodToWood = config<Vector3>("Transmute", "Core wood", new Vector3(10f, 10f, 3f), "Core wood to wood transmute recipe. First is core wood used, second basic wood result, third - workbench level required.\nSet 0 to wood to disable recipe. Set 0 to station to disable station requirement.");
		ancientBarkToWood = config<Vector3>("Transmute", "Ancient bark", new Vector3(10f, 10f, 4f), "Ancient bark to wood transmute recipe. First is ancient bark used, second basic wood result, third - workbench level required.\nSet 0 to wood to disable recipe. Set 0 to station to disable station requirement.");
		yggdrasilWoodToWood = config<Vector3>("Transmute", "Yggdrasil wood", new Vector3(10f, 10f, 5f), "Yggdrasil wood to wood transmute recipe. First is yggdrasil wood used, second basic wood result, third - workbench level required. \nSet 0 to wood to disable recipe. Set 0 to station to disable station requirement.");
		ashWoodToWood = config<Vector3>("Transmute", "Ash wood", new Vector3(10f, 10f, 5f), "Ash wood to wood transmute recipe. First is ash wood used, second basic wood result, third - workbench level required. \nSet 0 to wood to disable recipe. Set 0 to station to disable station requirement.");
		timberWoodToWood = config<Vector3>("Transmute", "Timberwood", new Vector3(10f, 10f, 5f), "Timberwood to wood transmute recipe. First is timberwood used, second basic wood result, third - workbench level required. \nSet 0 to wood to disable recipe. Set 0 to station to disable station requirement.");
		fineWoodToWood.SettingChanged += delegate
		{
			AddRecipes(ObjectDB.instance);
		};
		coreWoodToWood.SettingChanged += delegate
		{
			AddRecipes(ObjectDB.instance);
		};
		ancientBarkToWood.SettingChanged += delegate
		{
			AddRecipes(ObjectDB.instance);
		};
		yggdrasilWoodToWood.SettingChanged += delegate
		{
			AddRecipes(ObjectDB.instance);
		};
		ashWoodToWood.SettingChanged += delegate
		{
			AddRecipes(ObjectDB.instance);
		};
		timberWoodToWood.SettingChanged += delegate
		{
			AddRecipes(ObjectDB.instance);
		};
		fineWoodForFuel = config("Fuel", "Fine wood", defaultValue: false, "Use fine wood as a fuel at cooking station, bath and fireplaces");
		coreWoodForFuel = config("Fuel", "Core wood", defaultValue: true, "Use core wood as a fuel at cooking station, bath and fireplaces");
		ancientBarkForFuel = config("Fuel", "Ancient bark", defaultValue: true, "Use ancient bark as a fuel at cooking station, bath and fireplaces");
		yggdrasilWoodForFuel = config("Fuel", "Yggdrasil wood", defaultValue: false, "Use yggdrasil wood as a fuel at cooking station, bath and fireplaces");
		ashWoodForFuel = config("Fuel", "Ash wood", defaultValue: false, "Use ash wood as a fuel at cooking station, bath and fireplaces");
		timberWoodForFuel = config("Fuel", "Timberwood", defaultValue: false, "Use timberwood as a fuel at cooking station, bath and fireplaces");
		fineWoodForCoal = config("Coal", "Fine wood", defaultValue: true, "Use fine wood as a source of coal in smelters that convert regular wood to coal");
		coreWoodForCoal = config("Coal", "Core wood", defaultValue: true, "Use core wood as a source of coal in smelters that convert regular wood to coal");
		ancientBarkForCoal = config("Coal", "Ancient bark", defaultValue: true, "Use ancient bark as a source of coal in smelters that convert regular wood to coal");
		yggdrasilWoodForCoal = config("Coal", "Yggdrasil wood", defaultValue: false, "Use Yggdrasil wood as a source of coal in smelters that convert regular wood to coal");
		ashWoodForCoal = config("Coal", "Ash wood", defaultValue: false, "Use ash wood as a source of coal in smelters that convert regular wood to coal");
		timberWoodForCoal = config("Coal", "Timberwood", defaultValue: false, "Use timberwood as a source of coal in smelters that convert regular wood to coal");
		fineWoodForCoal.SettingChanged += delegate
		{
			CheckSmelterCoalConversions();
		};
		coreWoodForCoal.SettingChanged += delegate
		{
			CheckSmelterCoalConversions();
		};
		ancientBarkForCoal.SettingChanged += delegate
		{
			CheckSmelterCoalConversions();
		};
		yggdrasilWoodForCoal.SettingChanged += delegate
		{
			CheckSmelterCoalConversions();
		};
		ashWoodForCoal.SettingChanged += delegate
		{
			CheckSmelterCoalConversions();
		};
		timberWoodForCoal.SettingChanged += delegate
		{
			CheckSmelterCoalConversions();
		};
	}

	private ConfigEntry<T> config<T>(string group, string name, T defaultValue, ConfigDescription description, bool synchronizedSetting = true)
	{
		ConfigEntry<T> val = ((BaseUnityPlugin)this).Config.Bind<T>(group, name, defaultValue, description);
		((ConditionalConfigSync)configSync).AddConfigEntry<T>(val, (ConfigSyncMode)1, synchronizedSetting);
		return val;
	}

	private ConfigEntry<T> config<T>(string group, string name, T defaultValue, string description, bool synchronizedSetting = true)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Expected O, but got Unknown
		return config(group, name, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()), synchronizedSetting);
	}

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

	public static void LogInfo(object data)
	{
		if (loggingEnabled.Value)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo(data);
		}
	}

	private static bool IsModEnabled()
	{
		return modEnabled != null && modEnabled.Value;
	}

	private static bool AreFuelItemsReady()
	{
		return IsModEnabled() && (Object)(object)wood != (Object)null;
	}

	private static bool AreTransmuteItemsReady()
	{
		return AreFuelItemsReady();
	}

	private static ItemDrop GetItem(ObjectDB instance, string prefabName)
	{
		GameObject itemPrefab = instance.GetItemPrefab(prefabName);
		return ((Object)(object)itemPrefab == (Object)null) ? null : itemPrefab.GetComponent<ItemDrop>();
	}

	private static void InitializeItems(ObjectDB instance)
	{
		if (!((Object)(object)instance == (Object)null))
		{
			wood = GetItem(instance, "Wood");
			fineWood = GetItem(instance, "FineWood");
			coreWood = GetItem(instance, "RoundLog");
			ancientBark = GetItem(instance, "ElderBark");
			yggdrasilWood = GetItem(instance, "YggdrasilWood");
			ashWood = GetItem(instance, "Blackwood");
			timberWood = GetItem(instance, "Frostwood");
			workbench = GetWorkbench(instance);
		}
	}

	private static CraftingStation GetWorkbench(ObjectDB instance)
	{
		foreach (Recipe recipe in instance.m_recipes)
		{
			if ((Object)(object)recipe.m_craftingStation != (Object)null && (recipe.m_craftingStation.m_name == "$piece_workbench" || ((Object)recipe.m_craftingStation).name == "piece_workbench"))
			{
				return recipe.m_craftingStation;
			}
		}
		return null;
	}

	private static void AddRecipes(ObjectDB instance)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)instance == (Object)null) && AreTransmuteItemsReady())
		{
			AddTransmuteRecipe(instance, coreWood, wood, coreWoodToWood.Value);
			AddTransmuteRecipe(instance, fineWood, wood, fineWoodToWood.Value);
			AddTransmuteRecipe(instance, ancientBark, wood, ancientBarkToWood.Value);
			AddTransmuteRecipe(instance, yggdrasilWood, wood, yggdrasilWoodToWood.Value);
			AddTransmuteRecipe(instance, ashWood, wood, ashWoodToWood.Value);
			AddTransmuteRecipe(instance, timberWood, wood, timberWoodToWood.Value);
		}
	}

	private static void AddTransmuteRecipe(ObjectDB instance, ItemDrop itemUsed, ItemDrop itemResult, Vector3 ratio)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: 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_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Expected O, but got Unknown
		if ((Object)(object)itemUsed == (Object)null || (Object)(object)itemResult == (Object)null)
		{
			return;
		}
		int num = Mathf.CeilToInt(ratio.y);
		int num2 = Mathf.CeilToInt(ratio.x);
		int num3 = Mathf.CeilToInt(ratio.z);
		if (num == 0 || num2 == 0)
		{
			return;
		}
		if (num3 > 0 && (Object)(object)workbench == (Object)null)
		{
			LogInfo("Skipped transmute recipe for " + itemUsed.m_itemData.m_shared.m_name + ": workbench is not available");
			return;
		}
		GameObject val = itemUsed.m_itemData.m_dropPrefab;
		if ((Object)(object)val == (Object)null)
		{
			LogInfo("m_dropPrefab for " + itemUsed.m_itemData.m_shared.m_name + " is not set");
			string prefabName = itemUsed.GetPrefabName(((Object)((Component)itemUsed).gameObject).name);
			val = instance.GetItemPrefab(prefabName);
		}
		if ((Object)(object)val == (Object)null)
		{
			LogInfo("Prefab for " + itemUsed.m_itemData.m_shared.m_name + " is not found");
			return;
		}
		string recipeName = "shudnal.AnyWood".Replace(".", "_") + "_Transmute_" + ((Object)val).name + "_Wood";
		if (instance.m_recipes.RemoveAll((Recipe rec) => ((Object)rec).name == recipeName) > 0)
		{
			LogInfo("Removed recipe " + recipeName);
		}
		Recipe val2 = ScriptableObject.CreateInstance<Recipe>();
		((Object)val2).name = recipeName;
		val2.m_amount = num;
		val2.m_item = itemResult;
		val2.m_enabled = true;
		val2.m_resources = CollectionExtensions.AddToArray<Requirement>(val2.m_resources, new Requirement
		{
			m_amount = num2,
			m_resItem = itemUsed
		});
		if (num3 > 0)
		{
			val2.m_minStationLevel = num3;
			val2.m_craftingStation = workbench;
		}
		instance.m_recipes.Add(val2);
		LogInfo("Added " + recipeName + " transmute recipe " + itemUsed.m_itemData.m_shared.m_name + " => " + itemResult.m_itemData.m_shared.m_name);
	}

	private static bool IsSameItem(ItemData item, ItemDrop itemDrop)
	{
		if (item == null || (Object)(object)itemDrop == (Object)null)
		{
			return false;
		}
		if ((Object)(object)item.m_dropPrefab != (Object)null && (Object)(object)item.m_dropPrefab == (Object)(object)((Component)itemDrop).gameObject)
		{
			return true;
		}
		return item.m_shared.m_name == itemDrop.m_itemData.m_shared.m_name;
	}

	private static bool IsSameItem(ItemDrop left, ItemDrop right)
	{
		if ((Object)(object)left == (Object)null || (Object)(object)right == (Object)null)
		{
			return false;
		}
		if ((Object)(object)left == (Object)(object)right || (Object)(object)((Component)left).gameObject == (Object)(object)((Component)right).gameObject)
		{
			return true;
		}
		if ((Object)(object)((Component)left).gameObject != (Object)null && (Object)(object)((Component)right).gameObject != (Object)null && ((Object)((Component)left).gameObject).name == ((Object)((Component)right).gameObject).name)
		{
			return true;
		}
		return left.m_itemData.m_shared.m_name == right.m_itemData.m_shared.m_name;
	}

	private static bool HasConfiguredFuel(Inventory inventory, ItemDrop item, ConfigEntry<bool> enabled)
	{
		return (Object)(object)item != (Object)null && enabled != null && enabled.Value && inventory.HaveItem(item.m_itemData.m_shared.m_name, true);
	}

	private static void AddFuelItemName(List<string> items, ItemDrop item, ConfigEntry<bool> enabled = null)
	{
		if (!((Object)(object)item == (Object)null) && (enabled == null || enabled.Value))
		{
			string name = item.m_itemData.m_shared.m_name;
			if (!items.Contains(name))
			{
				items.Add(name);
			}
		}
	}

	private static List<string> GetFuelItemNames(ItemDrop builtIn)
	{
		List<string> list = new List<string>();
		if ((Object)(object)builtIn == (Object)null)
		{
			return list;
		}
		AddFuelItemName(list, builtIn);
		if (!AreFuelItemsReady() || !IsSameItem(builtIn, wood))
		{
			return list;
		}
		AddFuelItemName(list, ancientBark, ancientBarkForFuel);
		AddFuelItemName(list, coreWood, coreWoodForFuel);
		AddFuelItemName(list, fineWood, fineWoodForFuel);
		AddFuelItemName(list, yggdrasilWood, yggdrasilWoodForFuel);
		AddFuelItemName(list, ashWood, ashWoodForFuel);
		AddFuelItemName(list, timberWood, timberWoodForFuel);
		return list;
	}

	private static ItemDrop GetConfiguredFuelItem(ItemData item)
	{
		if (ancientBarkForFuel != null && ancientBarkForFuel.Value && IsSameItem(item, ancientBark))
		{
			return ancientBark;
		}
		if (coreWoodForFuel != null && coreWoodForFuel.Value && IsSameItem(item, coreWood))
		{
			return coreWood;
		}
		if (fineWoodForFuel != null && fineWoodForFuel.Value && IsSameItem(item, fineWood))
		{
			return fineWood;
		}
		if (yggdrasilWoodForFuel != null && yggdrasilWoodForFuel.Value && IsSameItem(item, yggdrasilWood))
		{
			return yggdrasilWood;
		}
		if (ashWoodForFuel != null && ashWoodForFuel.Value && IsSameItem(item, ashWood))
		{
			return ashWood;
		}
		if (timberWoodForFuel != null && timberWoodForFuel.Value && IsSameItem(item, timberWood))
		{
			return timberWood;
		}
		return null;
	}

	private static ItemDrop GetReplacementFuelItem(Inventory inventory, ItemDrop builtIn, ItemData requestedItem = null)
	{
		if (!AreFuelItemsReady() || inventory == null || !IsSameItem(builtIn, wood))
		{
			return null;
		}
		if (requestedItem != null)
		{
			ItemDrop configuredFuelItem = GetConfiguredFuelItem(requestedItem);
			if ((Object)(object)configuredFuelItem == (Object)null || !inventory.HaveItem(configuredFuelItem.m_itemData.m_shared.m_name, true))
			{
				return null;
			}
			LogInfo("Used " + configuredFuelItem.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return configuredFuelItem;
		}
		if (inventory.HaveItem(builtIn.m_itemData.m_shared.m_name, true))
		{
			return null;
		}
		if (HasConfiguredFuel(inventory, ancientBark, ancientBarkForFuel))
		{
			LogInfo("Used " + ancientBark.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return ancientBark;
		}
		if (HasConfiguredFuel(inventory, coreWood, coreWoodForFuel))
		{
			LogInfo("Used " + coreWood.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return coreWood;
		}
		if (HasConfiguredFuel(inventory, fineWood, fineWoodForFuel))
		{
			LogInfo("Used " + fineWood.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return fineWood;
		}
		if (HasConfiguredFuel(inventory, yggdrasilWood, yggdrasilWoodForFuel))
		{
			LogInfo("Used " + yggdrasilWood.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return yggdrasilWood;
		}
		if (HasConfiguredFuel(inventory, ashWood, ashWoodForFuel))
		{
			LogInfo("Used " + ashWood.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return ashWood;
		}
		if (HasConfiguredFuel(inventory, timberWood, timberWoodForFuel))
		{
			LogInfo("Used " + timberWood.m_itemData.m_shared.m_name + " instead of " + builtIn.m_itemData.m_shared.m_name);
			return timberWood;
		}
		return null;
	}

	private static string GetSmelterPrefabName(Smelter smelter)
	{
		if ((Object)(object)smelter == (Object)null)
		{
			return string.Empty;
		}
		ZNetView val = (((Object)(object)smelter.m_nview != (Object)null) ? smelter.m_nview : ((Component)smelter).GetComponentInParent<ZNetView>());
		if ((Object)(object)val != (Object)null)
		{
			return val.GetPrefabName();
		}
		Transform root = ((Component)smelter).transform.root;
		return ((Object)(object)root != (Object)null) ? Utils.GetPrefabName(((Component)root).gameObject) : Utils.GetPrefabName(((Component)smelter).gameObject);
	}

	private static string GetConversionItemPrefabName(ItemDrop item)
	{
		return ((Object)(object)item != (Object)null && (Object)(object)((Component)item).gameObject != (Object)null) ? Utils.GetPrefabName(((Component)item).gameObject) : string.Empty;
	}

	private static bool IsConversion(ItemConversion conversion, string fromPrefabName, string toPrefabName)
	{
		if (conversion == null)
		{
			return false;
		}
		return GetConversionItemPrefabName(conversion.m_from) == fromPrefabName && GetConversionItemPrefabName(conversion.m_to) == toPrefabName;
	}

	private static int FindConversionIndex(Smelter smelter, string fromPrefabName, string toPrefabName)
	{
		if (smelter?.m_conversion == null)
		{
			return -1;
		}
		for (int i = 0; i < smelter.m_conversion.Count; i++)
		{
			if (IsConversion(smelter.m_conversion[i], fromPrefabName, toPrefabName))
			{
				return i;
			}
		}
		return -1;
	}

	private static bool HasConversion(Smelter smelter, string fromPrefabName, string toPrefabName)
	{
		return FindConversionIndex(smelter, fromPrefabName, toPrefabName) >= 0;
	}

	private static int RemoveConversions(Smelter smelter, string fromPrefabName, string toPrefabName)
	{
		if (smelter?.m_conversion == null)
		{
			return 0;
		}
		int num = 0;
		for (int num2 = smelter.m_conversion.Count - 1; num2 >= 0; num2--)
		{
			if (IsConversion(smelter.m_conversion[num2], fromPrefabName, toPrefabName))
			{
				smelter.m_conversion.RemoveAt(num2);
				num++;
			}
		}
		return num;
	}

	private static bool IsKnownWoodPrefabName(string prefabName)
	{
		int result;
		switch (prefabName)
		{
		default:
			result = ((prefabName == "Frostwood") ? 1 : 0);
			break;
		case "Wood":
		case "FineWood":
		case "RoundLog":
		case "ElderBark":
		case "YggdrasilWood":
		case "Blackwood":
			result = 1;
			break;
		}
		return (byte)result != 0;
	}

	private static ItemDrop GetKnownWood(string prefabName)
	{
		ItemDrop val = null;
		switch (prefabName)
		{
		case "Wood":
			val = wood;
			break;
		case "FineWood":
			val = fineWood;
			break;
		case "RoundLog":
			val = coreWood;
			break;
		case "ElderBark":
			val = ancientBark;
			break;
		case "YggdrasilWood":
			val = yggdrasilWood;
			break;
		case "Blackwood":
			val = ashWood;
			break;
		case "Frostwood":
			val = timberWood;
			break;
		}
		if ((Object)(object)val != (Object)null)
		{
			return val;
		}
		GameObject val2 = (((Object)(object)ObjectDB.instance != (Object)null) ? ObjectDB.instance.GetItemPrefab(prefabName) : null);
		if ((Object)(object)val2 == (Object)null && (Object)(object)ZNetScene.instance != (Object)null)
		{
			val2 = ZNetScene.instance.GetPrefab(prefabName);
		}
		return ((Object)(object)val2 != (Object)null) ? val2.GetComponent<ItemDrop>() : null;
	}

	private static ItemConversion FindWoodToCoalConversion(Smelter smelter)
	{
		int num = FindConversionIndex(smelter, "Wood", "Coal");
		return (num >= 0) ? smelter.m_conversion[num] : null;
	}

	private static SmelterCoalRule GetOrCreateSmelterCoalRule(Smelter smelter, string smelterPrefabName = null)
	{
		if (!IsModEnabled() || (Object)(object)smelter == (Object)null || smelter.m_conversion == null)
		{
			return null;
		}
		if (FindWoodToCoalConversion(smelter) == null)
		{
			return null;
		}
		if (string.IsNullOrEmpty(smelterPrefabName))
		{
			smelterPrefabName = GetSmelterPrefabName(smelter);
		}
		if (string.IsNullOrEmpty(smelterPrefabName))
		{
			return null;
		}
		if (smelterCoalRules.TryGetValue(smelterPrefabName, out var value))
		{
			return value;
		}
		value = new SmelterCoalRule();
		for (int i = 0; i < smelter.m_conversion.Count; i++)
		{
			ItemConversion val = smelter.m_conversion[i];
			if (val != null && !(GetConversionItemPrefabName(val.m_to) != "Coal"))
			{
				string conversionItemPrefabName = GetConversionItemPrefabName(val.m_from);
				if (IsKnownWoodPrefabName(conversionItemPrefabName) && !value.originalWoodConversionIndices.ContainsKey(conversionItemPrefabName))
				{
					value.originalWoodConversionIndices.Add(conversionItemPrefabName, i);
				}
			}
		}
		smelterCoalRules.Add(smelterPrefabName, value);
		return value;
	}

	private static int GetCoalConversionInsertIndex(Smelter smelter, SmelterCoalRule rule, string itemPrefabName)
	{
		if (!rule.originalWoodConversionIndices.TryGetValue(itemPrefabName, out var value))
		{
			return smelter.m_conversion.Count;
		}
		int num = value;
		foreach (KeyValuePair<string, int> originalWoodConversionIndex in rule.originalWoodConversionIndices)
		{
			if (originalWoodConversionIndex.Value < value && !HasConversion(smelter, originalWoodConversionIndex.Key, "Coal"))
			{
				num--;
			}
		}
		return Mathf.Clamp(num, 0, smelter.m_conversion.Count);
	}

	private static void SynchronizeCoalConversion(Smelter smelter, string smelterPrefabName, SmelterCoalRule rule, string itemPrefabName, ConfigEntry<bool> enabled, ItemDrop coalOutput)
	{
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Expected O, but got Unknown
		if (enabled == null)
		{
			return;
		}
		if (!enabled.Value)
		{
			int num = RemoveConversions(smelter, itemPrefabName, "Coal");
			if (num > 0)
			{
				LogInfo("Removed " + itemPrefabName + " -> Coal from " + smelterPrefabName);
			}
		}
		else if (!HasConversion(smelter, itemPrefabName, "Coal"))
		{
			ItemDrop knownWood = GetKnownWood(itemPrefabName);
			if (!((Object)(object)knownWood == (Object)null) && !((Object)(object)coalOutput == (Object)null))
			{
				int coalConversionInsertIndex = GetCoalConversionInsertIndex(smelter, rule, itemPrefabName);
				smelter.m_conversion.Insert(coalConversionInsertIndex, new ItemConversion
				{
					m_from = knownWood,
					m_to = coalOutput
				});
				LogInfo("Added " + itemPrefabName + " -> Coal to " + smelterPrefabName);
			}
		}
	}

	private static void SynchronizeSmelterCoalConversions(Smelter smelter, string smelterPrefabName = null)
	{
		if (!IsModEnabled() || (Object)(object)smelter == (Object)null || smelter.m_conversion == null)
		{
			return;
		}
		ItemConversion val = FindWoodToCoalConversion(smelter);
		if (val != null)
		{
			if (string.IsNullOrEmpty(smelterPrefabName))
			{
				smelterPrefabName = GetSmelterPrefabName(smelter);
			}
			SmelterCoalRule orCreateSmelterCoalRule = GetOrCreateSmelterCoalRule(smelter, smelterPrefabName);
			if (orCreateSmelterCoalRule != null)
			{
				ItemDrop to = val.m_to;
				SynchronizeCoalConversion(smelter, smelterPrefabName, orCreateSmelterCoalRule, "FineWood", fineWoodForCoal, to);
				SynchronizeCoalConversion(smelter, smelterPrefabName, orCreateSmelterCoalRule, "RoundLog", coreWoodForCoal, to);
				SynchronizeCoalConversion(smelter, smelterPrefabName, orCreateSmelterCoalRule, "ElderBark", ancientBarkForCoal, to);
				SynchronizeCoalConversion(smelter, smelterPrefabName, orCreateSmelterCoalRule, "YggdrasilWood", yggdrasilWoodForCoal, to);
				SynchronizeCoalConversion(smelter, smelterPrefabName, orCreateSmelterCoalRule, "Blackwood", ashWoodForCoal, to);
				SynchronizeCoalConversion(smelter, smelterPrefabName, orCreateSmelterCoalRule, "Frostwood", timberWoodForCoal, to);
			}
		}
	}

	private static void SynchronizeSmelterPrefabCoalConversions()
	{
		if (!IsModEnabled() || (Object)(object)ZNetScene.instance == (Object)null)
		{
			return;
		}
		foreach (GameObject prefab in ZNetScene.instance.m_prefabs)
		{
			if (!((Object)(object)prefab == (Object)null))
			{
				string prefabName = Utils.GetPrefabName(prefab);
				Smelter[] componentsInChildren = prefab.GetComponentsInChildren<Smelter>(true);
				foreach (Smelter smelter in componentsInChildren)
				{
					SynchronizeSmelterCoalConversions(smelter, prefabName);
				}
			}
		}
	}

	private static void CheckSmelterCoalConversions()
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		if (IsModEnabled())
		{
			SynchronizeSmelterPrefabCoalConversions();
			Object[] array = Object.FindObjectsOfType(typeof(Smelter));
			for (int i = 0; i < array.Length; i++)
			{
				Smelter smelter = (Smelter)array[i];
				SynchronizeSmelterCoalConversions(smelter);
			}
		}
	}
}