using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("PlanetwideSpray")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.2.0")]
[module: UnverifiableCode]
namespace PlanetwideSpray;
public class Main_Patch
{
public class Status
{
public int incLevel;
public readonly int[] incCount = new int[11];
public int incDebt;
}
public static bool LimitSpray = true;
private static Status[] statusArr = null;
private const int MAX_INC_COUNT = 11;
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), "Begin")]
public static void SetArray()
{
statusArr = new Status[GameMain.data.factories.Length];
for (int i = 0; i < statusArr.Length; i++)
{
statusArr[i] = new Status();
}
}
[HarmonyPrefix]
[HarmonyPriority(600)]
[HarmonyPatch(typeof(PlanetFactory), "InsertInto")]
private static void AddItemInc(PlanetFactory __instance, int entityId, byte itemCount, ref byte itemInc)
{
ref EntityData reference = ref __instance.entityPool[entityId];
if (reference.assemblerId != 0 || reference.labId != 0 || !LimitSpray)
{
Status status = statusArr[__instance.index];
int num = itemCount * status.incLevel - itemInc;
if (num > 0 && status.incDebt < status.incCount[status.incLevel])
{
Interlocked.Add(ref status.incDebt, itemCount);
itemInc += (byte)num;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(FractionatorComponent), "InternalUpdate")]
private static void FractionatorSetInc(PlanetFactory factory, ref FractionatorComponent __instance)
{
Status status = statusArr[factory.index];
int num = __instance.fluidInputCount * status.incLevel - __instance.fluidInputInc;
if (num > 0 && status.incDebt < status.incCount[status.incLevel])
{
Interlocked.Add(ref status.incDebt, num / status.incLevel);
__instance.fluidInputInc += num;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(TurretComponent), "InternalUpdate")]
private static void TurretSetInc(PlanetFactory factory, ref TurretComponent __instance)
{
Status status = statusArr[factory.index];
int num = __instance.itemCount * status.incLevel - __instance.itemInc;
if (num > 0 && status.incDebt < status.incCount[status.incLevel])
{
Interlocked.Add(ref status.incDebt, num / status.incLevel);
__instance.itemInc += (short)num;
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(CargoTraffic), "SpraycoaterGameTick")]
private static void SpraycoaterGameTick_Prefix(CargoTraffic __instance)
{
if (statusArr == null)
{
SetArray();
}
Status status = statusArr[__instance.factory.index];
if (status == null)
{
status = new Status();
statusArr[__instance.factory.index] = status;
}
Array.Clear(status.incCount, 0, 11);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(SpraycoaterComponent), "InternalUpdate")]
private static void InternalUpdate(ref SpraycoaterComponent __instance, CargoTraffic _traffic, AnimData[] _animPool, int[] consumeRegister)
{
if (__instance.cargoBeltId > 0 || __instance.incCount <= 0)
{
return;
}
bool flag = false;
Status status = statusArr[_traffic.factory.index];
if (status.incDebt > 0)
{
flag = true;
int num = ((__instance.incSprayTimes * 2 < status.incDebt) ? (__instance.incSprayTimes * 2) : status.incDebt);
status.incDebt -= num;
__instance.extraIncCount -= num;
if (__instance.extraIncCount < 0)
{
int incCount = __instance.incCount;
__instance.incCount += __instance.extraIncCount;
__instance.extraIncCount = 0;
consumeRegister[__instance.incItemId] += incCount / __instance.incSprayTimes - __instance.incCount / __instance.incSprayTimes;
if (__instance.incCount <= 0)
{
__instance.incItemId = 0;
__instance.incAbility = 0;
}
}
}
status.incCount[__instance.incAbility] += __instance.extraIncCount + __instance.incCount;
int num2 = Mathf.RoundToInt((float)_animPool[__instance.entityId].state * 0.001f);
if (flag)
{
if (num2 < 15)
{
num2 = 60 - num2;
}
else if (num2 < 45)
{
num2 = 45;
}
}
_animPool[__instance.entityId].state = (uint)(1 + __instance.incAbility * 10 + 100 + num2 * 1000);
_animPool[__instance.entityId].time = (float)(__instance.incCount + __instance.extraIncCount) / (float)__instance.incCapacity;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(CargoTraffic), "SpraycoaterGameTick")]
private static void SpraycoaterGameTick_Postfix(CargoTraffic __instance)
{
Status status = statusArr[__instance.factory.index];
int num = 10;
while (num > 0 && status.incCount[num] <= 0)
{
num--;
}
status.incLevel = num;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UISpraycoaterWindow), "RefreshSpraycoaterWindow")]
private static void RefreshSpraycoaterWindow(UISpraycoaterWindow __instance, SpraycoaterComponent spraycoater)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
if (spraycoater.cargoBeltId > 0)
{
return;
}
if (spraycoater.incAbility == 0)
{
__instance.stateText.text = "[PlanetwideSpray] " + Localization.Translate("Insufficient supply");
((Graphic)__instance.stateText).color = __instance.powerLowColor;
return;
}
float power = __instance.factory.entityAnimPool[spraycoater.entityId].power;
if (power == 1f)
{
__instance.stateText.text = "[PlanetwideSpray] " + Localization.Translate("正常运转");
((Graphic)__instance.stateText).color = __instance.workNormalColor;
}
else if (power > 0.1f)
{
__instance.stateText.text = "[PlanetwideSpray] " + Localization.Translate("电力不足");
((Graphic)__instance.stateText).color = __instance.powerLowColor;
}
else
{
__instance.stateText.text = "[PlanetwideSpray] " + Localization.Translate("停止运转");
((Graphic)__instance.stateText).color = __instance.powerOffColor;
}
}
}
[BepInPlugin("starfi5h.plugin.PlanetwideSpray", "PlanetwideSpray", "1.1.2")]
public class Plugin : BaseUnityPlugin
{
public const string GUID = "starfi5h.plugin.PlanetwideSpray";
public const string NAME = "PlanetwideSpray";
public const string VERSION = "1.1.2";
public static ManualLogSource Log;
private static Harmony harmony;
public void Awake()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_001a: Expected O, but got Unknown
//IL_003d: Unknown result type (might be due to invalid IL or missing references)
//IL_0047: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
harmony = new Harmony("starfi5h.plugin.PlanetwideSpray");
ConfigEntry<int> val = ((BaseUnityPlugin)this).Config.Bind<int>("Cheat", "Force Proliferator Level", 0, new ConfigDescription("Spray everything insert by sorters if this value > 0\n(作弊选项)当此值>0, 使分捡器抓取的货物皆为此增产等级", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
ConfigEntry<bool> val2 = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Spray All Cargo", false, "Spray every item transfer by sorters (including products)\n喷涂任何分捡器抓取的货物(包含产物)");
if (val.Value > 0)
{
val.Value = Math.Min(val.Value, 10);
harmony.PatchAll(typeof(Cheat_Patch));
Cheat_Patch.IncAbility = val.Value;
((BaseUnityPlugin)this).Logger.LogDebug((object)("Cheat mode: Force Proliferator Level = " + Cheat_Patch.IncAbility));
}
else
{
harmony.PatchAll(typeof(Main_Patch));
Main_Patch.LimitSpray = !val2.Value;
}
}
}
public class Cheat_Patch
{
public static int IncAbility;
[HarmonyPrefix]
[HarmonyPatch(typeof(PlanetFactory), "InsertInto")]
private static void AddItemInc(byte itemCount, ref byte itemInc)
{
itemInc = (byte)(itemCount * IncAbility);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(CargoTraffic), "TryPickItemAtRear")]
[HarmonyPatch(typeof(CargoPath), "TryPickItemAtRear")]
private static void TryPickItemAtRear(ref byte stack, ref byte inc)
{
inc = (byte)(stack * IncAbility);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(FractionatorComponent), "InternalUpdate")]
private static void FractionatorSetInc(ref FractionatorComponent __instance)
{
__instance.fluidInputInc = __instance.fluidInputCount * IncAbility;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(TurretComponent), "BeltUpdate")]
private static void TurretSetInc(ref TurretComponent __instance)
{
__instance.itemInc = (short)(__instance.itemCount * IncAbility);
}
}