Decompiled source of HereFishy v0.1.1

HereFishy.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;

[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: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			int result;
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				result = ((instance != null && instance.HasFocus()) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
		int result2;
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				result2 = ((Object.op_Implicit((Object)(object)TextViewer.instance) && TextViewer.instance.IsVisible()) ? 1 : 0);
				goto IL_00dd;
			}
		}
		result2 = 1;
		goto IL_00dd;
		IL_00dd:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			return Input.GetKeyDown(value.ToLower());
		}
		catch
		{
			return false;
		}
	}
}
namespace HereFishy;

[BepInPlugin("aedenthorn.HereFishy", "Here Fishy", "0.6.1")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(CharacterAnimEvent), "GPower")]
	public static class CharacterAnimEvent_GPower_Patch
	{
		public static bool Prefix()
		{
			return fishyClip == null || Time.realtimeSinceStartup - lastHereFishy > fishyClip.length;
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	public static class InputText_Patch
	{
		public static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				__instance.AddString(text);
				__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
				return false;
			}
			return true;
		}
	}

	public static readonly bool isDebug = true;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<string> hotKey;

	public static ConfigEntry<bool> playHereFishy;

	public static ConfigEntry<bool> playWeeee;

	public static ConfigEntry<float> hereFishyVolume;

	public static ConfigEntry<float> weeVolume;

	public static ConfigEntry<float> maxFishyDistance;

	public static ConfigEntry<float> jumpSpeed;

	public static ConfigEntry<float> jumpHeight;

	public static BepInExPlugin context;

	public static AudioClip fishyClip;

	public static AudioClip weeClip;

	public static AudioSource fishAudio;

	public static float lastHereFishy;

	public static List<GameObject> hereFishyFishies = new List<GameObject>();

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	public void Awake()
	{
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 218, "Nexus mod ID for updates");
		hotKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "HotKey", "g", "Heeeeeeeeeeeeeeeeeeeeeeeeeeere Fishy Fishy Fishy key. Use https://docs.unity3d.com/Manual/ConventionalGameInput.html");
		maxFishyDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MaxFishyDistance", 100f, "Max distance Heeeeeeeeeeeeeeeeeeeeeeeeeeere Fishy Fishy Fishy can be heard");
		playHereFishy = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PlayHereFishy", true, "Heeeeeeeeeeeeeeeeeeeeeeeeeeere Fishy Fishy Fishy");
		playWeeee = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PlayWeeee", true, "Weeeeeeeeeeeeeeeeeeeee");
		hereFishyVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "HereFishyVolume", 1f, "Heeeeeeeeeeeeeeeeeeeeeeeeeeere Fishy Fishy Fishy volume");
		weeVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "WeeVolume", 1f, "Weeeeeeeeeeeeeeeeeeeee volume");
		jumpSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "JumpSpeed", 0.1f, "Fishy jump speed");
		jumpHeight = ((BaseUnityPlugin)this).Config.Bind<float>("General", "JumpHeight", 6f, "Fishy jump height");
		if (modEnabled.Value)
		{
			((MonoBehaviour)this).StartCoroutine(PreloadClipsCoroutine());
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	public void Update()
	{
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_015d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Expected O, but got Unknown
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_021d: Unknown result type (might be due to invalid IL or missing references)
		if (!modEnabled.Value || (Object)(object)Player.m_localPlayer == (Object)null || !Traverse.Create((object)Player.m_localPlayer).Method("TakeInput", Array.Empty<object>()).GetValue<bool>() || !AedenthornUtils.CheckKeyDown(hotKey.Value))
		{
			return;
		}
		Dbgl("pressed hotkey");
		lastHereFishy = Time.realtimeSinceStartup;
		((ZSyncAnimation)typeof(Player).GetField("m_zanim", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Player.m_localPlayer)).SetTrigger("gpower");
		if (playHereFishy.Value)
		{
			AudioSource val = ((Component)Player.m_localPlayer).gameObject.GetComponent<AudioSource>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)Player.m_localPlayer).gameObject.AddComponent<AudioSource>();
			}
			val.volume = Mathf.Clamp(hereFishyVolume.Value, 0.1f, 1f);
			val.clip = fishyClip;
			val.loop = false;
			val.spatialBlend = 1f;
			val.Play();
		}
		float num = maxFishyDistance.Value;
		Fish val2 = null;
		foreach (Fish instance in Fish.Instances)
		{
			Fish val3 = instance;
			if (Vector3.Distance(((Component)Player.m_localPlayer).transform.position, ((Component)val3).transform.position) < num)
			{
				float num2 = Vector3.Distance(((Component)Player.m_localPlayer).transform.position, ((Component)val3).gameObject.transform.position);
				if (num2 < num && !hereFishyFishies.Contains(((Component)val3).gameObject))
				{
					num = num2;
					val2 = val3;
				}
			}
		}
		if ((Object)(object)val2 != (Object)null)
		{
			Dbgl($"got closest fishy at {((Component)val2).gameObject.transform.position}");
			((MonoBehaviour)this).StartCoroutine(FishJump(val2, fishyClip.length));
		}
	}

	public static IEnumerator FishJump(Fish fish, float secs)
	{
		Vector3 origPos = ((Component)fish).gameObject.transform.position;
		Vector3 flatPos = origPos;
		if ((Object)(object)fish == (Object)null)
		{
			yield break;
		}
		hereFishyFishies.Add(((Component)fish).gameObject);
		yield return (object)new WaitForSeconds(secs);
		Dbgl("starting fish jump");
		if (playWeeee.Value)
		{
			fishAudio = ((Component)fish).gameObject.AddComponent<AudioSource>();
			fishAudio.volume = Mathf.Clamp(weeVolume.Value, 0.1f, 1f);
			fishAudio.clip = weeClip;
			fishAudio.loop = false;
			fishAudio.spatialBlend = 1f;
			fishAudio.Play();
		}
		while (true)
		{
			flatPos = Vector3.MoveTowards(flatPos, ((Component)Player.m_localPlayer).transform.position, jumpSpeed.Value);
			Vector3 playerPos = ((Component)Player.m_localPlayer).transform.position;
			float travelled = Vector3.Distance(flatPos, origPos);
			float total = Vector3.Distance(playerPos, origPos);
			float height = (float)Math.Sin((double)travelled * Math.PI / (double)total) * jumpHeight.Value;
			try
			{
				((Component)fish).gameObject.transform.position = new Vector3(flatPos.x, flatPos.y + height, flatPos.z);
			}
			catch
			{
				yield break;
			}
			if (Vector3.Distance(playerPos, ((Component)fish).gameObject.transform.position) < jumpSpeed.Value * 20f)
			{
			}
			if (Vector3.Distance(playerPos, ((Component)fish).gameObject.transform.position) < jumpSpeed.Value)
			{
				break;
			}
			yield return null;
		}
		Dbgl("taking fish");
		Object.Destroy((Object)(object)fishAudio);
		hereFishyFishies.Remove(((Component)fish).gameObject);
		fish.Pickup((Humanoid)(object)Player.m_localPlayer);
	}

	public static IEnumerator PreloadClipsCoroutine()
	{
		string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "HereFishy", "herefishy.wav");
		if (!File.Exists(path))
		{
			Dbgl("file " + path + " does not exist!");
			yield break;
		}
		string filename = "file:///" + path.Replace("\\", "/");
		Dbgl("getting audio clip from filename: " + filename);
		UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(filename, (AudioType)20);
		try
		{
			www.SendWebRequest();
			yield return null;
			if (www != null)
			{
				DownloadHandlerAudioClip dac = (DownloadHandlerAudioClip)www.downloadHandler;
				if (dac != null)
				{
					AudioClip ac = dac.audioClip;
					if ((Object)(object)ac != (Object)null)
					{
						Dbgl("audio clip is not null. samples: " + ac.samples);
						fishyClip = ac;
					}
					else
					{
						Dbgl("audio clip is null. data: " + ((DownloadHandler)dac).text);
					}
				}
				else
				{
					Dbgl("DownloadHandler is null. bytes downloaded: " + www.downloadedBytes);
				}
			}
			else
			{
				Dbgl("www is null " + www.url);
			}
		}
		finally
		{
			((IDisposable)www)?.Dispose();
		}
		path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "HereFishy", "wee.wav");
		filename = "file:///" + path.Replace("\\", "/");
		Dbgl("filename: " + filename);
		UnityWebRequest www2 = UnityWebRequestMultimedia.GetAudioClip(filename, (AudioType)20);
		try
		{
			www2.SendWebRequest();
			yield return null;
			if (www2 != null)
			{
				DownloadHandlerAudioClip dac2 = (DownloadHandlerAudioClip)www2.downloadHandler;
				if (dac2 != null)
				{
					AudioClip ac2 = dac2.audioClip;
					if ((Object)(object)ac2 != (Object)null)
					{
						Dbgl("audio clip is not null. samples: " + ac2.samples);
						weeClip = ac2;
					}
					else
					{
						Dbgl("audio clip is null. data: " + ((DownloadHandler)dac2).text);
					}
				}
				else
				{
					Dbgl("DownloadHandler is null. bytes downloaded: " + www2.downloadedBytes);
				}
			}
			else
			{
				Dbgl("www is null " + www2.url);
			}
		}
		finally
		{
			((IDisposable)www2)?.Dispose();
		}
	}
}