using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace DroneCapacityPlus;
[BepInPlugin("mandao.DroneCapacityPlus", "DroneCapacityPlus", "1.1.0")]
public class DroneCapacityPlusPlugin : BaseUnityPlugin
{
public const string PluginGuid = "mandao.DroneCapacityPlus";
public const string PluginName = "DroneCapacityPlus";
public const string PluginVersion = "1.1.0";
internal const int MinCapacity = 25;
internal const int MaxCapacity = 1000000;
private const int WindowId = 1146244942;
internal static ManualLogSource Log;
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<string> Mode;
internal static ConfigEntry<int> FixedCapacity;
internal static ConfigEntry<int> VanillaMultiplier;
private bool _uiVisible;
private string _capacityText = "10000";
private Rect _windowRect = new Rect(260f, 140f, 430f, 310f);
private void Awake()
{
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
//IL_006f: Expected O, but got Unknown
//IL_009f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a9: Expected O, but got Unknown
//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
//IL_00df: Expected O, but got Unknown
//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
//IL_00fe: Expected O, but got Unknown
//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
//IL_010e: Expected O, but got Unknown
//IL_011d: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "是否启用本模组。设为 false 后物流运输机载重恢复原版数值(需要重新进入游戏或读取存档)。");
Mode = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Mode", "Fixed", new ConfigDescription("Fixed = 使用 FixedCapacity 固定值;VanillaTimes = 原版科技载重 × VanillaMultiplier(保留科技成长曲线)。", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[2] { "Fixed", "VanillaTimes" }), Array.Empty<object>()));
FixedCapacity = ((BaseUnityPlugin)this).Config.Bind<int>("General", "FixedCapacity", 10000, new ConfigDescription("Fixed 模式下物流运输机单次运输的物品上限(原版为 25,科技满级为 200;允许 25 - 1000000)。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(25, 1000000), Array.Empty<object>()));
VanillaMultiplier = ((BaseUnityPlugin)this).Config.Bind<int>("General", "VanillaMultiplier", 10, new ConfigDescription("VanillaTimes 模式下,把原版科技带来的载重乘以该倍数(例如原版满级 200 × 10 = 2000)。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1000), Array.Empty<object>()));
Harmony val = new Harmony("mandao.DroneCapacityPlus");
TryPatch(val, typeof(DroneCapacityPatches));
TryPatch(val, typeof(TickEnforcePatches));
TryPatch(val, typeof(MinLoadPatches));
Log.LogInfo((object)("DroneCapacityPlus 已加载:Mode=" + Mode.Value + ", FixedCapacity=" + FixedCapacity.Value + ", VanillaMultiplier=" + VanillaMultiplier.Value));
if (GameMain.history != null)
{
CapacityState.Capture(GameMain.history);
}
}
private void Update()
{
if (Input.GetKeyDown((KeyCode)100) && (Input.GetKey((KeyCode)308) || Input.GetKey((KeyCode)307)))
{
_uiVisible = !_uiVisible;
if (_uiVisible)
{
_capacityText = FixedCapacity.Value.ToString();
}
}
}
private void OnGUI()
{
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Expected O, but got Unknown
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
if (_uiVisible)
{
float num = Mathf.Clamp((float)Screen.height / 1080f, 0.8f, 3f);
Matrix4x4 matrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(num, num, 1f));
_windowRect = GUI.Window(1146244942, _windowRect, new WindowFunction(DrawWindow), "Drone Capacity Plus (Alt+D)");
GUI.matrix = matrix;
}
}
private void DrawWindow(int id)
{
//IL_028d: Unknown result type (might be due to invalid IL or missing references)
GUILayout.BeginVertical(Array.Empty<GUILayoutOption>());
int num = ((GameMain.history != null) ? GameMain.history.logisticDroneCarries : (-1));
GUILayout.Label("Current in-game capacity: " + ((num >= 0) ? num.ToString() : "not in game"), Array.Empty<GUILayoutOption>());
GUILayout.Label("Mode: " + Mode.Value + " Config target: " + FixedCapacity.Value, Array.Empty<GUILayoutOption>());
if (!Enabled.Value)
{
GUILayout.Label("NOTE: Enabled=false in config. Set it to true and restart.", Array.Empty<GUILayoutOption>());
}
GUILayout.Space(6f);
GUILayout.Label("Set capacity (" + 25 + " - " + 1000000 + "):", Array.Empty<GUILayoutOption>());
_capacityText = GUILayout.TextField(_capacityText, 10, Array.Empty<GUILayoutOption>());
GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
if (GUILayout.Button("-10000", Array.Empty<GUILayoutOption>()))
{
Step(-10000);
}
if (GUILayout.Button("-1000", Array.Empty<GUILayoutOption>()))
{
Step(-1000);
}
if (GUILayout.Button("+1000", Array.Empty<GUILayoutOption>()))
{
Step(1000);
}
if (GUILayout.Button("+10000", Array.Empty<GUILayoutOption>()))
{
Step(10000);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
if (GUILayout.Button("x2", Array.Empty<GUILayoutOption>()))
{
ScaleBy(2f);
}
if (GUILayout.Button("/2", Array.Empty<GUILayoutOption>()))
{
ScaleBy(0.5f);
}
if (GUILayout.Button("10k", Array.Empty<GUILayoutOption>()))
{
SetCapacity(10000L);
}
if (GUILayout.Button("100k", Array.Empty<GUILayoutOption>()))
{
SetCapacity(100000L);
}
if (GUILayout.Button("500k", Array.Empty<GUILayoutOption>()))
{
SetCapacity(500000L);
}
if (GUILayout.Button("1M", Array.Empty<GUILayoutOption>()))
{
SetCapacity(1000000L);
}
GUILayout.EndHorizontal();
GUILayout.Space(6f);
if (GUILayout.Button("Apply (switch Mode to Fixed)", Array.Empty<GUILayoutOption>()))
{
ApplyText();
}
if (GUILayout.Button("Close", Array.Empty<GUILayoutOption>()))
{
_uiVisible = false;
}
GUILayout.EndVertical();
GUI.DragWindow(new Rect(0f, 0f, 10000f, 24f));
}
private void Step(int delta)
{
SetCapacity((long)ParseTextOrCurrent() + (long)delta);
}
private void ScaleBy(float factor)
{
SetCapacity((long)((float)ParseTextOrCurrent() * factor));
}
private void ApplyText()
{
if (int.TryParse(_capacityText, out var result))
{
SetCapacity(result);
}
else
{
_capacityText = FixedCapacity.Value.ToString();
}
}
private int ParseTextOrCurrent()
{
if (!int.TryParse(_capacityText, out var result))
{
return FixedCapacity.Value;
}
return result;
}
private void SetCapacity(long value)
{
if (value < 25)
{
value = 25L;
}
if (value > 1000000)
{
value = 1000000L;
}
int num = (int)value;
if (FixedCapacity.Value != num)
{
FixedCapacity.Value = num;
}
if (!string.Equals(Mode.Value, "Fixed", StringComparison.OrdinalIgnoreCase))
{
Mode.Value = "Fixed";
}
_capacityText = num.ToString();
if (GameMain.history != null)
{
CapacityState.Apply(GameMain.history);
}
Log.LogInfo((object)("运输机载重已设为 " + num));
}
private static void TryPatch(Harmony harmony, Type patchType)
{
try
{
harmony.PatchAll(patchType);
}
catch (Exception ex)
{
Log.LogError((object)("应用补丁失败 " + patchType.Name + ":" + ex));
}
}
}
internal static class CapacityState
{
private const int DefaultVanillaCarries = 25;
private static int _vanillaCarries = 25;
private static bool _hasBase;
internal static void Capture(GameHistoryData history)
{
_vanillaCarries = ClampVanilla(history.logisticDroneCarries);
_hasBase = true;
Apply(history);
}
internal static void Restore(GameHistoryData history)
{
if (!_hasBase)
{
_vanillaCarries = ClampVanilla(history.logisticDroneCarries);
_hasBase = true;
}
history.logisticDroneCarries = _vanillaCarries;
}
internal static int GetVanillaCarriesForMinLoad()
{
return ClampVanilla(_vanillaCarries);
}
private static int ClampVanilla(int value)
{
if (value < 1)
{
return 1;
}
if (value > 200)
{
return 200;
}
return value;
}
internal static void Apply(GameHistoryData history)
{
if (!_hasBase)
{
Restore(history);
}
int vanillaCarries = _vanillaCarries;
int num;
if (!DroneCapacityPlusPlugin.Enabled.Value)
{
num = vanillaCarries;
}
else if (string.Equals(DroneCapacityPlusPlugin.Mode.Value, "VanillaTimes", StringComparison.OrdinalIgnoreCase))
{
long num2 = (long)ClampVanilla(vanillaCarries) * (long)DroneCapacityPlusPlugin.VanillaMultiplier.Value;
num = (int)((num2 > int.MaxValue) ? int.MaxValue : num2);
}
else
{
num = DroneCapacityPlusPlugin.FixedCapacity.Value;
if (num < 25)
{
num = 25;
}
if (num > 1000000)
{
num = 1000000;
}
}
if (history.logisticDroneCarries != num)
{
history.logisticDroneCarries = num;
}
}
}
[HarmonyPatch(typeof(GameHistoryData))]
internal static class DroneCapacityPatches
{
[HarmonyPostfix]
[HarmonyPatch("SetForNewGame")]
private static void SetForNewGame_Postfix(GameHistoryData __instance)
{
if (DroneCapacityPlusPlugin.Enabled.Value)
{
CapacityState.Capture(__instance);
}
}
[HarmonyPrefix]
[HarmonyPatch("UnlockTechFunction")]
private static void UnlockTechFunction_Prefix(GameHistoryData __instance)
{
if (DroneCapacityPlusPlugin.Enabled.Value)
{
CapacityState.Restore(__instance);
}
}
[HarmonyPostfix]
[HarmonyPatch("UnlockTechFunction")]
private static void UnlockTechFunction_Postfix(GameHistoryData __instance)
{
if (DroneCapacityPlusPlugin.Enabled.Value)
{
CapacityState.Capture(__instance);
}
}
[HarmonyPostfix]
[HarmonyPatch("Import")]
private static void Import_Postfix(GameHistoryData __instance)
{
if (DroneCapacityPlusPlugin.Enabled.Value)
{
CapacityState.Capture(__instance);
}
}
[HarmonyPrefix]
[HarmonyPatch("Export")]
private static void Export_Prefix(GameHistoryData __instance)
{
if (DroneCapacityPlusPlugin.Enabled.Value)
{
CapacityState.Restore(__instance);
}
}
[HarmonyPostfix]
[HarmonyPatch("Export")]
private static void Export_Postfix(GameHistoryData __instance)
{
if (DroneCapacityPlusPlugin.Enabled.Value)
{
CapacityState.Apply(__instance);
}
}
}
[HarmonyPatch(typeof(PlanetTransport))]
internal static class TickEnforcePatches
{
[HarmonyPriority(0)]
[HarmonyPrefix]
[HarmonyPatch("GameTick")]
private static void GameTick_Prefix()
{
if (DroneCapacityPlusPlugin.Enabled.Value && GameMain.history != null)
{
CapacityState.Apply(GameMain.history);
}
}
}
[HarmonyPatch(typeof(StationComponent))]
internal static class MinLoadPatches
{
[HarmonyTranspiler]
[HarmonyPatch("InternalTickLocal")]
private static IEnumerable<CodeInstruction> InternalTickLocal_Transpiler(IEnumerable<CodeInstruction> instructions)
{
//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
//IL_01c4: Expected O, but got Unknown
List<CodeInstruction> list = new List<CodeInstruction>(instructions);
if (!DroneCapacityPlusPlugin.Enabled.Value)
{
return list;
}
MethodInfo methodInfo = AccessTools.Method(typeof(CapacityState), "GetVanillaCarriesForMinLoad", (Type[])null, (Type[])null);
if (methodInfo == null)
{
if (DroneCapacityPlusPlugin.Log != null)
{
DroneCapacityPlusPlugin.Log.LogError((object)"找不到起送量辅助方法,跳过该补丁。");
}
return list;
}
for (int i = 0; i + 7 < list.Count; i++)
{
if ((list[i].opcode == OpCodes.Ldarg_S || list[i].opcode == OpCodes.Ldarg) && list[i + 1].opcode == OpCodes.Ldc_I4_1 && list[i + 2].opcode == OpCodes.Sub && list[i + 3].opcode == OpCodes.Ldarg_0 && list[i + 4].opcode == OpCodes.Ldfld && list[i + 4].operand != null && list[i + 4].operand.ToString().Contains("deliveryDrones") && list[i + 5].opcode == OpCodes.Mul && (list[i + 6].opcode == OpCodes.Ldc_I4_S || list[i + 6].opcode == OpCodes.Ldc_I4) && Convert.ToInt32(list[i + 6].operand) == 100 && list[i + 7].opcode == OpCodes.Div)
{
list[i] = new CodeInstruction(OpCodes.Call, (object)methodInfo);
if (DroneCapacityPlusPlugin.Log != null)
{
DroneCapacityPlusPlugin.Log.LogInfo((object)"已修正起送量计算:起送阈值仍按原版载重计算。");
}
return list;
}
}
if (DroneCapacityPlusPlugin.Log != null)
{
DroneCapacityPlusPlugin.Log.LogWarning((object)"未找到起送量计算代码,起送阈值将按当前载重计算;低库存物流塔可能需要手动调低起送量。");
}
return list;
}
}