Decompiled source of LzD Stations Custom Ranges v1.1.14

LzD_StationsCustomRanges.dll

Decompiled 2 weeks ago
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;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LzD_StationsCustomRanges")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LzD_StationsCustomRanges")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4e5ca6f5-3af3-4caa-95b6-2f8932e2492e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LzD_StationsCustomRanges;

[BepInPlugin("lzd_stationscustomranges", "LzD Stations Custom Ranges", "1.1.14")]
[BepInProcess("valheim.exe")]
public class LzD_StationsCustomRanges : BaseUnityPlugin
{
	[HarmonyPatch(typeof(CraftingStation), "UpdateKnownStationsInRange")]
	private static class CraftingStation_UpdateKnownStationsInRange_Patch
	{
		private static void Postfix(List<CraftingStation> ___m_allStations)
		{
			if (!modEnabled.Value)
			{
				return;
			}
			foreach (CraftingStation ___m_allStation in ___m_allStations)
			{
				if (___m_allStation.m_name == "$piece_workbench")
				{
					if (workbenchEnabled.Value)
					{
						changeStationRange(___m_allStation, workbenchRange.Value);
						changeStationRangePerLevel(___m_allStation, workbenchRangeLvl.Value);
					}
				}
				else if (___m_allStation.m_name == "$piece_forge")
				{
					if (forgeEnabled.Value)
					{
						changeStationRange(___m_allStation, forgeRange.Value);
						changeStationRangePerLevel(___m_allStation, forgeRangeLvl.Value);
					}
				}
				else if (___m_allStation.m_name == "$piece_stonecutter")
				{
					if (stonecutterEnabled.Value)
					{
						changeStationRange(___m_allStation, stonecutterRange.Value);
					}
				}
				else if (___m_allStation.m_name == "$piece_blackforge")
				{
					if (blackforgeEnabled.Value)
					{
						changeStationRange(___m_allStation, blackforgeRange.Value);
						changeStationRangePerLevel(___m_allStation, blackforgeRangeLvl.Value);
					}
				}
				else if (___m_allStation.m_name == "$piece_artisanstation")
				{
					if (artisanEnabled.Value)
					{
						changeStationRange(___m_allStation, artisanTableRange.Value);
					}
				}
				else if (___m_allStation.m_name == "$piece_magetable" && galdrEnabled.Value)
				{
					changeStationRange(___m_allStation, galdrTableRange.Value);
					changeStationRangePerLevel(___m_allStation, galdrTableRangeLvl.Value);
				}
			}
		}

		private static void changeStationRange(CraftingStation station, float newRange)
		{
			if (globalEnabled.Value)
			{
				newRange = globalRange.Value;
			}
			if (station.m_rangeBuild != newRange && rangeEnabled.Value)
			{
				station.m_rangeBuild = newRange;
				Log($"Changing {station.m_name} range build to: {newRange}");
			}
		}

		private static void changeStationRangePerLevel(CraftingStation station, float newRangePerLevel)
		{
			if (globalEnabled.Value)
			{
				newRangePerLevel = globalRangePerLevel.Value;
			}
			if (station.m_extraRangePerLevel != newRangePerLevel && rangeLvlEnabled.Value)
			{
				station.m_extraRangePerLevel = newRangePerLevel;
				Log($"Changing {station.m_name} extra range per level to: {newRangePerLevel}");
			}
		}
	}

	[HarmonyPatch(typeof(StationExtension), "Awake")]
	private static class StationExtension_Awake_Patch
	{
		private static void Postfix(ref float ___m_maxStationDistance)
		{
			if (___m_maxStationDistance != extMaxDist.Value && extMaxDistEnabled.Value && modEnabled.Value)
			{
				___m_maxStationDistance = extMaxDist.Value;
				Log($"Changing extension max distance to: {___m_maxStationDistance}");
			}
		}
	}

	public const string modID = "lzd_stationscustomranges";

	public const string modName = "LzD Stations Custom Ranges";

	public const string modVersion = "1.1.14";

	private readonly Harmony harmony = new Harmony("lzd_stationscustomranges");

	private static ConfigEntry<bool> modEnabled;

	private static ConfigEntry<bool> logsEnabled;

	private static ConfigEntry<bool> rangeEnabled;

	private static ConfigEntry<bool> rangeLvlEnabled;

	private static ConfigEntry<bool> extMaxDistEnabled;

	private static ConfigEntry<bool> globalEnabled;

	private static ConfigEntry<float> globalRange;

