Decompiled source of QuickSwim v1.0.1

QuickSwim.dll

Decompiled 2 days ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace NadoFundo;

[BepInPlugin("yuri.QuickSwim", "Quick Swim", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public static ConfigEntry<bool> Ligado;

	public static ConfigEntry<bool> Mergulho;

	public static ConfigEntry<float> VelocidadeMin;

	public static ConfigEntry<float> VelocidadeMax;

	public static ConfigEntry<float> GastoMin;

	public static ConfigEntry<float> GastoMax;

	public static ConfigEntry<float> Regen;

	public static ConfigEntry<float> Exp;

	public static ConfigEntry<float> Dano;

	public static ConfigEntry<float> Nevoa;

	public static ConfigEntry<float> Brilho;

	public static float FatorNado(Player jogador)
	{
		float num = ((Character)jogador).GetSkillLevel((SkillType)103);
		if (num < 0f)
		{
			num = 0f;
		}
		if (num > 100f)
		{
			num = 100f;
		}
		return num / 100f;
	}

	private void Awake()
	{
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		Ligado = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turns the mod on in this game. Only your own game needs this mod.");
		Mergulho = ((BaseUnityPlugin)this).Config.Bind<bool>("Dive", "Enabled", true, "Hold Ctrl in the water and look with the mouse to dive or rise.");
		VelocidadeMin = ((BaseUnityPlugin)this).Config.Bind<float>("Swim", "SpeedMin", 2f, "Speed with the Swim skill at zero.");
		VelocidadeMax = ((BaseUnityPlugin)this).Config.Bind<float>("Swim", "SpeedMax", 7f, "Speed with the Swim skill at 100. It rises with the level.");
		GastoMin = ((BaseUnityPlugin)this).Config.Bind<float>("Stamina", "DrainAtStart", 14f, "Stamina spent swimming with little skill.");
		GastoMax = ((BaseUnityPlugin)this).Config.Bind<float>("Stamina", "DrainAtMax", 4f, "Stamina spent swimming with the skill at maximum.");
		Regen = ((BaseUnityPlugin)this).Config.Bind<float>("Stamina", "RegenWhileStill", 8f, "Stamina per second while still in the water, at maximum Swim.");
		Exp = ((BaseUnityPlugin)this).Config.Bind<float>("Swim", "Exp", 1f, "Multiplies Swim skill experience. 1 is the game default.");
		Dano = ((BaseUnityPlugin)this).Config.Bind<float>("Dive", "DamageWithoutStamina", 10f, "Damage per second underwater with no stamina.");
		Nevoa = ((BaseUnityPlugin)this).Config.Bind<float>("Water", "Fog", 0.08f, "Fog underwater. 0 turns it off.");
		Brilho = ((BaseUnityPlugin)this).Config.Bind<float>("Water", "Brightness", 0.45f, "How clear the water is. 0 is dark, 1 is bright.");
		new Harmony("yuri.QuickSwim").PatchAll();
	}
}
[HarmonyPatch(typeof(Player), "RaiseSkill")]
public static class ExpPatch
{
	private static void Prefix(Player __instance, SkillType skill, ref float value)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Invalid comparison between Unknown and I4
		if (Plugin.Ligado.Value && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && (int)skill == 103)
		{
			value *= Plugin.Exp.Value;
		}
	}
}
[HarmonyPatch(typeof(Character), "UpdateSwimming")]
public static class VelocidadePatch
{
	private static void Prefix(Character __instance)
	{
		Player val = (Player)(object)((__instance is Player) ? __instance : null);
		if (!((Object)(object)val == (Object)null) && !((Object)(object)val != (Object)(object)Player.m_localPlayer) && Plugin.Ligado.Value)
		{
			float num = Plugin.FatorNado(val);
			((Character)val).m_swimSpeed = Mathf.Lerp(Plugin.VelocidadeMin.Value, Plugin.VelocidadeMax.Value, num);
			val.m_swimStaminaDrainMinSkill = Plugin.GastoMin.Value;
			val.m_swimStaminaDrainMaxSkill = Plugin.GastoMax.Value;
		}
	}

	private static void Postfix(Character __instance, Rigidbody ___m_body)
	{
		//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_0072: Unknown result type (might be due to invalid IL or missing references)
		Player val = (Player)(object)((__instance is Player) ? __instance : null);
		if (!((Object)(object)val == (Object)null) && !((Object)(object)val != (Object)(object)Player.m_localPlayer) && Plugin.Ligado.Value && Plugin.Mergulho.Value && !(MergulhoPatch.Profundidade < 0.05f) && !((Object)(object)___m_body == (Object)null))
		{
			Vector3 linearVelocity = ___m_body.linearVelocity;
			linearVelocity.y = 0f;
			___m_body.linearVelocity = linearVelocity;
		}
	}
}
[HarmonyPatch(typeof(Player), "LateUpdate")]
public static class MergulhoPatch
{
	public static float Profundidade;

	private static float superficie;

	private static bool nevoaAtiva;

	private static bool fogLigado;

	private static int modo;

	private static float densidade;

	private static Color cor;

