Decompiled source of MidRunArtifacts v1.2.1

plugins/MidRunArtifacts/MidRunArtifacts.dll

Decompiled 2 months 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 System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HG.Reflection;
using MonoMod.RuntimeDetour.HookGen;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: OptIn]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.1.0")]
namespace MidRunArtifacts;

[BepInPlugin("com.KingEnderBrine.MidRunArtifacts", "Mid Run Artifacts", "1.2.1")]
public class MidRunArtifactsPlugin : BaseUnityPlugin
{
	public const string GUID = "com.KingEnderBrine.MidRunArtifacts";

	public const string Name = "Mid Run Artifacts";

	public const string Version = "1.2.1";

	private static readonly ConstructorInfo autoCompleteCtor = typeof(AutoComplete).GetConstructor(new Type[1] { typeof(Console) });

	private static readonly MethodInfo chatAddMessage = typeof(Chat).GetMethod("CCSay", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[1] { typeof(ConCommandArgs) }, null);

	private static List<string> commandsAutocomplete;

	private static string autocompleteLanguage;

	internal static MidRunArtifactsPlugin Instance { get; private set; }

	internal static ManualLogSource InstanceLogger
	{
		get
		{
			MidRunArtifactsPlugin instance = Instance;
			if (instance == null)
			{
				return null;
			}
			return ((BaseUnityPlugin)instance).Logger;
		}
	}

	private static List<string> CommandsAutocomplete
	{
		get
		{
			if (autocompleteLanguage == Language.currentLanguageName)
			{
				return commandsAutocomplete;
			}
			autocompleteLanguage = Language.currentLanguageName;
			return commandsAutocomplete = GatherCommandsAutocomplete();
		}
	}

	public static ConfigEntry<bool> EnableChatCommands { get; set; }

	private void Awake()
	{
		Instance = this;
		EnableChatCommands = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "EnableChatCommands", false, "Accept commands from any player in chat");
		HookEndpointManager.Add((MethodBase)autoCompleteCtor, (Delegate)new Action<Action<AutoComplete, Console>, AutoComplete, Console>(CommandArgsAutoCompletion));
		if (EnableChatCommands.Value)
		{
			HookEndpointManager.Add((MethodBase)chatAddMessage, (Delegate)new Action<Action<ConCommandArgs>, ConCommandArgs>(OnChatAddMessage));
		}
	}

	private void Destroy()
	{
		Instance = null;
		HookEndpointManager.Remove((MethodBase)autoCompleteCtor, (Delegate)new Action<Action<AutoComplete, Console>, AutoComplete, Console>(CommandArgsAutoCompletion));
		if (EnableChatCommands.Value)
		{
			HookEndpointManager.Remove((MethodBase)chatAddMessage, (Delegate)new Action<Action<ConCommandArgs>, ConCommandArgs>(OnChatAddMessage));
		}
	}

	private static void OnChatAddMessage(Action<ConCommandArgs> orig, ConCommandArgs args)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		orig(args);
		if (!NetworkServer.active || !Object.op_Implicit((Object)(object)RunArtifactManager.instance) || !((ConCommandArgs)(ref args))[0].StartsWith("/"))
		{
			return;
		}
		Lexer val = new Lexer(((ConCommandArgs)(ref args))[0]);
		string text = val.NextToken();
		if (text == null)
		{
			return;
		}
		bool? newState;
		switch (text)
		{
		default:
			return;
		case "mra_enable":
			newState = true;
			break;
		case "mra_disable":
			newState = false;
			break;
		case "mra_toggle":
			newState = null;
			break;
		}
		List<string> list = new List<string>();
		while (true)
		{
			string text2 = val.NextToken();
			if (text2 == null || text2 == ";")
			{
				break;
			}
			list.Add(text2);
		}
		ConCommandArgs args2 = default(ConCommandArgs);
		args2.userArgs = list;
		args2.commandName = text;
		args2.sender = args.sender;
		ToggleArtifact(args2, newState, fromChat: true);
	}

	private static void CommandArgsAutoCompletion(Action<AutoComplete, Console> orig, AutoComplete self, Console console)
	{
		orig(self, console);
		try
		{
			self.searchableStrings.AddRange(CommandsAutocomplete);
		}
		catch (Exception ex)
		{
			InstanceLogger.LogWarning((object)"Failed to gather autocomplete options");
			InstanceLogger.LogError((object)ex);
		}
	}

	private static List<string> GatherCommandsAutocomplete()
	{
		IEnumerable<string> enumerable = ArtifactCatalog.artifactDefs.Select((ArtifactDef a) => GetArgNameForArtifact(a, english: false));
		if (Language.currentLanguage.name != Language.english.name)
		{
			enumerable = enumerable.Union(ArtifactCatalog.artifactDefs.Select((ArtifactDef a) => GetArgNameForArtifact(a, english: true)));
		}
		List<string> list = new List<string>();
		list.AddRange(enumerable.Select((string el) => "mra_enable " + el));
		list.AddRange(enumerable.Select((string el) => "mra_disable " + el));
		list.AddRange(enumerable.Select((string el) => "mra_toggle " + el));
		list.Sort();
		return list;
	}

	[ConCommand(/*Could not decode attribute arguments.*/)]
	private static void CCEnable(ConCommandArgs args)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		ToggleArtifact(args, true);
	}

	[ConCommand(/*Could not decode attribute arguments.*/)]
	private static void CCDisable(ConCommandArgs args)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		ToggleArtifact(args, false);
	}

	[ConCommand(/*Could not decode attribute arguments.*/)]
	private static void CCToggle(ConCommandArgs args)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		ToggleArtifact(args);
	}

	private static void ToggleArtifact(ConCommandArgs args, bool? newState = null, bool fromChat = false)
	{
		if (!Object.op_Implicit((Object)(object)RunArtifactManager.instance))
		{
			LogMessage("You can only use this command while in a run");
			return;
		}
		if (!NetworkServer.active)
		{
			LogMessage("You must be a host to use this command");
			return;
		}
		if (((ConCommandArgs)(ref args)).Count == 0)
		{
			LogMessage("No arguments supplied");
			return;
		}
		ArtifactDef artifactDefFromString = GetArtifactDefFromString(((ConCommandArgs)(ref args))[0]);
		if (!Object.op_Implicit((Object)(object)artifactDefFromString))
		{
			LogMessage("Artifact with a given name was not found.");
		}
		else
		{
			RunArtifactManager.instance.SetArtifactEnabledServer(artifactDefFromString, newState ?? (!RunArtifactManager.instance.IsArtifactEnabled(artifactDefFromString)));
		}
		void LogMessage(string message)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			if (fromChat)
			{
				Chat.SendBroadcastChat((ChatMessageBase)new SimpleChatMessage
				{
					baseToken = message
				});
			}
			else
			{
				Debug.Log((object)message);
			}
		}
	}

	private static ArtifactDef GetArtifactDefFromString(string partialName)
	{
		ArtifactDef[] artifactDefs = ArtifactCatalog.artifactDefs;
		foreach (ArtifactDef val in artifactDefs)
		{
			if (GetArgNameForArtifact(val, english: false).ToLower().Contains(partialName.ToLower()) || (Language.english.name != Language.currentLanguage.name && GetArgNameForArtifact(val, english: true).ToLower().Contains(partialName.ToLower())))
			{
				return val;
			}
		}
		return null;
	}

	private static string GetArgNameForArtifact(ArtifactDef artifactDef, bool english)
	{
		return Regex.Replace(Language.GetString(artifactDef.nameToken, english ? Language.english.name : Language.currentLanguage.name), "[ '-]", string.Empty);
	}
}