Decompiled source of FasterBoats v1.4.0

FasterBoats.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FasterBoats")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FasterBoats")]
[assembly: AssemblyTitle("FasterBoats")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 FasterBoats
{
	[BepInPlugin("com.bouns.fasterboats", "Faster Boats", "1.4.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.bouns.fasterboats";

		public const string NAME = "Faster Boats";

		public const string VERSION = "1.4.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> SailMult;

		internal static ConfigEntry<float> BackwardMult;

		internal static ConfigEntry<float> SteerMult;

		internal static ConfigEntry<float> DampingMult;

		internal static ConfigEntry<float> MinWindFactor;

		internal static ConfigEntry<bool> ShowSpeed;

		private static readonly MethodInfo M_getSpeed = AccessTools.Method(typeof(Ship), "GetSpeed", (Type[])null, (Type[])null);

		private static Font _font;

		private static bool _fontTried;

		private void Awake()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Expected O, but got Unknown
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected O, but got Unknown
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Expected O, but got Unknown
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Expected O, but got Unknown
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch. false = boats behave vanilla.");
			SailMult = ((BaseUnityPlugin)this).Config.Bind<float>("Speed", "SailForceMultiplier", 2.5f, new ConfigDescription("Main speed knob: multiplies the forward force the sail produces.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 20f), Array.Empty<object>()));
			BackwardMult = ((BaseUnityPlugin)this).Config.Bind<float>("Speed", "BackwardForceMultiplier", 2f, new ConfigDescription("Multiplies reverse / paddle-back force. Applied per ship when it spawns.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 20f), Array.Empty<object>()));
			SteerMult = ((BaseUnityPlugin)this).Config.Bind<float>("Speed", "SteerForceMultiplier", 1.5f, new ConfigDescription("Multiplies turning (rudder) force. Applied per ship when it spawns.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 20f), Array.Empty<object>()));
			DampingMult = ((BaseUnityPlugin)this).Config.Bind<float>("Speed", "DampingMultiplier", 0.9f, new ConfigDescription("Multiplies water drag. Below 1 = less drag = higher top speed (the real lever for top speed, since force alone hits a soft cap). 1 = vanilla. Applied per ship when it spawns. Very low values make the boat slippery and slow to stop.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 1.5f), Array.Empty<object>()));
			MinWindFactor = ((BaseUnityPlugin)this).Config.Bind<float>("Wind", "MinWindFactor", 0.5f, new ConfigDescription("Floor on the wind-angle EFFICIENCY (0..1, not a force multiplier). 0 = vanilla (sailing straight into the wind is near a dead stop). 0.5 = at least 50% sail efficiency at any wind angle. 1 = wind direction ignored (always best angle). For raw speed use SailForceMultiplier instead.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			ShowSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowSpeed", true, "Show the boat speed on-screen (under the minimap) while aboard a ship.");
			try
			{
				new Harmony("com.bouns.fasterboats").PatchAll();
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Faster Boats v1.4.0 loaded (sail x" + SailMult.Value + ", back x" + BackwardMult.Value + ", steer x" + SteerMult.Value + ")."));
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Faster Boats v1.4.0 FAILED to patch (Valheim version mismatch?): " + ex));
			}
		}

		private void OnGUI()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			if (!ShowSpeed.Value)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			Ship val = FindMyShip(localPlayer);
			if (!((Object)(object)val == (Object)null))
			{
				string text = SpeedMs(val).ToString("0.0") + " m/s";
				GUIStyle val2 = new GUIStyle
				{
					alignment = (TextAnchor)5,
					fontSize = 18,
					fontStyle = (FontStyle)1,
					richText = false
				};
				Font val3 = GameFont();
				if ((Object)(object)val3 != (Object)null)
				{
					val2.font = val3;
				}
				Rect val4 = default(Rect);
				((Rect)(ref val4))..ctor(0f, (float)Screen.height * 0.235f, (float)Screen.width - 30f, 26f);
				val2.normal.textColor = new Color(0f, 0f, 0f, 0.8f);
				GUI.Label(new Rect(((Rect)(ref val4)).x + 2f, ((Rect)(ref val4)).y + 2f, ((Rect)(ref val4)).width, ((Rect)(ref val4)).height), text, val2);
				val2.normal.textColor = Color.white;
				GUI.Label(val4, text, val2);
			}
		}

		private static float SpeedMs(Ship s)
		{
			try
			{
				return (M_getSpeed != null) ? ((float)M_getSpeed.Invoke(s, null)) : 0f;
			}
			catch
			{
				return 0f;
			}
		}

		private static Ship FindMyShip(Player local)
		{
			List<IMonoUpdater> instances = Ship.Instances;
			if (instances == null)
			{
				return null;
			}
			for (int i = 0; i < instances.Count; i++)
			{
				IMonoUpdater obj = instances[i];
				Ship val = (Ship)(object)((obj is Ship) ? obj : null);
				if ((Object)(object)val != (Object)null && val.IsPlayerInBoat(local))
				{
					return val;
				}
			}
			return null;
		}

		private static Font GameFont()
		{
			if (_fontTried)
			{
				return _font;
			}
			_fontTried = true;
			try
			{
				Font val = null;
				Font val2 = null;
				Font[] array = Resources.FindObjectsOfTypeAll<Font>();
				foreach (Font val3 in array)
				{
					if (!((Object)(object)val3 == (Object)null))
					{
						string text = ((Object)val3).name ?? "";
						if ((Object)(object)val == (Object)null && text.IndexOf("Norse", StringComparison.OrdinalIgnoreCase) >= 0)
						{
							val = val3;
						}
						else if ((Object)(object)val2 == (Object)null && text.IndexOf("Averia", StringComparison.OrdinalIgnoreCase) >= 0)
						{
							val2 = val3;
						}
					}
				}
				_font = (((Object)(object)val != (Object)null) ? val : val2);
			}
			catch
			{
				_font = null;
			}
			return _font;
		}
	}
	[HarmonyPatch(typeof(Ship), "Awake")]
	internal static class Patch_Ship_Awake
	{
		private static readonly FieldInfo F_back = AccessTools.Field(typeof(Ship), "m_backwardForce");

		private static readonly FieldInfo F_steer = AccessTools.Field(typeof(Ship), "m_stearForce");

		private static readonly FieldInfo F_damp = AccessTools.Field(typeof(Ship), "m_damping");

		private static readonly FieldInfo F_dampF = AccessTools.Field(typeof(Ship), "m_dampingForward");

		private static readonly FieldInfo F_dampS = AccessTools.Field(typeof(Ship), "m_dampingSideway");

		private static void Scale(FieldInfo f, Ship s, float mult)
		{
			if (!(f == null))
			{
				float num = (float)f.GetValue(s);
				f.SetValue(s, num * mult);
			}
		}

		private static void Postfix(Ship __instance)
		{
			if (Plugin.Enabled.Value && !((Object)(object)__instance == (Object)null))
			{
				Scale(F_back, __instance, Plugin.BackwardMult.Value);
				Scale(F_steer, __instance, Plugin.SteerMult.Value);
				float value = Plugin.DampingMult.Value;
				if (value != 1f)
				{
					Scale(F_damp, __instance, value);
					Scale(F_dampF, __instance, value);
					Scale(F_dampS, __instance, value);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Ship), "GetSailForce")]
	internal static class Patch_Ship_GetSailForce
	{
		private static void Postfix(ref Vector3 __result)
		{
			//IL_000f: 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_0023: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.Enabled.Value)
			{
				__result *= Plugin.SailMult.Value;
			}
		}
	}
	[HarmonyPatch(typeof(Ship), "GetWindAngleFactor")]
	internal static class Patch_Ship_GetWindAngleFactor
	{
		private static void Postfix(ref float __result)
		{
			if (Plugin.Enabled.Value)
			{
				float value = Plugin.MinWindFactor.Value;
				if (value > 0f && __result < value)
				{
					__result = value;
				}
			}
		}
	}
}