	private static void Postfix(Player __instance)
	{
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return;
		}
		if (!Plugin.Ligado.Value || !((Character)__instance).IsSwimming())
		{
			Profundidade = 0f;
			RestaurarNevoa();
			return;
		}
		float num = Plugin.FatorNado(__instance);
		float num2 = Mathf.Lerp(Plugin.GastoMin.Value, Plugin.GastoMax.Value, num);
		bool flag = Tecla((KeyCode)306) || Tecla((KeyCode)305);
		bool flag2 = Tecla((KeyCode)32);
		float profundidade = Profundidade;
		if (Plugin.Mergulho.Value && flag)
		{
			Profundidade -= ((Character)__instance).GetLookDir().y * ((Character)__instance).m_swimSpeed * Time.deltaTime;
		}
		if (flag2)
		{
			Profundidade = Mathf.MoveTowards(Profundidade, 0f, ((Character)__instance).m_swimSpeed * 3f * Time.deltaTime);
		}
		if (Profundidade < 0f)
		{
			Profundidade = 0f;
		}
		if (Profundidade > 60f)
		{
			Profundidade = 60f;
		}
		if (profundidade <= 0.05f)
		{
			superficie = ((Component)__instance).transform.position.y;
		}
		if (Profundidade > 0.05f)
		{
			Vector3 position = ((Component)__instance).transform.position;
			float num3 = superficie - Profundidade;
			float num4 = Chao(position, superficie);
			if (num3 < num4)
			{
				num3 = num4;
				Profundidade = superficie - num3;
				if (Profundidade < 0f)
				{
					Profundidade = 0f;
				}
			}
			position.y = num3;
			((Component)__instance).transform.position = position;
		}
		if (Profundidade > 0.6f)
		{
			((Character)__instance).UseStamina(num2 * Time.deltaTime);
			if (__instance.GetStamina() <= 0.5f && Plugin.Dano.Value > 0f)
			{
				float num5 = ((Character)__instance).GetHealth() - Plugin.Dano.Value * Time.deltaTime;
				if (num5 < 0f)
				{
					num5 = 0f;
				}
				((Character)__instance).SetHealth(num5);
			}
			AplicarNevoa();
		}
		else
		{
			RestaurarNevoa();
			Vector3 velocity = ((Character)__instance).GetVelocity();
			if (((Vector3)(ref velocity)).magnitude < 0.35f && Plugin.Regen.Value > 0f)
			{
				((Character)__instance).AddStamina(Plugin.Regen.Value * num * Time.deltaTime);
			}
		}
	}

	private static float Chao(Vector3 pos, float agua)
	{
		//IL_001e: 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_0039: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = default(Vector3);
		((Vector3)(ref val))..ctor(pos.x, agua - 0.3f, pos.z);
		RaycastHit val2 = default(RaycastHit);
		if (Physics.Raycast(val, Vector3.down, ref val2, 120f))
		{
			return ((RaycastHit)(ref val2)).point.y + 0.7f;
		}
		return agua - 60f;
	}

	private static bool Tecla(KeyCode tecla)
	{
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		Type type = Type.GetType("UnityEngine.Input, UnityEngine.InputLegacyModule");
		if (type == null)
		{
			return false;
		}
		MethodInfo method = type.GetMethod("GetKey", new Type[1] { typeof(KeyCode) });
		if (method == null)
		{
			return false;
		}
		return (bool)method.Invoke(null, new object[1] { tecla });
	}

	private static void AplicarNevoa()
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		if (!(Plugin.Nevoa.Value <= 0f))
		{
			if (!nevoaAtiva)
			{
				fogLigado = LerBool("fog");
				modo = LerInt("fogMode");
				densidade = LerFloat("fogDensity");
				cor = LerCor("fogColor");
				nevoaAtiva = true;
			}
			float num = Plugin.Brilho.Value;
			if (num < 0.05f)
			{
				num = 0.05f;
			}
			Escrever("fog", true);
			Escrever("fogMode", 2);
			Escrever("fogDensity", Plugin.Nevoa.Value);
			Escrever("fogColor", (object)new Color(0.04f * num, 0.16f * num, 0.28f * num, 1f));
		}
	}

	private static void RestaurarNevoa()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		if (nevoaAtiva)
		{
			Escrever("fog", fogLigado);
			Escrever("fogMode", modo);
			Escrever("fogDensity", densidade);
			Escrever("fogColor", cor);
			nevoaAtiva = false;
		}
	}

	private static Type TipoCena()
	{
		return Type.GetType("UnityEngine.RenderSettings, UnityEngine.CoreModule");
	}

	private static void Escrever(string nome, object valor)
	{
		Type type = TipoCena();
		if (type == null)
		{
			return;
		}
		PropertyInfo property = type.GetProperty(nome);
		if (!(property == null))
		{
			if (nome == "fogMode")
			{
				valor = Enum.ToObject(property.PropertyType, valor);
			}
			property.SetValue(null, valor, null);
		}
	}

	private static bool LerBool(string nome)
	{
		object obj = Ler(nome);
		return obj is bool && (bool)obj;
	}

	private static int LerInt(string nome)
	{
		object obj = Ler(nome);
		if (obj == null)
		{
			return 2;
		}
		return Convert.ToInt32(obj);
	}

	private static float LerFloat(string nome)
	{
		object obj = Ler(nome);
		if (obj == null)
		{
			return 0f;
		}
		return Convert.ToSingle(obj);
	}

	private static Color LerCor(string nome)
	{
		//IL_0021: 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_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		if (!(Ler(nome) is Color result))
		{
			return Color.gray;
		}
		return result;
	}

	private static object Ler(string nome)
	{
		Type type = TipoCena();
		if (type == null)
		{
			return null;
		}
		PropertyInfo property = type.GetProperty(nome);
		if (property == null)
		{
			return null;
		}
		return property.GetValue(null, null);
	}
}