Decompiled source of WiresGrassTweaks v0.2.3

WiresGrassTweaks.dll

Decompiled a week 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 Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;

[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: AssemblyCompany("GrassPresets")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GrassPresets")]
[assembly: AssemblyTitle("GrassPresets")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 GrassPresets
{
	[BepInPlugin("kajetan.valheim.grasspresets", "Grass Presets", "0.2.2")]
	public sealed class GrassPresetsPlugin : BaseUnityPlugin
	{
		private struct RendererDefaults
		{
			public bool UseLod;

			public bool UseXZ;

			public float LodMin;

			public float LodMax;

			public ShadowCastingMode Shadows;
		}

		private sealed class ConfigurationManagerAttributes
		{
			public Action<ConfigEntryBase> CustomDrawer;
		}

		[HarmonyPatch(typeof(ClutterSystem), "Awake")]
		private static class ClutterAwakePatch
		{
			private static void Postfix(ClutterSystem __instance)
			{
				if ((Object)(object)Instance != (Object)null)
				{
					Instance.OnClutterAwake(__instance);
				}
			}
		}

		[HarmonyPatch(typeof(InstanceRenderer), "OnEnable")]
		private static class RendererEnablePatch
		{
			private static void Postfix(InstanceRenderer __instance)
			{
				if ((Object)(object)Instance != (Object)null)
				{
					Instance.OnRendererEnable(__instance);
				}
			}
		}

		public const string Guid = "kajetan.valheim.grasspresets";

		public const string Name = "Grass Presets";

		public const string Version = "0.2.2";

		internal static GrassPresetsPlugin Instance;

		private Harmony _harmony;

		private bool _suppressEvents;

		private string _lastSelectablePreset = "Enhanced";

		private ConfigEntry<string> _preset;

		private ConfigEntry<bool> _useLod;

		private ConfigEntry<bool> _useXZ;

		private ConfigEntry<float> _lodMin;

		private ConfigEntry<float> _lodMax;

		private ConfigEntry<float> _scaleMin;

		private ConfigEntry<float> _scaleMax;

		private ConfigEntry<float> _amount;

		private ConfigEntry<float> _distance;

		private ConfigEntry<float> _patch;

		private ConfigEntry<float> _pushFade;

		private ConfigEntry<ShadowCastingMode> _shadows;

		private bool _haveClutterDefaults;

		private float _defaultAmount = 1f;

		private float _defaultDistance = 40f;

		private float _defaultPatch = 8f;

		private float _defaultPushFade = 0.05f;

		private readonly Dictionary<Clutter, Vector2> _clutterScales = new Dictionary<Clutter, Vector2>();

		private readonly Dictionary<int, RendererDefaults> _rendererDefaults = new Dictionary<int, RendererDefaults>();

		private void Awake()
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d9: Expected O, but got Unknown
			Instance = this;
			_preset = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Preset", "Enhanced", new ConfigDescription("Choose Default, Enhanced, or Ultra. Editing any setting below automatically changes the state to Custom.", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[4] { "Default", "Enhanced", "Ultra", "Custom" }), new object[1]
			{
				new ConfigurationManagerAttributes
				{
					CustomDrawer = DrawPresetSelector
				}
			}));
			_useLod = ((BaseUnityPlugin)this).Config.Bind<bool>("Grass Settings", "UseLod", true, "Use LOD.");
			_useXZ = ((BaseUnityPlugin)this).Config.Bind<bool>("Grass Settings", "UseXZLodDistance", true, "Use XZ LOD distance.");
			_lodMin = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "LodMinDistanceMult", 1f, "Minimum LOD distance multiplier.");
			_lodMax = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "LodMaxDistanceMult", 1f, "Maximum LOD distance multiplier.");
			_scaleMin = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "ScaleMinMult", 1f, "Clutter minimum scale multiplier.");
			_scaleMax = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "ScaleMaxMult", 1f, "Clutter maximum scale multiplier.");
			_amount = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "AmountMult", 0.5f, "Clutter amount multiplier.");
			_distance = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "ClutterDistance", 200f, "Clutter draw distance.");
			_patch = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "GrassPatchSize", 12f, "Grass patch size.");
			_pushFade = ((BaseUnityPlugin)this).Config.Bind<float>("Grass Settings", "PlayerPushFade", 0.05f, "Player push fade.");
			_shadows = ((BaseUnityPlugin)this).Config.Bind<ShadowCastingMode>("Grass Settings", "ShadowCastingMode", (ShadowCastingMode)1, "Grass/clutter shadow casting.");
			_preset.SettingChanged += OnPresetChanged;
			SubscribeCustom<bool>(_useLod);
			SubscribeCustom<bool>(_useXZ);
			SubscribeCustom<float>(_lodMin);
			SubscribeCustom<float>(_lodMax);
			SubscribeCustom<float>(_scaleMin);
			SubscribeCustom<float>(_scaleMax);
			SubscribeCustom<float>(_amount);
			SubscribeCustom<float>(_distance);
			SubscribeCustom<float>(_patch);
			SubscribeCustom<float>(_pushFade);
			SubscribeCustom<ShadowCastingMode>(_shadows);
			if (_preset.Value != "Custom")
			{
				_lastSelectablePreset = _preset.Value;
			}
			_harmony = new Harmony("kajetan.valheim.grasspresets");
			_harmony.PatchAll();
		}

		private static void DrawPreset(ConfigEntryBase entry)
		{
			string text = (entry.BoxedValue as string) ?? "Enhanced";
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			GUILayout.Label("Current: " + text, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(115f) });
			if (GUILayout.Button("Default", Array.Empty<GUILayoutOption>()))
			{
				entry.BoxedValue = "Default";
			}
			if (GUILayout.Button("Enhanced", Array.Empty<GUILayoutOption>()))
			{
				entry.BoxedValue = "Enhanced";
			}
			if (GUILayout.Button("Ultra", Array.Empty<GUILayoutOption>()))
			{
				entry.BoxedValue = "Ultra";
			}
			GUILayout.EndHorizontal();
		}

		private void SubscribeCustom<T>(ConfigEntry<T> entry)
		{
			entry.SettingChanged += OnCustomSettingChanged;
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
			Instance = null;
		}

		private void DrawPresetSelector(ConfigEntryBase entry)
		{
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			GUI.enabled = _preset.Value != "Default";
			if (GUILayout.Button("Default", Array.Empty<GUILayoutOption>()))
			{
				_preset.Value = "Default";
			}
			GUI.enabled = _preset.Value != "Enhanced";
			if (GUILayout.Button("Enhanced", Array.Empty<GUILayoutOption>()))
			{
				_preset.Value = "Enhanced";
			}
			GUI.enabled = _preset.Value != "Ultra";
			if (GUILayout.Button("Ultra", Array.Empty<GUILayoutOption>()))
			{
				_preset.Value = "Ultra";
			}
			GUI.enabled = true;
			if (_preset.Value == "Custom")
			{
				GUILayout.Label("Custom", Array.Empty<GUILayoutOption>());
			}
			GUILayout.EndHorizontal();
		}

		private void OnPresetChanged(object sender, EventArgs e)
		{
			if (!_suppressEvents)
			{
				string value = _preset.Value;
				if (value == "Custom")
				{
					ApplyAll(rebuild: true);
					return;
				}
				_lastSelectablePreset = value;
				WritePresetSettings(value);
				ApplyAll(rebuild: true);
			}
		}

		private void OnCustomSettingChanged(object sender, EventArgs e)
		{
			if (!_suppressEvents)
			{
				_suppressEvents = true;
				_preset.Value = "Custom";
				_suppressEvents = false;
				ApplyAll(rebuild: true);
			}
		}

		private void SetPresetValueOnly(string value)
		{
			_suppressEvents = true;
			_preset.Value = value;
			_suppressEvents = false;
		}

		private void WritePresetSettings(string preset)
		{
			_suppressEvents = true;
			_useLod.Value = true;
			_useXZ.Value = true;
			_lodMin.Value = 1f;
			_lodMax.Value = 1f;
			_scaleMin.Value = 1f;
			_scaleMax.Value = 1f;
			_pushFade.Value = (_haveClutterDefaults ? _defaultPushFade : 0.05f);
			_shadows.Value = (ShadowCastingMode)1;
			if (preset == "Default")
			{
				_amount.Value = (_haveClutterDefaults ? _defaultAmount : 1f);
				_distance.Value = (_haveClutterDefaults ? _defaultDistance : 40f);
				_patch.Value = (_haveClutterDefaults ? _defaultPatch : 8f);
			}
			else if (preset == "Ultra")
			{
				_amount.Value = 1f;
				_distance.Value = 200f;
				_patch.Value = 12f;
			}
			else
			{
				_amount.Value = 0.5f;
				_distance.Value = 200f;
				_patch.Value = 12f;
			}
			_suppressEvents = false;
			((BaseUnityPlugin)this).Config.Save();
		}

		internal void OnClutterAwake(ClutterSystem clutter)
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)clutter == (Object)null)
			{
				return;
			}
			_defaultAmount = clutter.m_amountScale;
			_defaultDistance = clutter.m_distance;
			_defaultPatch = clutter.m_grassPatchSize;
			_defaultPushFade = clutter.m_playerPushFade;
			_haveClutterDefaults = true;
			_clutterScales.Clear();
			foreach (Clutter item in clutter.m_clutter)
			{
				if (item != null)
				{
					_clutterScales[item] = new Vector2(item.m_scaleMin, item.m_scaleMax);
				}
			}
			if (_preset.Value != "Custom")
			{
				WritePresetSettings(_preset.Value);
			}
			ApplyClutter(clutter, rebuild: false);
		}

		internal void OnRendererEnable(InstanceRenderer renderer)
		{
			//IL_0065: 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)
			if (!((Object)(object)renderer == (Object)null))
			{
				int instanceID = ((Object)renderer).GetInstanceID();
				if (!_rendererDefaults.ContainsKey(instanceID))
				{
					_rendererDefaults[instanceID] = new RendererDefaults
					{
						UseLod = renderer.m_useLod,
						UseXZ = renderer.m_useXZLodDistance,
						LodMin = renderer.m_lodMinDistance,
						LodMax = renderer.m_lodMaxDistance,
						Shadows = renderer.m_shadowCasting
					};
				}
				ApplyRenderer(renderer);
			}
		}

		private void ApplyAll(bool rebuild)
		{
			if ((Object)(object)ClutterSystem.instance != (Object)null && _haveClutterDefaults)
			{
				ApplyClutter(ClutterSystem.instance, rebuild);
			}
			InstanceRenderer[] array = Resources.FindObjectsOfTypeAll<InstanceRenderer>();
			foreach (InstanceRenderer renderer in array)
			{
				OnRendererEnable(renderer);
			}
		}

		private void ApplyClutter(ClutterSystem clutter, bool rebuild)
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)clutter == (Object)null)
			{
				return;
			}
			clutter.m_amountScale = _amount.Value;
			clutter.m_distance = _distance.Value;
			clutter.m_grassPatchSize = _patch.Value;
			clutter.m_playerPushFade = _pushFade.Value;
			foreach (KeyValuePair<Clutter, Vector2> clutterScale in _clutterScales)
			{
				if (clutterScale.Key != null)
				{
					clutterScale.Key.m_scaleMin = clutterScale.Value.x * _scaleMin.Value;
					clutterScale.Key.m_scaleMax = clutterScale.Value.y * _scaleMax.Value;
				}
			}
			if (rebuild)
			{
				clutter.ClearAll();
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Grass state " + _preset.Value + ": amount=" + clutter.m_amountScale + ", distance=" + clutter.m_distance + ", patch=" + clutter.m_grassPatchSize));
		}

		private void ApplyRenderer(InstanceRenderer renderer)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			if (_rendererDefaults.TryGetValue(((Object)renderer).GetInstanceID(), out var value))
			{
				renderer.m_useLod = _useLod.Value;
				renderer.m_useXZLodDistance = _useXZ.Value;
				renderer.m_lodMinDistance = value.LodMin * _lodMin.Value;
				renderer.m_lodMaxDistance = value.LodMax * _lodMax.Value;
				renderer.m_shadowCasting = _shadows.Value;
			}
		}
	}
	internal sealed class ConfigurationManagerAttributes
	{
		public Action<ConfigEntryBase> CustomDrawer;
	}
}