Decompiled source of MyBuildCamera v1.0.5

BuildCamera.dll

Decompiled 7 hours ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
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: AssemblyTitle("BuildCamera")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("FSOS-X R10 EXP")]
[assembly: AssemblyProduct("BuildCamera")]
[assembly: AssemblyCopyright("Copyright © FSOS-X R10 EXP 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f31d020b-6e67-4d10-93ed-c312d3bc5216")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MyValheimBuildCamera;

[BepInPlugin("EndosBuildCamera", "My Build Camera", "1.0.5")]
public class BuildCameraBase : BaseUnityPlugin
{
	private const string ModGUID = "EndosBuildCamera";

	private const string ModName = "My Build Camera";

	private const string ModVersion = "1.0.5";

	private static Harmony harmony;

	public static ConfigEntry<KeyCode> ToggleKey;

	public static ConfigEntry<float> MoveSpeed;

	public static bool CameraActive = false;

	public static Vector3 CameraPosition;

	public static Quaternion CameraRotation;

	public static Vector3 OriginalEyePosition;

	public static bool EyePositionSaved = false;

	private static FieldInfo rightItemField = typeof(Humanoid).GetField("m_rightItem", BindingFlags.Instance | BindingFlags.NonPublic);

	private static FieldInfo maxPlaceDistanceField = typeof(Player).GetField("m_maxPlaceDistance", BindingFlags.Instance | BindingFlags.NonPublic);

	private void Awake()
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ActivationKey", (KeyCode)98, "The key used to toggle the build camera on and off.");
		MoveSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "FlightSpeed", 20f, "How fast the camera moves.");
		harmony = new Harmony("EndosBuildCamera");
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"My Build Camera v1.0.5 successfully started!");
	}

	private void Update()
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Invalid comparison between Unknown and I4
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: 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_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			return;
		}
		ItemData val = null;
		if (rightItemField != null)
		{
			val = (ItemData)rightItemField.GetValue(Player.m_localPlayer);
		}
		bool flag = val != null && (int)val.m_shared.m_itemType == 19;
		if (CameraActive && !flag)
		{
			DeactivateBuildCamera();
			return;
		}
		if (flag && Input.GetKeyDown(ToggleKey.Value))
		{
			CameraActive = !CameraActive;
			if (CameraActive)
			{
				if ((Object)(object)((Character)Player.m_localPlayer).m_eye != (Object)null)
				{
					OriginalEyePosition = ((Component)((Character)Player.m_localPlayer).m_eye).transform.localPosition;
					EyePositionSaved = true;
				}
				CameraPosition = ((Component)((Character)Player.m_localPlayer).m_eye).transform.position;
				CameraRotation = ((Component)((Character)Player.m_localPlayer).m_eye).transform.rotation;
			}
			else
			{
				DeactivateBuildCamera();
			}
		}
		if (CameraActive)
		{
			MoveCamera();
			if (maxPlaceDistanceField != null)
			{
				maxPlaceDistanceField.SetValue(Player.m_localPlayer, 500f);
			}
		}
	}

	private void DeactivateBuildCamera()
	{
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		CameraActive = false;
		if ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			return;
		}
		if (maxPlaceDistanceField != null)
		{
			maxPlaceDistanceField.SetValue(Player.m_localPlayer, 5f);
		}
		if (EyePositionSaved && (Object)(object)((Character)Player.m_localPlayer).m_eye != (Object)null)
		{
			((Component)((Character)Player.m_localPlayer).m_eye).transform.localPosition = OriginalEyePosition;
			EyePositionSaved = false;
		}
		GameCamera instance = GameCamera.instance;
		if ((Object)(object)instance != (Object)null)
		{
			((Component)instance).transform.position = ((Component)((Character)Player.m_localPlayer).m_eye).transform.position;
			((Component)instance).transform.rotation = ((Component)((Character)Player.m_localPlayer).m_eye).transform.rotation;
			FieldInfo field = typeof(GameCamera).GetField("m_tmpPos", BindingFlags.Instance | BindingFlags.NonPublic);
			FieldInfo field2 = typeof(GameCamera).GetField("m_tmpRot", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				field.SetValue(instance, ((Component)((Character)Player.m_localPlayer).m_eye).transform.position);
			}
			if (field2 != null)
			{
				field2.SetValue(instance, ((Component)((Character)Player.m_localPlayer).m_eye).transform.rotation);
			}
		}
	}

	private void MoveCamera()
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: 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_00a2: 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_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Invalid comparison between Unknown and I4
		//IL_019b: 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_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		float axis = Input.GetAxis("Mouse ScrollWheel");
		if (Mathf.Abs(axis) > 0.01f)
		{
			MoveSpeed.Value = Mathf.Clamp(MoveSpeed.Value + axis * 50f, 1f, 100f);
		}
		float num = MoveSpeed.Value * Time.deltaTime;
		if (Input.GetKey((KeyCode)304) || Input.GetKey((KeyCode)303))
		{
			num *= 2.5f;
		}
		Vector3 val = Vector3.zero;
		if (Input.GetKey((KeyCode)119))
		{
			val += CameraRotation * Vector3.forward;
		}
		if (Input.GetKey((KeyCode)115))
		{
			val += CameraRotation * Vector3.back;
		}
		if (Input.GetKey((KeyCode)97))
		{
			val += CameraRotation * Vector3.left;
		}
		if (Input.GetKey((KeyCode)100))
		{
			val += CameraRotation * Vector3.right;
		}
		if (Input.GetKey((KeyCode)32))
		{
			val += Vector3.up;
		}
		if (Input.GetKey((KeyCode)306))
		{
			val += Vector3.down;
		}
		CameraPosition += val * num;
		if (!InventoryGui.IsVisible() && !Menu.IsVisible() && !Hud.IsPieceSelectionVisible() && (int)Cursor.lockState == 1)
		{
			float num2 = Input.GetAxisRaw("Mouse X");
			float num3 = Input.GetAxisRaw("Mouse Y");
			if (Mathf.Abs(num2) < 0.05f)
			{
				num2 = 0f;
			}
			if (Mathf.Abs(num3) < 0.05f)
			{
				num3 = 0f;
			}
			Vector3 eulerAngles = ((Quaternion)(ref CameraRotation)).eulerAngles;
			eulerAngles.x -= num3 * 2f;
			eulerAngles.y += num2 * 2f;
			eulerAngles.z = 0f;
			CameraRotation = Quaternion.Euler(eulerAngles);
		}
	}
}
[HarmonyPatch(typeof(GameCamera), "UpdateCamera")]
internal static class GameCamera_Patch
{
	private static FieldInfo tmpPosField = typeof(GameCamera).GetField("m_tmpPos", BindingFlags.Instance | BindingFlags.NonPublic);

