Decompiled source of InterstellarLogisticsOpt v1.1.0

InterstellarLogisticsOpt.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using UXAssist.Common;
using UXAssist.UI;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("InterstellarLogisticsOpt")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Phase-disperses interstellar logistics scheduling to flatten CPU spikes / 相位分散星际物流调度以削平卡顿")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+64d7bfc247046795d471f498ffb90a266d243cdc")]
[assembly: AssemblyProduct("InterstellarLogisticsOpt")]
[assembly: AssemblyTitle("InterstellarLogisticsOpt")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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 InterstellarLogisticsOpt
{
	[BepInPlugin("org.fyyy.interstellarlogisticsopt", "InterstellarLogisticsOpt", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class InterstellarLogisticsOptPlugin : BaseUnityPlugin
	{
		private static class GalacticTransportPatch
		{
			[HarmonyPrefix]
			[HarmonyPatch(typeof(GalacticTransport), "GameTick")]
			private static bool GameTick_Prefix(GalacticTransport __instance, long time)
			{
				//IL_013b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0141: Invalid comparison between Unknown and I4
				//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
				//IL_0105: Invalid comparison between Unknown and I4
				//IL_0162: Unknown result type (might be due to invalid IL or missing references)
				//IL_0168: Invalid comparison between Unknown and I4
				//IL_0109: Unknown result type (might be due to invalid IL or missing references)
				//IL_010f: Invalid comparison between Unknown and I4
				//IL_0113: Unknown result type (might be due to invalid IL or missing references)
				//IL_0119: Invalid comparison between Unknown and I4
				if (!ModEnabled.Value)
				{
					return true;
				}
				int value = AmortizeFactor.Value;
				if (value <= 1)
				{
					return true;
				}
				GameData gameData = __instance.gameData;
				GalaxyData galaxy = gameData.galaxy;
				GameHistoryData history = gameData.history;
				PlanetFactory[] factories = gameData.factories;
				FactoryProductionStat[] factoryStatPool = gameData.statistics.production.factoryStatPool;
				TrafficStatistics traffic = gameData.statistics.traffic;
				float logisticShipSailSpeedModified = history.logisticShipSailSpeedModified;
				float num = (history.logisticShipWarpDrive ? history.logisticShipWarpSpeedModified : logisticShipSailSpeedModified);
				int logisticShipCarries = history.logisticShipCarries;
				StationComponent[] stationPool = __instance.stationPool;
				int stationCursor = __instance.stationCursor;
				for (int i = 1; i < 7; i++)
				{
					int num2 = i % 6;
					int num3;
					switch (i)
					{
					default:
						num3 = 60;
						break;
					case 2:
					case 3:
						num3 = 30;
						break;
					case 1:
						num3 = 10;
						break;
					}
					int num4 = num3 * value;
					int num5 = (int)(time % num4);
					for (int j = 1 + num5; j < stationCursor; j += num4)
					{
						StationComponent val = stationPool[j];
						if (val != null && val.id > 0 && val.gid == j)
						{
							if (num2 >= 1 && num2 <= 4 && ((int)val.routePriority == 2 || (int)val.routePriority == 3 || (int)val.routePriority == 4))
							{
								val.DetermineDispatch(logisticShipSailSpeedModified, num, logisticShipCarries, num2, stationPool, factoryStatPool, factories, galaxy, traffic);
							}
							else if (num2 == 5 && (int)val.routePriority == 2)
							{
								val.DetermineDispatch(logisticShipSailSpeedModified, num, logisticShipCarries, num2, stationPool, factoryStatPool, factories, galaxy, traffic);
							}
							else if (num2 == 0 && (int)val.routePriority == 1)
							{
								val.DetermineDispatch(logisticShipSailSpeedModified, num, logisticShipCarries, num2, stationPool, factoryStatPool, factories, galaxy, traffic);
							}
						}
					}
				}
				return false;
			}
		}

		private static class DispatchPatch
		{
			[HarmonyPrefix]
			[HarmonyPatch(typeof(StationComponent), "DetermineDispatch")]
			private static bool DetermineDispatch_Prefix(StationComponent __instance)
			{
				if (!ModEnabled.Value)
				{
					return true;
				}
				if (__instance.idleShipCount == 0 || __instance.energy <= 6000000)
				{
					return false;
				}
				return true;
			}
		}

		private static class UIConfig
		{
			public static void Init()
			{
				I18N.Add("InterstellarLogisticsOpt", "InterstellarLogisticsOpt", "星际物流优化");
				I18N.Add("Phase-disperse interstellar logistics scheduling", "Phase-disperse interstellar logistics scheduling", "相位分散星际物流调度以削平卡顿");
				I18N.Add("Amortize factor (simulated frames)", "Amortize factor (simulated frames)", "调度间隔系数(模拟帧)");
				I18N.Apply();
				MyConfigWindow.OnUICreated = (Action<MyConfigWindow, RectTransform>)Delegate.Combine(MyConfigWindow.OnUICreated, new Action<MyConfigWindow, RectTransform>(CreateUI));
			}

			private static void CreateUI(MyConfigWindow wnd, RectTransform trans)
			{
				((MyWindowWithTabs)wnd).AddSplitter(trans, 10f);
				((MyWindowWithTabs)wnd).AddTabGroup(trans, "InterstellarLogisticsOpt", "tab-group-interstellarlogisticsopt");
				RectTransform val = ((MyWindowWithTabs)wnd).AddTab(trans, "InterstellarLogisticsOpt");
				float num = 10f;
				((MyWindow)wnd).AddCheckBox(0f, num, val, ModEnabled, "Phase-disperse interstellar logistics scheduling", 15);
				num += 36f;
				Text val2 = ((MyWindow)wnd).AddText2(0f, num + 5f, val, "Amortize factor (simulated frames)", 14, "label");
				((MyWindow)wnd).AddSlider<int>(0f + val2.preferredWidth + 10f, num, val, AmortizeFactor, (ValueMapper<int>)(object)new RangeValueMapper<int>(1, 30), "0", 200f).WithSmallerHandle(10f, 0f);
			}
		}

		public static readonly ManualLogSource Logger = Logger.CreateLogSource("InterstellarLogisticsOpt");

		public static ConfigEntry<bool> ModEnabled;

		public static ConfigEntry<int> AmortizeFactor;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Phase-disperse interstellar logistics scheduling to flatten CPU spikes / 相位分散星际物流调度以削平卡顿");
			AmortizeFactor = ((BaseUnityPlugin)this).Config.Bind<int>("General", "AmortizeFactor", 1, new ConfigDescription("Scheduling amortization (\"simulated frames\"). 1 = off: runs the vanilla scheduler unchanged (recommended baseline). 2+ enables phase-dispersed amortization — each tower is scheduled every period*factor ticks (e.g. 5 = every 50/150/300 instead of 10/30/60), cutting total CPU at the cost of logistics responsiveness; load stays evenly spread across ticks (no spikes). Requires Enabled = true. / 调度平摊系数(\"模拟帧\")。1=关闭:原版调度逻辑不变(推荐基线)。2 及以上启用相位分散平摊——每塔改为每 period*factor tick 调度一次(如 5 = 每 50/150/300 而非 10/30/60),用调度响应速度换取更低的总 CPU,且负载仍逐帧平摊不产生尖峰。需 Enabled=true。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 30), Array.Empty<object>()));
			_harmony = new Harmony("org.fyyy.interstellarlogisticsopt");
			_harmony.PatchAll(typeof(GalacticTransportPatch));
			_harmony.PatchAll(typeof(DispatchPatch));
			UIConfig.Init();
			Logger.LogInfo((object)"InterstellarLogisticsOpt loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "org.fyyy.interstellarlogisticsopt";

		public const string PLUGIN_NAME = "InterstellarLogisticsOpt";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}