Decompiled source of AntiZookaPreview v1.0.1

plugins/AntiZookaPreview.dll

Decompiled 4 days ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Peak;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 AntiZookaPreview
{
	[HarmonyPatch(typeof(CharacterItems), "Update")]
	public static class AntiZookaPreviewPatch
	{
		public static void Postfix(CharacterItems __instance, Character ___character)
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.EnablePreview == null || !Plugin.EnablePreview.Value || (Object)(object)__instance == (Object)null || (Object)(object)___character == (Object)null || !___character.IsLocal)
			{
				return;
			}
			Item val = (((Object)(object)___character.data != (Object)null) ? ___character.data.currentItem : null);
			if ((Object)(object)val == (Object)null)
			{
				AntiZookaPreviewRenderer.Clear();
				return;
			}
			Action_RaycastSpawnSomething component = ((Component)val).GetComponent<Action_RaycastSpawnSomething>();
			if ((Object)(object)component == (Object)null)
			{
				AntiZookaPreviewRenderer.Clear();
				return;
			}
			Vector3 position = ComputeSpawnPosition(component);
			AntiZookaPreviewRenderer.Show(component, position);
		}

		private static Vector3 ComputeSpawnPosition(Action_RaycastSpawnSomething action)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			Transform rayOrigin = action.rayOrigin;
			Vector3 forward = ((Component)MainCamera.instance).transform.forward;
			float maxDistance = action.maxDistance;
			float backOffFromHitDistance = action.backOffFromHitDistance;
			LayerMask hitLayerMask = action.hitLayerMask;
			QueryTriggerInteraction queryTriggerInteraction = action.queryTriggerInteraction;
			RaycastHit val = default(RaycastHit);
			if (Physics.Raycast(rayOrigin.position, forward, ref val, maxDistance, LayerMask.op_Implicit(hitLayerMask), queryTriggerInteraction) && !(((RaycastHit)(ref val)).distance < backOffFromHitDistance))
			{
				return ((RaycastHit)(ref val)).point - forward * backOffFromHitDistance;
			}
			return rayOrigin.position + forward * (maxDistance - backOffFromHitDistance);
		}
	}
	public static class AntiZookaPreviewRenderer
	{
		private static GameObject _preview;

		public static void Show(Action_RaycastSpawnSomething action, Vector3 position)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)action == (Object)null || (Object)(object)action.prefabToSpawn == (Object)null)
			{
				return;
			}
			if ((Object)(object)_preview == (Object)null)
			{
				_preview = CreatePreview(action);
				if ((Object)(object)_preview == (Object)null)
				{
					return;
				}
				ApplyTransparency(_preview);
			}
			_preview.transform.SetPositionAndRotation(position, Quaternion.identity);
			_preview.SetActive(true);
		}

		public static void Clear()
		{
			if ((Object)(object)_preview != (Object)null)
			{
				_preview.SetActive(false);
			}
		}

		public static void Dispose()
		{
			if ((Object)(object)_preview != (Object)null)
			{
				Object.Destroy((Object)(object)_preview);
				_preview = null;
			}
		}

		private static GameObject CreatePreview(Action_RaycastSpawnSomething action)
		{
			if ((Object)(object)action.prefabToSpawn == (Object)null)
			{
				return null;
			}
			GameObject obj = Object.Instantiate<GameObject>(action.prefabToSpawn, AntiZookaPreviewRoot.Root);
			((Object)obj).name = "AntiZookaPreview";
			Cleanup(obj);
			return obj;
		}

		private static void Cleanup(GameObject preview)
		{
			Collider[] componentsInChildren = preview.GetComponentsInChildren<Collider>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Object.Destroy((Object)(object)componentsInChildren[i]);
			}
			Component[] componentsInChildren2 = preview.GetComponentsInChildren<Component>();
			foreach (Component val in componentsInChildren2)
			{
				Behaviour val2 = (Behaviour)(object)((val is Behaviour) ? val : null);
				if (val2 != null && ((object)val).GetType().Name == "AudioSource")
				{
					val2.enabled = false;
				}
			}
			Rigidbody[] componentsInChildren3 = preview.GetComponentsInChildren<Rigidbody>();
			for (int i = 0; i < componentsInChildren3.Length; i++)
			{
				componentsInChildren3[i].isKinematic = true;
			}
		}

		private static void ApplyTransparency(GameObject preview)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			float num = (((Object)(object)Plugin.Instance != (Object)null && Plugin.PreviewTransparency != null) ? Plugin.PreviewTransparency.Value : 0.6f);
			if (num >= 0.99f)
			{
				return;
			}
			Renderer[] componentsInChildren = preview.GetComponentsInChildren<Renderer>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Material material = componentsInChildren[i].material;
				if (material.HasProperty("_Color"))
				{
					Color color = material.color;
					color.a = num;
					material.color = color;
				}
			}
		}
	}
	public static class AntiZookaPreviewRoot
	{
		private static GameObject _root;

		public static Transform Root
		{
			get
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_001c: Expected O, but got Unknown
				if ((Object)(object)_root == (Object)null)
				{
					_root = new GameObject("AntiZookaPreview.PreviewRoot");
					Object.DontDestroyOnLoad((Object)(object)_root);
				}
				return _root.transform;
			}
		}

		public static void Dispose()
		{
			if ((Object)(object)_root != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)_root);
				_root = null;
			}
		}
	}
	public static class ColoredLogging
	{
		private static readonly MethodInfo SetConsoleColorMethod;

		private static readonly TextWriter ConsoleStream;

		static ColoredLogging()
		{
			Type type = typeof(BaseUnityPlugin).Assembly.GetType("BepInEx.ConsoleManager");
			if (type != null)
			{
				SetConsoleColorMethod = type.GetMethod("SetConsoleColor", BindingFlags.Static | BindingFlags.Public);
				ConsoleStream = type.GetProperty("ConsoleStream", BindingFlags.Static | BindingFlags.Public)?.GetValue(null) as TextWriter;
			}
		}

		private static void SetColor(ConsoleColor color)
		{
			SetConsoleColorMethod?.Invoke(null, new object[1] { color });
		}

		private static void WriteColored(string message, ConsoleColor color, bool newline = true)
		{
			if (ConsoleStream != null)
			{
				SetColor(color);
				if (newline)
				{
					ConsoleStream.WriteLine(message);
				}
				else
				{
					ConsoleStream.Write(message);
				}
				SetColor(ConsoleColor.Gray);
			}
		}

		public static void LogColor(this ManualLogSource log, string message, ConsoleColor color = ConsoleColor.Gray)
		{
			WriteColored(message, color);
		}

		public static void LogColorW(this ManualLogSource log, string message, ConsoleColor color = ConsoleColor.Yellow)
		{
			WriteColored(message, color);
		}

		public static void LogColorE(this ManualLogSource log, string message, ConsoleColor color = ConsoleColor.Red)
		{
			WriteColored(message, color);
		}

		public static void LogColorS(this ManualLogSource log, string message, ConsoleColor color = ConsoleColor.Green)
		{
			WriteColored(message, color);
		}

		public static void LogColorM(this ManualLogSource log, string message, ConsoleColor color = ConsoleColor.Magenta)
		{
			WriteColored(message, color);
		}

		public static void LogColorC(this ManualLogSource log, string message, ConsoleColor color = ConsoleColor.Cyan)
		{
			WriteColored(message, color);
		}
	}
	public static class PluginInfo
	{
		public const string Guid = "AntiZookaPreview";

		public const string Name = "AntiZookaPreview";

		public const string Version = "1.0.0";
	}
	[BepInPlugin("AntiZookaPreview", "AntiZookaPreview", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<bool> EnablePreview;

		public static ConfigEntry<float> PreviewTransparency;

		internal static ManualLogSource Log { get; private set; }

		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Log.LogColorM("          反重力炮预览 v1.0.0 加载完成");
			BindConfig();
			new Harmony("AntiZookaPreview").PatchAll(Assembly.GetExecutingAssembly());
			Log.LogColorC("          反重力炮预览 v1.0.0 已就绪");
		}

		private void BindConfig()
		{
			EnablePreview = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable Preview", true, "手持反重力炮时显示 AntiSphere 生成位置预览");
			PreviewTransparency = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Preview Transparency", 0.6f, "预览透明度(0=完全透明,1=完全可见)");
		}

		private void OnDestroy()
		{
			AntiZookaPreviewRenderer.Dispose();
			AntiZookaPreviewRoot.Dispose();
		}
	}
}