	private static FieldInfo tmpRotField = typeof(GameCamera).GetField("m_tmpRot", BindingFlags.Instance | BindingFlags.NonPublic);

	private static bool Prefix(GameCamera __instance)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: 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)
		if (BuildCameraBase.CameraActive)
		{
			((Component)__instance).transform.position = BuildCameraBase.CameraPosition;
			((Component)__instance).transform.rotation = BuildCameraBase.CameraRotation;
			if (tmpPosField != null)
			{
				tmpPosField.SetValue(__instance, BuildCameraBase.CameraPosition);
			}
			if (tmpRotField != null)
			{
				tmpRotField.SetValue(__instance, BuildCameraBase.CameraRotation);
			}
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(PlayerController), "TakeInput")]
internal static class PlayerController_Patch
{
	private static bool Prefix()
	{
		if (BuildCameraBase.CameraActive)
		{
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(Player), "UpdatePlacement")]
internal static class Player_UpdatePlacement_Patch
{
	private static void Prefix(Player __instance, ref Vector3 ___m_lookDir)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: 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_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		if (BuildCameraBase.CameraActive)
		{
			___m_lookDir = BuildCameraBase.CameraRotation * Vector3.forward;
			if ((Object)(object)((Character)__instance).m_eye != (Object)null)
			{
				((Component)((Character)__instance).m_eye).transform.position = BuildCameraBase.CameraPosition;
				((Component)((Character)__instance).m_eye).transform.rotation = BuildCameraBase.CameraRotation;
			}
			Vector3 eulerAngles = ((Quaternion)(ref BuildCameraBase.CameraRotation)).eulerAngles;
			((Component)__instance).transform.rotation = Quaternion.Euler(0f, eulerAngles.y, 0f);
		}
	}
}
[HarmonyPatch(typeof(Player), "LateUpdate")]
internal static class Player_LateUpdate_Patch
{
	private static void Postfix(Player __instance)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		if (BuildCameraBase.CameraActive && BuildCameraBase.EyePositionSaved && (Object)(object)((Character)__instance).m_eye != (Object)null)
		{
			((Component)((Character)__instance).m_eye).transform.localPosition = BuildCameraBase.OriginalEyePosition;
		}
	}
}
[HarmonyPatch(typeof(Player), "AutoPickup")]
internal static class Player_AutoPickup_Patch
{
	private static Vector3 originalCharacterPosition;

	private static void Prefix(Player __instance)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		if (BuildCameraBase.CameraActive)
		{
			originalCharacterPosition = ((Component)__instance).transform.position;
			((Component)__instance).transform.position = BuildCameraBase.CameraPosition;
		}
	}

	private static void Postfix(Player __instance)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		if (BuildCameraBase.CameraActive)
		{
			((Component)__instance).transform.position = originalCharacterPosition;
		}
	}
}