using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BeltThrottle")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BeltThrottle")]
[assembly: AssemblyTitle("BeltThrottle")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BeltThrottle;
internal static class MonitorParams
{
internal const int SlotEnabled = 15;
internal const int SlotRate = 16;
internal const int RequiredLength = 128;
private const int RateMask = 16777215;
private const int UnitShift = 24;
internal static int Encode(MonitorSetting s)
{
int num = (int)Math.Round(s.RatePerMinute * 100f);
if (num < 0)
{
num = 0;
}
if (num > 16777215)
{
num = 16777215;
}
return num | (int)(((s.unit == 1) ? 1u : 0u) << 24);
}
internal static int DecodeRate(int packed)
{
return packed & 0xFFFFFF;
}
internal static int DecodeUnit(int packed)
{
return (((packed >> 24) & 0xFF) == 1) ? 1 : 0;
}
}
internal static class BlueprintCompat
{
internal static void WriteSlots(BuildingParameters bp, int planetId, int objectId)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0008: Invalid comparison between Unknown and I4
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
if ((int)bp.type != 15)
{
return;
}
int[] parameters = bp.parameters;
if (parameters != null && parameters.Length >= 128)
{
MonitorSetting monitorSetting = ThrottleSettings.Peek(planetId, objectId);
if (monitorSetting == null || !monitorSetting.enabled || monitorSetting.rate <= 0f || monitorSetting.RatePerMinute <= 0f)
{
parameters[15] = 0;
parameters[16] = 0;
}
else
{
parameters[15] = 1;
parameters[16] = MonitorParams.Encode(monitorSetting);
}
}
}
internal static void CarryOut(BuildingParameters bp, int[] dst)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0008: Invalid comparison between Unknown and I4
//IL_0019: Unknown result type (might be due to invalid IL or missing references)
if ((int)bp.type == 15 && dst != null && dst.Length >= 128)
{
int[] parameters = bp.parameters;
if (parameters != null && parameters.Length >= 128)
{
dst[15] = parameters[15];
dst[16] = parameters[16];
}
}
}
internal static void CarryIn(BuildingParameters bp, int[] src)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0008: Invalid comparison between Unknown and I4
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
if ((int)bp.type != 15)
{
return;
}
int[] parameters = bp.parameters;
if (parameters != null && parameters.Length >= 128)
{
if (src == null || src.Length < 128)
{
parameters[15] = 0;
parameters[16] = 0;
}
else
{
parameters[15] = src[15];
parameters[16] = src[16];
}
}
}
internal static void Apply(BuildingParameters bp, PlanetFactory factory, int objectId)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0008: Invalid comparison between Unknown and I4
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
if ((int)bp.type == 15 && factory != null && objectId > 0)
{
int[] parameters = bp.parameters;
if (parameters != null && parameters.Length >= 128)
{
ThrottleSettings.ApplyFromBlueprint(factory.planetId, objectId, parameters[15], parameters[16]);
}
}
}
internal static void ApplyRaw(PlanetFactory factory, int objectId, int[] parameters)
{
if (factory != null && objectId > 0 && parameters != null && parameters.Length >= 128)
{
EntityData[] entityPool = factory.entityPool;
if (entityPool != null && objectId < entityPool.Length && entityPool[objectId].id == objectId && entityPool[objectId].monitorId != 0)
{
ThrottleSettings.ApplyFromBlueprint(factory.planetId, objectId, parameters[15], parameters[16]);
}
}
}
}
[HarmonyPatch(typeof(BuildingParameters), "CopyFromFactoryObject")]
internal static class Patch_BP_CopyFromFactoryObject
{
private static bool _err;
private static void Postfix(BuildingParameters __instance, int __0, PlanetFactory __1)
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
try
{
if (__1 != null)
{
BlueprintCompat.WriteSlots(__instance, __1.planetId, __0);
}
}
catch (Exception e)
{
LogOnce("复制时写入限速参数", e);
}
}
private static void LogOnce(string stage, Exception e)
{
if (!_err && Plugin.Log != null)
{
_err = true;
Plugin.Log.LogWarning((object)("[BeltThrottle] " + stage + "失败(后续同类错误不再输出): " + e.Message));
}
}
}
[HarmonyPatch(typeof(BuildingParameters), "ToParamsArray")]
internal static class Patch_BP_ToParamsArray
{
private static void Postfix(BuildingParameters __instance, ref int[] __0)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
try
{
BlueprintCompat.CarryOut(__instance, __0);
}
catch
{
}
}
}
[HarmonyPatch(typeof(BuildingParameters), "FromParamsArray")]
internal static class Patch_BP_FromParamsArray
{
private static void Postfix(BuildingParameters __instance, int[] __0)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
try
{
BlueprintCompat.CarryIn(__instance, __0);
}
catch
{
}
}
}
[HarmonyPatch(typeof(BuildingParameters), "PasteToFactoryObject")]
internal static class Patch_BP_PasteToFactoryObject
{
private static bool _err;
private static void Postfix(BuildingParameters __instance, int __0, PlanetFactory __1, bool __result)
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
if (!__result)
{
return;
}
try
{
BlueprintCompat.Apply(__instance, __1, __0);
}
catch (Exception e)
{
LogOnce("粘贴时应用限速参数", e);
}
}
private static void LogOnce(string stage, Exception e)
{
if (!_err && Plugin.Log != null)
{
_err = true;
Plugin.Log.LogWarning((object)("[BeltThrottle] " + stage + "失败(后续同类错误不再输出): " + e.Message));
}
}
}
[HarmonyPatch(typeof(BuildingParameters), "ApplyPrebuildParametersToEntity")]
internal static class Patch_BP_ApplyPrebuildParametersToEntity
{
private static bool _err;
private static void Postfix(int __0, int[] __3, PlanetFactory __5)
{
try
{
BlueprintCompat.ApplyRaw(__5, __0, __3);
}
catch (Exception e)
{
LogOnce("新建建筑应用限速参数", e);
}
}
private static void LogOnce(string stage, Exception e)
{
if (!_err && Plugin.Log != null)
{
_err = true;
Plugin.Log.LogWarning((object)("[BeltThrottle] " + stage + "失败(后续同类错误不再输出): " + e.Message));
}
}
}
internal sealed class Gate
{
public float ratio;
public float target;
public int full;
public float stack;
}
internal static class GateManager
{
private static readonly Dictionary<CargoPath, Gate> Empty = new Dictionary<CargoPath, Gate>();
private static volatile Dictionary<CargoPath, Gate> _gates = Empty;
private static bool _dirty = true;
private static int _frames;
private static readonly HashSet<CargoPath> Logged = new HashSet<CargoPath>();
private static readonly HashSet<long> Warned = new HashSet<long>();
private static readonly Dictionary<CargoPath, float> StackCache = new Dictionary<CargoPath, float>();
private static readonly Dictionary<CargoPath, long> StackTick = new Dictionary<CargoPath, long>();
private static long _lastEmptyWarn = -100000L;
internal static void Invalidate()
{
_dirty = true;
}
internal static bool IsBlocked(CargoPath path)
{
if (path == null)
{
return false;
}
Dictionary<CargoPath, Gate> gates = _gates;
if (gates.Count == 0)
{
return false;
}
if (!gates.TryGetValue(path, out var value))
{
return false;
}
double num = GameMain.gameTick;
return Math.Floor(num * (double)value.ratio) <= Math.Floor((num - 1.0) * (double)value.ratio);
}
internal static void Frame()
{
_frames++;
if ((!_dirty && _frames % 15 != 0) || (!_dirty && _gates.Count == 0))
{
return;
}
_dirty = false;
try
{
Rebuild();
}
catch (Exception ex)
{
if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)("[BeltThrottle] 构建限流快照失败: " + ex.Message));
}
}
}
private static void Rebuild()
{
//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
//IL_0101: Unknown result type (might be due to invalid IL or missing references)
//IL_0133: Unknown result type (might be due to invalid IL or missing references)
//IL_0195: Unknown result type (might be due to invalid IL or missing references)
//IL_019a: Unknown result type (might be due to invalid IL or missing references)
//IL_019c: Unknown result type (might be due to invalid IL or missing references)
//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
//IL_021c: Unknown result type (might be due to invalid IL or missing references)
//IL_0278: Unknown result type (might be due to invalid IL or missing references)
//IL_0285: Unknown result type (might be due to invalid IL or missing references)
//IL_037e: Unknown result type (might be due to invalid IL or missing references)
if (Plugin.Enabled == null || !Plugin.Enabled.Value || GameMain.data == null)
{
Publish(Empty);
return;
}
GameData data = GameMain.data;
PlanetFactory[] factories = data.factories;
if (factories == null)
{
Publish(Empty);
return;
}
Dictionary<CargoPath, Gate> dictionary = null;
long gameTick = GameMain.gameTick;
int num = Math.Min(data.factoryCount, factories.Length);
for (int i = 0; i < num; i++)
{
PlanetFactory val = factories[i];
if (val == null || val.cargoTraffic == null)
{
continue;
}
CargoTraffic cargoTraffic = val.cargoTraffic;
if (cargoTraffic.monitorPool == null || cargoTraffic.beltPool == null)
{
continue;
}
int planetId = val.planetId;
int num2 = Math.Min(cargoTraffic.monitorCursor, cargoTraffic.monitorPool.Length);
int num3 = Math.Min(cargoTraffic.beltCursor, cargoTraffic.beltPool.Length);
if (num2 <= 1)
{
continue;
}
for (int j = 1; j < num2; j++)
{
MonitorComponent val2 = cargoTraffic.monitorPool[j];
if (val2.id != j)
{
continue;
}
MonitorSetting monitorSetting = ThrottleSettings.Peek(planetId, val2.entityId);
if (monitorSetting == null || !monitorSetting.enabled || monitorSetting.rate <= 0f)
{
continue;
}
int targetBeltId = val2.targetBeltId;
if (targetBeltId <= 0 || targetBeltId >= num3)
{
WarnOnce(j, planetId, "监控器#" + j + " 指向的传送带编号无效(targetBeltId=" + targetBeltId + ")");
continue;
}
BeltComponent val3 = cargoTraffic.beltPool[targetBeltId];
if (val3.id != targetBeltId)
{
WarnOnce(j, planetId, "监控器#" + j + " 指向的传送带已不存在(belt#" + targetBeltId + ")");
continue;
}
if (val3.segPathId <= 0)
{
WarnOnce(j, planetId, "监控器#" + j + " 所在传送带没有货运路径(segPathId=0)");
continue;
}
CargoPath cargoPath = cargoTraffic.GetCargoPath(val3.segPathId);
if (cargoPath == null)
{
WarnOnce(j, planetId, "监控器#" + j + " 的货运路径取不到(segPathId=" + val3.segPathId + ")");
continue;
}
float num4 = (float)((val3.speed <= 0) ? 1 : val3.speed) * 360f;
float num5 = 1f;
if (Plugin.StackAware != null && Plugin.StackAware.Value)
{
num5 = SampleStack(cargoPath, gameTick);
}
if (num5 < 1f)
{
num5 = 1f;
}
float ratePerMinute = monitorSetting.RatePerMinute;
float num6 = ratePerMinute / (num4 * num5);
if (num6 <= 0f || num6 >= 1f)
{
continue;
}
if (dictionary == null)
{
dictionary = new Dictionary<CargoPath, Gate>();
}
if (!dictionary.TryGetValue(cargoPath, out var value) || !(num6 >= value.ratio))
{
Gate gate = new Gate();
gate.ratio = num6;
gate.target = ratePerMinute;
gate.full = (int)num4;
gate.stack = num5;
dictionary[cargoPath] = gate;
if (Logged.Add(cargoPath) && Plugin.Log != null)
{
Plugin.Log.LogInfo((object)$"[BeltThrottle] 限流生效:监控器#{j}(entity {val2.entityId}) 目标 {ratePerMinute:0.#} 个/分,满速 {num4:0} 个/分,堆叠×{num5:0.##},每 tick 放行 {num6:P1}");
}
}
}
}
Publish(dictionary ?? Empty);
if ((dictionary != null && dictionary.Count != 0) || gameTick - _lastEmptyWarn < 300)
{
return;
}
string text = ThrottleSettings.DescribeEnabled();
if (!string.IsNullOrEmpty(text))
{
_lastEmptyWarn = gameTick;
if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)("[BeltThrottle] 有限速设置但没有接管任何传送带:设置=" + text + ";当前工厂 planetId=" + DescribePlanets(factories, num)));
}
}
}
private static string DescribePlanets(PlanetFactory[] factories, int count)
{
string text = "";
for (int i = 0; i < count; i++)
{
if (factories[i] != null)
{
if (text.Length > 0)
{
text += ",";
}
text += factories[i].planetId;
}
}
if (text.Length != 0)
{
return text;
}
return "(无)";
}
private static void Publish(Dictionary<CargoPath, Gate> map)
{
if (map == _gates)
{
return;
}
if (map.Count == 0 && _gates.Count > 0)
{
if (Plugin.Log != null)
{
Plugin.Log.LogInfo((object)"[BeltThrottle] 所有限流已解除,传送带恢复原速");
}
Logged.Clear();
StackCache.Clear();
StackTick.Clear();
}
_gates = map;
}
private static void WarnOnce(int monitorId, int planetId, string message)
{
if (Plugin.Log != null)
{
long item = ((long)planetId << 32) | (uint)monitorId;
if (Warned.Add(item))
{
Plugin.Log.LogWarning((object)("[BeltThrottle] " + message));
}
}
}
private static float SampleStack(CargoPath path, long now)
{
if (StackCache.TryGetValue(path, out var value) && StackTick.TryGetValue(path, out var value2) && now >= value2 && now - value2 < 120)
{
return value;
}
float num = 1f;
try
{
num = ComputeAvgStack(path);
}
catch
{
num = 1f;
}
StackCache[path] = num;
StackTick[path] = now;
return num;
}
private static float ComputeAvgStack(CargoPath path)
{
if (path == null || path.buffer == null || path.cargoContainer == null)
{
return 1f;
}
byte[] buffer = path.buffer;
Cargo[] cargoPool = path.cargoContainer.cargoPool;
if (cargoPool == null)
{
return 1f;
}
int cursor = path.cargoContainer.cursor;
int num = Math.Min(path.pathLength, buffer.Length);
if (num <= 20)
{
return 1f;
}
long num2 = 0L;
int num3 = 0;
for (int i = 0; i + 4 < num; i++)
{
if (buffer[i] != 250)
{
continue;
}
int num4 = buffer[i + 1] - 1 + (buffer[i + 2] - 1) * 100 + (buffer[i + 3] - 1) * 10000 + (buffer[i + 4] - 1) * 1000000;
if (num4 > 0 && num4 < cursor && num4 < cargoPool.Length)
{
int stack = cargoPool[num4].stack;
if (stack > 0)
{
num2 += stack;
num3++;
}
}
i += 4;
}
if (num3 == 0)
{
return 1f;
}
return (float)num2 / (float)num3;
}
internal static float GetRatio(CargoTraffic traffic, MonitorComponent m, int planetId)
{
//IL_0003: Unknown result type (might be due to invalid IL or missing references)
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: 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_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_005c: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
//IL_0092: Unknown result type (might be due to invalid IL or missing references)
if (traffic == null || m.id <= 0 || traffic.beltPool == null)
{
return 0f;
}
int targetBeltId = m.targetBeltId;
if (targetBeltId <= 0 || targetBeltId >= traffic.beltPool.Length || targetBeltId >= traffic.beltCursor)
{
return 0f;
}
BeltComponent val = traffic.beltPool[targetBeltId];
if (val.id != targetBeltId)
{
return 0f;
}
MonitorSetting monitorSetting = ThrottleSettings.Peek(planetId, m.entityId);
if (monitorSetting == null || !monitorSetting.enabled || monitorSetting.rate <= 0f)
{
return 0f;
}
int num = ((val.speed <= 0) ? 1 : val.speed);
float num2 = monitorSetting.RatePerMinute / ((float)num * 360f);
if (!(num2 >= 1f))
{
return num2;
}
return 0f;
}
}
internal static class MonitorPanel
{
private const string RootName = "BeltThrottlePanel";
private const float ExtraHeight = 56f;
private static GameObject _root;
private static Button _switchButton;
private static Image _switchBg;
private static Text _switchText;
private static InputField _rateField;
private static Text _unitText;
private static Text _hintText;
private static MonitorSetting _current;
private static int _boundEntity;
private static UIMonitorWindow _window;
private static readonly Dictionary<int, Vector2> BasePos = new Dictionary<int, Vector2>();
private static readonly Color OnColor = new Color(0.13f, 0.6f, 0.7f, 0.95f);
private static readonly Color OffColor = new Color(0.26f, 0.3f, 0.35f, 0.95f);
private static readonly Color TextColor = new Color(0.85f, 0.9f, 0.95f, 1f);
private static readonly Color DimColor = new Color(0.6f, 0.7f, 0.8f, 1f);
internal static void Attach(UIMonitorWindow w)
{
if (!((Object)(object)w == (Object)null) && !((Object)(object)w.monitorWindowRect == (Object)null))
{
_window = w;
Transform monitorWindowRect = (Transform)(object)w.monitorWindowRect;
Transform val = monitorWindowRect.Find("BeltThrottlePanel");
BasePos.Remove(((Object)w).GetInstanceID());
ExtendHeight(w);
if ((Object)(object)val != (Object)null)
{
_root = ((Component)val).gameObject;
Rebind();
Refresh(w);
}
else
{
Create(w, monitorWindowRect);
Refresh(w);
}
}
}
internal static void Refresh(UIMonitorWindow w)
{
if ((Object)(object)w == (Object)null || (Object)(object)_root == (Object)null || !Object.op_Implicit((Object)(object)_root) || w.cargoTraffic == null || w.factory == null || w.monitorId == 0 || w.cargoTraffic.monitorPool == null || w.monitorId >= w.cargoTraffic.monitorPool.Length || w.cargoTraffic.monitorPool[w.monitorId].id != w.monitorId)
{
return;
}
int entityId = w.cargoTraffic.monitorPool[w.monitorId].entityId;
if (entityId > 0)
{
_boundEntity = entityId;
_current = ThrottleSettings.Get(w.factory.planetId, entityId);
if ((Object)(object)_rateField != (Object)null)
{
_rateField.SetTextWithoutNotify(FormatRate(_current.rate));
}
if ((Object)(object)_unitText != (Object)null)
{
_unitText.text = ((_current.unit == 1) ? "个/秒" : "个/分钟");
}
ApplySwitchVisual(_current.enabled);
UpdateHint();
}
}
internal static void ExtendHeight(UIMonitorWindow w)
{
//IL_0049: 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_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
//IL_0074: Unknown result type (might be due to invalid IL or missing references)
//IL_007f: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: 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_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
//IL_009c: Unknown result type (might be due to invalid IL or missing references)
if (!((Object)(object)w == (Object)null) && !((Object)(object)w.monitorWindowRect == (Object)null))
{
RectTransform monitorWindowRect = w.monitorWindowRect;
int instanceID = ((Object)w).GetInstanceID();
if (!BasePos.TryGetValue(instanceID, out var value))
{
value = monitorWindowRect.anchoredPosition;
BasePos[instanceID] = value;
}
Vector2 sizeDelta = monitorWindowRect.sizeDelta;
monitorWindowRect.sizeDelta = new Vector2(sizeDelta.x, sizeDelta.y + 56f);
float num = 0f;
if (Mathf.Abs(monitorWindowRect.anchorMax.y - monitorWindowRect.anchorMin.y) < 0.001f)
{
num = (0f - (1f - monitorWindowRect.pivot.y)) * 56f;
}
monitorWindowRect.anchoredPosition = new Vector2(value.x, value.y + num);
}
}
private static void Create(UIMonitorWindow w, Transform host)
{
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Expected O, but got Unknown
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_006b: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_0080: Unknown result type (might be due to invalid IL or missing references)
//IL_008b: Unknown result type (might be due to invalid IL or missing references)
//IL_009f: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
//IL_0119: Unknown result type (might be due to invalid IL or missing references)
//IL_011e: Unknown result type (might be due to invalid IL or missing references)
//IL_0130: Unknown result type (might be due to invalid IL or missing references)
//IL_0136: Unknown result type (might be due to invalid IL or missing references)
//IL_014f: Expected O, but got Unknown
//IL_014f: Unknown result type (might be due to invalid IL or missing references)
//IL_015f: Unknown result type (might be due to invalid IL or missing references)
//IL_0169: Unknown result type (might be due to invalid IL or missing references)
//IL_019f: Unknown result type (might be due to invalid IL or missing references)
//IL_01a9: Expected O, but got Unknown
//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
//IL_01de: Unknown result type (might be due to invalid IL or missing references)
//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
//IL_021b: Unknown result type (might be due to invalid IL or missing references)
//IL_0234: Expected O, but got Unknown
//IL_0258: Unknown result type (might be due to invalid IL or missing references)
//IL_0271: Expected O, but got Unknown
//IL_02f1: Unknown result type (might be due to invalid IL or missing references)
//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
//IL_0308: Unknown result type (might be due to invalid IL or missing references)
//IL_0313: Unknown result type (might be due to invalid IL or missing references)
//IL_0319: Unknown result type (might be due to invalid IL or missing references)
//IL_031e: Unknown result type (might be due to invalid IL or missing references)
//IL_031f: Unknown result type (might be due to invalid IL or missing references)
//IL_0329: Unknown result type (might be due to invalid IL or missing references)
//IL_032a: Unknown result type (might be due to invalid IL or missing references)
//IL_0334: Unknown result type (might be due to invalid IL or missing references)
//IL_0335: Unknown result type (might be due to invalid IL or missing references)
//IL_033f: Unknown result type (might be due to invalid IL or missing references)
//IL_0363: Unknown result type (might be due to invalid IL or missing references)
//IL_0379: Unknown result type (might be due to invalid IL or missing references)
//IL_0392: Expected O, but got Unknown
//IL_03e2: Unknown result type (might be due to invalid IL or missing references)
//IL_03fb: Expected O, but got Unknown
//IL_0400: Unknown result type (might be due to invalid IL or missing references)
//IL_043c: Unknown result type (might be due to invalid IL or missing references)
//IL_0441: Unknown result type (might be due to invalid IL or missing references)
//IL_044c: Unknown result type (might be due to invalid IL or missing references)
//IL_0456: Unknown result type (might be due to invalid IL or missing references)
//IL_0461: Unknown result type (might be due to invalid IL or missing references)
//IL_046b: Unknown result type (might be due to invalid IL or missing references)
//IL_0476: Unknown result type (might be due to invalid IL or missing references)
//IL_0480: Unknown result type (might be due to invalid IL or missing references)
//IL_048b: Unknown result type (might be due to invalid IL or missing references)
//IL_049f: Unknown result type (might be due to invalid IL or missing references)
//IL_04ae: Unknown result type (might be due to invalid IL or missing references)
GameObject val = new GameObject("BeltThrottlePanel", new Type[1] { typeof(RectTransform) });
val.transform.SetParent(host, false);
val.transform.SetAsLastSibling();
RectTransform val2 = (RectTransform)val.transform;
val2.anchorMin = new Vector2(0f, 0f);
val2.anchorMax = new Vector2(1f, 0f);
val2.pivot = new Vector2(0.5f, 0f);
val2.anchoredPosition = new Vector2(0f, 8f);
val2.sizeDelta = new Vector2(-36f, 42f);
val.AddComponent<LayoutElement>().ignoreLayout = true;
Image obj = val.AddComponent<Image>();
((Graphic)obj).color = new Color(0f, 0f, 0f, 0.22f);
((Graphic)obj).raycastTarget = false;
Font font = GetFont(w);
GameObject val3 = new GameObject("btSwitch", new Type[3]
{
typeof(RectTransform),
typeof(Image),
typeof(Button)
});
val3.transform.SetParent(val.transform, false);
Place((RectTransform)val3.transform, 8f, 52f, 26f);
_switchBg = val3.GetComponent<Image>();
((Graphic)_switchBg).color = OffColor;
_switchButton = val3.GetComponent<Button>();
((Selectable)_switchButton).targetGraphic = (Graphic)(object)_switchBg;
((Selectable)_switchButton).transition = (Transition)0;
((UnityEvent)_switchButton.onClick).AddListener(new UnityAction(OnSwitchClick));
Text obj2 = NewText(val3.transform, font, "关", 13, (TextAnchor)4);
RectTransform val4 = (RectTransform)((Component)obj2).transform;
val4.anchorMin = Vector2.zero;
val4.anchorMax = Vector2.one;
val4.offsetMin = Vector2.zero;
val4.offsetMax = Vector2.zero;
_switchText = obj2;
Text obj3 = NewText(val.transform, font, "限速器", 14, (TextAnchor)3);
((Object)obj3).name = "btText_label";
Place((RectTransform)((Component)obj3).transform, 68f, 60f, 20f);
Text obj4 = NewText(val.transform, font, "目标流速", 14, (TextAnchor)3);
((Object)obj4).name = "btText_label2";
Place((RectTransform)((Component)obj4).transform, 138f, 64f, 20f);
if ((Object)(object)w.cargoFlowInputField != (Object)null)
{
InputField val5 = Object.Instantiate<InputField>(w.cargoFlowInputField, val.transform);
((Object)val5).name = "btRate";
((UnityEventBase)val5.onValueChanged).RemoveAllListeners();
((UnityEventBase)val5.onEndEdit).RemoveAllListeners();
((UnityEvent<string>)(object)val5.onEndEdit).AddListener((UnityAction<string>)OnRateEndEdit);
GameObject val6 = new GameObject("bg", new Type[2]
{
typeof(RectTransform),
typeof(Image)
});
val6.transform.SetParent(((Component)val5).transform, false);
val6.transform.SetAsFirstSibling();
RectTransform val7 = (RectTransform)val6.transform;
val7.anchorMin = Vector2.zero;
val7.anchorMax = Vector2.one;
val7.offsetMin = Vector2.zero;
val7.offsetMax = Vector2.zero;
Image component = val6.GetComponent<Image>();
((Graphic)component).color = new Color(1f, 1f, 1f, 0.15f);
((Graphic)component).raycastTarget = false;
Place((RectTransform)((Component)val5).transform, 206f, 84f, 26f);
_rateField = val5;
}
else if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)"[BeltThrottle] 面板里没有 cargoFlowInputField,速度输入框无法创建。");
}
_unitText = NewText(val.transform, font, "个/分钟", 13, (TextAnchor)3);
((Object)_unitText).name = "btText_unit";
Place((RectTransform)((Component)_unitText).transform, 296f, 64f, 20f);
((Graphic)_unitText).color = DimColor;
_hintText = NewText(val.transform, font, "", 12, (TextAnchor)5);
((Object)_hintText).name = "btText_hint";
RectTransform val8 = (RectTransform)((Component)_hintText).transform;
val8.anchorMin = new Vector2(0f, 0.5f);
val8.anchorMax = new Vector2(1f, 0.5f);
val8.pivot = new Vector2(1f, 0.5f);
val8.anchoredPosition = new Vector2(-10f, 0f);
val8.sizeDelta = new Vector2(200f, 20f);
((Graphic)_hintText).color = DimColor;
_root = val;
}
private static void Rebind()
{
//IL_0085: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Expected O, but got Unknown
//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
//IL_00aa: Expected O, but got Unknown
_switchButton = null;
_switchBg = null;
_switchText = null;
_rateField = null;
_unitText = null;
_hintText = null;
if ((Object)(object)_root == (Object)null)
{
return;
}
Transform transform = _root.transform;
Transform val = transform.Find("btSwitch");
if ((Object)(object)val != (Object)null)
{
_switchButton = ((Component)val).GetComponent<Button>();
_switchBg = ((Component)val).GetComponent<Image>();
if ((Object)(object)_switchButton != (Object)null)
{
((UnityEvent)_switchButton.onClick).RemoveListener(new UnityAction(OnSwitchClick));
((UnityEvent)_switchButton.onClick).AddListener(new UnityAction(OnSwitchClick));
}
Transform val2 = val.Find("btText");
if ((Object)(object)val2 != (Object)null)
{
_switchText = ((Component)val2).GetComponent<Text>();
}
}
Transform val3 = transform.Find("btRate");
if ((Object)(object)val3 != (Object)null)
{
_rateField = ((Component)val3).GetComponent<InputField>();
if ((Object)(object)_rateField != (Object)null)
{
((UnityEvent<string>)(object)_rateField.onEndEdit).RemoveListener((UnityAction<string>)OnRateEndEdit);
((UnityEvent<string>)(object)_rateField.onEndEdit).AddListener((UnityAction<string>)OnRateEndEdit);
}
}
Transform val4 = transform.Find("btText_unit");
if ((Object)(object)val4 != (Object)null)
{
_unitText = ((Component)val4).GetComponent<Text>();
}
Transform val5 = transform.Find("btText_hint");
if ((Object)(object)val5 != (Object)null)
{
_hintText = ((Component)val5).GetComponent<Text>();
}
}
private static void OnSwitchClick()
{
if (_current != null)
{
_current.enabled = !_current.enabled;
ThrottleSettings.Save();
GateManager.Invalidate();
ApplySwitchVisual(_current.enabled);
UpdateHint();
if (Plugin.Log != null)
{
Plugin.Log.LogInfo((object)string.Format("[BeltThrottle] 监控器 entity#{0} 限速器{1}", _boundEntity, _current.enabled ? "开启" : "关闭"));
}
}
}
private static void ApplySwitchVisual(bool on)
{
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)_switchBg != (Object)null)
{
((Graphic)_switchBg).color = (on ? OnColor : OffColor);
}
if ((Object)(object)_switchText != (Object)null)
{
_switchText.text = (on ? "开" : "关");
}
}
private static void OnRateEndEdit(string text)
{
if (_current != null)
{
if (!float.TryParse(text, out var result))
{
result = _current.rate;
}
if (result < 0f)
{
result = 0f;
}
if (result > 100000f)
{
result = 100000f;
}
_current.rate = result;
ThrottleSettings.Save();
GateManager.Invalidate();
if ((Object)(object)_rateField != (Object)null)
{
_rateField.SetTextWithoutNotify(FormatRate(result));
}
UpdateHint();
}
}
private static void UpdateHint()
{
//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)_hintText == (Object)null || _current == null)
{
return;
}
if (!_current.enabled)
{
_hintText.text = "关闭 = 原版监控器";
return;
}
if (_current.rate <= 0f)
{
_hintText.text = "速率 0 = 不限流";
return;
}
CargoTraffic val = (((Object)(object)_window != (Object)null) ? _window.cargoTraffic : null);
int planetId = (((Object)(object)_window != (Object)null && _window.factory != null) ? _window.factory.planetId : 0);
float num = 0f;
if (val != null && (Object)(object)_window != (Object)null && _window.monitorId > 0 && val.monitorPool != null && _window.monitorId < val.monitorPool.Length)
{
num = GateManager.GetRatio(val, val.monitorPool[_window.monitorId], planetId);
}
_hintText.text = ((num <= 0f) ? "目标 ≥ 满速,不限流" : $"放行 {num * 100f:0.#}%");
}
private static string FormatRate(float v)
{
return v.ToString("0.###");
}
private static Font GetFont(UIMonitorWindow w)
{
try
{
if ((Object)(object)w.periodText != (Object)null && (Object)(object)w.periodText.font != (Object)null)
{
return w.periodText.font;
}
if ((Object)(object)w.cargoFlowInputField != (Object)null && (Object)(object)w.cargoFlowInputField.textComponent != (Object)null && (Object)(object)w.cargoFlowInputField.textComponent.font != (Object)null)
{
return w.cargoFlowInputField.textComponent.font;
}
}
catch
{
}
try
{
return Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
}
catch
{
return null;
}
}
private static Text NewText(Transform parent, Font font, string content, int size, TextAnchor align)
{
//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_0052: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
GameObject val = new GameObject("btText", new Type[2]
{
typeof(RectTransform),
typeof(Text)
});
val.transform.SetParent(parent, false);
Text component = val.GetComponent<Text>();
component.font = font;
component.fontSize = size;
component.text = content;
component.alignment = align;
((Graphic)component).color = TextColor;
component.horizontalOverflow = (HorizontalWrapMode)1;
component.verticalOverflow = (VerticalWrapMode)1;
((Graphic)component).raycastTarget = false;
return component;
}
private static void Place(RectTransform rt, float x, float width, float height)
{
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_0053: Unknown result type (might be due to invalid IL or missing references)
rt.anchorMin = new Vector2(0f, 0.5f);
rt.anchorMax = new Vector2(0f, 0.5f);
rt.pivot = new Vector2(0f, 0.5f);
rt.anchoredPosition = new Vector2(x, 0f);
rt.sizeDelta = new Vector2(width, height);
}
}
internal static class Patches
{
[HarmonyPatch(typeof(CargoPath), "Update")]
internal static class Patch_CargoPath_Update
{
private static bool Prefix(CargoPath __instance)
{
try
{
return !GateManager.IsBlocked(__instance);
}
catch
{
return true;
}
}
}
[HarmonyPatch(typeof(CargoTraffic), "Import")]
internal static class Patch_CargoTraffic_Import
{
private static void Postfix(CargoTraffic __instance)
{
GateManager.Invalidate();
}
}
internal static void Apply(Harmony harmony)
{
PatchOne(harmony, typeof(Patch_CargoPath_Update));
PatchOne(harmony, typeof(Patch_CargoTraffic_Import));
PatchOne(harmony, typeof(Patch_UIMonitorWindow_OnOpen));
PatchOne(harmony, typeof(Patch_UIMonitorWindow_OnMonitorIdChange));
PatchOne(harmony, typeof(Patch_BP_CopyFromFactoryObject));
PatchOne(harmony, typeof(Patch_BP_ToParamsArray));
PatchOne(harmony, typeof(Patch_BP_FromParamsArray));
PatchOne(harmony, typeof(Patch_BP_PasteToFactoryObject));
PatchOne(harmony, typeof(Patch_BP_ApplyPrebuildParametersToEntity));
}
private static void PatchOne(Harmony harmony, Type type)
{
try
{
harmony.CreateClassProcessor(type).Patch();
}
catch (Exception ex)
{
if (Plugin.Log != null)
{
Plugin.Log.LogError((object)("[BeltThrottle] 打补丁失败 " + type.Name + ": " + ex));
}
}
}
}
[HarmonyPatch(typeof(UIMonitorWindow), "_OnOpen")]
internal static class Patch_UIMonitorWindow_OnOpen
{
private static void Postfix(UIMonitorWindow __instance)
{
try
{
MonitorPanel.Attach(__instance);
}
catch (Exception ex)
{
if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)("[BeltThrottle] 面板创建失败: " + ex));
}
}
}
}
[HarmonyPatch(typeof(UIMonitorWindow), "OnMonitorIdChange")]
internal static class Patch_UIMonitorWindow_OnMonitorIdChange
{
private static void Postfix(UIMonitorWindow __instance)
{
try
{
MonitorPanel.Refresh(__instance);
}
catch (Exception ex)
{
if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)("[BeltThrottle] 面板刷新失败: " + ex.Message));
}
}
}
}
[BepInPlugin("LDG.BeltThrottle", "Belt Throttle for Cargo Monitor", "1.3.2")]
public class Plugin : BaseUnityPlugin
{
public const string GUID = "LDG.BeltThrottle";
public const string NAME = "Belt Throttle for Cargo Monitor";
public const string VERSION = "1.3.2";
internal static ManualLogSource Log;
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<bool> StackAware;
internal static ConfigEntry<bool> VerboseLog;
private Harmony _harmony;
private bool _frameErrorLogged;
private void Awake()
{
//IL_007b: Unknown result type (might be due to invalid IL or missing references)
//IL_0085: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("throttle", "Enabled", true, "总开关。关掉后所有流速监控器都恢复原版行为。\nMaster switch. When off, every cargo monitor returns to vanilla behaviour.");
StackAware = ((BaseUnityPlugin)this).Config.Bind<bool>("throttle", "StackAware", true, "按「物品个数」限流:传送带上的货物如果堆叠了 N 个,就自动按 N 倍折算,填 120 表示每分钟 120 个物品而不是 120 个格子。\n关掉则按「货物格子」限流,和原版监控器显示的数字含义一致。\nThrottle by item count (accounts for stacked cargo).");
VerboseLog = ((BaseUnityPlugin)this).Config.Bind<bool>("throttle", "VerboseLog", false, "输出每个监控器的限流细节(目标流速、满速、放行比例)。排查问题时打开。\nLog per-monitor throttling details.");
ThrottleSettings.Init(Paths.ConfigPath);
_harmony = new Harmony("LDG.BeltThrottle");
try
{
Patches.Apply(_harmony);
((BaseUnityPlugin)this).Logger.LogInfo((object)"[BeltThrottle] Harmony patches applied.");
VerifyPatch("CargoPath.Update", typeof(CargoPath), "Update");
VerifyPatch("BuildingParameters.ToParamsArray", typeof(BuildingParameters), "ToParamsArray", new Type[2]
{
typeof(int[]).MakeByRefType(),
typeof(int).MakeByRefType()
});
VerifyPatch("BuildingParameters.FromParamsArray", typeof(BuildingParameters), "FromParamsArray", new Type[1] { typeof(int[]) });
VerifyPatch("BuildingParameters.PasteToFactoryObject", typeof(BuildingParameters), "PasteToFactoryObject", new Type[2]
{
typeof(int),
typeof(PlanetFactory)
});
VerifyPatch("BuildingParameters.ApplyPrebuildParametersToEntity", typeof(BuildingParameters), "ApplyPrebuildParametersToEntity", new Type[6]
{
typeof(int),
typeof(int),
typeof(int),
typeof(int[]),
typeof(string),
typeof(PlanetFactory)
});
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogError((object)("[BeltThrottle] Patch failed: " + ex));
}
((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("[BeltThrottle] {0} v{1} loaded. 打开流速监控器面板,底部「限速器」开关 + 目标流速输入框。", "Belt Throttle for Cargo Monitor", "1.3.2"));
}
private static void VerifyPatch(string label, Type type, string method, Type[] argTypes = null)
{
try
{
MethodInfo method2 = type.GetMethod(method, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, argTypes ?? Type.EmptyTypes, null);
if (method2 == null)
{
Log.LogWarning((object)("[BeltThrottle] patch check: " + label + " 方法不存在!"));
return;
}
Patches patchInfo = Harmony.GetPatchInfo((MethodBase)method2);
int num = ((patchInfo != null && patchInfo.Prefixes != null) ? patchInfo.Prefixes.Count : 0);
int num2 = ((patchInfo != null && patchInfo.Postfixes != null) ? patchInfo.Postfixes.Count : 0);
if (num + num2 == 0)
{
Log.LogError((object)("[BeltThrottle] patch check: " + label + " 没有挂到补丁,限流不会生效!"));
}
else
{
Log.LogInfo((object)$"[BeltThrottle] patch check: {label} prefix={num} postfix={num2}");
}
}
catch (Exception ex)
{
Log.LogWarning((object)("[BeltThrottle] patch check 失败(" + label + "): " + ex.Message));
}
}
private void Update()
{
try
{
GateManager.Frame();
ThrottleSettings.FlushIfDirty();
}
catch (Exception ex)
{
if (!_frameErrorLogged)
{
_frameErrorLogged = true;
((BaseUnityPlugin)this).Logger.LogError((object)("[BeltThrottle] 刷新限流快照异常(后续不再重复输出): " + ex));
}
}
}
private void OnDestroy()
{
if (_harmony != null)
{
_harmony.UnpatchSelf();
_harmony = null;
}
}
}
internal sealed class MonitorSetting
{
public bool enabled;
public float rate;
public int unit;
public float RatePerMinute
{
get
{
if (unit != 1)
{
return rate;
}
return rate * 60f;
}
}
public MonitorSetting Clone()
{
return new MonitorSetting
{
enabled = enabled,
rate = rate,
unit = unit
};
}
}
internal static class ThrottleSettings
{
private static readonly Dictionary<long, MonitorSetting> Map = new Dictionary<long, MonitorSetting>();
private static string _path;
private static bool _loaded;
private static bool _dirty;
internal static void ApplyFromBlueprint(int planetId, int entityId, int slotEnabled, int slotRate)
{
if (planetId >= 0 && entityId > 0)
{
long key = Key(planetId, entityId);
int num = MonitorParams.DecodeRate(slotRate);
if (slotEnabled != 0 && num > 0)
{
MonitorSetting monitorSetting = new MonitorSetting();
monitorSetting.enabled = true;
monitorSetting.unit = MonitorParams.DecodeUnit(slotRate);
monitorSetting.rate = (float)num / 100f;
Map[key] = monitorSetting;
_dirty = true;
GateManager.Invalidate();
}
}
}
internal static void FlushIfDirty()
{
if (_dirty)
{
_dirty = false;
Save();
}
}
internal static void Init(string configDir)
{
if (!_loaded)
{
_loaded = true;
string text = Path.Combine(configDir, "LDG.BeltThrottle.settings.txt");
string text2 = Path.Combine(configDir, "local.dsp.beltthrottle.settings.txt");
_path = ((File.Exists(text) || !File.Exists(text2)) ? text : text2);
Load();
_path = text;
}
}
private static long Key(int planetId, int entityId)
{
return ((long)planetId << 32) | (uint)entityId;
}
internal static MonitorSetting Peek(int planetId, int entityId)
{
if (!Map.TryGetValue(Key(planetId, entityId), out var value))
{
return null;
}
return value;
}
internal static string DescribeEnabled()
{
StringBuilder stringBuilder = new StringBuilder();
foreach (KeyValuePair<long, MonitorSetting> item in Map)
{
MonitorSetting value = item.Value;
if (value != null && value.enabled)
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append("、");
}
stringBuilder.Append((int)(item.Key >> 32)).Append('#').Append((int)(item.Key & 0xFFFFFFFFu))
.Append("=")
.Append(value.RatePerMinute.ToString("0.#", CultureInfo.InvariantCulture));
}
}
return stringBuilder.ToString();
}
internal static MonitorSetting Get(int planetId, int entityId)
{
if (Map.TryGetValue(Key(planetId, entityId), out var value))
{
return value;
}
value = new MonitorSetting();
value.enabled = false;
value.rate = 60f;
value.unit = 0;
Map[Key(planetId, entityId)] = value;
return value;
}
internal static void Save()
{
if (string.IsNullOrEmpty(_path))
{
return;
}
try
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("# BeltThrottle settings. planetId,entityId,enabled,rate,unit(0=perMinute,1=perSecond)");
foreach (KeyValuePair<long, MonitorSetting> item in Map)
{
MonitorSetting value = item.Value;
if (value.enabled || !(value.rate <= 0f))
{
int num = (int)(item.Key >> 32);
int num2 = (int)(item.Key & 0xFFFFFFFFu);
stringBuilder.Append(num.ToString(CultureInfo.InvariantCulture)).Append(',').Append(num2.ToString(CultureInfo.InvariantCulture))
.Append(',')
.Append(value.enabled ? "1" : "0")
.Append(',')
.Append(value.rate.ToString("0.####", CultureInfo.InvariantCulture))
.Append(',')
.Append(value.unit.ToString(CultureInfo.InvariantCulture))
.AppendLine();
}
}
File.WriteAllText(_path, stringBuilder.ToString(), Encoding.UTF8);
}
catch (Exception ex)
{
if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)("[BeltThrottle] 保存限速设置失败: " + ex.Message));
}
}
}
private static void Load()
{
if (!File.Exists(_path))
{
return;
}
try
{
string[] array = File.ReadAllLines(_path, Encoding.UTF8);
foreach (string text in array)
{
if (string.IsNullOrEmpty(text) || text[0] == '#')
{
continue;
}
string[] array2 = text.Split(',');
if (array2.Length >= 5 && int.TryParse(array2[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) && int.TryParse(array2[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var result2) && float.TryParse(array2[3], NumberStyles.Float, CultureInfo.InvariantCulture, out var result3))
{
if (!int.TryParse(array2[4], NumberStyles.Integer, CultureInfo.InvariantCulture, out var result4))
{
result4 = 0;
}
MonitorSetting monitorSetting = new MonitorSetting();
monitorSetting.enabled = array2[2] == "1";
monitorSetting.rate = result3;
monitorSetting.unit = ((result4 == 1) ? 1 : 0);
Map[Key(result, result2)] = monitorSetting;
}
}
}
catch (Exception ex)
{
if (Plugin.Log != null)
{
Plugin.Log.LogWarning((object)("[BeltThrottle] 读取限速设置失败: " + ex.Message));
}
}
}
}