Decompiled source of PlanetMiner v3.0.9

PlanetMiner.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("PlanetMiner")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PlanetMiner")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("14898231-414a-4a7c-a343-fa3adec8e4fa")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PlanetMiner;

[BepInPlugin("crecheng.PlanetMiner", "PlanetMiner", "3.0.9")]
public class PlanetMiner : BaseUnityPlugin
{
	public const string Version = "3.0.9";

	public const int uesEnergy = 20000000;

	private static long frame = 0L;

	private static uint seed = 100000u;

	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_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: 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_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e9: Invalid comparison between Unknown and I4
		//IL_034c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0353: Unknown result type (might be due to invalid IL or missing references)
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_0229: Unknown result type (might be due to invalid IL or missing references)
		//IL_022b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a7: Invalid comparison between Unknown and I4
		//IL_031a: Unknown result type (might be due to invalid IL or missing references)
		//IL_033d: Unknown result type (might be due to invalid IL or missing references)
		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>>();
		if (__instance.minerPool[0].seed == 0)
		{
			Random random = new Random();
			__instance.minerPool[0].seed = (uint)(__instance.planet.id * 100000 + random.Next(1, 9999));
		}
		else
		{
			seed = __instance.minerPool[0].seed;
		}
		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 == null || 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;
				}
				if (val3.energyMax / 2 > val3.energy)
				{
					StationStore val5 = val3.storage[val3.storage.Length - 2];
					int itemId2 = val5.itemId;
					int count = val5.count;
					if (itemId2 > 0 && count > 0)
					{
						long num2 = ((ProtoSet<ItemProto>)(object)LDB.items).Select(itemId2)?.HeatValue ?? 0;
						if (num2 > 0)
						{
							int num3 = Math.Min((int)((val3.energyMax - val3.energy) / num2), count);
							int num4 = split_inc_level(ref val5.count, ref val5.inc, num3);
							double num5 = 1.0;
							if (num3 > 0 && num4 > 0)
							{
								num5 = Cargo.incTableMilli[num4 / num3] + 1.0;
							}
							val3.energy += (long)((double)(num3 * num2) * num5);
							val3.storage[val3.storage.Length - 2].inc = val5.inc;
							val3.storage[val3.storage.Length - 2].count = val5.count;
						}
					}
				}
				if (val4.max <= val4.count)
				{
					continue;
				}
				if (dictionary.ContainsKey(itemId))
				{
					if (val3.energy < 20000000)
					{
						continue;
					}
					float num6 = 0f;
					bool flag = (int)LDB.veins.GetVeinTypeByItemId(itemId) == 7;
					foreach (int item in dictionary[itemId])
					{
						num6 = ((!flag) ? (num6 + (float)(GetMine(veinPool, item, miningCostRate, __instance.planet.factory) ? 1 : 0)) : (num6 + ((veinPool.Length > item && veinPool[item].productId > 0) ? ((float)veinPool[item].amount / 6000f) : 0f)));
					}
					val3.storage[k].count += (int)num6;
					array[itemId] += ((val2 != null) ? ((int)num6) : 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 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 = true;
			if (miningRate < 0.99999f)
			{
				seed = (uint)((int)((ulong)((long)(seed % 2147483646 + 1) * 48271L) % 2147483647uL) - 1);
				flag = (double)seed / 2147483646.0 < (double)miningRate;
			}
			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;
	}
}