Decompiled source of ClearTheAir v1.0.3

ClearTheAir.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
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;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ClearTheAir")]
[assembly: AssemblyDescription("ClearTheAir")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("ClearTheAir")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("b3201fce-dc8e-457b-8adc-c625dbf2fed6")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.3.0")]
namespace ClearTheAir;

[BepInPlugin("dev.crystal.cleartheair", "ClearTheAir", "1.0.3.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class ClearTheAirPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Demister))]
	private static class Demister_Patches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void Awake_Postfix(Demister __instance)
		{
			SetMistClearRadius(__instance, 1f, MistClearRadiusMultiplier.Value);
		}
	}

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

	public static ConfigEntry<float> MistClearRadiusMultiplier;

	private static Harmony sDemisterHarmony;

	private static readonly FieldInfo sAllDemistersField;

	private float mMistClearRadiusMultiplier;

	static ClearTheAirPlugin()
	{
		sAllDemistersField = typeof(Demister).GetField("m_instances", BindingFlags.Static | BindingFlags.NonPublic);
	}

	private void Awake()
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		MistClearRadiusMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Mist", "MistClearRadiusMultiplier", 1f, "Multiplier to apply to the for clear radius of all items which can clear mist. Game default 1.");
		MistClearRadiusMultiplier.SettingChanged += MistClearRadiusMultiplier_SettingChanged;
		ClampConfig();
		mMistClearRadiusMultiplier = MistClearRadiusMultiplier.Value;
		sDemisterHarmony = new Harmony("dev.crystal.cleartheair_Demister");
		sDemisterHarmony.PatchAll(typeof(Demister_Patches));
	}

	private void OnDestroy()
	{
		sDemisterHarmony.UnpatchSelf();
	}

	private void MistClearRadiusMultiplier_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (Demister item in (List<Demister>)sAllDemistersField.GetValue(null))
		{
			SetMistClearRadius(item, mMistClearRadiusMultiplier, MistClearRadiusMultiplier.Value);
		}
		mMistClearRadiusMultiplier = MistClearRadiusMultiplier.Value;
	}

	private void ClampConfig()
	{
		if (MistClearRadiusMultiplier.Value < 0.1f)
		{
			MistClearRadiusMultiplier.Value = 0.1f;
		}
		if (MistClearRadiusMultiplier.Value > 100f)
		{
			MistClearRadiusMultiplier.Value = 100f;
		}
	}

	private static void SetMistClearRadius(Demister demister, float oldMultiplier, float newMultiplier)
	{
		float endRange = demister.m_forceField.endRange / oldMultiplier * newMultiplier;
		demister.m_forceField.endRange = endRange;
	}
}