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.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_006a: Unknown result type (might be due to invalid IL or missing references)
//IL_006f: Unknown result type (might be due to invalid IL or missing references)
//IL_0071: Unknown result type (might be due to invalid IL or missing references)
//IL_007b: Unknown result type (might be due to invalid IL or missing references)
//IL_0091: Unknown result type (might be due to invalid IL or missing references)
//IL_0144: Unknown result type (might be due to invalid IL or missing references)
//IL_0149: Unknown result type (might be due to invalid IL or missing references)
//IL_014b: Unknown result type (might be due to invalid IL or missing references)
//IL_0154: Unknown result type (might be due to invalid IL or missing references)
//IL_0178: Unknown result type (might be due to invalid IL or missing references)
//IL_017a: Unknown result type (might be due to invalid IL or missing references)
//IL_0180: Invalid comparison between Unknown and I4
//IL_0199: Unknown result type (might be due to invalid IL or missing references)
//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
//IL_01f9: 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>>();
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_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 = true;
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;
}
}