Decompiled source of StaffKeeper v1.0.0

StaffKeeper.dll

Decompiled 6 hours 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;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;
using YAPYAP;

[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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.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 StaffKeeper
{
	[BepInPlugin("StaffKeeper", "StaffKeeper", "1.0.0")]
	public class StaffKeeper : BaseUnityPlugin
	{
		private ConfigEntry<KeyboardShortcut> _toggleHotkey;

		private ConfigEntry<string> _customWandNames;

		private bool _showWindow;

		private Rect _windowRect = new Rect(100f, 100f, 400f, 500f);

		private Vector2 _scrollPos;

		private List<GameObject> _wandPrefabs = new List<GameObject>();

		private YapNetworkManager _networkManager;

		private void Awake()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)"StaffKeeper Awake 被调用!");
			_toggleHotkey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "ToggleHotkey", new KeyboardShortcut((KeyCode)287, Array.Empty<KeyCode>()), "打开/关闭生成窗口的热键");
			_customWandNames = ((BaseUnityPlugin)this).Config.Bind<string>("Wands", "CustomWandNames", "", "手动指定的法杖预制体名称(每行一个),若为空则自动匹配名称含 'Wand' 的预制体");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"StaffKeeper 初始化完成,配置文件已绑定");
		}

		private void Start()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"StaffKeeper Start 被调用");
			_networkManager = Object.FindObjectOfType<YapNetworkManager>();
			if ((Object)(object)_networkManager == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"未找到 YapNetworkManager,将在 Update 中重试");
			}
			else
			{
				RefreshWandList();
			}
		}

		private void RefreshWandList()
		{
			_wandPrefabs.Clear();
			if ((Object)(object)_networkManager == (Object)null)
			{
				return;
			}
			List<GameObject> spawnPrefabs = ((NetworkManager)_networkManager).spawnPrefabs;
			if (spawnPrefabs == null || spawnPrefabs.Count == 0)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"NetworkManager 中没有注册任何预制体");
				return;
			}
			HashSet<string> hashSet = (from s in _customWandNames.Value.Split(new char[2] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
				select s.Trim() into s
				where !string.IsNullOrEmpty(s)
				select s).ToHashSet();
			foreach (GameObject item in spawnPrefabs)
			{
				if ((Object)(object)item == (Object)null)
				{
					continue;
				}
				if (hashSet.Count > 0)
				{
					if (hashSet.Contains(((Object)item).name))
					{
						_wandPrefabs.Add(item);
					}
				}
				else if (((Object)item).name.Contains("Wand") && (Object)(object)item.GetComponent<NetworkPuppetProp>() != (Object)null)
				{
					_wandPrefabs.Add(item);
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"找到 {_wandPrefabs.Count} 个法杖预制体");
		}

		private void Update()
		{
			//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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Invalid comparison between Unknown and I4
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Invalid comparison between Unknown and I4
			if ((Object)(object)_networkManager == (Object)null)
			{
				_networkManager = Object.FindObjectOfType<YapNetworkManager>();
				if ((Object)(object)_networkManager != (Object)null)
				{
					RefreshWandList();
				}
			}
			KeyboardShortcut value = _toggleHotkey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"热键被按下");
				_showWindow = !_showWindow;
				if (_showWindow)
				{
					RefreshWandList();
					Input.ResetInputAxes();
				}
			}
			if (_showWindow)
			{
				if ((int)Cursor.lockState > 0)
				{
					Cursor.lockState = (CursorLockMode)0;
				}
				if (!Cursor.visible)
				{
					Cursor.visible = true;
				}
			}
			else if ((int)Cursor.lockState == 0)
			{
				Cursor.lockState = (CursorLockMode)1;
				Cursor.visible = false;
			}
		}

		private void OnDestroy()
		{
			Cursor.lockState = (CursorLockMode)1;
			Cursor.visible = false;
		}

		private void OnGUI()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			if (_showWindow)
			{
				_windowRect = GUILayout.Window(0, _windowRect, new WindowFunction(DrawWindow), "StaffKeeper - 法杖生成器", Array.Empty<GUILayoutOption>());
			}
		}

		private void DrawWindow(int windowId)
		{
			//IL_0038: 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_0055: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.BeginVertical(Array.Empty<GUILayoutOption>());
			if (_wandPrefabs.Count == 0)
			{
				GUILayout.Label("未找到法杖预制体,请检查配置。", Array.Empty<GUILayoutOption>());
			}
			else
			{
				_scrollPos = GUILayout.BeginScrollView(_scrollPos, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(400f) });
				foreach (GameObject wandPrefab in _wandPrefabs)
				{
					if (GUILayout.Button(((Object)wandPrefab).name, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(30f) }))
					{
						SpawnWandForLocalPlayer(wandPrefab);
					}
				}
				GUILayout.EndScrollView();
			}
			if (GUILayout.Button("关闭", Array.Empty<GUILayoutOption>()))
			{
				_showWindow = false;
				Cursor.lockState = (CursorLockMode)1;
				Cursor.visible = false;
			}
			GUILayout.EndVertical();
			GUI.DragWindow();
		}

		private void SpawnWandForLocalPlayer(GameObject wandPrefab)
		{
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Pawn.LocalInstance == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"未找到本地玩家");
				return;
			}
			Pawn localInstance = Pawn.LocalInstance;
			PawnPropInteractions propInteractions = localInstance.PropInteractions;
			if ((Object)(object)propInteractions == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"玩家缺少 PropInteractions 组件");
				return;
			}
			if ((Object)(object)propInteractions.NetworkCurrentRightHandNetworkProp != (Object)null)
			{
				propInteractions.NetworkCurrentRightHandNetworkProp.HandleDrop(((NetworkBehaviour)localInstance).netIdentity);
			}
			NetworkIdentity component = wandPrefab.GetComponent<NetworkIdentity>();
			if ((Object)(object)component == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"预制体缺少 NetworkIdentity");
				return;
			}
			NetworkIdentity val = _networkManager.SpawnNetworkPrefab(component, ((Component)localInstance).transform.position + ((Component)localInstance).transform.forward * 0.5f, Quaternion.identity, true);
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"生成法杖失败");
				return;
			}
			NetworkPuppetProp component2 = ((Component)val).GetComponent<NetworkPuppetProp>();
			if ((Object)(object)component2 == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"生成的物体不是 NetworkPuppetProp");
				return;
			}
			component2.ServerHandleHeld(propInteractions, true);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("已为房主生成法杖: " + ((Object)wandPrefab).name));
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "StaffKeeper";

		public const string PLUGIN_NAME = "StaffKeeper";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}