Decompiled source of GrillspettFlattenEarth v1.0.0

GrillspettFlattenEarth.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: AssemblyCompany("GrillspettFlattenEarth")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GrillspettFlattenEarth")]
[assembly: AssemblyTitle("GrillspettFlattenEarth")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 GrillspettFlattenEarth
{
	[BepInPlugin("grillspett.valheim.flattenearth", "Grillspett Flatten Earth", "1.0.0")]
	public class FlattenEarthPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "grillspett.valheim.flattenearth";

		public const string PluginName = "Grillspett Flatten Earth";

		public const string PluginVersion = "1.0.0";

		private static ConfigEntry<KeyCode> _flattenKey;

		private static ConfigEntry<KeyCode> _increaseRadiusKey;

		private static ConfigEntry<KeyCode> _decreaseRadiusKey;

		private static ConfigEntry<float> _radius;

		private static ConfigEntry<float> _radiusStep;

		private static ConfigEntry<bool> _squareArea;

		private static ConfigEntry<float> _staminaCost;

		private static ConfigEntry<float> _cooldown;

		private static readonly FieldInfo PlacementGhostField = typeof(Player).GetField("m_placementGhost", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo PlacementStatusField = typeof(Player).GetField("m_placementStatus", BindingFlags.Instance | BindingFlags.NonPublic);

		private static ManualLogSource _log;

		private float _lastUseTime = float.NegativeInfinity;

		private void Awake()
		{
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Expected O, but got Unknown
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Expected O, but got Unknown
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			_log = ((BaseUnityPlugin)this).Logger;
			_flattenKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "FlattenKey", (KeyCode)278, "Key to press while the Hoe's Level tool is selected. Flattens a large area around the cursor to the terrain height under the cursor.");
			_increaseRadiusKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "IncreaseRadiusKey", (KeyCode)280, "Key to press to increase the flatten radius.");
			_decreaseRadiusKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "DecreaseRadiusKey", (KeyCode)281, "Key to press to decrease the flatten radius.");
			_radius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Radius", 20f, new ConfigDescription("Radius in meters of the area to flatten.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(2f, 100f), Array.Empty<object>()));
			_radiusStep = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RadiusStep", 5f, new ConfigDescription("How much the radius changes per press of the increase/decrease radius keys.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 50f), Array.Empty<object>()));
			_squareArea = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SquareArea", false, "Flatten a square area instead of a circle.");
			_staminaCost = ((BaseUnityPlugin)this).Config.Bind<float>("General", "StaminaCost", 0f, new ConfigDescription("Stamina consumed per use. 0 disables the stamina requirement entirely.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			_cooldown = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CooldownSeconds", 0.3f, "Minimum time in seconds between two uses.");
			if (PlacementGhostField == null || PlacementStatusField == null)
			{
				_log.LogError((object)"Could not find Player's internal placement fields - the game must have updated in a way that broke this mod. Flatten hotkey disabled.");
			}
			_log.LogInfo((object)string.Format("{0} {1} loaded. Hotkey: {2}", "Grillspett Flatten Earth", "1.0.0", _flattenKey.Value));
		}

		private void Update()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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)
			if (PlacementGhostField == null || PlacementStatusField == null)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null))
			{
				if (Input.GetKeyDown(_increaseRadiusKey.Value))
				{
					ChangeRadius(localPlayer, _radiusStep.Value);
				}
				else if (Input.GetKeyDown(_decreaseRadiusKey.Value))
				{
					ChangeRadius(localPlayer, 0f - _radiusStep.Value);
				}
				if (Input.GetKeyDown(_flattenKey.Value) && !(Time.time - _lastUseTime < _cooldown.Value))
				{
					TryFlatten(localPlayer);
				}
			}
		}

		private static void ChangeRadius(Player player, float delta)
		{
			ConfigEntry<float> radius = _radius;
			radius.Value += delta;
			((Character)player).Message((MessageType)1, $"Flatten radius: {_radius.Value:0}m", 0, (Sprite)null, false);
		}

		private void TryFlatten(Player player)
		{
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: 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_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			if (!((Character)player).InPlaceMode())
			{
				return;
			}
			Piece selectedPiece = player.GetSelectedPiece();
			if ((Object)(object)selectedPiece == (Object)null)
			{
				return;
			}
			GameObject gameObject = ((Component)selectedPiece).gameObject;
			TerrainModifier componentInChildren = gameObject.GetComponentInChildren<TerrainModifier>(true);
			TerrainOp componentInChildren2 = gameObject.GetComponentInChildren<TerrainOp>(true);
			if ((Object)(object)componentInChildren == (Object)null && (Object)(object)componentInChildren2 == (Object)null)
			{
				((Character)player).Message((MessageType)2, "Select one of the Hoe's ground tools first (e.g. Raise/Level Ground)", 0, (Sprite)null, false);
				_log.LogInfo((object)("[Diagnostics] Selected piece '" + ((Object)gameObject).name + "' ('" + selectedPiece.m_name + "') has no terrain-editing component."));
				return;
			}
			object? value = PlacementGhostField.GetValue(player);
			GameObject val = (GameObject)((value is GameObject) ? value : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			if ((int)(PlacementStatus)PlacementStatusField.GetValue(player) != 0)
			{
				((Character)player).Message((MessageType)2, "Can't flatten here", 0, (Sprite)null, false);
				return;
			}
			if (_staminaCost.Value > 0f)
			{
				if (!((Character)player).HaveStamina(_staminaCost.Value))
				{
					((Character)player).Message((MessageType)2, "Not enough stamina", 0, (Sprite)null, false);
					return;
				}
				((Character)player).UseStamina(_staminaCost.Value);
			}
			_lastUseTime = Time.time;
			Vector3 position = val.transform.position;
			Quaternion rotation = val.transform.rotation;
			if ((Object)(object)componentInChildren2 != (Object)null)
			{
				ApplyViaTerrainOp(player, selectedPiece, componentInChildren2, position, rotation);
			}
			else
			{
				ApplyViaTerrainModifier(player, selectedPiece, componentInChildren, position, rotation);
			}
			((Character)player).Message((MessageType)1, $"Flattened terrain (radius {_radius.Value:0}m)", 0, (Sprite)null, false);
		}

		private void ApplyViaTerrainOp(Player player, Piece piece, TerrainOp terrainOp, Vector3 pos, Quaternion rot)
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			Settings settings = terrainOp.m_settings;
			bool level = settings.m_level;
			float levelRadius = settings.m_levelRadius;
			bool square = settings.m_square;
			float levelOffset = settings.m_levelOffset;
			bool raise = settings.m_raise;
			bool smooth = settings.m_smooth;
			bool paintCleared = settings.m_paintCleared;
			settings.m_level = true;
			settings.m_levelRadius = _radius.Value;
			settings.m_square = _squareArea.Value;
			settings.m_levelOffset = 0f;
			settings.m_raise = false;
			settings.m_smooth = false;
			settings.m_paintCleared = false;
			try
			{
				player.PlacePiece(piece, pos, rot, true, false);
			}
			finally
			{
				settings.m_level = level;
				settings.m_levelRadius = levelRadius;
				settings.m_square = square;
				settings.m_levelOffset = levelOffset;
				settings.m_raise = raise;
				settings.m_smooth = smooth;
				settings.m_paintCleared = paintCleared;
			}
		}

		private void ApplyViaTerrainModifier(Player player, Piece piece, TerrainModifier terrainModifier, Vector3 pos, Quaternion rot)
		{
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			bool level = terrainModifier.m_level;
			float levelRadius = terrainModifier.m_levelRadius;
			bool square = terrainModifier.m_square;
			float levelOffset = terrainModifier.m_levelOffset;
			bool smooth = terrainModifier.m_smooth;
			bool paintCleared = terrainModifier.m_paintCleared;
			bool playerModifiction = terrainModifier.m_playerModifiction;
			terrainModifier.m_level = true;
			terrainModifier.m_levelRadius = _radius.Value;
			terrainModifier.m_square = _squareArea.Value;
			terrainModifier.m_levelOffset = 0f;
			terrainModifier.m_smooth = false;
			terrainModifier.m_paintCleared = false;
			terrainModifier.m_playerModifiction = true;
			try
			{
				player.PlacePiece(piece, pos, rot, true, false);
				Heightmap val = Heightmap.FindHeightmap(pos);
				if ((Object)(object)val != (Object)null)
				{
					TerrainComp.UpgradeTerrain(val);
				}
			}
			finally
			{
				terrainModifier.m_level = level;
				terrainModifier.m_levelRadius = levelRadius;
				terrainModifier.m_square = square;
				terrainModifier.m_levelOffset = levelOffset;
				terrainModifier.m_smooth = smooth;
				terrainModifier.m_paintCleared = paintCleared;
				terrainModifier.m_playerModifiction = playerModifiction;
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		internal IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}