Decompiled source of RowingCrew v1.0.0

BepInEx/plugins/RowingCrew/RowingCrew.dll

Decompiled 4 days 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 HarmonyLib;
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("RowingCrew")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+88a978ccf2ac96488ec027d6531d8be53cdbc27f")]
[assembly: AssemblyProduct("RowingCrew")]
[assembly: AssemblyTitle("RowingCrew")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RowingCrew;

[BepInPlugin("local.valheim.rowingcrew", "Rowing Crew", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Ship), "CustomFixedUpdate")]
	private static class RowingPatch
	{
		[HarmonyPrefix]
		private static void Prefix(Ship __instance, List<Player> ___m_players, out float __state)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Invalid comparison between Unknown and I4
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Invalid comparison between Unknown and I4
			__state = __instance.m_backwardForce;
			Speed speedSetting = __instance.GetSpeedSetting();
			if ((int)speedSetting == 2 || (int)speedSetting == 1)
			{
				float num = SpeedFor((SimulatedCrew > 0) ? SimulatedCrew : CountCrew(___m_players));
				__instance.m_backwardForce *= num * num;
			}
		}

		[HarmonyFinalizer]
		private static void Finalizer(Ship __instance, float __state)
		{
			__instance.m_backwardForce = __state;
		}
	}

	private static ConfigEntry<float> speedWithTwo;

	private static ConfigEntry<float> speedWithThree;

	private static ConfigEntry<float> speedWithFourOrMore;

	private static int requestedCrew;

	private static int SimulatedCrew
	{
		get
		{
			if (!Terminal.m_cheat)
			{
				return 0;
			}
			return requestedCrew;
		}
	}

	private void Awake()
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Expected O, but got Unknown
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		speedWithTwo = BindSpeed("SpeedWith2Players", 2f);
		speedWithThree = BindSpeed("SpeedWith3Players", 2.5f);
		speedWithFourOrMore = BindSpeed("SpeedWith4OrMorePlayers", 3f);
		new Harmony("local.valheim.rowingcrew").PatchAll(typeof(Plugin).Assembly);
		new ConsoleCommand("rowingcrew", "[players | off] show rowing status, or simulate a crew size (needs devcommands)", new ConsoleEvent(RunCommand), false, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Extra players aboard now speed up paddling.");
	}

	private static void RunCommand(ConsoleEventArgs args)
	{
		if (args.Length > 1)
		{
			if (!Terminal.m_cheat)
			{
				args.Context.AddString("Run devcommands first to simulate a crew.");
				return;
			}
			if (args[1].ToLowerInvariant() == "off")
			{
				requestedCrew = 0;
			}
			else
			{
				int num = default(int);
				if (!args.TryParameterInt(1, ref num) || num < 1)
				{
					args.Context.AddString("Usage: rowingcrew [players | off]");
					return;
				}
				requestedCrew = num;
			}
		}
		args.Context.AddString(Status());
	}

	private static string Status()
	{
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		string text = ((SimulatedCrew > 0) ? $" (simulating {SimulatedCrew})" : "");
		Ship localShip = Ship.GetLocalShip();
		if ((Object)(object)localShip == (Object)null)
		{
			return "Not aboard a ship" + text + ".";
		}
		int num = 0;
		foreach (Player allPlayer in Player.GetAllPlayers())
		{
			if (localShip.IsPlayerInBoat(allPlayer))
			{
				num++;
			}
		}
		float num2 = SpeedFor((SimulatedCrew > 0) ? SimulatedCrew : num);
		return $"Aboard: {num}{text}. Paddling multiplier: x{num2:0.##}. Setting: {localShip.GetSpeedSetting()}. Speed: {localShip.GetSpeed():0.00} m/s. Ship owner: {localShip.IsOwner()}.";
	}

	private static float SpeedFor(int crew)
	{
		return SpeedMultiplier(crew, speedWithTwo.Value, speedWithThree.Value, speedWithFourOrMore.Value);
	}

	private ConfigEntry<float> BindSpeed(string key, float defaultSpeed)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		return ((BaseUnityPlugin)this).Config.Bind<float>("Rowing", key, defaultSpeed, new ConfigDescription("Paddling speed as a multiple of vanilla speed.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 5f), Array.Empty<object>()));
	}

	internal static int CountCrew(List<Player> players)
	{
		if (players == null)
		{
			return 0;
		}
		int num = 0;
		for (int i = 0; i < players.Count; i++)
		{
			if ((Object)(object)players[i] == (Object)null)
			{
				continue;
			}
			bool flag = false;
			for (int j = 0; j < i; j++)
			{
				if (flag)
				{
					break;
				}
				flag = (Object)(object)players[j] == (Object)(object)players[i];
			}
			if (!flag)
			{
				num++;
			}
		}
		return num;
	}

	internal static float SpeedMultiplier(int crew, float withTwo, float withThree, float withFourOrMore)
	{
		if (crew <= 1)
		{
			return 1f;
		}
		float num = crew switch
		{
			3 => withThree, 
			2 => withTwo, 
			_ => withFourOrMore, 
		};
		if (num > 1f && !float.IsInfinity(num))
		{
			return num;
		}
		return 1f;
	}
}