Decompiled source of SurtlingBoatsPlus v1.1.2

SurtlingBoatsPlus.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
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: AssemblyVersion("0.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 SurtlingBoatsPlus
{
	[BepInPlugin("saymonn.SurtlingBoatsPlus", "SurtlingBoatsPlus", "1.1.2")]
	public class Plugin : BaseUnityPlugin
	{
		public enum SpeedUnit
		{
			KilometersPerHour,
			MilesPerHour
		}

		[HarmonyPatch(typeof(Ship), "CustomFixedUpdate")]
		private static class ShipPatch
		{
			private static void Postfix(Ship __instance, float fixedDeltaTime)
			{
				//IL_004f: 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_0079: Invalid comparison between Unknown and I4
				//IL_008d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0098: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
				//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_00d8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
				if (!modEnabled.Value || !thrustEnabled.Value)
				{
					return;
				}
				ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
				if (Object.op_Implicit((Object)(object)component) && component.IsValid() && component.IsOwner() && component.GetZDO().GetBool("SurtlingBoatsPlus_enabled", false))
				{
					float num = ThrustFor(__instance.GetSpeedSetting());
					if (!(num <= 0f) && ConsumeFuel(__instance, component.GetZDO(), fixedDeltaTime))
					{
						float num2 = (((int)__instance.GetSpeedSetting() == 1) ? (-1f) : 1f);
						Vector3 val = ((Component)__instance).transform.position + ((Component)__instance).transform.forward * __instance.m_stearForceOffset;
						Vector3 val2 = num2 * ((Component)__instance).transform.forward * (__instance.m_backwardForce * num * (1f - Mathf.Abs(__instance.GetRudderValue())));
						Rigidbody component2 = ((Component)__instance).GetComponent<Rigidbody>();
						component2.AddForceAtPosition(val2 * (component2.mass * fixedDeltaTime), val, (ForceMode)1);
						lastThrust = Time.time;
					}
				}
			}
		}

		private const string FuelKey = "SurtlingBoatsPlus_fuel";

		private const string EnabledKey = "SurtlingBoatsPlus_enabled";

		private static ConfigEntry<float> thrustBack;

		private static ConfigEntry<float> thrustSlow;

		private static ConfigEntry<float> thrustHalf;

		private static ConfigEntry<float> thrustFull;

		private static ConfigEntry<string> item;

		private static ConfigEntry<float> secondsPerCore;

		private static ConfigEntry<KeyCode> toggleKey;

		private static ConfigEntry<bool> showSpeed;

		private static ConfigEntry<SpeedUnit> speedUnit;

		private static ConfigEntry<bool> modEnabled;

		private static ConfigEntry<bool> thrustEnabled;

		private static ConfigEntry<bool> cameraEnabled;

		private static ConfigEntry<float> cameraMaxDistance;

		private static ConfigEntry<float> cameraZoomSpeed;

		private static float vanillaZoomSpeed;

		private static float vanillaMaxDistance;

		private static Image indicator;

		private static TMP_Text speedText;

		private static float lastThrust;

		private void Awake()
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Expected O, but got Unknown
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Expected O, but got Unknown
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Expected O, but got Unknown
			//IL_022e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Expected O, but got Unknown
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0275: Expected O, but got Unknown
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch. When off, the whole mod is inactive and the game behaves as if it were not installed.");
			thrustEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Thrust", "Enabled", true, "Enable the engine: Surtling cores in the boat's chest add thrust, toggled with the toggle key.");
			cameraEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera", "Enabled", true, "Enable the wider camera zoom while steering a boat.");
			thrustBack = ((BaseUnityPlugin)this).Config.Bind<float>("Thrust", "Back", 1.5f, new ConfigDescription("Extra thrust as a multiple of the game's oar force, added on top of normal propulsion (0 = no boost). Reverse.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 8f), Array.Empty<object>()));
			thrustSlow = ((BaseUnityPlugin)this).Config.Bind<float>("Thrust", "Slow", 2f, new ConfigDescription("Extra thrust as a multiple of the game's oar force, added on top of normal propulsion (0 = no boost). Gear 1 (slow).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 8f), Array.Empty<object>()));
			thrustHalf = ((BaseUnityPlugin)this).Config.Bind<float>("Thrust", "Half", 4f, new ConfigDescription("Extra thrust as a multiple of the game's oar force, added on top of normal propulsion (0 = no boost). Gear 2 (half sail).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 15f), Array.Empty<object>()));
			thrustFull = ((BaseUnityPlugin)this).Config.Bind<float>("Thrust", "Full", 8f, new ConfigDescription("Extra thrust as a multiple of the game's oar force, added on top of normal propulsion (0 = no boost). Gear 3 (full sail).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			item = ((BaseUnityPlugin)this).Config.Bind<string>("Fuel", "Item", "$item_surtlingcore", "Item consumed as fuel, given by its localization key ($item_...).");
			secondsPerCore = ((BaseUnityPlugin)this).Config.Bind<float>("Fuel", "SecondsPerCore", 300f, "Seconds of powered travel per item. 0 = free fuel.");
			toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ToggleKey", (KeyCode)304, "Key that switches the engine on and off while steering a boat.");
			showSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowSpeed", true, "Show the boat's speed under the wind indicator while steering.");
			speedUnit = ((BaseUnityPlugin)this).Config.Bind<SpeedUnit>("Display", "SpeedUnit", SpeedUnit.KilometersPerHour, "Unit of the displayed speed.");
			cameraMaxDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "MaxDistance", 20f, new ConfigDescription("How far the camera can zoom out while steering a boat. The game's default is 6.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(6f, 50f), Array.Empty<object>()));
			cameraZoomSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "ZoomSpeed", 2f, new ConfigDescription("Mouse wheel zoom speed multiplier while steering a boat (1 = game default).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 6f), Array.Empty<object>()));
			new Harmony("saymonn.SurtlingBoatsPlus").PatchAll();
		}

		private void Update()
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			Ship val = (Object.op_Implicit((Object)(object)localPlayer) ? localPlayer.GetControlledShip() : null);
			bool num = Object.op_Implicit((Object)(object)val) && modEnabled.Value;
			SetHudVisible(num);
			UpdateCamera(num && cameraEnabled.Value);
			if (num)
			{
				if (thrustEnabled.Value && Input.GetKeyDown(toggleKey.Value) && !Chat.instance.HasFocus() && !Console.IsVisible() && !Menu.IsVisible())
				{
					ZDO zDO = ((Component)val).GetComponent<ZNetView>().GetZDO();
					bool flag = !zDO.GetBool("SurtlingBoatsPlus_enabled", false);
					zDO.Set("SurtlingBoatsPlus_enabled", flag);
					((Character)localPlayer).Message((MessageType)1, flag ? "Surtling engine: on" : "Surtling engine: off", 0, (Sprite)null, false);
				}
				UpdateHud(val);
			}
		}

		private static void UpdateCamera(bool onBoat)
		{
			GameCamera instance = GameCamera.instance;
			if (Object.op_Implicit((Object)(object)instance))
			{
				if (vanillaZoomSpeed == 0f)
				{
					vanillaZoomSpeed = instance.m_zoomSens;
					vanillaMaxDistance = instance.m_maxDistanceBoat;
				}
				instance.m_zoomSens = vanillaZoomSpeed * (onBoat ? cameraZoomSpeed.Value : 1f);
				instance.m_maxDistanceBoat = (onBoat ? cameraMaxDistance.Value : vanillaMaxDistance);
			}
		}

		private static void SetHudVisible(bool visible)
		{
			if (Object.op_Implicit((Object)(object)indicator))
			{
				((Component)indicator).gameObject.SetActive(visible && thrustEnabled.Value);
			}
			if (Object.op_Implicit((Object)(object)speedText))
			{
				((Component)speedText).gameObject.SetActive(visible && showSpeed.Value);
			}
		}

		private static void UpdateHud(Ship ship)
		{
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)indicator))
			{
				indicator = CreateIndicator();
			}
			if (!Object.op_Implicit((Object)(object)speedText))
			{
				speedText = CreateSpeedText();
			}
			if (Object.op_Implicit((Object)(object)indicator))
			{
				((Component)indicator).gameObject.SetActive(thrustEnabled.Value);
				((Graphic)indicator).color = new Color(1f, 1f, 1f, (Time.time - lastThrust < 0.2f) ? 1f : 0.3f);
			}
			if (Object.op_Implicit((Object)(object)speedText))
			{
				((Component)speedText).gameObject.SetActive(showSpeed.Value);
				bool flag = speedUnit.Value == SpeedUnit.MilesPerHour;
				float num = Mathf.Abs(ship.GetSpeed());
				speedText.text = (num * (flag ? 2.23694f : 3.6f)).ToString("0") + (flag ? " mph" : " km/h");
			}
		}

		private static Image CreateIndicator()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = ObjectDB.instance.m_items.Find((GameObject i) => i.GetComponent<ItemDrop>()?.m_itemData.m_shared.m_name == item.Value);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return null;
			}
			Image obj = ((Component)CreateHudElement("SurtlingBoatsPlusIndicator", new Vector2(0f, -50f), new Vector2(30f, 30f))).gameObject.AddComponent<Image>();
			obj.sprite = val.GetComponent<ItemDrop>().m_itemData.GetIcon();
			return obj;
		}

		private static TMP_Text CreateSpeedText()
		{
			//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)
			TextMeshProUGUI obj = ((Component)CreateHudElement("SurtlingBoatsPlusSpeed", new Vector2(0f, -80f), new Vector2(120f, 24f))).gameObject.AddComponent<TextMeshProUGUI>();
			((TMP_Text)obj).font = Hud.instance.m_staminaText.font;
			((TMP_Text)obj).fontSize = 20f;
			((TMP_Text)obj).alignment = (TextAlignmentOptions)514;
			((Graphic)obj).raycastTarget = false;
			return (TMP_Text)(object)obj;
		}

		private static RectTransform CreateHudElement(string name, Vector2 offset, Vector2 size)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: 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_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: 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_005c: 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)
			RectTransform shipWindIndicatorRoot = Hud.instance.m_shipWindIndicatorRoot;
			RectTransform val = (RectTransform)((Transform)shipWindIndicatorRoot).parent;
			RectTransform component = new GameObject(name, new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
			((Transform)component).SetParent((Transform)(object)val, false);
			component.sizeDelta = size;
			((Transform)component).position = ((Transform)shipWindIndicatorRoot).position + Vector2.op_Implicit(offset) * ((Transform)val).lossyScale.x;
			return component;
		}

		private static float ThrustFor(Speed speed)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected I4, but got Unknown
			return (speed - 1) switch
			{
				0 => thrustBack.Value, 
				1 => thrustSlow.Value, 
				2 => thrustHalf.Value, 
				3 => thrustFull.Value, 
				_ => 0f, 
			};
		}

		private static bool ConsumeFuel(Ship ship, ZDO zdo, float dt)
		{
			if (secondsPerCore.Value <= 0f)
			{
				return true;
			}
			float num = zdo.GetFloat("SurtlingBoatsPlus_fuel", 0f);
			if (num <= 0f)
			{
				Container componentInChildren = ((Component)ship).GetComponentInChildren<Container>();
				Inventory val = ((componentInChildren != null) ? componentInChildren.GetInventory() : null);
				if (val == null || !val.HaveItem(item.Value, true))
				{
					return false;
				}
				val.RemoveItem(item.Value, 1, -1, true);
				num = secondsPerCore.Value;
			}
			zdo.Set("SurtlingBoatsPlus_fuel", num - dt);
			return true;
		}
	}
}