Decompiled source of Crafting Station Level Range v1.5.1

CraftingStationLevelRange.dll

Decompiled 3 weeks 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 BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CraftingStationLevelRange")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CraftingStationLevelRange")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f070453f-a62d-4fa2-8bab-64b29bfa0ead")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("smallo.mods.craftingstationlevelrange", "Crafting Station Level Range", "1.5.1")]
internal class CraftingStationLevelRangePlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(CraftingStation), "UpdateKnownStationsInRange")]
	private static class UpdateKnownStationsInRange_Patch
	{
		private static void Postfix(Player player)
		{
			foreach (CraftingStation item in (List<CraftingStation>)AccessTools.Field(typeof(CraftingStation), "m_allStations").GetValue(null))
			{
				if (item.m_name == "$piece_cauldron")
				{
					continue;
				}
				int level = item.GetLevel(true);
				if (level > 1)
				{
					int num = stationDefaultRange.Value + stationAmountIncrease.Value * (level - 1);
					ChangeStationRange(item, num);
					continue;
				}
				if (parentInheritance.Value && (item.m_name == "$piece_stonecutter" || item.m_name == "$piece_artisanstation"))
				{
					ChangeChildStationRange(item);
				}
				if (item.m_name == "$piece_workbench" || item.m_name == "$piece_forge")
				{
					ChangeStationRange(item, stationDefaultRange.Value);
				}
			}
		}
	}

	private static ManualLogSource logger;

	private static ConfigEntry<bool> enableMod;

	private static ConfigEntry<int> stationAmountIncrease;

	private static ConfigEntry<int> stationDefaultRange;

	private static ConfigEntry<double> stationSearchRange;

	private static ConfigEntry<bool> parentInheritance;

	private static ConfigEntry<double> inheritanceAmount;

	private void Awake()
	{
		logger = ((BaseUnityPlugin)this).Logger;
		enableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Global", "EnableMod", true, "Enable or disable this mod");
		stationDefaultRange = ((BaseUnityPlugin)this).Config.Bind<int>("2 - General", "DefaultRange", 20, "The range of a level 1 workbench (vanilla game is 20)");
		stationAmountIncrease = ((BaseUnityPlugin)this).Config.Bind<int>("2 - General", "IncreaseAmount", 10, "Amount to increase the station by for each level");
		stationSearchRange = ((BaseUnityPlugin)this).Config.Bind<double>("3 - Advanced", "SearchRange", 120.0, "The range to search for crafting stations while holding a hammer. Warning: larger numbers may potentially cause lag, only increase the number if your stations will ever get above this build range.");
		parentInheritance = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Range Inheritance", "ParentInheritance", true, "Allow secondary Crafting Stations (Stonecutter or Artisans Table) to inherit the range of their parent stations (Workbench or Forge) if they are within range");
		inheritanceAmount = ((BaseUnityPlugin)this).Config.Bind<double>("4 - Range Inheritance", "InheritanceAmount", 1.0, "Amount to multiply the inheritance value by. You may want the secondary station to have a lesser value. (Example: 0.5 would be 50% the amount of the IncreaseAmount variable)");
		if (enableMod.Value)
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	public static (bool, CraftingStation) IsParentWorkbenchInRange(CraftingStation station, string workbenchType, float searchRange)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: 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_0044: 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_0061: 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)
		CraftingStation val = CraftingStation.FindClosestStationInRange(workbenchType, ((Component)station).transform.position, searchRange);
		if ((Object)(object)val == (Object)null)
		{
			return (false, val);
		}
		Vector2 val2 = new Vector2(((Component)val).transform.position.x, ((Component)val).transform.position.z);
		Vector2 val3 = default(Vector2);
		((Vector2)(ref val3))..ctor(((Component)station).transform.position.x, ((Component)station).transform.position.z);
		if (Vector2.Distance(val2, val3) <= val.m_rangeBuild)
		{
			return (true, val);
		}
		return (false, val);
	}

	public static void ChangeStationRange(CraftingStation station, float newRange)
	{
		if (station.m_rangeBuild != newRange)
		{
			CraftingStation component = ((Component)((Component)station).GetComponent<ZNetView>()).GetComponent<CraftingStation>();
			component.m_rangeBuild = newRange;
			component.m_areaMarker.GetComponent<CircleProjector>().m_radius = newRange;
			component.m_areaMarker.GetComponent<CircleProjector>().m_nrOfSegments = (int)Math.Ceiling(Math.Max(5f, 4f * newRange));
			logger.LogInfo((object)$"{station.m_name} ({((Object)station).GetInstanceID()}) set to {newRange} range");
		}
	}

	public static void ChangeChildStationRange(CraftingStation station)
	{
		var (flag, val) = IsParentWorkbenchInRange(station, "$piece_workbench", (float)stationSearchRange.Value);
		if (!flag)
		{
			ChangeStationRange(station, stationDefaultRange.Value);
		}
		if (flag && val.GetLevel(true) > 1)
		{
			float newRange = (float)stationDefaultRange.Value + (float)stationAmountIncrease.Value * (float)(val.GetLevel(true) - 1) * (float)inheritanceAmount.Value;
			ChangeStationRange(station, newRange);
		}
		else if (flag && val.GetLevel(true) == 1)
		{
			ChangeStationRange(station, stationDefaultRange.Value);
		}
	}
}