Decompiled source of MyBuildCamera v1.0.12

BuildCamera.dll

Decompiled a month 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.12")]
public class BuildCameraBase : BaseUnityPlugin
{
	[HarmonyPatch(typeof(GameCamera), "UpdateCamera")]
	private 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 (CameraActive)
			{
				((Component)__instance).transform.position = CameraPosition;
				((Component)__instance).transform.rotation = CameraRotation;
				if (tmpPosField != null)
				{
					tmpPosField.SetValue(__instance, CameraPosition);
				}
				if (tmpRotField != null)
				{
					tmpRotField.SetValue(__instance, CameraRotation);
				}
				return false;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(PlayerController), "TakeInput")]
	private static class PlayerController_Patch
	{
		private static bool Prefix()
		{
			return !CameraActive;
		}
	}

	[HarmonyPatch(typeof(Player), "UpdatePlacement")]
	private static class Player_UpdatePlacement_Patch
	{
		private static void Prefix(Player __instance, ref Vector3 ___m_lookDir, ref float ___m_maxPlaceDistance)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: 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_0076: Unknown result type (might be due to invalid IL or missing references)
			if (!CameraActive)
			{
				return;
			}
			bool num = (Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus();
			bool flag = Console.IsVisible();
			if (!num && !flag)
			{
				___m_lookDir = CameraRotation * Vector3.forward;
				___m_maxPlaceDistance = 100f;
				if ((Object)(object)((Character)__instance).m_eye != (Object)null)
				{
					((Component)((Character)__instance).m_eye).transform.position = CameraPosition;
					((Component)((Character)__instance).m_eye).transform.rotation = CameraRotation;
				}
			}
		}
	}

	[HarmonyPatch(typeof(Player), "LateUpdate")]
	private 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 (CameraActive && EyePositionSaved && (Object)(object)((Character)__instance).m_eye != (Object)null)
			{
				((Component)((Character)__instance).m_eye).transform.localPosition = OriginalEyePosition;
			}
		}
	}

	[HarmonyPatch(typeof(SE_Stats), "UpdateStatusEffect")]
	private static class SE_DemisterCamera_Patch
	{
		private static void Postfix(SE_Stats __instance, Character ___m_character)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			if (CameraActive && EnableDemisterCheck.Value && ((Object)__instance).name == "SE_Demister" && (Object)(object)Player.m_localPlayer != (Object)null && (Object)(object)___m_character == (Object)(object)Player.m_localPlayer)
			{
				GameObject val = GameObject.Find("Demister(Clone)");
				if ((Object)(object)val != (Object)null)
				{
					val.transform.position = CameraPosition;
				}
			}
		}
	}

	[HarmonyPatch(typeof(ZNetScene), "Update")]
	private static class ZNetScene_Culling_Patch
	{
		private static Vector3 originalPlayerPos;

		private static bool positionShifted;

		private static void Prefix()
		{
			//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)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			if (CameraActive && (Object)(object)Player.m_localPlayer != (Object)null)
			{
				originalPlayerPos = ((Component)Player.m_localPlayer).transform.position;
				((Component)Player.m_localPlayer).transform.position = CameraPosition;
				positionShifted = true;
			}
		}

		private static void Postfix()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			if (positionShifted && (Object)(object)Player.m_localPlayer != (Object)null)
			{
				((Component)Player.m_localPlayer).transform.position = originalPlayerPos;
				positionShifted = false;
			}
		}
	}

	private static Harmony harmony;

	public static ConfigEntry<float> MoveSpeedMultiplier;

	public static ConfigEntry<float> ResourcePickupRange;

	public static ConfigEntry<KeyCode> ToggleKey;

	public static ConfigEntry<bool> EnableDemisterCheck;

	public static ConfigEntry<bool> EnableCameraLight;

	public static ConfigEntry<float> CameraLightIntensity;

	public static ConfigEntry<float> CameraLightRange;

	public static ConfigEntry<bool> InvertMouseY;

	public static ConfigEntry<float> MouseSensitivity;

	public static bool CameraActive = false;

	public static Vector3 CameraPosition;

	public static Quaternion CameraRotation;

	public static Vector3 OriginalEyePosition;

	public static bool EyePositionSaved = false;

	private static GameObject cameraLightObject;

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

	private static float pickupTimer = 0f;

	private void Awake()
	{
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Expected O, but got Unknown
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "Toggle build mode", (KeyCode)98, "The key used to toggle the build camera.");
		MoveSpeedMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Camera Move Speed Multiplier", 3f, "Multiplier for camera flight and movement speed.");
		ResourcePickupRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Resource Pickup Range", 10f, "The radius around the camera within which items are actively picked up.");
		EnableDemisterCheck = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable Camera Demister", true, "Enables Wisplight demister functionality around the flying camera position.");
		EnableCameraLight = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera Light", "Enable Light", true, "Enables a building flashlight attached to the camera.");
		CameraLightIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Camera Light", "Light Intensity", 1.5f, "The brightness intensity of the camera flashlight.");
		CameraLightRange = ((BaseUnityPlugin)this).Config.Bind<float>("Camera Light", "Light Range", 40f, "How far the camera flashlight shines.");
		InvertMouseY = ((BaseUnityPlugin)this).Config.Bind<bool>("Controls & Comfort", "Invert Mouse Y", false, "Inverts the vertical mouse lookup controls.");
		MouseSensitivity = ((BaseUnityPlugin)this).Config.Bind<float>("Controls & Comfort", "Mouse Sensitivity Multiplier", 1f, "Adjusts the mouse look sensitivity while using the camera.");
		harmony = new Harmony("EndosBuildCamera");
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"My Build Camera Has succesful 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_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Invalid comparison between Unknown and I4
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: 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 = false;
		if (val != null)
		{
			bool num = (int)val.m_shared.m_itemType == 19;
			bool flag2 = (Object)(object)val.m_dropPrefab != (Object)null && ((Object)val.m_dropPrefab).name == "Hoe";
			flag = num || flag2;
		}
		if (CameraActive && !flag)
		{
			DeactivateBuildCamera();
			((Character)Player.m_localPlayer).Message((MessageType)1, "Build Camera: Disabled", 0, (Sprite)null, false);
			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;
				UpdateCameraLight();
				((Character)Player.m_localPlayer).Message((MessageType)2, "Build Camera: Enabled", 0, (Sprite)null, false);
			}
			else
			{
				DeactivateBuildCamera();
				((Character)Player.m_localPlayer).Message((MessageType)2, "Build Camera: Disabled", 0, (Sprite)null, false);
			}
		}
		if (CameraActive && (!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !Console.IsVisible())
		{
			MoveCamera();
			UpdateCameraLight();
			pickupTimer += Time.deltaTime;
			if (pickupTimer >= 0.3f)
			{
				ExecuteCameraAutoPickup();
				pickupTimer = 0f;
			}
		}
	}

	private void DeactivateBuildCamera()
	{
		//IL_0079: 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_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		CameraActive = false;
		UpdateCameraLight();
		if (!((Object)(object)Player.m_localPlayer == (Object)null))
		{
			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;
				typeof(GameCamera).GetField("m_tmpPos", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(instance, ((Component)((Character)Player.m_localPlayer).m_eye).transform.position);
				typeof(GameCamera).GetField("m_tmpRot", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(instance, ((Component)((Character)Player.m_localPlayer).m_eye).transform.rotation);
			}
		}
	}

	private void MoveCamera()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: 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_0047: 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_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: 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_007a: 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_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: 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_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: 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_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: 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_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Invalid comparison between Unknown and I4
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		float num = 5f * MoveSpeedMultiplier.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") * MouseSensitivity.Value;
			float num3 = Input.GetAxisRaw("Mouse Y") * MouseSensitivity.Value;
			Vector3 eulerAngles = ((Quaternion)(ref CameraRotation)).eulerAngles;
			eulerAngles.x += (InvertMouseY.Value ? (num3 * 2f) : ((0f - num3) * 2f));
			eulerAngles.y += num2 * 2f;
			eulerAngles.z = 0f;
			CameraRotation = Quaternion.Euler(eulerAngles);
		}
	}

	private void UpdateCameraLight()
	{
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Expected O, but got Unknown
		if (CameraActive && EnableCameraLight.Value)
		{
			if ((Object)(object)cameraLightObject == (Object)null)
			{
				cameraLightObject = new GameObject("BuildCameraLight");
				Light obj = cameraLightObject.AddComponent<Light>();
				obj.type = (LightType)0;
				obj.spotAngle = 60f;
			}
			Light component = cameraLightObject.GetComponent<Light>();
			component.intensity = CameraLightIntensity.Value;
			component.range = CameraLightRange.Value;
			cameraLightObject.transform.position = CameraPosition;
			cameraLightObject.transform.rotation = CameraRotation;
		}
		else if ((Object)(object)cameraLightObject != (Object)null)
		{
			Object.Destroy((Object)(object)cameraLightObject);
			cameraLightObject = null;
		}
	}

	private void ExecuteCameraAutoPickup()
	{
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Invalid comparison between Unknown and I4
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Invalid comparison between Unknown and I4
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Invalid comparison between Unknown and I4
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Invalid comparison between Unknown and I4
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			return;
		}
		ItemDrop[] array = Object.FindObjectsByType<ItemDrop>((FindObjectsSortMode)0);
		foreach (ItemDrop val in array)
		{
			if ((Object)(object)val == (Object)null || val.m_itemData == null || val.m_itemData.m_shared == null)
			{
				continue;
			}
			ZNetView component = ((Component)val).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid() || (component.GetZDO() != null && (component.GetZDO().GetLong("crafterID", 0L) != 0L || component.GetZDO().GetBool("cooked", false) || component.GetZDO().GetLong("InWorldUi", 0L) != 0)) || (Object)(object)((Component)val).GetComponentInParent<ItemStand>() != (Object)null)
			{
				continue;
			}
			string text = ((Object)((Component)val).gameObject).name.ToLower();
			ItemType itemType = val.m_itemData.m_shared.m_itemType;
			if (((int)itemType == 1 || (int)itemType == 13 || (int)itemType == 2 || (int)itemType == 9 || text.Contains("wood") || text.Contains("stone") || text.Contains("iron") || text.Contains("ore")) && Vector3.Distance(CameraPosition, ((Component)val).transform.position) <= ResourcePickupRange.Value && ((Humanoid)Player.m_localPlayer).GetInventory().CanAddItem(val.m_itemData, -1))
			{
				if (!component.IsOwner())
				{
					component.ClaimOwnership();
				}
				((Component)val).transform.position = ((Component)Player.m_localPlayer).transform.position;
				((Humanoid)Player.m_localPlayer).Pickup(((Component)val).gameObject, true, true);
			}
		}
	}
}