Decompiled source of DistributeSpaceWarpers v1.0.7

DistributeSpaceWarper.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("DistributeSpaceWarper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DistributeSpaceWarper")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("DD23704E-3C0C-4D6B-8EB1-F847CFB2BC2D")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DistributeSpaceWarper
{
	public static class Config
	{
		public static class General
		{
			public static ConfigEntry<bool> WarperRemoteMode;

			public static ConfigEntry<bool> WarperTransportCost;

			public static ConfigEntry<int> WarperRemoteTransportCost;

			public static ConfigEntry<int> WarperTickCount;

			public static ConfigEntry<int> WarperLocalTransportCost;

			public static ConfigEntry<bool> WarpersRequiredToggleAutomation;
		}

		public static class Utility
		{
			public static ConfigEntry<bool> DisableMod;

			public static ConfigEntry<bool> UninstallMod;
		}

		private static readonly string GENERAL_SECTION = "General";

		private static readonly string UTILITY_SECTION = "Utility";

		internal static void Init(ConfigFile config)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Expected O, but got Unknown
			General.WarperTickCount = config.Bind<int>(GENERAL_SECTION, "WarperTickCount", 60, new ConfigDescription("Default number of ticks before distributing warpers. Note: Maximum of 260, defaults to 60", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 275), new object[1]
			{
				new { }
			}));
			General.WarperRemoteMode = config.Bind<bool>(GENERAL_SECTION, "WarperRemoteMode", false, "By default only search local ILS/PLS for supplies. Enable this to get Warpers from different planets as well");
			General.WarperTransportCost = config.Bind<bool>(GENERAL_SECTION, "WarperTransportCost", true, "If enabled, transporting Warpers costs 1 warper. Disable for moving Warpers at no costs.");
			General.WarpersRequiredToggleAutomation = config.Bind<bool>(GENERAL_SECTION, "WarpersRequiredToggleAutomation", true, "If enabled, when `Warpers Required` toggle ticked on, this will auto fill the warper slot. When toggle is ticked off this will stop filling the wraper slot from suppliers");
			General.WarperLocalTransportCost = config.Bind<int>(GENERAL_SECTION, "WarperLocalTransportCost", 1, new ConfigDescription("If enabled, transporting Warpers costs 1 warper. Disable for moving Warpers at no costs.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), new object[1]
			{
				new { }
			}));
			General.WarperRemoteTransportCost = config.Bind<int>(GENERAL_SECTION, "WarperRemoteTransportCost", 2, new ConfigDescription("Default cost of transporting Warpers from different planets. Note: Maximum of 10, defaults to 2", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), new object[1]
			{
				new { }
			}));
			Utility.DisableMod = config.Bind<bool>(UTILITY_SECTION, "DisableMod", false, "While true this will disable all mod effects but will not remove additional slot from ILS. Useful if uninstalling mod failed for some reason.");
			Utility.UninstallMod = config.Bind<bool>(UTILITY_SECTION, "UninstallMod", false, "WARNING!!! BACKUP YOUR SAVE BEFORE DOING THIS!!! This will not work if mod cannot load properly! If this is true, mod will remove additional slot from all current ILS. This will destroy any items in additional slot To correctly uninstall mod and get vanilla save please follow this steps. Step #1: Set UninstallMod to true. Step #2: Load your save. Step #3: Save your game. Step #4: Exit the game and remove this mod.");
		}
	}
	public static class ModDebug
	{
		private static ManualLogSource Logger { get; set; }

		public static void SetLogger(ManualLogSource logger)
		{
			Logger = logger;
		}

		public static void Assert(bool condition)
		{
		}

		public static void Log(object message)
		{
			Logger.Log((LogLevel)16, message);
		}

		public static void Error(object message)
		{
			Logger.Log((LogLevel)2, message);
		}

		public static void Trace(object message)
		{
			Logger.Log((LogLevel)16, (object)("DISTR_SPACE_WARP-" + message));
		}

		public static void LogPlanetType(PlanetData planet)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected I4, but got Unknown
			EPlanetType type = planet.type;
			EPlanetType val = type;
			switch ((int)val)
			{
			case 5:
				Log("Gas");
				break;
			case 3:
				Log("Desert");
				break;
			case 4:
				Log("Ice");
				break;
			case 2:
				Log("Ocean");
				break;
			case 1:
				Log("Vocano");
				break;
			case 0:
				Log("None");
				break;
			}
		}

		public static void LogCmdMode(int mode)
		{
			switch (mode)
			{
			case -1:
				Log("CmdMode: Destruct Mode");
				break;
			case -2:
				Log("CmdMode: Upgrade Mode");
				break;
			case 1:
				Log("CmdMode: Normal Build Mode");
				break;
			case 2:
				Log("CmdMode: Build Mode - Belt");
				break;
			case 3:
				Log("CmdMode: Build Mode - Inserter");
				break;
			case 4:
				Log("CmdMode: Build Mode - Ground");
				break;
			case 0:
				break;
			}
		}
	}
	internal static class Patch
	{
		private static bool ModDisabled => Config.Utility.DisableMod.Value || Config.Utility.UninstallMod.Value;

		private static bool RemoteTransfer => Config.General.WarperRemoteMode.Value;

		public static int WarperTickCount => Config.General.WarperTickCount.Value;

		public static bool WarperTransportCost => Config.General.WarperTransportCost.Value;

		public static int WarperLocalTransportCost => Config.General.WarperLocalTransportCost.Value;

		public static int WarperRemoteTransportCost => Config.General.WarperRemoteTransportCost.Value;

		[HarmonyPatch(typeof(GalacticTransport), "GameTick")]
		[HarmonyPostfix]
		public static void GalacticTransport_GameTick_Postfix(GalacticTransport __instance)
		{
			try
			{
				if (ModDisabled || Time.frameCount % WarperTickCount != 0 || __instance.gameData == null)
				{
					return;
				}
				int warperId = 1210;
				int maxWarperCount = 50;
				List<StationComponent> list = new List<StationComponent>();
				PlanetFactory[] factories = __instance.gameData.factories;
				foreach (PlanetFactory val in factories)
				{
					if (val == null || val.transport == null)
					{
						continue;
					}
					PlanetTransport transport = val.transport;
					for (int j = 1; j < transport.stationCursor; j++)
					{
						StationComponent val2 = transport.stationPool[j];
						if (val2 != null && val2.id == j && !val2.isCollector && (val2.isStellar || (RemoteTransfer && IsRemoteWarperSupplier(val2, warperId)) || val2.storage.Any((StationStore s) => s.itemId == warperId && ((int)s.localLogic == 1 || (int)s.remoteLogic == 1))))
						{
							list.Add(val2);
						}
					}
				}
				if (RemoteTransfer)
				{
					StationComponent[] stationPool = __instance.stationPool;
					foreach (StationComponent val3 in stationPool)
					{
						if (val3 != null && !val3.isCollector && val3.isStellar && val3.storage.Any((StationStore s) => s.itemId == warperId && (int)s.remoteLogic == 1) && !list.Contains(val3))
						{
							list.Add(val3);
						}
					}
				}
				List<StationComponent> list2 = new List<StationComponent>();
				List<StationComponent> list3 = new List<StationComponent>();
				IdentifyStations(list, list2, list3, warperId, maxWarperCount);
				foreach (StationComponent item in list3)
				{
					TransferWarpersToReceiver(item, list2, warperId, maxWarperCount);
				}
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[DistributeSpaceWarper] Error during GalacticTransport GameTick: " + ex.Message + "\n" + ex.StackTrace));
			}
		}

		private static void IdentifyStations(List<StationComponent> stations, List<StationComponent> supplierStations, List<StationComponent> receiverStations, int warperId, int maxWarperCount)
		{
			foreach (StationComponent station in stations)
			{
				if (IsWarperSupplier(station, warperId))
				{
					Debug.Log((object)$"Station {station.id} is added to supplier stations.");
					supplierStations.Add(station);
				}
				else if (NeedsWarpers(station, maxWarperCount))
				{
					Debug.Log((object)$"Station {station.id} needs warpers.");
					receiverStations.Add(station);
				}
			}
		}

		private static bool IsWarperSupplier(StationComponent station, int warperId)
		{
			bool flag = station.warperCount > 0 && station.storage.Any((StationStore s) => s.itemId == warperId && (int)s.localLogic == 1);
			if (flag)
			{
				Debug.Log((object)$"Station {station.id} is identified as a warper supplier.");
			}
			return flag;
		}

		private static bool IsRemoteWarperSupplier(StationComponent station, int warperId)
		{
			bool flag = station.storage.Any((StationStore s) => s.itemId == warperId && (int)s.remoteLogic == 1);
			if (flag)
			{
				Debug.Log((object)$"Station {station.id} is identified as a remote warper supplier.");
			}
			return flag;
		}

		private static bool NeedsWarpers(StationComponent station, int maxWarperCount)
		{
			bool flag = station.warperCount < maxWarperCount && station.warperNecessary;
			if (flag)
			{
				Debug.Log((object)$"Station {station.id} needs warpers (current: {station.warperCount}, max: {maxWarperCount}).");
			}
			return flag;
		}

		private static void TransferWarpersToReceiver(StationComponent receiver, List<StationComponent> suppliers, int warperId, int maxWarperCount)
		{
			int num = maxWarperCount - receiver.warperCount;
			Debug.Log((object)$"Receiver station {receiver.id} needs {num} warpers.");
			if (num <= 0)
			{
				Debug.Log((object)$"Receiver station {receiver.id} does not need more warpers.");
				return;
			}
			int num7 = default(int);
			foreach (StationComponent supplier in suppliers)
			{
				if (supplier.warperCount <= 0)
				{
					Debug.Log((object)$"Supplier station {supplier.id} has no warpers left to transfer.");
					continue;
				}
				int num2 = CalculateTransportCost(supplier, receiver);
				Debug.Log((object)$"Transport cost from supplier {supplier.id} to receiver {receiver.id} is {num2}.");
				int num3 = supplier.storage.Where((StationStore s) => s.itemId == warperId && ((int)s.localLogic == 1 || (int)s.remoteLogic == 1)).Sum((StationStore s) => s.count);
				int num4 = num3 - num2;
				int num5 = Mathf.Min(num, num4);
				num5 = Mathf.Min(num5, num3 - num2);
				if (num5 <= 0)
				{
					Debug.Log((object)$"No transferable warpers from supplier {supplier.id} to receiver {receiver.id}.");
					continue;
				}
				int num6 = num5 + num2;
				Debug.Log((object)$"Removing {num6} warpers from supplier station {supplier.id}.");
				supplier.TakeItem(ref warperId, ref num6, ref num7);
				receiver.warperCount += num5;
				Debug.Log((object)$"Added {num5} warpers to receiver station {receiver.id} (new count: {receiver.warperCount}).");
				UpdateTraffic(receiver);
				num -= num5;
				if (num > 0)
				{
					continue;
				}
				Debug.Log((object)$"Receiver station {receiver.id} has received enough warpers.");
				break;
			}
		}

		private static int CalculateTransportCost(StationComponent supplier, StationComponent receiver)
		{
			return WarperTransportCost ? ((supplier.planetId == receiver.planetId) ? WarperLocalTransportCost : WarperRemoteTransportCost) : 0;
		}

		private static void UpdateTraffic(StationComponent receiver)
		{
			receiver.UpdateNeeds();
			if (GameMain.data == null)
			{
				return;
			}
			GalaxyData galaxy = GameMain.data.galaxy;
			PlanetData val = ((galaxy != null) ? galaxy.PlanetById(receiver.planetId) : null);
			if (val?.factory?.transport != null)
			{
				try
				{
					val.factory.transport.RefreshStationTraffic(0);
					val.factory.transport.RefreshDispenserTraffic(0);
				}
				catch (Exception)
				{
				}
			}
			if (GameMain.data.galacticTransport != null)
			{
				GameMain.data.galacticTransport.RefreshTraffic(receiver.gid);
			}
		}
	}
	[BepInPlugin("BumpyClock.DSP.DistributeSpaceWarper", "Distribute Space Warper", "1.0.7")]
	[BepInProcess("DSPGAME.exe")]
	public class Plugin : BaseUnityPlugin
	{
		private static ManualLogSource _logger;

		private const string PluginGuid = "BumpyClock.DSP.DistributeSpaceWarper";

		private const string PluginName = "Distribute Space Warper";

		private const string PluginVersion = "1.0.7";

		public void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("BumpyClock.DSP.DistributeSpaceWarper");
			Config.Init(((BaseUnityPlugin)this).Config);
			val.PatchAll(typeof(Plugin));
			val.PatchAll(typeof(Patch));
		}

		public void Start()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			_logger.LogInfo((object)"Loaded!");
			ModDebug.SetLogger(_logger);
			Debugger.Break();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "DistributeSpaceWarper";

		public const string PLUGIN_NAME = "DistributeSpaceWarper";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}