Decompiled source of PlanetMinerNew v1.0.0

PlanetMiner.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 PlanetMiner
{
	[BepInPlugin("crecheng.PlanetMiner", "PlanetMiner", "3.1.4")]
	public class PlanetMiner : BaseUnityPlugin
	{
		public const string Version = "3.1.4";

		public const int uesEnergy = 20000000;

		private static long frame;

		private static double costFrac;

		private void Start()
		{
			Harmony.CreateAndPatchAll(typeof(PlanetMiner), (string)null);
		}

		private void Update()
		{
			frame++;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTickLabResearchMode")]
		private static void Miner(FactorySystem __instance)
		{
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			//IL_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Invalid comparison between Unknown and I4
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e8: Invalid comparison between Unknown and I4
			bool flag = false;
			try
			{
				Type type = Type.GetType("Weaver.Optimizations.IOptimizedPlanet, DSP_Weaver");
				if (type != null)
				{
					MethodInfo methodInfo = Type.GetType("Weaver.Optimizations.OptimizedStarCluster, DSP_Weaver")?.GetMethod("GetOptimizedPlanet", BindingFlags.Static | BindingFlags.Public);
					if (methodInfo != null)
					{
						object obj = methodInfo.Invoke(null, new object[1] { __instance.planet });
						if (obj != null)
						{
							PropertyInfo property = type.GetProperty("Status");
							if (property != null)
							{
								object value = property.GetValue(obj);
								Type type2 = Type.GetType("Weaver.Optimizations.OptimizedPlanetStatus, DSP_Weaver");
								if (type2 != null)
								{
									object obj2 = Enum.Parse(type2, "Running");
									flag = value.Equals(obj2);
								}
							}
						}
					}
				}
			}
			catch
			{
			}
			if (flag)
			{
				return;
			}
			GameHistoryData history = GameMain.history;
			float miningSpeedScale = history.miningSpeedScale;
			if (miningSpeedScale <= 0f)
			{
				return;
			}
			int num = (int)(120f / miningSpeedScale);
			num = ((num <= 0) ? 1 : num);
			if (frame % num != 0)
			{
				return;
			}
			VeinData[] veinPool = __instance.factory.veinPool;
			Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>();
			for (int i = 0; i < veinPool.Length; i++)
			{
				VeinData val = veinPool[i];
				if (val.amount > 0 && val.productId > 0)
				{
					AddVeinData(dictionary, val.productId, i);
				}
			}
			float miningCostRate = history.miningCostRate;
			PlanetTransport transport = __instance.planet.factory.transport;
			FactoryProductionStat val2 = GameMain.statistics.production.factoryStatPool[__instance.factory.index];
			int[] array = val2?.productRegister;
			StationComponent[] stationPool = transport.stationPool;
			foreach (StationComponent val3 in stationPool)
			{
				if (val3?.storage == null)
				{
					continue;
				}
				for (int k = 0; k < val3.storage.Length; k++)
				{
					StationStore val4 = val3.storage[k];
					int itemId = val4.itemId;
					if (val4.count < 0)
					{
						val3.storage[k].count = 0;
					}
					if ((int)val4.localLogic != 2)
					{
						continue;
					}
					GenerateEnergy(val3);
					if (val4.max <= val4.count)
					{
						continue;
					}
					if (dictionary.ContainsKey(itemId))
					{
						if (val3.energy < 20000000)
						{
							continue;
						}
						float num2 = 0f;
						bool flag2 = (int)LDB.veins.GetVeinTypeByItemId(itemId) == 7;
						foreach (int item in dictionary[itemId])
						{
							num2 = ((!flag2) ? (num2 + (float)(GetMine(veinPool, item, miningCostRate, __instance.planet.factory) ? 1 : 0)) : (num2 + ((veinPool.Length > item && veinPool[item].productId > 0) ? ((float)veinPool[item].amount / 6000f) : 0f)));
						}
						val3.storage[k].count += (int)num2;
						array[itemId] += ((val2 != null) ? ((int)num2) : 0);
						val3.energy -= 20000000;
					}
					else if (itemId == __instance.planet.waterItemId)
					{
						val3.storage[k].count += 100;
						array[itemId] += ((val2 != null) ? 100 : 0);
						val3.energy -= 20000000;
					}
				}
			}
		}

		private static void GenerateEnergy(StationComponent sc)
		{
			//IL_0045: 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_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			int num = sc.storage.Length - 2;
			if (num < 0 || num >= sc.storage.Length || sc.energyMax / 2 <= sc.energy)
			{
				return;
			}
			StationStore val = sc.storage[num];
			int itemId = val.itemId;
			int count = val.count;
			if (itemId <= 0 || count <= 0)
			{
				return;
			}
			long num2 = ((ProtoSet<ItemProto>)(object)LDB.items).Select(itemId)?.HeatValue ?? 0;
			if (num2 <= 0)
			{
				return;
			}
			int num3 = Math.Min((int)((sc.energyMax - sc.energy) / num2), count);
			int num4 = split_inc_level(ref val.count, ref val.inc, num3);
			double num5 = 1.0;
			if (num3 > 0 && num4 > 0)
			{
				int num6 = num4 / num3;
				if (num6 >= 0 && num6 < Cargo.incTableMilli.Length)
				{
					num5 += Cargo.incTableMilli[num6];
				}
			}
			sc.energy += (long)((double)(num3 * num2) * num5);
			sc.storage[num].inc = val.inc;
			sc.storage[num].count = val.count;
		}

		private static void AddVeinData(Dictionary<int, List<int>> veins, int item, int index)
		{
			if (!veins.ContainsKey(item))
			{
				veins.Add(item, new List<int>());
			}
			veins[item].Add(index);
		}

		public static bool GetMine(VeinData[] veinDatas, int index, float miningRate, PlanetFactory factory)
		{
			if (veinDatas.Length == 0 || veinDatas[index].productId <= 0)
			{
				return false;
			}
			if (veinDatas[index].amount > 0)
			{
				bool flag = false;
				if (miningRate > 0f)
				{
					costFrac += miningRate;
					flag = (int)costFrac > 0;
					costFrac -= (flag ? 1 : 0);
				}
				if (flag)
				{
					veinDatas[index].amount = veinDatas[index].amount - 1;
					factory.veinGroups[veinDatas[index].groupIndex].amount--;
					if (veinDatas[index].amount <= 0)
					{
						short groupIndex = veinDatas[index].groupIndex;
						factory.veinGroups[groupIndex].count--;
						factory.RemoveVeinWithComponents(index);
						factory.RecalculateVeinGroup((int)groupIndex);
					}
				}
				return true;
			}
			short groupIndex2 = veinDatas[index].groupIndex;
			factory.veinGroups[groupIndex2].count--;
			factory.RemoveVeinWithComponents(index);
			factory.RecalculateVeinGroup((int)groupIndex2);
			return false;
		}

		private static int split_inc_level(ref int count, ref int totalinc, int requireCount)
		{
			int num = totalinc / count;
			int num2 = totalinc - num * count;
			count -= requireCount;
			num2 -= count;
			num = ((num2 > 0) ? (num * requireCount + num2) : (num * requireCount));
			totalinc -= num;
			return num;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "PlanetMiner";

		public const string PLUGIN_NAME = "PlanetMiner";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}