	private static ConfigEntry<float> globalRangePerLevel;

	private static ConfigEntry<bool> workbenchEnabled;

	private static ConfigEntry<float> workbenchRange;

	private static ConfigEntry<float> workbenchRangeLvl;

	private static ConfigEntry<bool> forgeEnabled;

	private static ConfigEntry<float> forgeRange;

	private static ConfigEntry<float> forgeRangeLvl;

	private static ConfigEntry<bool> stonecutterEnabled;

	private static ConfigEntry<float> stonecutterRange;

	private static ConfigEntry<bool> blackforgeEnabled;

	private static ConfigEntry<float> blackforgeRange;

	private static ConfigEntry<float> blackforgeRangeLvl;

	private static ConfigEntry<bool> artisanEnabled;

	private static ConfigEntry<float> artisanTableRange;

	private static ConfigEntry<bool> galdrEnabled;

	private static ConfigEntry<float> galdrTableRange;

	private static ConfigEntry<float> galdrTableRangeLvl;

	private static ConfigEntry<float> extMaxDist;

	private void Awake()
	{
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "a. Mod enabled", true, "Enable or disable the mod completely");
		logsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "b. Logs enabled", false, "Enable or disable logs completely");
		rangeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "c. Base range mods enabled", true, "Enable base or disable range customizations completely");
		rangeLvlEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "d. Range per level mods enabled", true, "Enable or disable range per level customizations completely");
		globalEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Global", "a. Global settings enabled", false, "Enable or disable settings that apply to all stations");
		globalRange = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Global", "b. Global base range", 30f, "Base range to apply to all stations");
		globalRangePerLevel = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Global", "c. Global range per level", 6f, "Range per level to apply to all stations");
		workbenchEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Workbench", "a. Enabled", true, "Enable or disable workbench modifications completely");
		workbenchRange = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Workbench", "b. Base range", 30f, "Workbench base range");
		workbenchRangeLvl = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Workbench", "c. Range per level", 6f, "Workbench range per level");
		forgeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Forge", "a. Enabled", true, "Enable or disable forge modifications completely");
		forgeRange = ((BaseUnityPlugin)this).Config.Bind<float>("4 - Forge", "b. Base range", 30f, "Forge base range");
		forgeRangeLvl = ((BaseUnityPlugin)this).Config.Bind<float>("4 - Forge", "c. Range per level", 4f, "Forge range per level");
		stonecutterEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("5 - Stonecutter", "a. Enabled", true, "Enable or disable stonecutter modifications completely");
		stonecutterRange = ((BaseUnityPlugin)this).Config.Bind<float>("5 - Stonecutter", "b. Base range", 54f, "Stonecutter base range");
		blackforgeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("6 - Blackforge", "a. Enabled", true, "Enable or disable blackforge modifications completely");
		blackforgeRange = ((BaseUnityPlugin)this).Config.Bind<float>("6 - Blackforge", "b. Base range", 40f, "Blackforge base range");
		blackforgeRangeLvl = ((BaseUnityPlugin)this).Config.Bind<float>("6 - Blackforge", "c. Range per level", 7f, "Blackforge range per level");
		artisanEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("7 - Artisan Table", "a. Enabled", true, "Enable or disable artisan table modifications completely");
		artisanTableRange = ((BaseUnityPlugin)this).Config.Bind<float>("7 - Artisan Table", "b. Base range", 54f, "Artisan table base range");
		galdrEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("8 - Galdr Table", "a. Enabled", true, "Enable or disable galdr table modifications completely");
		galdrTableRange = ((BaseUnityPlugin)this).Config.Bind<float>("8 - Galdr Table", "b. Base range", 40f, "Galdr table base range");
		galdrTableRangeLvl = ((BaseUnityPlugin)this).Config.Bind<float>("8 - Galdr Table", "c. Range per level", 7f, "Galdr table range per level");
		extMaxDistEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Upgrades build distance", "a. Enabled", true, "Enable or disable station upgrades maximum distance customizations completely");
		extMaxDist = ((BaseUnityPlugin)this).Config.Bind<float>("9 - Upgrades build distance", "b. Max Distance", 15f, "Maximum distance multiplier for station upgrades");
		if (modEnabled.Value)
		{
			harmony.PatchAll();
			Log("LzD Stations Custom Ranges mod initialized");
		}
	}

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

	private static void Log(string msg)
	{
		if (logsEnabled.Value)
		{
			Debug.Log((object)msg);
		}
	}
}