Decompiled source of IKnowThings v1.0.3

IKnowThings.dll

Decompiled 8 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("IKnowThings")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("IKnowThings")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 IKnowThings
{
	public static class Admin
	{
		public static IAdmin Instance = new DefaultAdmin();

		public static bool Enabled
		{
			get
			{
				return Instance.Enabled;
			}
			set
			{
				Instance.Enabled = value;
			}
		}

		public static bool Checking
		{
			get
			{
				return Instance.Checking;
			}
			set
			{
				Instance.Checking = value;
			}
		}

		public static void ManualCheck()
		{
			Instance.ManualCheck();
		}

		public static void Verify(string text)
		{
			Instance.Verify(text);
		}

		public static void AutomaticCheck()
		{
			Instance.AutomaticCheck();
		}

		public static void Reset()
		{
			Instance.Reset();
		}
	}
	public interface IAdmin
	{
		bool Enabled { get; set; }

		bool Checking { get; set; }

		void ManualCheck();

		void Verify(string text);

		void AutomaticCheck();

		void Reset();
	}
	public class DefaultAdmin : IAdmin
	{
		public virtual bool Enabled { get; set; }

		public virtual bool Checking { get; set; }

		protected void Check()
		{
			if (Object.op_Implicit((Object)(object)ZNet.instance))
			{
				Checking = true;
				if (ZNet.instance.IsServer())
				{
					OnSuccess();
				}
				else
				{
					ZNet.instance.Unban("admintest");
				}
			}
		}

		public void Verify(string text)
		{
			if (text == "Unbanning user admintest")
			{
				OnSuccess();
			}
			else
			{
				OnFail();
			}
		}

		public virtual void AutomaticCheck()
		{
			Check();
		}

		protected virtual void OnSuccess()
		{
			IKnowThingsPlugin.IKnowThingsLogger.LogInfo((object)"Admin status verified.");
			Checking = false;
			Enabled = true;
		}

		protected virtual void OnFail()
		{
			Checking = false;
			Enabled = false;
		}

		public virtual void ManualCheck()
		{
			Check();
		}

		public virtual void Reset()
		{
			Checking = false;
			Enabled = false;
		}
	}
	[HarmonyPatch(typeof(ZNet), "RPC_RemotePrint")]
	public class ZNet_RPC_RemotePrint
	{
		private static bool Prefix(string text)
		{
			if (!Admin.Checking)
			{
				return true;
			}
			Admin.Verify(text);
			return false;
		}
	}
	[HarmonyPatch(typeof(Game), "Awake")]
	public class AdminReset
	{
		private static void Postfix()
		{
			Admin.Reset();
		}
	}
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	public class AdminCheck
	{
		private static void Postfix()
		{
			if (!Admin.Checking)
			{
				Admin.AutomaticCheck();
			}
		}
	}
	[BepInPlugin("Azumatt.IKnowThings", "IKnowThings", "1.0.3")]
	public class IKnowThingsPlugin : BaseUnityPlugin
	{
		public enum Toggle
		{
			On = 1,
			Off = 0
		}

		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order;

			[UsedImplicitly]
			public bool? Browsable;

			[UsedImplicitly]
			public string? Category;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer;
		}

		private class AcceptableShortcuts : AcceptableValueBase
		{
			public AcceptableShortcuts()
				: base(typeof(KeyboardShortcut))
			{
			}

			public override object Clamp(object value)
			{
				return value;
			}

			public override bool IsValid(object value)
			{
				return true;
			}

			public override string ToDescriptionString()
			{
				return "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
			}
		}

		internal const string ModName = "IKnowThings";

		internal const string ModVersion = "1.0.3";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.IKnowThings";

		private readonly Harmony _harmony = new Harmony("Azumatt.IKnowThings");

		public static readonly ManualLogSource IKnowThingsLogger = Logger.CreateLogSource("IKnowThings");

		public static ConfigEntry<Toggle> DisableUnlockMessages = null;

		public void Awake()
		{
			DisableUnlockMessages = ((BaseUnityPlugin)this).Config.Bind<Toggle>("1 - General", "DisableUnlockMessages", Toggle.Off, "Disables the messages that appear when you unlock a new recipe or item. Helps prevent spam when learning many recipes at once.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
		}
	}
	[HarmonyPatch(typeof(MessageHud), "QueueUnlockMsg")]
	internal static class MessageHudQueueUnlockMsgPatch
	{
		private static bool Prefix(MessageHud __instance)
		{
			return IKnowThingsPlugin.DisableUnlockMessages.Value != IKnowThingsPlugin.Toggle.On;
		}
	}
	[HarmonyPatch(typeof(Terminal), "InitTerminal")]
	internal static class TerminalInitTerminalPatch
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static ConsoleEvent <>9__1_0;

			public static ConsoleEvent <>9__2_0;

			public static ConsoleEvent <>9__3_0;

			public static ConsoleOptionsFetcher <>9__3_1;

			public static ConsoleEvent <>9__4_0;

			public static Func<CraftingStation, string> <>9__4_2;

			public static ConsoleOptionsFetcher <>9__4_1;

			internal void <AddLearnAllCommand>b__1_0(ConsoleEventArgs args)
			{
				LearnOrUnlearnAllRecipes(args, learn: true);
			}

			internal void <AddUnlearnAllCommand>b__2_0(ConsoleEventArgs args)
			{
				LearnOrUnlearnAllRecipes(args, learn: false);
			}

			internal void <AddLearnCategoryCommand>b__3_0(ConsoleEventArgs args)
			{
				if (args.Length < 1)
				{
					AddError(args.Context, "You must provide a category (e.g., 'weapons', 'armor').");
					return;
				}
				string category = args[0];
				LearnOrUnlearnRecipesByCategory(args, category, learn: true);
			}

			internal List<string> <AddLearnCategoryCommand>b__3_1()
			{
				return Enum.GetNames(typeof(ItemType)).ToList();
			}

			internal void <AddLearnStationCommand>b__4_0(ConsoleEventArgs args)
			{
				if (args.Length < 1)
				{
					AddError(args.Context, "You must provide a station name (e.g., 'workbench', 'forge').");
					return;
				}
				string stationName = args[0];
				LearnOrUnlearnRecipesByStation(args, stationName, learn: true);
			}

			internal List<string> <AddLearnStationCommand>b__4_1()
			{
				List<string> list = (from r in Resources.FindObjectsOfTypeAll<CraftingStation>()
					select r?.m_name).Distinct().ToList();
				list.Remove(null);
				return list;
			}

			internal string <AddLearnStationCommand>b__4_2(CraftingStation r)
			{
				return r?.m_name;
			}
		}

		private static void Postfix(Terminal __instance)
		{
			AddLearnAllCommand();
			AddUnlearnAllCommand();
			AddLearnCategoryCommand();
			AddLearnStationCommand();
		}

		private static void AddLearnAllCommand()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			object obj = <>c.<>9__1_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					LearnOrUnlearnAllRecipes(args, learn: true);
				};
				<>c.<>9__1_0 = val;
				obj = (object)val;
			}
			Command("learnrecipes", "Learns all recipes for your player. Only admins can execute this command", (ConsoleEvent)obj);
		}

		private static void AddUnlearnAllCommand()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			object obj = <>c.<>9__2_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					LearnOrUnlearnAllRecipes(args, learn: false);
				};
				<>c.<>9__2_0 = val;
				obj = (object)val;
			}
			Command("unlearnrecipes", "Un-Learns all recipes for your player. Only admins can execute this command", (ConsoleEvent)obj);
		}

		private static void AddLearnCategoryCommand()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			object obj = <>c.<>9__3_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					if (args.Length < 1)
					{
						AddError(args.Context, "You must provide a category (e.g., 'weapons', 'armor').");
					}
					else
					{
						string category = args[0];
						LearnOrUnlearnRecipesByCategory(args, category, learn: true);
					}
				};
				<>c.<>9__3_0 = val;
				obj = (object)val;
			}
			object obj2 = <>c.<>9__3_1;
			if (obj2 == null)
			{
				ConsoleOptionsFetcher val2 = () => Enum.GetNames(typeof(ItemType)).ToList();
				<>c.<>9__3_1 = val2;
				obj2 = (object)val2;
			}
			Command("learnbycategory", "Learns recipes by category. Only admins can execute this command", (ConsoleEvent)obj, (ConsoleOptionsFetcher?)obj2);
		}

		private static void AddLearnStationCommand()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			object obj = <>c.<>9__4_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					if (args.Length < 1)
					{
						AddError(args.Context, "You must provide a station name (e.g., 'workbench', 'forge').");
					}
					else
					{
						string stationName = args[0];
						LearnOrUnlearnRecipesByStation(args, stationName, learn: true);
					}
				};
				<>c.<>9__4_0 = val;
				obj = (object)val;
			}
			object obj2 = <>c.<>9__4_1;
			if (obj2 == null)
			{
				ConsoleOptionsFetcher val2 = delegate
				{
					List<string> list = (from r in Resources.FindObjectsOfTypeAll<CraftingStation>()
						select r?.m_name).Distinct().ToList();
					list.Remove(null);
					return list;
				};
				<>c.<>9__4_1 = val2;
				obj2 = (object)val2;
			}
			Command("learnbystation", "Learns recipes by station. Only admins can execute this command", (ConsoleEvent)obj, (ConsoleOptionsFetcher?)obj2);
		}

		private static void LearnOrUnlearnAllRecipes(ConsoleEventArgs args, bool learn)
		{
			if (Admin.Enabled)
			{
				Player localPlayer = Player.m_localPlayer;
				int num = 0;
				int num2 = 0;
				int num3 = 0;
				int num4 = 0;
				if (!((Object)(object)localPlayer != (Object)null) || !((Object)(object)ObjectDB.instance != (Object)null))
				{
					return;
				}
				if (learn)
				{
					foreach (Recipe recipe in ObjectDB.instance.m_recipes)
					{
						if (!string.IsNullOrWhiteSpace(recipe.m_item?.m_itemData?.m_shared?.m_name) && !localPlayer.m_knownRecipes.Contains(recipe.m_item?.m_itemData?.m_shared?.m_name))
						{
							localPlayer.m_knownRecipes.Add(recipe.m_item?.m_itemData?.m_shared?.m_name);
							localPlayer.UpdateKnownRecipesList();
							num++;
						}
					}
					PieceTable[] array = Resources.FindObjectsOfTypeAll<PieceTable>();
					for (int i = 0; i < array.Length; i++)
					{
						foreach (GameObject piece in array[i].m_pieces)
						{
							Piece component = piece.GetComponent<Piece>();
							if (!((Object)(object)component == (Object)null) && !string.IsNullOrWhiteSpace(component.m_name) && !localPlayer.m_knownRecipes.Contains(component.m_name))
							{
								localPlayer.m_knownRecipes.Add(component.m_name);
								localPlayer.UpdateKnownRecipesList();
								num4++;
							}
						}
					}
					CraftingStation[] array2 = Resources.FindObjectsOfTypeAll<CraftingStation>();
					foreach (CraftingStation val in array2)
					{
						int level = val.GetLevel(true);
						if (localPlayer.m_knownStations.TryGetValue(val.m_name, out var value))
						{
							if (value < level)
							{
								localPlayer.m_knownStations[val.m_name] = level;
								localPlayer.UpdateKnownRecipesList();
								num2++;
							}
						}
						else if (!string.IsNullOrWhiteSpace(val.m_name))
						{
							localPlayer.m_knownStations.Add(val.m_name, level);
							localPlayer.UpdateKnownRecipesList();
							num3++;
						}
					}
					IKnowThingsPlugin.IKnowThingsLogger.LogInfo((object)$"Learned {num} recipes, {num4} pieces, {num2 + num3} stations ({num2} upgraded, {num3} new) and 0 life lessons");
					args.Context.AddString($"Learned {num} recipes, {num4} pieces, {num2 + num3} stations ({num2} upgraded, {num3} new) and 0 life lessons");
				}
				else
				{
					localPlayer.m_knownRecipes.Clear();
					localPlayer.m_knownStations.Clear();
					localPlayer.m_knownMaterial.Clear();
					IKnowThingsPlugin.IKnowThingsLogger.LogInfo((object)"Reset all recipes, pieces, stations and life lessons");
					args.Context.AddString("Reset all recipes, pieces, stations and life lessons");
				}
			}
			else
			{
				args.Context.AddString("You must be an admin to use this command");
			}
		}

		private static void LearnOrUnlearnRecipesByCategory(ConsoleEventArgs args, string category, bool learn)
		{
			if (!Admin.Enabled)
			{
				AddError(args.Context, "You must be an admin to use this command");
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer != (Object)null) || !((Object)(object)ObjectDB.instance != (Object)null))
			{
				return;
			}
			int num = 0;
			foreach (Recipe recipe in ObjectDB.instance.m_recipes)
			{
				if (((object)(ItemType)(ref recipe.m_item.m_itemData.m_shared.m_itemType)).ToString() != category)
				{
					continue;
				}
				string text = recipe.m_item?.m_itemData?.m_shared?.m_name;
				if (string.IsNullOrWhiteSpace(text))
				{
					continue;
				}
				if (learn)
				{
					if (!localPlayer.m_knownRecipes.Contains(text))
					{
						localPlayer.m_knownRecipes.Add(text);
						localPlayer.UpdateKnownRecipesList();
					}
				}
				else if (localPlayer.m_knownRecipes.Contains(text))
				{
					localPlayer.m_knownRecipes.Remove(text);
					localPlayer.UpdateKnownRecipesList();
				}
				num++;
			}
			AddMessage(args.Context, $"Processed {num} recipes in the category {category}");
		}

		private static void LearnOrUnlearnRecipesByStation(ConsoleEventArgs args, string stationName, bool learn)
		{
			if (!Admin.Enabled)
			{
				AddError(args.Context, "You must be an admin to use this command");
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer != (Object)null) || !((Object)(object)ObjectDB.instance != (Object)null))
			{
				return;
			}
			int num = 0;
			foreach (Recipe recipe in ObjectDB.instance.m_recipes)
			{
				if (recipe.m_craftingStation?.m_name != stationName)
				{
					continue;
				}
				string text = recipe.m_item?.m_itemData?.m_shared?.m_name;
				if (string.IsNullOrWhiteSpace(text))
				{
					continue;
				}
				if (learn)
				{
					if (!localPlayer.m_knownRecipes.Contains(text))
					{
						localPlayer.m_knownRecipes.Add(text);
						localPlayer.UpdateKnownRecipesList();
					}
				}
				else if (localPlayer.m_knownRecipes.Contains(text))
				{
					localPlayer.m_knownRecipes.Remove(text);
					localPlayer.UpdateKnownRecipesList();
				}
				num++;
			}
			AddMessage(args.Context, $"Processed {num} recipes for the station {stationName}");
		}

		public static ConsoleEvent Catch(ConsoleEvent action)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			ConsoleEvent action2 = action;
			return (ConsoleEvent)delegate(ConsoleEventArgs args)
			{
				try
				{
					action2.Invoke(args);
				}
				catch (InvalidOperationException ex)
				{
					AddError(args.Context, ex.Message);
				}
			};
		}

		public static void AddMessage(Terminal context, string message)
		{
			context.AddString(message);
			Player localPlayer = Player.m_localPlayer;
			if (localPlayer != null)
			{
				((Character)localPlayer).Message((MessageType)1, message, 0, (Sprite)null);
			}
		}

		public static void AddError(Terminal context, string message)
		{
			AddMessage(context, "Error: " + message);
		}

		public static void Command(string name, string description, ConsoleEvent action, ConsoleOptionsFetcher? fetcher = null)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			new ConsoleCommand(name, description, Catch(action), true, true, false, false, false, fetcher, false, false, false);
		}
	}
}