Decompiled source of Object Ping v2.0.6

ObjectPing.dll

Decompiled 3 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ObjectPing")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("ObjectPing")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("2.0.6")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.6.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace ObjectPing
{
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class ZNetScene_Awake_Patch
	{
		private static GameObject placementEffect = new GameObject("PingVisualEffect");

		private static GameObject visualEffect = new GameObject("PingVisualEffect");

		private static GameObject visualEffect2 = new GameObject("PingVisualEffect2");

		private static GameObject soundEffect = new GameObject("PingSoundEffect");

		private static void Postfix(ZNetScene __instance)
		{
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: 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_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			ObjectPingPlugin._placementmarkerContainer.SetActive(false);
			Player component = Game.instance.m_playerPrefab.GetComponent<Player>();
			if ((Object)(object)component.m_placementMarkerInstance == (Object)null)
			{
				component.m_placementMarkerInstance = Object.Instantiate<GameObject>(component.m_placeMarker, ObjectPingPlugin._placementmarkerContainer.transform, false);
			}
			ObjectPingPlugin._placementmarkercopy.transform.SetParent(ObjectPingPlugin._placementmarkerContainer.transform);
			ZNetView obj = ObjectPingPlugin._placementmarkercopy.AddComponent<ZNetView>();
			obj.m_persistent = true;
			obj.m_distant = true;
			obj.m_syncInitialScale = true;
			PingDestruction pingDestruction = ObjectPingPlugin._placementmarkercopy.AddComponent<PingDestruction>();
			pingDestruction.m_triggerOnAwake = true;
			pingDestruction.m_timeout = 5f;
			placementEffect = Object.Instantiate<GameObject>(component.m_placementMarkerInstance, ObjectPingPlugin._placementmarkercopy.transform, false);
			placementEffect.transform.localPosition = Vector3.zero;
			placementEffect.transform.localScale = new Vector3(10f, 10f, 10f);
			visualEffect = Object.Instantiate<GameObject>(((Component)__instance.GetPrefab("vfx_sledge_hit").transform.Find("waves")).gameObject, ObjectPingPlugin._placementmarkercopy.transform, false);
			soundEffect = Object.Instantiate<GameObject>(__instance.GetPrefab("sfx_lootspawn"), ObjectPingPlugin._placementmarkercopy.transform, false);
			Object.Destroy((Object)(object)soundEffect.GetComponent<ZNetView>());
			ZSFX component2 = soundEffect.GetComponent<ZSFX>();
			visualEffect2 = Object.Instantiate<GameObject>(__instance.GetPrefab("fx_guardstone_activate"), ObjectPingPlugin._placementmarkercopy.transform, false);
			Object.Destroy((Object)(object)visualEffect2.GetComponent<ZNetView>());
			visualEffect2.transform.localPosition = new Vector3(0f, 0f, 0f);
			visualEffect2.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
			MainModule main = ((Component)visualEffect2.gameObject.transform.Find("Dome")).GetComponent<ParticleSystem>().main;
			((MainModule)(ref main)).startColor = new MinMaxGradient(Color.red);
			component2.m_minPitch = 0.8f;
			component2.m_maxPitch = 0.85f;
			component2.m_distanceReverb = true;
			component2.m_vol = 1f;
			component2.m_useCustomReverbDistance = true;
			component2.m_customReverbDistance = 10f;
			component2.m_delay = 1f;
			component2.m_time = 1f;
			__instance.m_namedPrefabs.Add(StringExtensionMethods.GetStableHashCode(((Object)ObjectPingPlugin._placementmarkercopy).name), ObjectPingPlugin._placementmarkercopy);
		}
	}
	[HarmonyPatch(typeof(Hud), "UpdateCrosshair")]
	internal static class PlayerUpdatePatch
	{
		private static Camera? _cam;

		private static int _layermask = 1;

		private const int Layer1 = 1;

		private const int Layer2 = 2;

		private const int Layer3 = 4;

		private const int Layer4 = 5;

		private const int Layer5 = 8;

		private const int Layer6 = 13;

		private const int Layer7 = 14;

		private const int Layer8 = 16;

		private const int Layer9 = 17;

		private const int Layer10 = 18;

		private const int Layer11 = 19;

		private const int Layer12 = 21;

		private const int Layer13 = 23;

		private const int Layer14 = 24;

		private const int Layer15 = 26;

		private const int Layer16 = 27;

		private const int Layer17 = 31;

		private static int _layermask1 = 2;

		private static int _layermask2 = 4;

		private static int _layermask3 = 16;

		private static int _layermask4 = 32;

		private static int _layermask5 = 256;

		private static int _layermask6 = 8192;

		private static int _layermask7 = 16384;

		private static int _layermask8 = 65536;

		private static int _layermask9 = 131072;

		private static int _layermask10 = 262144;

		private static int _layermask11 = 524288;

		private static int _layermask12 = 2097152;

		private static int _layermask13 = 8388608;

		private static int _layermask14 = 16777216;

		private static int _layermask15 = 67108864;

		private static int _layermask16 = 134217728;

		private static int _layermask17 = int.MinValue;

		private static int _finalmask = ~(_layermask1 | _layermask2 | _layermask3 | _layermask4 | _layermask5 | _layermask6 | _layermask7 | _layermask8 | _layermask9 | _layermask10 | _layermask11 | _layermask12 | _layermask13 | _layermask14 | _layermask15 | _layermask16 | _layermask17);

		private static Transform visualEffect = null;

		private static void Postfix(Hud __instance, Player player, float bowDrawPercentage)
		{
			//IL_0005: 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_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: 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_012c: 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_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			if (!ObjectPingPlugin._keyboardShortcut.Value.IsKeyDown() || !((Character)player).TakeInput())
			{
				return;
			}
			_cam = GameCamera.instance.m_camera;
			if ((Object)(object)_cam == (Object)null || (Object)(object)((Component)_cam).transform == (Object)null)
			{
				return;
			}
			Transform transform = ((Component)_cam).transform;
			_cam.ScreenPointToRay(transform.position);
			RaycastHit val = default(RaycastHit);
			if (Physics.Raycast(transform.position, transform.forward, ref val, float.PositiveInfinity, _finalmask))
			{
				Vector3 point = ((RaycastHit)(ref val)).point;
				string text = ((Object)((Component)((Component)((RaycastHit)(ref val)).collider).transform.root).gameObject).name.Replace("(Clone)", "");
				if (!(text == "Player"))
				{
					string text2 = CheckAndGrab(((Component)((Component)((RaycastHit)(ref val)).collider).transform.root).gameObject);
					text = (string.IsNullOrEmpty(text2) ? text : text2);
					ObjectPingPlugin.ObjectPingLogger.LogDebug((object)("You targeted " + text));
					GameObject prefab = ZNetScene.instance.GetPrefab("PingPrefab");
					Quaternion.Euler(0f, 360f, 0f);
					Object.Instantiate<GameObject>(prefab, point, Quaternion.identity).transform.rotation = Quaternion.LookRotation(((RaycastHit)(ref val)).transform.forward, ((RaycastHit)(ref val)).normal);
					DoPingText(point, text);
				}
			}
		}

		public static void DoPingText(Vector3 pos, string taggedFab = "")
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			WorldTextInstance val = new WorldTextInstance
			{
				m_worldPos = pos,
				m_gui = Object.Instantiate<GameObject>(DamageText.instance.m_worldTextBase, ((Component)DamageText.instance).transform)
			};
			val.m_textField = val.m_gui.GetComponent<TMP_Text>();
			DamageText.instance.m_worldTexts.Add(val);
			((Graphic)val.m_textField).color = ObjectPingPlugin.TextColor.Value;
			val.m_textField.fontSize = 24f;
			val.m_textField.text = taggedFab;
			val.m_timer = -2f;
		}

		private static string CheckAndGrab(GameObject obj)
		{
			Type[] obj2 = new Type[15]
			{
				typeof(Fermenter),
				typeof(Piece),
				typeof(Fireplace),
				typeof(CookingStation),
				typeof(MineRock5),
				typeof(MineRock),
				typeof(Destructible),
				typeof(TreeLog),
				typeof(TreeBase),
				typeof(Pickable),
				typeof(Plant),
				typeof(Beehive),
				typeof(SapCollector),
				typeof(HoverText),
				typeof(Character)
			};
			StringBuilder stringBuilder = new StringBuilder();
			Type[] array = obj2;
			foreach (Type type in array)
			{
				Component component = obj.GetComponent(type);
				if (!((Object)(object)component != (Object)null))
				{
					continue;
				}
				FieldInfo field = type.GetField("m_name", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					string text = (string)field.GetValue(component);
					stringBuilder.AppendLine(text.StartsWith("$") ? Localization.instance.Localize(text) : text);
					continue;
				}
				PropertyInfo property = type.GetProperty("m_name", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (property != null && property.PropertyType == typeof(string))
				{
					string text2 = (string)property.GetValue(component);
					stringBuilder.AppendLine(text2.StartsWith("$") ? Localization.instance.Localize(text2) : text2);
					continue;
				}
				FieldInfo field2 = type.GetField("m_text", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field2 != null)
				{
					string text3 = (string)field2.GetValue(component);
					stringBuilder.AppendLine(text3.StartsWith("$") ? Localization.instance.Localize(text3) : text3);
					continue;
				}
				MethodInfo method = type.GetMethod("GetHoverName", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method != null && method.ReturnType == typeof(string))
				{
					string text4 = (string)method.Invoke(component, null);
					stringBuilder.AppendLine(text4.StartsWith("$") ? Localization.instance.Localize(text4) : text4);
					continue;
				}
				MethodInfo method2 = type.GetMethod("GetHoverText", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method2 != null && method2.ReturnType == typeof(string))
				{
					string text5 = (string)method2.Invoke(component, null);
					stringBuilder.AppendLine(text5.StartsWith("$") ? Localization.instance.Localize(text5) : text5);
				}
			}
			return stringBuilder.ToString().TrimEnd(Array.Empty<char>());
		}
	}
	public class PingDestruction : MonoBehaviour
	{
		public float m_timeout = 1f;

		public bool m_triggerOnAwake;

		public ZNetView m_nview;

		public void Awake()
		{
			m_nview = ((Component)this).GetComponent<ZNetView>();
			if (m_triggerOnAwake)
			{
				Trigger();
			}
		}

		public void Trigger()
		{
			((MonoBehaviour)this).InvokeRepeating("DestroyNow", m_timeout, 1f);
		}

		public void Trigger(float timeout)
		{
			((MonoBehaviour)this).InvokeRepeating("DestroyNow", timeout, 1f);
		}

		public void OnDestroy()
		{
			Cancel();
			TryDestroyGameObject();
		}

		public void DestroyNow()
		{
			TryDestroyGameObject();
		}

		public void Cancel()
		{
			((MonoBehaviour)this).CancelInvoke("DestroyNow");
		}

		public void OnDisable()
		{
			Cancel();
		}

		private void OnPreCull()
		{
			TryDestroyGameObject();
		}

		private void TryDestroyGameObject()
		{
			try
			{
				if (Object.op_Implicit((Object)(object)m_nview))
				{
					if (m_nview.IsValid() && m_nview.IsOwner())
					{
						ZNetScene.instance.Destroy(((Component)this).gameObject);
					}
				}
				else
				{
					Object.Destroy((Object)(object)((Component)this).gameObject);
				}
			}
			catch (Exception ex)
			{
				ObjectPingPlugin.ObjectPingLogger.LogError((object)("Failed to destroy game object: " + ex.Message + " " + ((Object)((Component)this).gameObject).name));
			}
		}
	}
	[BepInPlugin("Azumatt.ObjectPing", "ObjectPing", "2.0.6")]
	public class ObjectPingPlugin : BaseUnityPlugin
	{
		private class AcceptableShortcuts : AcceptableValueBase
		{
			public AcceptableShortcuts()
				: base(typeof(KeyboardShortcut))
			{
			}

			public override object Clamp(object value)
			{
				return value;
			}

			public override bool IsValid(object value)
			{
				return true;
			}

			public override string ToDescriptionString()
			{
				return "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
			}
		}

		internal const string ModName = "ObjectPing";

		internal const string ModVersion = "2.0.6";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.ObjectPing";

		private static string ConfigFileName = "Azumatt.ObjectPing.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony _harmony = new Harmony("Azumatt.ObjectPing");

		public static readonly ManualLogSource ObjectPingLogger;

		internal static GameObject _placementmarkercopy;

		internal static GameObject _placementmarkerContainer;

		internal static ConfigEntry<KeyboardShortcut> _keyboardShortcut;

		internal static ConfigEntry<Color> TextColor;

		public void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_0043: 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)
			//IL_005d: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			_keyboardShortcut = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("1 - General", "Ping Keyboard Shortcut", KeyboardShortcut.Empty, new ConfigDescription("Set up your keys you'd like to use to trigger the ping.", (AcceptableValueBase)(object)new AcceptableShortcuts(), Array.Empty<object>()));
			TextColor = ((BaseUnityPlugin)this).Config.Bind<Color>("1 - General", "Ping Text Color", Color.cyan, new ConfigDescription("Color of the world text when pinging.", (AcceptableValueBase)null, Array.Empty<object>()));
			_placementmarkercopy = new GameObject("PingPrefab");
			Object.DontDestroyOnLoad((Object)(object)_placementmarkercopy);
			_placementmarkerContainer = new GameObject("PingPlacementMarker Container");
			Object.DontDestroyOnLoad((Object)(object)_placementmarkerContainer);
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				ObjectPingLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				ObjectPingLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				ObjectPingLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		static ObjectPingPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			ObjectPingLogger = Logger.CreateLogSource("ObjectPing");
			_placementmarkercopy = null;
			_placementmarkerContainer = null;
			_keyboardShortcut = null;
			TextColor = null;
		}
	}
	public static class KeyboardExtensions
	{
		public static bool IsKeyDown(this KeyboardShortcut shortcut)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if ((int)((KeyboardShortcut)(ref shortcut)).MainKey != 0 && Input.GetKeyDown(((KeyboardShortcut)(ref shortcut)).MainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}

		public static bool IsKeyHeld(this KeyboardShortcut shortcut)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if ((int)((KeyboardShortcut)(ref shortcut)).MainKey != 0 && Input.GetKey(((KeyboardShortcut)(ref shortcut)).MainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}
	}
}