Decompiled source of StationLaunchSystem v1.0.2

StationLaunchSystem.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using DysonSphereMods.Shared;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Valoneu")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyDescription("Auto-launch rockets and sails from ILS stations to the Dyson sphere.")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+f7c7e874f0e58613a08732df52f0a23d8afbf545")]
[assembly: AssemblyProduct("DysonSphereMods")]
[assembly: AssemblyTitle("StationLaunchSystem")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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 DysonSphereMods.Shared
{
	public static class Log
	{
		private static ManualLogSource _logger;

		public static void Init(ManualLogSource logger)
		{
			_logger = logger;
		}

		public static void Debug(object data)
		{
			ManualLogSource logger = _logger;
			if (logger != null)
			{
				logger.LogDebug(data);
			}
		}

		public static void Info(object data)
		{
			ManualLogSource logger = _logger;
			if (logger != null)
			{
				logger.LogInfo(data);
			}
		}

		public static void Warning(object data)
		{
			ManualLogSource logger = _logger;
			if (logger != null)
			{
				logger.LogWarning(data);
			}
		}

		public static void Error(object data)
		{
			ManualLogSource logger = _logger;
			if (logger != null)
			{
				logger.LogError(data);
			}
		}

		public static void Fatal(object data)
		{
			ManualLogSource logger = _logger;
			if (logger != null)
			{
				logger.LogFatal(data);
			}
		}

		public static void Message(object data)
		{
			ManualLogSource logger = _logger;
			if (logger != null)
			{
				logger.LogMessage(data);
			}
		}

		public static void LogOnce(string msg, ref bool flag, params object[] args)
		{
			if (flag)
			{
				return;
			}
			flag = true;
			try
			{
				string[] array = ((args == null) ? Array.Empty<string>() : args.Select((object arg) => (arg != null) ? ((!(arg is int) && !(arg is string) && !arg.GetType().IsPrimitive) ? JsonUtility.ToJson(arg) : arg.ToString()) : "null").ToArray());
				object[] args2 = array;
				Info(string.Format(msg, args2));
			}
			catch (Exception arg2)
			{
				Warning($"LogOnce failed to format message: {msg}. Exception: {arg2}");
			}
		}
	}
	public static class MultiplierService
	{
		private static readonly Dictionary<string, float> _multipliers = new Dictionary<string, float>();

		private static bool _isDirty;

		public static event Action OnMultipliersChanged;

		public static void SetMultiplier(string key, float value)
		{
			if (!_multipliers.TryGetValue(key, out var value2) || Math.Abs(value2 - value) > 0.0001f)
			{
				_multipliers[key] = value;
				_isDirty = true;
			}
		}

		public static float GetMultiplier(string key, float defaultValue = 1f)
		{
			if (!_multipliers.TryGetValue(key, out var value))
			{
				return defaultValue;
			}
			return value;
		}

		public static void CommitChanges()
		{
			if (_isDirty)
			{
				_isDirty = false;
				MultiplierService.OnMultipliersChanged?.Invoke();
			}
		}
	}
	public static class TickManager
	{
		private static long _lastSlowTick = -1L;

		private static long _lastLazyTick = -1L;

		private static bool _patched = false;

		public static event Action OnSlowTick;

		public static event Action OnLazyTick;

		public static void Patch(Harmony harmony)
		{
			if (!_patched)
			{
				_patched = true;
				harmony.PatchAll(typeof(TickManager));
			}
		}

		[HarmonyPatch(typeof(GameMain), "Begin")]
		[HarmonyPostfix]
		public static void Init()
		{
			_lastSlowTick = -1L;
			_lastLazyTick = -1L;
		}

		[HarmonyPatch(typeof(GameLogic), "LogicFrame")]
		[HarmonyPostfix]
		public static void GameTick()
		{
			long gameTick = GameMain.gameTick;
			if (gameTick / 60 > _lastSlowTick)
			{
				_lastSlowTick = gameTick / 60;
				TickManager.OnSlowTick?.Invoke();
			}
			if (gameTick / 600 > _lastLazyTick)
			{
				_lastLazyTick = gameTick / 600;
				TickManager.OnLazyTick?.Invoke();
			}
		}
	}
	public abstract class WindowBase
	{
		public Rect WindowRect;

		protected Vector2 ScrollPos;

		private bool _isResizing;

		private Rect _resizeRect = new Rect(0f, 0f, 15f, 15f);

		public Vector2 MinSize = new Vector2(300f, 200f);

		public int WindowId { get; protected set; }

		public string Title { get; set; }

		public bool IsVisible { get; set; }

		protected WindowBase(int windowId, string title, Rect defaultRect)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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)
			WindowId = windowId;
			Title = title;
			WindowRect = defaultRect;
		}

		public virtual void OnGUI()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			if (IsVisible)
			{
				GUI.backgroundColor = new Color(0.08f, 0.12f, 0.22f, 0.95f);
				WindowRect = GUILayout.Window(WindowId, WindowRect, new WindowFunction(DrawWindowInternal), Title, Array.Empty<GUILayoutOption>());
				GUI.backgroundColor = Color.white;
				((Rect)(ref WindowRect)).x = Mathf.Clamp(((Rect)(ref WindowRect)).x, 0f - ((Rect)(ref WindowRect)).width + 50f, (float)(Screen.width - 50));
				((Rect)(ref WindowRect)).y = Mathf.Clamp(((Rect)(ref WindowRect)).y, -20f, (float)(Screen.height - 50));
			}
		}

		private void DrawWindowInternal(int id)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			//IL_00dd: Expected O, but got Unknown
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Invalid comparison between Unknown and I4
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Invalid comparison between Unknown and I4
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			DrawWindowHeader();
			ScrollPos = GUILayout.BeginScrollView(ScrollPos, Array.Empty<GUILayoutOption>());
			DrawWindowContent();
			GUILayout.EndScrollView();
			DrawWindowFooter();
			((Rect)(ref _resizeRect)).x = ((Rect)(ref WindowRect)).width - 20f;
			((Rect)(ref _resizeRect)).y = ((Rect)(ref WindowRect)).height - 20f;
			((Rect)(ref _resizeRect)).width = 20f;
			((Rect)(ref _resizeRect)).height = 20f;
			GUI.Label(_resizeRect, "↘", new GUIStyle(GUI.skin.label)
			{
				alignment = (TextAnchor)4,
				fontSize = 20,
				normal = new GUIStyleState
				{
					textColor = new Color(0.6f, 0.6f, 0.6f, 0.8f)
				}
			});
			Event current = Event.current;
			bool flag = false;
			if ((int)current.type == 0 && ((Rect)(ref _resizeRect)).Contains(current.mousePosition))
			{
				_isResizing = true;
				flag = true;
				current.Use();
			}
			else if ((int)current.type == 1)
			{
				_isResizing = false;
			}
			else if ((int)current.type == 3 && _isResizing)
			{
				ref Rect windowRect = ref WindowRect;
				((Rect)(ref windowRect)).width = ((Rect)(ref windowRect)).width + current.delta.x;
				ref Rect windowRect2 = ref WindowRect;
				((Rect)(ref windowRect2)).height = ((Rect)(ref windowRect2)).height + current.delta.y;
				((Rect)(ref WindowRect)).width = Mathf.Max(MinSize.x, ((Rect)(ref WindowRect)).width);
				((Rect)(ref WindowRect)).height = Mathf.Max(MinSize.y, ((Rect)(ref WindowRect)).height);
				current.Use();
			}
			if ((int)current.type == 0 && !flag)
			{
				GUIUtility.keyboardControl = 0;
			}
			GUI.DragWindow();
		}

		protected virtual void DrawWindowHeader()
		{
		}

		protected abstract void DrawWindowContent();

		protected virtual void DrawWindowFooter()
		{
		}

		public virtual void Toggle()
		{
			IsVisible = !IsVisible;
		}
	}
}
namespace StationLaunchSystem
{
	[BepInPlugin("com.Valoneu.StationLaunchSystem", "StationLaunchSystem", "1.0.2")]
	[BepInProcess("DSPGAME.exe")]
	public class StationLaunchSystemPlugin : BaseUnityPlugin
	{
		public const string GUID = "com.Valoneu.StationLaunchSystem";

