Decompiled source of CraftingStationTweakz v1.0.0

plugins\CraftingStationTweakz.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using CraftingStationTweakz.Configs;
using Jotunn;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CraftingStationTweakz")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CraftingStationTweakz")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CraftingStationTweakz
{
	[BepInPlugin("DeathWizsh.CraftingStationTweakz", "CraftingStationTweakz", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class CraftingStationTweakz : BaseUnityPlugin
	{
		public const string PluginGUID = "DeathWizsh.CraftingStationTweakz";

		public const string PluginName = "CraftingStationTweakz";

		public const string PluginVersion = "1.0.0";

		public static CraftingStationTweakz Instance;

		public static string configFileName = "DeathWizsh.CraftingStationTweakz.cfg";

		public static string configFileFullPath;

		private const float DebounceSeconds = 0.3f;

		private Coroutine extensionUpdateRoutine;

		private Coroutine craftingStationUpdateRoutine;

		private void Awake()
		{
			Instance = this;
			PluginConfig.Init();
			PrefabManager.OnPrefabsRegistered += PatchPieces;
		}

		private void PatchPieces()
		{
			UpdateExtensionPieces();
			UpdateCraftingStation();
			PrefabManager.OnPrefabsRegistered -= PatchPieces;
		}

		public void RequestUpdateExtensionPieces()
		{
			if (extensionUpdateRoutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(extensionUpdateRoutine);
			}
			extensionUpdateRoutine = ((MonoBehaviour)this).StartCoroutine(DebounceUpdateExtensionPieces());
		}

		private IEnumerator DebounceUpdateExtensionPieces()
		{
			yield return (object)new WaitForSeconds(0.3f);
			extensionUpdateRoutine = null;
			UpdateExtensionPieces();
		}

		public void RequestUpdateCraftingStation()
		{
			if (craftingStationUpdateRoutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(craftingStationUpdateRoutine);
			}
			craftingStationUpdateRoutine = ((MonoBehaviour)this).StartCoroutine(DebounceUpdateCraftingStation());
		}

		private IEnumerator DebounceUpdateCraftingStation()
		{
			yield return (object)new WaitForSeconds(0.3f);
			craftingStationUpdateRoutine = null;
			UpdateCraftingStation();
		}

		public void UpdateExtensionPieces()
		{
			foreach (GameObject item in ZNetScene.instance.m_prefabs.FindAll((GameObject item) => ((Object)item).name.Contains("_ext")))
			{
				StationExtension component = item.GetComponent<StationExtension>();
				Piece component2 = item.GetComponent<Piece>();
				if (!((Object)(object)component == (Object)null) && !((Object)(object)component2 == (Object)null))
				{
					component.m_maxStationDistance = PluginConfig.extensionRange.Value;
					component2.m_spaceRequirement = PluginConfig.extensionSpaceRange.Value;
				}
			}
			foreach (StationExtension allExtension in StationExtension.m_allExtensions)
			{
				if (!((Object)(object)allExtension == (Object)null))
				{
					Piece component3 = ((Component)allExtension).GetComponent<Piece>();
					if (!((Object)(object)component3 == (Object)null))
					{
						Logger.LogWarning((object)("Adjusting live upgrade instance " + ((Object)allExtension).name + "..."));
						allExtension.m_maxStationDistance = PluginConfig.extensionRange.Value;
						component3.m_spaceRequirement = PluginConfig.extensionSpaceRange.Value;
					}
				}
			}
		}

		public void UpdateCraftingStation()
		{
			foreach (GameObject item in ZNetScene.instance.m_prefabs.FindAll((GameObject item) => Object.op_Implicit((Object)(object)item.GetComponent<CraftingStation>())))
			{
				CraftingStation component = item.GetComponent<CraftingStation>();
				Piece component2 = item.GetComponent<Piece>();
				if (!((Object)(object)component == (Object)null) && !((Object)(object)component2 == (Object)null))
				{
					component.m_rangeBuild = PluginConfig.stationBuildRange.Value;
					component.m_extraRangePerLevel = PluginConfig.stationBuildRangePerUpgrade.Value;
				}
			}
			foreach (CraftingStation allStation in CraftingStation.m_allStations)
			{
				if (!((Object)(object)allStation == (Object)null))
				{
					Logger.LogWarning((object)("Adjusting live crafting station instance " + ((Object)allStation).name + "..."));
					allStation.m_rangeBuild = PluginConfig.stationBuildRange.Value;
					allStation.m_extraRangePerLevel = PluginConfig.stationBuildRangePerUpgrade.Value;
				}
			}
		}

		static CraftingStationTweakz()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			configFileFullPath = configPath + directorySeparatorChar + configFileName;
		}
	}
}
namespace CraftingStationTweakz.Configs
{
	internal static class PluginConfig
	{
		public static string sectionGeneral = "General";

		public static ConfigEntry<float> stationBuildRange;

		public static ConfigEntry<float> stationBuildRangePerUpgrade;

		public static ConfigEntry<float> stationPlayerBaseRange;

		public static ConfigEntry<float> extensionRange;

		public static ConfigEntry<float> extensionSpaceRange;

		private static int entryCount = 1000;

		private static FileSystemWatcher configWatcher;

		public static void Init()
		{
			InitGeneralConfig();
		}

		public static void InitGeneralConfig()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			//IL_0071: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Expected O, but got Unknown
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Expected O, but got Unknown
			//IL_0100: Expected O, but got Unknown
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Expected O, but got Unknown
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Expected O, but got Unknown
			//IL_018f: Expected O, but got Unknown
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Expected O, but got Unknown
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: Expected O, but got Unknown
			//IL_022e: Expected O, but got Unknown
			try
			{
				((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.SaveOnConfigSet = false;
				stationBuildRange = ((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.Bind<float>(new ConfigDefinition(sectionGeneral, "Build range"), 20f, new ConfigDescription("The base build range of the crafting station.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 50f), new object[1] { (object)new ConfigurationManagerAttributes
				{
					IsAdminOnly = false,
					Order = HandleOrder()
				} }));
				stationBuildRange.SettingChanged += delegate
				{
					CraftingStationTweakz.Instance.RequestUpdateCraftingStation();
				};
				stationBuildRangePerUpgrade = ((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.Bind<float>(new ConfigDefinition(sectionGeneral, "Build range increase per upgrade"), 4f, new ConfigDescription("Each upgrade increases the crafting station's build range.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(4f, 50f), new object[1] { (object)new ConfigurationManagerAttributes
				{
					IsAdminOnly = false,
					Order = HandleOrder()
				} }));
				stationBuildRangePerUpgrade.SettingChanged += delegate
				{
					CraftingStationTweakz.Instance.RequestUpdateCraftingStation();
				};
				extensionRange = ((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.Bind<float>(new ConfigDefinition(sectionGeneral, "Upgrade range"), 10f, new ConfigDescription("The maximum distance at which upgrades still connect to the crafting station.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 50f), new object[1] { (object)new ConfigurationManagerAttributes
				{
					IsAdminOnly = false,
					Order = HandleOrder()
				} }));
				extensionRange.SettingChanged += delegate
				{
					CraftingStationTweakz.Instance.RequestUpdateExtensionPieces();
				};
				((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.SaveOnConfigSet = true;
				extensionSpaceRange = ((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.Bind<float>(new ConfigDefinition(sectionGeneral, "Space requirement between upgrades"), 0f, new ConfigDescription("The minimum required spacing between upgrades in order to place them (game default is 2).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), new object[1] { (object)new ConfigurationManagerAttributes
				{
					IsAdminOnly = false,
					Order = HandleOrder()
				} }));
				extensionSpaceRange.SettingChanged += delegate
				{
					CraftingStationTweakz.Instance.RequestUpdateExtensionPieces();
				};
				configWatcher = new FileSystemWatcher(Paths.ConfigPath, CraftingStationTweakz.configFileName);
				configWatcher.Changed += OnConfigFileChange;
				configWatcher.Created += OnConfigFileChange;
				configWatcher.Renamed += OnConfigFileChange;
				configWatcher.IncludeSubdirectories = true;
				configWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
				configWatcher.EnableRaisingEvents = true;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Could not initialise general config: " + ex));
			}
		}

		private static void OnConfigFileChange(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(CraftingStationTweakz.configFileFullPath))
			{
				return;
			}
			try
			{
				((BaseUnityPlugin)CraftingStationTweakz.Instance).Config.Reload();
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Something went wrong while reloading the config, please check if the file exists and the entries are valid! " + ex));
			}
		}

		private static int HandleOrder()
		{
			entryCount--;
			if (entryCount >= 0)
			{
				return entryCount;
			}
			return 0;
		}
	}
}