Decompiled source of GeneralPurposeCommands v1.1.0

GeneralPurposeCommands.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GeneralPurposeCommands.Patches;
using GeneralPurposeCommands.Utilities;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
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("GeneralPurposeCommands")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+88bcd1c845a70d01207abca171c6f2162d1bef3d")]
[assembly: AssemblyProduct("General Purpose Commands")]
[assembly: AssemblyTitle("GeneralPurposeCommands")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace GeneralPurposeCommands
{
	[BepInPlugin("com.sirh.plugin.generalpurposecommands", "General Purpose Commands", "1.0.0")]
	public class GeneralPurposeCommands : BaseUnityPlugin
	{
		private const string modGUID = "com.sirh.plugin.generalpurposecommands";

		private const string modName = "General Purpose Commands";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("com.sirh.plugin.generalpurposecommands");

		private static GeneralPurposeCommands Instance;

		public static ManualLogSource Logger;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"General Purpose Commands is loaded!");
			harmony.PatchAll(typeof(GeneralPurposeCommands));
			harmony.PatchAll(typeof(GeneralPurposeCommandsPatch));
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "GeneralPurposeCommands";

		public const string PLUGIN_NAME = "General Purpose Commands";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}
namespace GeneralPurposeCommands.Utilities
{
	public class Result<T>
	{
		public bool IsOk { get; set; }

		public T Value { get; set; }

		public string ErrorMessage { get; set; }

		public Result(T value)
		{
			IsOk = true;
			Value = value;
		}

		public Result(string errorMessage)
		{
			IsOk = false;
			ErrorMessage = errorMessage;
		}

		public static Result<T> Ok(T value)
		{
			return new Result<T>(value);
		}

		public static Result<T> Err(string errorMessage)
		{
			return new Result<T>(errorMessage);
		}

		public void Match(Action<Result<T>> Ok, Action<Result<T>> Err)
		{
			if (IsOk)
			{
				Ok(this);
			}
			else
			{
				Err(this);
			}
		}
	}
}
namespace GeneralPurposeCommands.Patches
{
	[HarmonyPatch(typeof(SemiFunc))]
	[HarmonyPatch("Command")]
	public class GeneralPurposeCommandsPatch : MonoBehaviour
	{
		[CompilerGenerated]
		private sealed class <SendMessage>d__3 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public string message;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <SendMessage>d__3(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Expected O, but got Unknown
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = (object)new WaitForSeconds(2f);
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					PlayerAvatar.instance.ChatMessageSend(message, false);
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		public static GeneralPurposeCommandsPatch instance;

		public void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			else
			{
				Object.Destroy((Object)(object)this);
			}
		}

		[HarmonyPostfix]
		public static void Additional_Command(string _command)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)instance == (Object)null)
			{
				instance = new GameObject("GeneralPurposeCommandsPatch").AddComponent<GeneralPurposeCommandsPatch>();
			}
			string[] array = _command.Trim().ToLower().Split(" ");
			string text = array[0];
			string[] args = ((array.Length >= 2) ? new Span<string>(array, 1, array.Length - 1).ToArray() : Array.Empty<string>());
			(text switch
			{
				"addmoney" => AddMoney(args), 
				"listitems" => ListItems(), 
				"spawnitem" => SpawnItem(args), 
				_ => Result<string>.Err("NO_COMMAND_CALLED"), 
			}).Match(delegate(Result<string> result)
			{
				((MonoBehaviour)instance).StartCoroutine(SendMessage(result.Value));
			}, delegate(Result<string> err)
			{
				if (!err.ErrorMessage.Equals("NO_COMMAND_CALLED"))
				{
					((MonoBehaviour)instance).StartCoroutine(SendMessage(err.ErrorMessage));
				}
			});
		}

		[IteratorStateMachine(typeof(<SendMessage>d__3))]
		public static IEnumerator SendMessage(string message)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <SendMessage>d__3(0)
			{
				message = message
			};
		}

		public static Result<string> AddMoney(string[] args)
		{
			if (args.Length == 0)
			{
				string text = "Amount is missing.";
				GeneralPurposeCommands.Logger.LogError((object)text);
				return Result<string>.Err(text);
			}
			if (!int.TryParse(args[0], out var result))
			{
				string text2 = "Amount must be an integer type number.";
				GeneralPurposeCommands.Logger.LogError((object)text2);
				return Result<string>.Err(text2);
			}
			SemiFunc.StatSetRunCurrency(SemiFunc.StatGetRunCurrency() + result);
			string text3 = $"New Total: {SemiFunc.StatGetRunCurrency()}";
			GeneralPurposeCommands.Logger.LogInfo((object)text3);
			return Result<string>.Ok(text3);
		}

		private static Result<string> ListItems()
		{
			string text = "Items:\n";
			foreach (Item value in StatsManager.instance.itemDictionary.Values)
			{
				text = text + value.itemName + "\n";
			}
			Debug.Log((object)text);
			return Result<string>.Ok("Check Logs");
		}

		private static Result<string> SpawnItem(string[] args)
		{
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: 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_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0300: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: 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_0317: Unknown result type (might be due to invalid IL or missing references)
			//IL_031c: Unknown result type (might be due to invalid IL or missing references)
			//IL_034d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0351: Unknown result type (might be due to invalid IL or missing references)
			//IL_0359: Unknown result type (might be due to invalid IL or missing references)
			//IL_0363: Unknown result type (might be due to invalid IL or missing references)
			//IL_0368: Unknown result type (might be due to invalid IL or missing references)
			//IL_036d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			int result = 1;
			string text = "";
			if (args.Length > 1)
			{
				text = ((!int.TryParse(args[^1], out result)) ? args.Aggregate((string x, string y) => x + " " + y) : args[..^1].Aggregate((string x, string y) => x + " " + y));
			}
			if (string.IsNullOrEmpty(text))
			{
				Debug.LogWarning((object)"Item name is empty!");
				return Result<string>.Err("nuh uh");
			}
			string text2 = text.Trim();
			Item val = null;
			foreach (Item value in StatsManager.instance.itemDictionary.Values)
			{
				if (string.Equals(value.itemAssetName, text2, StringComparison.OrdinalIgnoreCase) || string.Equals(value.itemName, text2, StringComparison.OrdinalIgnoreCase))
				{
					val = value;
					break;
				}
			}
			Transform transform = ((Component)Camera.main).transform;
			Vector3 val2 = transform.position + transform.forward * 2f;
			Quaternion identity = Quaternion.identity;
			if ((Object)(object)val != (Object)null)
			{
				if ((Object)(object)val.prefab == (Object)null)
				{
					Debug.LogWarning((object)("Item prefab is null for: " + text2));
					return Result<string>.Err("nuh uh");
				}
				GameObject obj = Resources.Load<GameObject>("Valuables/" + ((Object)val.prefab).name);
				string text3 = (((Object)(object)obj != (Object)null) ? "Valuables/" : "Items/");
				Debug.Log((object)(((Object)(object)obj != (Object)null) ? ("Found valuable prefab in Resources/Valuables. Using '" + text3 + "'.") : ("Valuable prefab not found in Resources/Valuables; using '" + text3 + "' folder.")));
				if (SemiFunc.IsMultiplayer())
				{
					for (int i = 0; i < result; i++)
					{
						InstantiateRoomObject(text3 + ((Object)val.prefab).name, val2 + transform.up * (float)i * 0.2f, identity, 0);
					}
				}
				else
				{
					GameObject val3 = Resources.Load<GameObject>(text3 + ((Object)val.prefab).name);
					if ((Object)(object)val3 != (Object)null)
					{
						for (int j = 0; j < result; j++)
						{
							Object.Instantiate<GameObject>(val3, val2 + transform.up * (float)j * 0.2f, identity);
						}
					}
					else
					{
						Debug.LogWarning((object)("Prefab not found for singleplayer spawn: " + text3 + ((Object)val.prefab).name));
					}
				}
				string itemName = val.itemName;
				Debug.Log((object)("Spawned item: " + itemName + " at " + ((object)(Vector3)(ref val2)).ToString()));
				return Result<string>.Ok("yippee");
			}
			Debug.Log((object)"Item not found in StatsManager; searching Resources subfolders for a valuable prefab.");
			if ((Object)(object)TryLoadValuablePrefab(text2, out var foundPath) != (Object)null)
			{
				if (SemiFunc.IsMultiplayer())
				{
					for (int k = 0; k < result; k++)
					{
						InstantiateRoomObject(foundPath, val2 + transform.up * (float)k * 0.2f, identity, 0);
					}
				}
				else
				{
					GameObject val4 = Resources.Load<GameObject>(foundPath);
					if ((Object)(object)val4 != (Object)null)
					{
						for (int l = 0; l < result; l++)
						{
							Object.Instantiate<GameObject>(val4, val2 + transform.up * (float)l * 0.2f, identity);
						}
					}
					else
					{
						Debug.LogWarning((object)("Prefab not found for singleplayer spawn: " + foundPath));
					}
				}
				Debug.Log((object)("Spawned valuable from path: " + foundPath + " at " + ((object)(Vector3)(ref val2)).ToString()));
				return Result<string>.Ok("yippee");
			}
			Debug.LogWarning((object)("No item or valuable found with name: " + text2));
			return Result<string>.Err("nuh uh");
		}

		private static GameObject TryLoadValuablePrefab(string searchName, out string foundPath)
		{
			foundPath = "";
			string[] array = new string[7] { "01 Tiny", "02 Small", "03 Medium", "04 Big", "05 Wide", "06 Tall", "07 Very Tall" };
			foreach (string text in array)
			{
				string text2 = "Valuables/" + text + "/" + searchName;
				GameObject val = Resources.Load<GameObject>(text2);
				if ((Object)(object)val != (Object)null)
				{
					foundPath = text2;
					return val;
				}
			}
			string text3 = "Items/Removed Items/" + searchName;
			GameObject val2 = Resources.Load<GameObject>(text3);
			if ((Object)(object)val2 != (Object)null)
			{
				foundPath = text3;
				return val2;
			}
			return null;
		}

		public static GameObject InstantiateRoomObject(string prefabName, Vector3 position, Quaternion rotation, byte group = 0, object[] data = null)
		{
			//IL_003c: 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_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			if (PhotonNetwork.CurrentRoom == null)
			{
				Debug.LogError((object)"Can not Instantiate before the client joined/created a room.");
				return null;
			}
			try
			{
				return (GameObject)GetMethod(typeof(PhotonNetwork), "NetworkInstantiate", typeof(InstantiateParameters)).Invoke(null, new object[3]
				{
					(object)new InstantiateParameters(prefabName, position, rotation, group, data, (byte)0, (int[])null, PhotonNetwork.LocalPlayer, PhotonNetwork.ServerTimestamp),
					true,
					false
				});
			}
			catch (Exception ex)
			{
				Debug.LogError((object)ex);
				return null;
			}
		}

		public static MethodInfo GetMethod(Type type, string name, Type uniqueParamType)
		{
			return (from x in type.GetRuntimeMethods()
				where x.Name.Equals(name)
				select x into r
				let p = r.GetParameters()
				where p.Any((ParameterInfo o) => uniqueParamType.IsAssignableFrom(o.ParameterType))
				select r).ToList()[0];
		}
	}
}