Decompiled source of Underwater v1.0.8

Underwater.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using CrystalLib;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Underwater")]
[assembly: AssemblyDescription("Underwater")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("Underwater")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3cdab7b7-951c-4013-8b57-7931e76589d4")]
[assembly: AssemblyFileVersion("1.0.8.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.8.0")]
namespace Underwater;

[BepInPlugin("dev.crystal.underwater", "Underwater", "1.0.7.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class UnderwaterPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Character))]
	private static class Character_Patches
	{
		[HarmonyPatch("InLiquidDepth")]
		[HarmonyPrefix]
		private static bool InLiquidDepth_Prefix(Character __instance, ref float __result)
		{
			__result = 0f;
			if (__instance.IsPlayer())
			{
				return PlayerSwims.Value;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(GameCamera))]
	private static class GameCamera_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Checking,
			Searching2,
			Checking2,
			Updating,
			Finishing
		}

		[HarmonyPatch("GetCameraPosition")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> GetCameraPosition_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			TranspilerState state = TranspilerState.Searching;
			CodeInstruction instruction2 = null;
			CodeInstruction instruction3 = null;
			CodeInstruction instruction4 = null;
			foreach (CodeInstruction instruction in instructions)
			{
				switch (state)
				{
				case TranspilerState.Searching:
					if (instruction.opcode == OpCodes.Ldloc_S)
					{
						instruction2 = instruction;
						state = TranspilerState.Checking;
					}
					else
					{
						yield return instruction;
					}
					break;
				case TranspilerState.Checking:
					if (instruction4 != null && instruction.opcode == OpCodes.Call && ((MethodInfo)instruction.operand).Name.Equals("GetLiquidLevel"))
					{
						CodeInstruction val;
						instruction4 = (val = null);
						instruction3 = (val = val);
						instruction2 = val;
						state = TranspilerState.Searching2;
						break;
					}
					if (instruction3 != null && instruction.opcode == OpCodes.Ldc_I4_S && (sbyte)instruction.operand == 10)
					{
						instruction4 = instruction;
						break;
					}
					if (instruction.opcode == OpCodes.Ldc_R4 && (float)instruction.operand == 1f)
					{
						instruction3 = instruction;
						break;
					}
					yield return instruction2;
					instruction2 = null;
					if (instruction3 != null)
					{
						yield return instruction3;
						instruction3 = null;
					}
					if (instruction4 != null)
					{
						yield return instruction4;
						instruction4 = null;
					}
					yield return instruction;
					state = TranspilerState.Searching;
					break;
				case TranspilerState.Searching2:
					if (instruction.opcode == OpCodes.Ldarg_0)
					{
						instruction2 = instruction;
						state = TranspilerState.Checking2;
					}
					break;
				case TranspilerState.Checking2:
					if (instruction.opcode == OpCodes.Ldc_I4_0)
					{
						instruction3 = instruction;
						state = TranspilerState.Updating;
					}
					else
					{
						instruction2 = null;
						state = TranspilerState.Searching2;
					}
					break;
				case TranspilerState.Updating:
					yield return instruction2;
					yield return instruction3;
					yield return instruction;
					state = TranspilerState.Finishing;
					break;
				case TranspilerState.Finishing:
					yield return instruction;
					break;
				}
			}
		}
	}

	public const string ModId = "dev.crystal.underwater";

	public static ConfigEntry<bool> PlayerSwims;

	public static ConfigEntry<bool> CameraIgnoreWater;

	public static ConfigEntry<Key> ToggleSwimKey;

	private static InputBinding sToggleSwimBinding;

	private static Harmony sCharacterHarmony;

	private static Harmony sGameCameraHarmony;

	private void Awake()
	{
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Expected O, but got Unknown
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: 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
		PlayerSwims = ((BaseUnityPlugin)this).Config.Bind<bool>("Underwater", "PlayerSwims", true, "Whether players should swim or ignore water, walking along the terrain beneath it. Can also be toggled by pressing Backspace. Game default true.");
		PlayerSwims.SettingChanged += IgnoreWater_SettingChanged;
		CameraIgnoreWater = ((BaseUnityPlugin)this).Config.Bind<bool>("Underwater", "CameraIgnoreWater", false, "Whether the camera should ignore water, allowing it to move beneath the surface. This setting is implied true if PlayerSwims is false. Game default false.");
		CameraIgnoreWater.SettingChanged += IgnoreWater_SettingChanged;
		ToggleSwimKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Underwater", "ToggleSwimKey", (Key)65, "Binds a shortcut key for toggling the PlayerSwims option.");
		sToggleSwimBinding = new InputBinding("ToggleSwim", ToggleSwimKey);
		sToggleSwimBinding.InputPressed += ToggleSwimBinding_InputPressed;
		sCharacterHarmony = new Harmony("dev.crystal.underwater_Character");
		sGameCameraHarmony = new Harmony("dev.crystal.underwater_GameCamera");
		sCharacterHarmony.PatchAll(typeof(Character_Patches));
		if (!PlayerSwims.Value || CameraIgnoreWater.Value)
		{
			sGameCameraHarmony.PatchAll(typeof(GameCamera_Patches));
		}
	}

	private void OnDestroy()
	{
		sCharacterHarmony.UnpatchSelf();
		sGameCameraHarmony.UnpatchSelf();
		sToggleSwimBinding.Dispose();
	}

	private void IgnoreWater_SettingChanged(object sender, EventArgs e)
	{
		sGameCameraHarmony.UnpatchSelf();
		if (!PlayerSwims.Value || CameraIgnoreWater.Value)
		{
			sGameCameraHarmony.PatchAll(typeof(GameCamera_Patches));
		}
	}

	private void ToggleSwimBinding_InputPressed(object sender, InputEventArgs e)
	{
		PlayerSwims.Value = !PlayerSwims.Value;
		((Character)e.Player).Message((MessageType)1, PlayerSwims.Value ? "Swimming On" : "Swimming Off", 0, (Sprite)null);
	}
}