Decompiled source of ItemSpawner REPOLib42 Compat v1.0.0

ItemSpawner_REPOLib42_Compat.dll

Decompiled a day 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 BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using REPOLib.Modules;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ItemSpawner_REPOLib42_Compat")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ItemSpawner_REPOLib42_Compat")]
[assembly: AssemblyTitle("ItemSpawner_REPOLib42_Compat")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 ItemSpawner_REPOLib42_Compat
{
	[BepInPlugin("ItemSpawner.REPOLib42Compat", "ItemSpawner REPOLib 4.2 Compatibility", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "ItemSpawner.REPOLib42Compat";

		public const string PluginName = "ItemSpawner REPOLib 4.2 Compatibility";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"ItemSpawner REPOLib 4.2 Compatibility 1.0.0 loaded.");
			try
			{
				Harmony val = new Harmony("ItemSpawner.REPOLib42Compat");
				val.PatchAll(typeof(Plugin).Assembly);
				Log.LogInfo((object)"Harmony patches applied.");
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Harmony patching failed: " + ex));
			}
		}
	}
	[HarmonyPatch(typeof(PlayerAvatar), "Start")]
	internal static class PlayerAvatarStartPatch
	{
		private static void Postfix(PlayerAvatar __instance)
		{
			try
			{
				if (!((Object)(object)__instance == (Object)null) && !((Object)(object)SemiFunc.PlayerAvatarLocal() != (Object)(object)__instance) && !((Object)(object)((Component)__instance).gameObject.GetComponent<ItemSpawnerGui>() != (Object)null))
				{
					((Component)__instance).gameObject.AddComponent<ItemSpawnerGui>();
					Plugin.Log.LogInfo((object)"[ItemSpawner] GUI component attached to local player.");
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Failed to attach ItemSpawnerGui: " + ex));
			}
		}
	}
	public class ItemSpawnerGui : MonoBehaviour
	{
		private enum ListMode
		{
			None,
			Items,
			Valuables
		}

		private bool visible = true;

		private bool onGuiSeen;

		private ListMode listMode = ListMode.None;

		private string command = "";

		private string status = "";

		private string listFilter = "";

		private Vector2 listScroll;

		private const float CollapsedHeight = 190f;

		private const float ExpandedHeight = 420f;

		private Rect window = new Rect(20f, 20f, 460f, 190f);

		private GUIStyle labelStyle;

		private GUIStyle titleStyle;

		private void OnGUI()
		{
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			if (!onGuiSeen)
			{
				onGuiSeen = true;
				Plugin.Log.LogInfo((object)"[ItemSpawner] Player-attached OnGUI is running.");
			}
			EnsureStyles();
			if (!visible)
			{
				if (GUI.Button(new Rect(10f, 10f, 110f, 24f), "ItemSpawner"))
				{
					visible = true;
				}
			}
			else
			{
				window = GUI.Window(((Object)this).GetInstanceID(), window, new WindowFunction(DrawWindow), "ItemSpawner REPOLib 4.2 Compatibility");
			}
		}

		private void EnsureStyles()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			if (labelStyle == null)
			{
				labelStyle = new GUIStyle(GUI.skin.label);
			}
			if (titleStyle == null)
			{
				titleStyle = new GUIStyle(GUI.skin.label)
				{
					fontStyle = (FontStyle)1,
					fontSize = 14
				};
			}
		}

		private void DrawWindow(int id)
		{
			GUILayout.Label("Spawn Item / Valuable", titleStyle, Array.Empty<GUILayoutOption>());
			GUILayout.Space(5f);
			GUILayout.Label("Name (paste with Ctrl+V works too):", labelStyle, Array.Empty<GUILayoutOption>());
			GUI.SetNextControlName("SpawnItemTextField");
			command = GUILayout.TextField(command ?? "", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(28f) });
			GUILayout.Space(5f);
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			if (GUILayout.Button("Spawn", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(30f) }))
			{
				status = SpawnByName(ParseCommand(command));
			}
			if (GUILayout.Button("Hide", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(30f) }))
			{
				visible = false;
			}
			GUILayout.EndHorizontal();
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			if (GUILayout.Button((listMode == ListMode.Items) ? "Hide Items" : "All Items", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(26f) }))
			{
				ToggleList(ListMode.Items);
			}
			if (GUILayout.Button((listMode == ListMode.Valuables) ? "Hide Valuables" : "All Valuables", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(26f) }))
			{
				ToggleList(ListMode.Valuables);
			}
			GUILayout.EndHorizontal();
			if (!string.IsNullOrEmpty(status))
			{
				GUILayout.Space(5f);
				GUILayout.Label(status, labelStyle, Array.Empty<GUILayoutOption>());
			}
			if (listMode != ListMode.None)
			{
				DrawList();
			}
			GUI.DragWindow();
		}

		private void ToggleList(ListMode mode)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			listMode = ((listMode != mode) ? mode : ListMode.None);
			((Rect)(ref window)).height = ((listMode == ListMode.None) ? 190f : 420f);
			listFilter = "";
			listScroll = Vector2.zero;
			int count = GetAllItems().Count;
			int num = Valuables.AllValuables?.Count ?? (-1);
			Plugin.Log.LogInfo((object)$"[ItemSpawner] List opened ({mode}). Items found={count}, Valuables.AllValuables={num}");
		}

		private static List<Item> GetAllItems()
		{
			return (from x in Resources.FindObjectsOfTypeAll<Item>()
				where (Object)(object)x != (Object)null && x.prefab != null
				select x).Distinct().ToList();
		}

		private void DrawList()
		{
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.Space(8f);
			GUILayout.Label((listMode == ListMode.Items) ? "All Items - Filter:" : "All Valuables - Filter:", labelStyle, Array.Empty<GUILayoutOption>());
			listFilter = GUILayout.TextField(listFilter ?? "", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(24f) });
			IEnumerable<(string, string)> source = ((listMode == ListMode.Items) ? (from x in GetAllItems()
				select (Display: string.IsNullOrEmpty(x.itemName) ? ((Object)x).name : x.itemName, Raw: ((Object)x).name)) : (from x in Valuables.AllValuables
				where x != null && !string.IsNullOrEmpty(((PrefabRef<GameObject>)(object)x).PrefabName)
				select (Display: ((PrefabRef<GameObject>)(object)x).PrefabName, Raw: ((PrefabRef<GameObject>)(object)x).PrefabName)));
			source = source.Where<(string, string)>(((string Display, string Raw) n) => !string.IsNullOrEmpty(n.Raw));
			if (!string.IsNullOrEmpty(listFilter))
			{
				source = source.Where<(string, string)>(((string Display, string Raw) n) => n.Display.IndexOf(listFilter, StringComparison.OrdinalIgnoreCase) >= 0 || n.Raw.IndexOf(listFilter, StringComparison.OrdinalIgnoreCase) >= 0);
			}
			List<(string, string)> list = (from n in source.Distinct()
				orderby n.Display
				select n).ToList();
			GUILayout.Label(list.Count + " match(es). Click one to fill it in above.", labelStyle, Array.Empty<GUILayoutOption>());
			listScroll = GUILayout.BeginScrollView(listScroll, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(180f) });
			foreach (var item in list)
			{
				string text = ((item.Item1 == item.Item2) ? item.Item2 : (item.Item1 + "  (" + item.Item2 + ")"));
				if (GUILayout.Button(text, GUI.skin.label, Array.Empty<GUILayoutOption>()))
				{
					command = item.Item2;
				}
			}
			GUILayout.EndScrollView();
		}

		private string ParseCommand(string raw)
		{
			string text = (raw ?? "").Trim();
			if (text.StartsWith("/"))
			{
				text = text.Substring(1).Trim();
			}
			if (text.StartsWith("additem", StringComparison.OrdinalIgnoreCase))
			{
				text = text.Substring("additem".Length).Trim();
			}
			return text;
		}

		private string SpawnByName(string key)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: 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_0201: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(key))
			{
				return "Item name is empty!";
			}
			PlayerAvatar val = SemiFunc.PlayerAvatarLocal();
			Vector3 val2;
			if ((Object)(object)val != (Object)null)
			{
				val2 = ((Component)val).transform.position + ((Component)val).transform.forward * 2f + Vector3.up;
			}
			else
			{
				Camera main = Camera.main;
				if ((Object)(object)main == (Object)null)
				{
					return "Local player and camera not found.";
				}
				val2 = ((Component)main).transform.position + ((Component)main).transform.forward * 2f;
			}
			Quaternion identity = Quaternion.identity;
			try
			{
				List<Item> allItems = GetAllItems();
				Item val3 = ((IEnumerable<Item>)allItems).FirstOrDefault((Func<Item, bool>)((Item x) => (Object)(object)x != (Object)null && (string.Equals((x.itemName ?? "").Trim(), key, StringComparison.OrdinalIgnoreCase) || string.Equals((((Object)x).name ?? "").Trim(), key, StringComparison.OrdinalIgnoreCase))));
				if ((Object)(object)val3 == (Object)null)
				{
					val3 = ((IEnumerable<Item>)allItems).FirstOrDefault((Func<Item, bool>)((Item x) => (Object)(object)x != (Object)null && ((x.itemName ?? "").IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0 || (((Object)x).name ?? "").IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0)));
				}
				if ((Object)(object)val3 != (Object)null)
				{
					GameObject val4 = Items.SpawnItem(val3, val2, identity);
					return ((Object)(object)val4 != (Object)null) ? ("Spawned item: " + val3.itemName + " (" + ((Object)val3).name + ")") : "Item spawn failed.";
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Item spawn error: " + ex));
				return "Item error: " + ex.GetType().Name;
			}
			try
			{
				PrefabRef val5 = ((IEnumerable<PrefabRef>)Valuables.AllValuables).FirstOrDefault((Func<PrefabRef, bool>)((PrefabRef x) => x != null && !string.IsNullOrEmpty(((PrefabRef<GameObject>)(object)x).PrefabName) && string.Equals(((PrefabRef<GameObject>)(object)x).PrefabName.Trim(), key, StringComparison.OrdinalIgnoreCase)));
				if (val5 == null)
				{
					val5 = ((IEnumerable<PrefabRef>)Valuables.AllValuables).FirstOrDefault((Func<PrefabRef, bool>)((PrefabRef x) => x != null && !string.IsNullOrEmpty(((PrefabRef<GameObject>)(object)x).PrefabName) && ((PrefabRef<GameObject>)(object)x).PrefabName.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0));
				}
				if (val5 != null)
				{
					GameObject val6 = Valuables.SpawnValuable(val5, val2, identity);
					return ((Object)(object)val6 != (Object)null) ? ("Spawned valuable: " + ((PrefabRef<GameObject>)(object)val5).PrefabName) : "Valuable spawn failed.";
				}
			}
			catch (Exception ex2)
			{
				Plugin.Log.LogError((object)("Valuable spawn error: " + ex2));
				return "Valuable error: " + ex2.GetType().Name;
			}
			return "Not found: " + key;
		}
	}
}