Decompiled source of BuildSpace v1.0.4

BuildSpace.dll

Decompiled a year 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;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BuildSpace")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("BuildSpace")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("10565550-7931-4506-a65f-96055766b7e5")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.4.0")]
namespace BuildSpace;

[BepInPlugin("dev.crystal.buildspace", "BuildSpace", "1.0.4.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class BuildSpacePlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(CraftingStation))]
	private static class CraftingStation_Patches
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void Start_Postfix(CraftingStation __instance)
		{
			SetBuildRadius(__instance, 1f, BuildRadiusMultiplier.Value);
		}
	}

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

	public static ConfigEntry<float> BuildRadiusMultiplier;

	private static Harmony sCraftingStationHarmony;

	private static readonly FieldInfo sAllStationsField;

	private float mBuildRadiusMultiplier;

	static BuildSpacePlugin()
	{
		sAllStationsField = typeof(CraftingStation).GetField("m_allStations", 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
		BuildRadiusMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Build", "BuildRadiusMultiplier", 1f, "Multiplier to apply to the build radius of crafting stations. Game default 1.");
		BuildRadiusMultiplier.SettingChanged += BuildRadiusMultiplier_SettingChanged;
		ClampConfig();
		mBuildRadiusMultiplier = BuildRadiusMultiplier.Value;
		sCraftingStationHarmony = new Harmony("dev.crystal.buildspace_CraftingStation");
		sCraftingStationHarmony.PatchAll(typeof(CraftingStation_Patches));
	}

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

	private void BuildRadiusMultiplier_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (CraftingStation item in (List<CraftingStation>)sAllStationsField.GetValue(null))
		{
			SetBuildRadius(item, mBuildRadiusMultiplier, BuildRadiusMultiplier.Value);
		}
		mBuildRadiusMultiplier = BuildRadiusMultiplier.Value;
	}

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

	private static void SetBuildRadius(CraftingStation station, float oldMultiplier, float newMultiplier)
	{
		float num = (station.m_rangeBuild = station.m_rangeBuild / oldMultiplier * newMultiplier);
		GameObject areaMarker = station.m_areaMarker;
		CircleProjector val = ((areaMarker != null) ? areaMarker.GetComponent<CircleProjector>() : null);
		if ((Object)(object)val != (Object)null)
		{
			val.m_radius = num;
			val.m_nrOfSegments = (int)(num * 4f);
		}
	}
}