Decompiled source of CraftableCooking v1.0.1

CraftableCooking.dll

Decompiled 12 hours 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 BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
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("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6c72e118-eaa6-41d3-96e2-42213ed3b42d")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CraftableCooking;

public class SpitPlus : MonoBehaviour
{
	public string TitleToken;

	public string HoverToken;

	public string CachedHover;

	public string CachedName;

	public string CachedLang;
}
[BepInPlugin("Maslinka.CraftableCooking", "Craftable Cooking", "1.2.3")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class CraftableCookingPlugin : BaseUnityPlugin
{
	private class StationDef
	{
		public string Vanilla;

		public string Clone;

		public string Token;

		public string HoverToken;

		public string Fuel;

		public int FuelAmount;
	}

	public const string PluginGuid = "Maslinka.CraftableCooking";

	public const string PluginName = "Craftable Cooking";

	public const string PluginVersion = "1.2.3";

	public const float SkillXpPerMeat = 1f;

	public static ManualLogSource Log;

	private static readonly StationDef[] Defs = new StationDef[3]
	{
		new StationDef
		{
			Vanilla = "piece_cookingstation",
			Clone = "piece_cookingstation_plus",
			Token = "piece_cs_plus",
			HoverToken = "$csp_cook"
		},
		new StationDef
		{
			Vanilla = "piece_cookingstation_iron",
			Clone = "piece_cookingstation_iron_plus",
			Token = "piece_csiron_plus",
			HoverToken = "$csp_cook"
		},
		new StationDef
		{
			Vanilla = "piece_oven",
			Clone = "piece_oven_plus",
			Token = "piece_oven_plus",
			HoverToken = "$csp_view",
			Fuel = "Wood",
			FuelAmount = 1
		}
	};

	private static bool locResolved;

	private static Type locType;

	private static PropertyInfo locProp;

	private static FieldInfo locField;

	private static MethodInfo mLocalize;

	private static MethodInfo mLang;

	private static readonly Dictionary<int, SpitPlus> markerCache = new Dictionary<int, SpitPlus>();

	private void Awake()
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		AddTranslations();
		PrefabManager.OnVanillaPrefabsAvailable += Setup;
		Harmony val = new Harmony("Maslinka.CraftableCooking");
		Type[] array = new Type[13]
		{
			typeof(CookInteractPatch),
			typeof(CookUseItemPatch),
			typeof(CookHoverPatch),
			typeof(CookHoverNamePatch),
			typeof(FireInteractPatch),
			typeof(FireUseItemPatch),
			typeof(FireHoverPatch),
			typeof(FireHoverNamePatch),
			typeof(SwitchInteractPatch),
			typeof(SwitchUseItemPatch),
			typeof(SwitchHoverPatch),
			typeof(SwitchHoverNamePatch),
			typeof(DoCraftingPatch)
		};
		foreach (Type type in array)
		{
			try
			{
				val.CreateClassProcessor(type).Patch();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Patch " + type.Name + " failed: " + ex.Message));
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Craftable Cooking by Maslinka loaded!");
	}

	private void AddTranslations()
	{
		CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
		string text = "English";
		localization.AddTranslation(ref text, new Dictionary<string, string>
		{
			{ "piece_cs_plus", "Cooking Station+" },
			{ "piece_csiron_plus", "Iron Cooking Station+" },
			{ "piece_oven_plus", "Stone Oven+" },
			{ "csp_cook", "Cook" },
			{ "csp_view", "View" }
		});
		text = "Ukrainian";
		localization.AddTranslation(ref text, new Dictionary<string, string>
		{
			{ "piece_cs_plus", "Рожни+" },
			{ "piece_csiron_plus", "Залізна кухонна станція+" },
			{ "piece_oven_plus", "Кам’яна піч+" },
			{ "csp_cook", "Приготувати" },
			{ "csp_view", "Переглянути" }
		});
		text = "Russian";
		localization.AddTranslation(ref text, new Dictionary<string, string>
		{
			{ "piece_cs_plus", "Вертел+" },
			{ "piece_csiron_plus", "Железная кухонная станция+" },
			{ "piece_oven_plus", "Каменная печь+" },
			{ "csp_cook", "Приготовить" },
			{ "csp_view", "Просмотреть" }
		});
	}

	private void Setup()
	{
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_0306: Unknown result type (might be due to invalid IL or missing references)
		//IL_0312: Unknown result type (might be due to invalid IL or missing references)
		//IL_031e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c3: Expected O, but got Unknown
		//IL_034c: Unknown result type (might be due to invalid IL or missing references)
		//IL_035c: Expected O, but got Unknown
		//IL_0366: Unknown result type (might be due to invalid IL or missing references)
		//IL_0370: Expected O, but got Unknown
		PrefabManager.OnVanillaPrefabsAvailable -= Setup;
		StationDef[] defs = Defs;
		foreach (StationDef stationDef in defs)
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab(stationDef.Vanilla);
			Piece val = (((Object)(object)prefab != (Object)null) ? prefab.GetComponent<Piece>() : null);
			if ((Object)(object)prefab == (Object)null || (Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Vanilla prefab/piece not found: " + stationDef.Vanilla));
				continue;
			}
			GameObject val2 = PrefabManager.Instance.CreateClonedPrefab(stationDef.Clone, prefab);
			CookingStation[] componentsInChildren = val2.GetComponentsInChildren<CookingStation>(true);
			if (componentsInChildren.Length == 0)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("No CookingStation on clone: " + stationDef.Clone));
				continue;
			}
			SpitPlus spitPlus = val2.AddComponent<SpitPlus>();
			spitPlus.TitleToken = "$" + stationDef.Token;
			spitPlus.HoverToken = stationDef.HoverToken;
			CraftingStation val3 = val2.GetComponent<CraftingStation>();
			if ((Object)(object)val3 == (Object)null)
			{
				val3 = val2.AddComponent<CraftingStation>();
			}
			val3.m_name = "$" + stationDef.Token;
			val3.m_icon = val.m_icon;
			val3.m_discoverRange = 4f;
			val3.m_rangeBuild = 4f;
			val3.m_useDistance = 3f;
			val3.m_craftRequireRoof = false;
			val3.m_craftRequireFire = false;
			int num = 0;
			HashSet<string> hashSet = new HashSet<string>();
			CookingStation[] array = componentsInChildren;
			foreach (CookingStation val4 in array)
			{
				foreach (ItemConversion item in val4.m_conversion)
				{
					if (!((Object)(object)item.m_from == (Object)null) && !((Object)(object)item.m_to == (Object)null))
					{
						string name = ((Object)((Component)item.m_from).gameObject).name;
						string name2 = ((Object)((Component)item.m_to).gameObject).name;
						if (hashSet.Add(name + ">" + name2))
						{
							AddCook(stationDef, name, name2);
							num++;
						}
					}
				}
			}
			List<RequirementConfig> list = new List<RequirementConfig>();
			Requirement[] resources = val.m_resources;
			foreach (Requirement val5 in resources)
			{
				if (!((Object)(object)val5.m_resItem == (Object)null))
				{
					list.Add(new RequirementConfig(((Object)((Component)val5.m_resItem).gameObject).name, val5.m_amount, 0, val5.m_recover));
				}
			}
			PieceConfig val6 = new PieceConfig
			{
				Name = "$" + stationDef.Token,
				Description = "$" + stationDef.Token,
				PieceTable = "Hammer",
				Category = "Crafting",
				CraftingStation = (((Object)(object)val.m_craftingStation != (Object)null) ? ((Object)((Component)val.m_craftingStation).gameObject).name : ""),
				Requirements = list.ToArray()
			};
			PieceManager.Instance.AddPiece(new CustomPiece(val2, false, val6));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Craftable Cooking: added " + stationDef.Clone + ", recipes: " + num));
		}
	}

	private void AddCook(StationDef def, string raw, string cooked)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Expected O, but got Unknown
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: 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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Expected O, but got Unknown
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Expected O, but got Unknown
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected O, but got Unknown
		List<RequirementConfig> list = new List<RequirementConfig>
		{
			new RequirementConfig(raw, 1, 0, true)
		};
		if (!string.IsNullOrEmpty(def.Fuel) && def.Fuel != raw)
		{
			list.Add(new RequirementConfig(def.Fuel, def.FuelAmount, 0, true));
		}
		RecipeConfig val = new RecipeConfig
		{
			Name = "Recipe_CraftableCooking_" + def.Clone + "_" + cooked,
			Item = cooked,
			Amount = 1,
			CraftingStation = def.Clone,
			MinStationLevel = 1,
			Requirements = list.ToArray()
		};
		ItemManager.Instance.AddRecipe(new CustomRecipe(val));
	}

	private static void ResolveLoc()
	{
		if (locResolved)
		{
			return;
		}
		locResolved = true;
		try
		{
			locType = AccessTools.TypeByName("Localization");
			if (!(locType == null))
			{
				locProp = AccessTools.Property(locType, "instance");
				locField = AccessTools.Field(locType, "instance");
				mLocalize = AccessTools.Method(locType, "Localize", new Type[1] { typeof(string) }, (Type[])null);
				mLang = AccessTools.Method(locType, "GetSelectedLanguage", (Type[])null, (Type[])null);
			}
		}
		catch
		{
		}
	}

	private static object LocInst()
	{
		ResolveLoc();
		if (locType == null)
		{
			return null;
		}
		try
		{
			if (locProp != null)
			{
				return locProp.GetValue(null, null);
			}
			if (locField != null)
			{
				return locField.GetValue(null);
			}
		}
		catch
		{
		}
		return null;
	}

	public static string CurrentLanguage()
	{
		try
		{
			object obj = LocInst();
			if (obj != null && mLang != null)
			{
				return (string)mLang.Invoke(obj, null);
			}
		}
		catch
		{
		}
		return "";
	}

	public static string Localize(string s)
	{
		try
		{
			object obj = LocInst();
			if (obj != null && mLocalize != null)
			{
				return (string)mLocalize.Invoke(obj, new object[1] { s });
			}
		}
		catch
		{
		}
		return s;
	}

	public static SpitPlus FindMarker(Component c)
	{
		if ((Object)(object)c == (Object)null)
		{
			return null;
		}
		int instanceID = ((Object)c).GetInstanceID();
		if (markerCache.TryGetValue(instanceID, out var value))
		{
			return value;
		}
		value = c.GetComponentInParent<SpitPlus>();
		markerCache[instanceID] = value;
		return value;
	}

	public static bool OpenCraft(SpitPlus sp, Humanoid user, bool hold, bool alt)
	{
		CraftingStation val = ((Component)sp).GetComponent<CraftingStation>() ?? ((Component)sp).GetComponentInParent<CraftingStation>();
		if ((Object)(object)val == (Object)null)
		{
			Log.LogWarning((object)("No CraftingStation on " + ((Object)sp).name));
			return false;
		}
		return val.Interact(user, hold, alt);
	}

	private static void Refresh(SpitPlus sp)
	{
		string text = CurrentLanguage();
		if (sp.CachedHover == null || !(sp.CachedLang == text))
		{
			sp.CachedLang = text;
			sp.CachedName = Localize(sp.TitleToken);
			sp.CachedHover = Localize(sp.TitleToken + "\n[<color=yellow><b>$KEY_Use</b></color>] " + sp.HoverToken);
		}
	}

	public static string HoverText(SpitPlus sp)
	{
		Refresh(sp);
		return sp.CachedHover;
	}

	public static string HoverName(SpitPlus sp)
	{
		Refresh(sp);
		return sp.CachedName;
	}
}
[HarmonyPatch(typeof(CookingStation), "Interact")]
internal static class CookInteractPatch
{
	private static bool Prefix(CookingStation __instance, Humanoid user, bool hold, bool alt, ref bool __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus == (Object)null)
		{
			return true;
		}
		__result = CraftableCookingPlugin.OpenCraft(spitPlus, user, hold, alt);
		return false;
	}
}
[HarmonyPatch(typeof(CookingStation), "UseItem")]
internal static class CookUseItemPatch
{
	private static bool Prefix(CookingStation __instance, ref bool __result)
	{
		if ((Object)(object)CraftableCookingPlugin.FindMarker((Component)(object)__instance) == (Object)null)
		{
			return true;
		}
		__result = false;
		return false;
	}
}
[HarmonyPatch(typeof(CookingStation), "GetHoverText")]
internal static class CookHoverPatch
{
	private static void Postfix(CookingStation __instance, ref string __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus != (Object)null)
		{
			__result = CraftableCookingPlugin.HoverText(spitPlus);
		}
	}
}
[HarmonyPatch(typeof(CookingStation), "GetHoverName")]
internal static class CookHoverNamePatch
{
	private static void Postfix(CookingStation __instance, ref string __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus != (Object)null)
		{
			__result = CraftableCookingPlugin.HoverName(spitPlus);
		}
	}
}
[HarmonyPatch(typeof(Fireplace), "Interact")]
internal static class FireInteractPatch
{
	private static bool Prefix(Fireplace __instance, Humanoid user, bool hold, bool alt, ref bool __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus == (Object)null)
		{
			return true;
		}
		__result = CraftableCookingPlugin.OpenCraft(spitPlus, user, hold, alt);
		return false;
	}
}
[HarmonyPatch(typeof(Fireplace), "UseItem")]
internal static class FireUseItemPatch
{
	private static bool Prefix(Fireplace __instance, ref bool __result)
	{
		if ((Object)(object)CraftableCookingPlugin.FindMarker((Component)(object)__instance) == (Object)null)
		{
			return true;
		}
		__result = false;
		return false;
	}
}
[HarmonyPatch(typeof(Fireplace), "GetHoverText")]
internal static class FireHoverPatch
{
	private static void Postfix(Fireplace __instance, ref string __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus != (Object)null)
		{
			__result = CraftableCookingPlugin.HoverText(spitPlus);
		}
	}
}
[HarmonyPatch(typeof(Fireplace), "GetHoverName")]
internal static class FireHoverNamePatch
{
	private static void Postfix(Fireplace __instance, ref string __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus != (Object)null)
		{
			__result = CraftableCookingPlugin.HoverName(spitPlus);
		}
	}
}
[HarmonyPatch(typeof(Switch), "Interact")]
internal static class SwitchInteractPatch
{
	private static bool Prefix(Switch __instance, Humanoid character, bool hold, bool alt, ref bool __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus == (Object)null)
		{
			return true;
		}
		__result = CraftableCookingPlugin.OpenCraft(spitPlus, character, hold, alt);
		return false;
	}
}
[HarmonyPatch(typeof(Switch), "UseItem")]
internal static class SwitchUseItemPatch
{
	private static bool Prefix(Switch __instance, ref bool __result)
	{
		if ((Object)(object)CraftableCookingPlugin.FindMarker((Component)(object)__instance) == (Object)null)
		{
			return true;
		}
		__result = false;
		return false;
	}
}
[HarmonyPatch(typeof(Switch), "GetHoverText")]
internal static class SwitchHoverPatch
{
	private static void Postfix(Switch __instance, ref string __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus != (Object)null)
		{
			__result = CraftableCookingPlugin.HoverText(spitPlus);
		}
	}
}
[HarmonyPatch(typeof(Switch), "GetHoverName")]
internal static class SwitchHoverNamePatch
{
	private static void Postfix(Switch __instance, ref string __result)
	{
		SpitPlus spitPlus = CraftableCookingPlugin.FindMarker((Component)(object)__instance);
		if ((Object)(object)spitPlus != (Object)null)
		{
			__result = CraftableCookingPlugin.HoverName(spitPlus);
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
internal static class DoCraftingPatch
{
	private static void Prefix(Player player, out bool __state)
	{
		__state = false;
		try
		{
			CraftingStation val = (((Object)(object)player != (Object)null) ? player.GetCurrentCraftingStation() : null);
			__state = (Object)(object)val != (Object)null && (Object)(object)((Component)val).GetComponentInChildren<SpitPlus>() != (Object)null;
		}
		catch
		{
		}
	}

	private static void Postfix(Player player, bool __state)
	{
		if (!__state || (Object)(object)player == (Object)null)
		{
			return;
		}
		try
		{
			((Character)player).RaiseSkill((SkillType)105, 1f);
		}
		catch (Exception ex)
		{
			CraftableCookingPlugin.Log.LogWarning((object)("RaiseSkill: " + ex.Message));
		}
	}
}