		public const string NAME = "StationLaunchSystem";

		public const string VERSION = "1.0.2";

		public static ConfigEntry<bool> ModEnabled;

		public static ConfigEntry<int> RocketsPerTick;

		public static ConfigEntry<int> SailsPerTick;

		private void Awake()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			Log.Init(((BaseUnityPlugin)this).Logger);
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable/disable automatic launching from depot stations.");
			RocketsPerTick = ((BaseUnityPlugin)this).Config.Bind<int>("General", "RocketsPerTick", 5, "Max rockets per tick per station.");
			SailsPerTick = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SailsPerTick", 20, "Max solar sails per tick per station.");
			new Harmony("com.Valoneu.StationLaunchSystem").PatchAll(typeof(GameBeginPatch));
			Log.Info("StationLaunchSystem v1.0.2 loaded!");
		}
	}
	[HarmonyPatch]
	public static class GameBeginPatch
	{
		private static bool _subscribed;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameMain), "Begin")]
		public static void OnGameBegin()
		{
			try
			{
				if (_subscribed)
				{
					GameMain.logic.onFactoryFrameEnd -= LaunchLogic.OnFactoryFrameEnd;
					_subscribed = false;
				}
				GameMain.logic.onFactoryFrameEnd += LaunchLogic.OnFactoryFrameEnd;
				_subscribed = true;
				Log.Info("[StationLaunch] Subscribed to onFactoryFrameEnd");
			}
			catch (Exception ex)
			{
				Log.Info("[StationLaunch] ERROR subscribing: " + ex.Message);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameMain), "End")]
		public static void OnGameEnd()
		{
			try
			{
				if (_subscribed)
				{
					GameMain.logic.onFactoryFrameEnd -= LaunchLogic.OnFactoryFrameEnd;
					_subscribed = false;
				}
			}
			catch
			{
			}
		}
	}
	public static class LaunchLogic
	{
		private struct ConstructibleShell
		{
			public DysonShell shell;

			public float absY;
		}

		private static readonly int[] ROCKET_ITEM_IDS = new int[7] { 1503, 9488, 9489, 9490, 9491, 9492, 9510 };

		private static readonly int[] SAIL_ITEM_IDS = new int[2] { 1501, 6006 };

		private static Dictionary<int, (long tick, List<ConstructibleShell> shells)> _shellCaches = new Dictionary<int, (long, List<ConstructibleShell>)>();

		private static bool IsRocketItem(int id)
		{
			for (int i = 0; i < ROCKET_ITEM_IDS.Length; i++)
			{
				if (ROCKET_ITEM_IDS[i] == id)
				{
					return true;
				}
			}
			return false;
		}

		private static bool IsSailItem(int id)
		{
			for (int i = 0; i < SAIL_ITEM_IDS.Length; i++)
			{
				if (SAIL_ITEM_IDS[i] == id)
				{
					return true;
				}
			}
			return false;
		}

		private static List<ConstructibleShell> GetShellCache(DysonSphere sphere, long tick)
		{
			if (_shellCaches.TryGetValue(sphere.starData.index, out (long, List<ConstructibleShell>) value) && tick - value.Item1 < 120)
			{
				return value.Item2;
			}
			List<ConstructibleShell> list = new List<ConstructibleShell>();
			if (sphere.layersIdBased == null)
			{
				return list;
			}
			for (int i = 1; i < sphere.layersIdBased.Length; i++)
			{
				DysonSphereLayer val = sphere.layersIdBased[i];
				if (val == null || val.id != i)
				{
					continue;
				}
				for (int j = 1; j < val.shellCursor; j++)
				{
					DysonShell val2 = val.shellPool[j];
					if (val2 == null || val2.id != j || !IsShellReady(val2) || val2.nodes == null || val2.nodes.Count == 0 || val2.nodecps == null)
					{
						continue;
					}
					bool flag = true;
					for (int k = 0; k < val2.nodes.Count; k++)
					{
						int num = (val2.vertsqOffset[k + 1] - val2.vertsqOffset[k]) * val2.cpPerVertex;
						if (val2.nodecps[k] < num)
						{
							flag = false;
							break;
						}
					}
					if (flag)
					{
						continue;
					}
					float num2 = 0f;
					if (val2.nodes == null || val2.nodes.Count <= 0)
					{
						continue;
					}
					foreach (DysonNode node in val2.nodes)
					{
						num2 += Math.Abs(node.pos.y);
					}
					list.Add(new ConstructibleShell
					{
						shell = val2,
						absY = num2 / (float)val2.nodes.Count
					});
				}
			}
			list.Sort((ConstructibleShell a, ConstructibleShell b) => a.absY.CompareTo(b.absY));
			_shellCaches[sphere.starData.index] = (tick, list);
			return list;
		}

		public static void OnFactoryFrameEnd()
		{
			try
			{
				if (!StationLaunchSystemPlugin.ModEnabled.Value)
				{
					return;
				}
				GameData data = GameMain.data;
				if (data == null)
				{
					return;
				}
				long gameTick = GameMain.gameTick;
				if (gameTick % 10 != 0L)
				{
					return;
				}
				for (int i = 0; i < data.factoryCount; i++)
				{
					PlanetFactory val = data.factories[i];
					if (val?.transport == null || val.planet == null)
					{
						continue;
					}
					int num = val.planet.star?.index ?? (-1);
					if (num < 0 || num >= data.dysonSpheres.Length)
					{
						continue;
					}
					DysonSphere val2 = data.dysonSpheres[num];
					if (val2 == null)
					{
						continue;
					}
					GameStatData statistics = GameMain.statistics;
					object obj;
					if (statistics == null)
					{
						obj = null;
					}
					else
					{
						ProductionStatistics production = statistics.production;
						if (production == null)
						{
							obj = null;
						}
						else
						{
							FactoryProductionStat[] factoryStatPool = production.factoryStatPool;
							obj = ((factoryStatPool == null) ? null : factoryStatPool[val.index]?.consumeRegister);
						}
					}
					int[] consumeReg = (int[])obj;
					LaunchRockets(val, val2, consumeReg, gameTick);
					LaunchSails(val, val2, consumeReg, gameTick);
				}
			}
			catch (Exception ex)
			{
				Log.Info("[StationLaunch] ERROR: " + ex.Message);
			}
		}

		private static void LaunchRockets(PlanetFactory factory, DysonSphere sphere, int[] consumeReg, long tick)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			int num = StationLaunchSystemPlugin.RocketsPerTick.Value * 10;
			if (num <= 0 || sphere.GetAutoNodeCount() <= 0)
			{
				return;
			}
			for (int i = 1; i < factory.transport.stationCursor; i++)
			{
				StationComponent val = factory.transport.stationPool[i];
				if (val == null || val.id != i || !val.isStellar || val.isCollector)
				{
					continue;
				}
				for (int j = 0; j < val.storage.Length; j++)
				{
					if ((int)val.storage[j].localLogic != 0 || !IsRocketItem(val.storage[j].itemId) || val.storage[j].count <= 0)
					{
						continue;
					}
					int itemId = val.storage[j].itemId;
					int num2 = Math.Min(num, val.storage[j].count);
					int num3 = 0;
					for (int k = 0; k < num2; k++)
					{
						if (sphere.GetAutoNodeCount() <= 0)
						{
							break;
						}
						DysonNode autoDysonNode = sphere.GetAutoDysonNode(i * 17 + k);
						if (autoDysonNode == null)
						{
							break;
						}
						sphere.OrderConstructSp(autoDysonNode);
						sphere.ConstructSp(autoDysonNode);
						num3++;
					}
					if (num3 <= 0)
					{
						continue;
					}
					val.storage[j].count -= num3;
					if (consumeReg != null)
					{
						lock (consumeReg)
						{
							consumeReg[itemId] += num3;
						}
					}
				}
			}
		}

		private static bool IsShellReady(DysonShell shell)
		{
			if (shell == null || shell.nodes == null || shell.frames == null)
			{
				return false;
			}
			foreach (DysonNode node in shell.nodes)
			{
				if (node.sp < node.spMax)
				{
					return false;
				}
			}
			foreach (DysonFrame frame in shell.frames)
			{
				if (frame.spA + frame.spB < frame.spMax)
				{
					return false;
				}
			}
			return true;
		}

		private static void LaunchSails(PlanetFactory factory, DysonSphere sphere, int[] consumeReg, long tick)
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			int num = StationLaunchSystemPlugin.SailsPerTick.Value * 10;
			if (num <= 0)
			{
				return;
			}
			List<ConstructibleShell> shellCache = GetShellCache(sphere, tick);
			if (shellCache.Count == 0)
			{
				return;
			}
			int num2 = -1;
			int[] productRegister = sphere.productRegister;
			for (int i = 1; i < factory.transport.stationCursor; i++)
			{
				StationComponent val = factory.transport.stationPool[i];
				if (val == null || val.id != i || !val.isStellar || val.isCollector)
				{
					continue;
				}
				int num3 = 0;
				for (int j = 0; j < val.storage.Length; j++)
				{
					if ((int)val.storage[j].localLogic != 0 || !IsSailItem(val.storage[j].itemId) || val.storage[j].count <= 0)
					{
						continue;
					}
					num2 = val.storage[j].itemId;
					int num4 = Math.Min(num - num3, val.storage[j].count);
					if (num4 <= 0)
					{
						continue;
					}
					int num5 = 0;
					foreach (ConstructibleShell item in shellCache)
					{
						DysonShell shell = item.shell;
						for (int k = 0; k < shell.nodes.Count; k++)
						{
							int num6 = (shell.vertsqOffset[k + 1] - shell.vertsqOffset[k]) * shell.cpPerVertex;
							while (shell.nodecps[k] < num6 && num5 < num4)
							{
								shell.Construct(k, true);
								num5++;
								if (productRegister != null)
								{
									lock (productRegister)
									{
										productRegister[11903]++;
									}
								}
							}
							if (num5 >= num4)
							{
								break;
							}
						}
						if (num5 >= num4)
						{
							break;
						}
					}
					if (num5 > 0)
					{
						val.storage[j].count -= num5;
						num3 += num5;
						if (consumeReg != null)
						{
							lock (consumeReg)
							{
								consumeReg[num2] += num5;
							}
						}
					}
					if (num3 >= num)
					{
						break;
					}
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.Valoneu.StationLaunchSystem";

		public const string PLUGIN_NAME = "StationLaunchSystem";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}