Decompiled source of PlanetMiner v3.1.1

PlanetMiner.dll

Decompiled 3 weeks 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.1.1")]
public class PlanetMiner : BaseUnityPlugin
{
	public const string Version = "3.1.1";

	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_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Invalid comparison between Unknown and I4
		//IL_0206: Unknown result type (might be due to invalid IL or missing references)
		//IL_020d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_0266: Invalid comparison between Unknown and I4
		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?.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 flag = (int)LDB.veins.GetVeinTypeByItemId(itemId) == 7;
					foreach (int item in dictionary[itemId])
					{
						num2 = ((!flag) ? (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_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: 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)
		{
			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)
			{
				num5 += Cargo.incTableMilli[num4 / num3];
			}
			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 = 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;
	}
}