using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 DysonSphereMods.Shared;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
[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("Valoneu")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyDescription("Auto-spray items planet-wide using proliferator from station storage.")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+f8914ec5d6c5105716cc35f47e78ae71109eb882")]
[assembly: AssemblyProduct("DysonSphereMods")]
[assembly: AssemblyTitle("DistributeSpray")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace DysonSphereMods.Shared
{
public static class Log
{
private static ManualLogSource _logger;
public static void Init(ManualLogSource logger)
{
_logger = logger;
}
public static void Debug(object data)
{
ManualLogSource logger = _logger;
if (logger != null)
{
logger.LogDebug(data);
}
}
public static void Info(object data)
{
ManualLogSource logger = _logger;
if (logger != null)
{
logger.LogInfo(data);
}
}
public static void Warning(object data)
{
ManualLogSource logger = _logger;
if (logger != null)
{
logger.LogWarning(data);
}
}
public static void Error(object data)
{
ManualLogSource logger = _logger;
if (logger != null)
{
logger.LogError(data);
}
}
public static void Fatal(object data)
{
ManualLogSource logger = _logger;
if (logger != null)
{
logger.LogFatal(data);
}
}
public static void Message(object data)
{
ManualLogSource logger = _logger;
if (logger != null)
{
logger.LogMessage(data);
}
}
public static void LogOnce(string msg, ref bool flag, params object[] args)
{
if (flag)
{
return;
}
flag = true;
try
{
string[] array = ((args == null) ? Array.Empty<string>() : args.Select((object arg) => (arg != null) ? ((!(arg is int) && !(arg is string) && !arg.GetType().IsPrimitive) ? JsonUtility.ToJson(arg) : arg.ToString()) : "null").ToArray());
object[] args2 = array;
Info(string.Format(msg, args2));
}
catch (Exception arg2)
{
Warning($"LogOnce failed to format message: {msg}. Exception: {arg2}");
}
}
}
public static class MultiplierService
{
private static readonly Dictionary<string, float> _multipliers = new Dictionary<string, float>();
private static bool _isDirty;
public static event Action OnMultipliersChanged;
public static void SetMultiplier(string key, float value)
{
if (!_multipliers.TryGetValue(key, out var value2) || Math.Abs(value2 - value) > 0.0001f)
{
_multipliers[key] = value;
_isDirty = true;
}
}
public static float GetMultiplier(string key, float defaultValue = 1f)
{
if (!_multipliers.TryGetValue(key, out var value))
{
return defaultValue;
}
return value;
}
public static void CommitChanges()
{
if (_isDirty)
{
_isDirty = false;
MultiplierService.OnMultipliersChanged?.Invoke();
}
}
}
public static class TickManager
{
private static long _lastSlowTick = -1L;
private static long _lastLazyTick = -1L;
private static bool _patched = false;
public static event Action OnSlowTick;
public static event Action OnLazyTick;
public static void Patch(Harmony harmony)
{
if (!_patched)
{
_patched = true;
harmony.PatchAll(typeof(TickManager));
}
}
[HarmonyPatch(typeof(GameMain), "Begin")]
[HarmonyPostfix]
public static void Init()
{
_lastSlowTick = -1L;
_lastLazyTick = -1L;
}
[HarmonyPatch(typeof(GameLogic), "LogicFrame")]
[HarmonyPostfix]
public static void GameTick()
{
long gameTick = GameMain.gameTick;
if (gameTick / 60 > _lastSlowTick)
{
_lastSlowTick = gameTick / 60;
TickManager.OnSlowTick?.Invoke();
}
if (gameTick / 600 > _lastLazyTick)
{
_lastLazyTick = gameTick / 600;
TickManager.OnLazyTick?.Invoke();
}
}
}
public abstract class WindowBase
{
public Rect WindowRect;
protected Vector2 ScrollPos;
private bool _isResizing;
private Rect _resizeRect = new Rect(0f, 0f, 15f, 15f);
public Vector2 MinSize = new Vector2(300f, 200f);
public int WindowId { get; protected set; }
public string Title { get; set; }
public bool IsVisible { get; set; }
protected WindowBase(int windowId, string title, Rect defaultRect)
{
//IL_0015: 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_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_0049: 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)
WindowId = windowId;
Title = title;
WindowRect = defaultRect;
}
public virtual void OnGUI()
{
//IL_001d: 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_0050: Expected O, but got Unknown
//IL_004b: 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_0055: Unknown result type (might be due to invalid IL or missing references)
if (IsVisible)
{
GUI.backgroundColor = new Color(0.08f, 0.12f, 0.22f, 0.95f);
WindowRect = GUILayout.Window(WindowId, WindowRect, new WindowFunction(DrawWindowInternal), Title, Array.Empty<GUILayoutOption>());
GUI.backgroundColor = Color.white;
((Rect)(ref WindowRect)).x = Mathf.Clamp(((Rect)(ref WindowRect)).x, 0f - ((Rect)(ref WindowRect)).width + 50f, (float)(Screen.width - 50));
((Rect)(ref WindowRect)).y = Mathf.Clamp(((Rect)(ref WindowRect)).y, -20f, (float)(Screen.height - 50));
}
}
private void DrawWindowInternal(int id)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: 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_009a: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
//IL_00af: Unknown result type (might be due to invalid IL or missing references)
//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
//IL_00d8: Expected O, but got Unknown
//IL_00dd: Expected O, but got Unknown
//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
//IL_0115: Unknown result type (might be due to invalid IL or missing references)
//IL_011b: Invalid comparison between Unknown and I4
//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
//IL_012a: Unknown result type (might be due to invalid IL or missing references)
//IL_0130: Invalid comparison between Unknown and I4
//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
//IL_014d: Unknown result type (might be due to invalid IL or missing references)
//IL_016a: Unknown result type (might be due to invalid IL or missing references)
DrawWindowHeader();
ScrollPos = GUILayout.BeginScrollView(ScrollPos, Array.Empty<GUILayoutOption>());
DrawWindowContent();
GUILayout.EndScrollView();
DrawWindowFooter();
((Rect)(ref _resizeRect)).x = ((Rect)(ref WindowRect)).width - 20f;
((Rect)(ref _resizeRect)).y = ((Rect)(ref WindowRect)).height - 20f;
((Rect)(ref _resizeRect)).width = 20f;
((Rect)(ref _resizeRect)).height = 20f;
GUI.Label(_resizeRect, "↘", new GUIStyle(GUI.skin.label)
{
alignment = (TextAnchor)4,
fontSize = 20,
normal = new GUIStyleState
{
textColor = new Color(0.6f, 0.6f, 0.6f, 0.8f)
}
});
Event current = Event.current;
bool flag = false;
if ((int)current.type == 0 && ((Rect)(ref _resizeRect)).Contains(current.mousePosition))
{
_isResizing = true;
flag = true;
current.Use();
}
else if ((int)current.type == 1)
{
_isResizing = false;
}
else if ((int)current.type == 3 && _isResizing)
{
ref Rect windowRect = ref WindowRect;
((Rect)(ref windowRect)).width = ((Rect)(ref windowRect)).width + current.delta.x;
ref Rect windowRect2 = ref WindowRect;
((Rect)(ref windowRect2)).height = ((Rect)(ref windowRect2)).height + current.delta.y;
((Rect)(ref WindowRect)).width = Mathf.Max(MinSize.x, ((Rect)(ref WindowRect)).width);
((Rect)(ref WindowRect)).height = Mathf.Max(MinSize.y, ((Rect)(ref WindowRect)).height);
current.Use();
}
if ((int)current.type == 0 && !flag)
{
GUIUtility.keyboardControl = 0;
}
GUI.DragWindow();
}
protected virtual void DrawWindowHeader()
{
}
protected abstract void DrawWindowContent();
protected virtual void DrawWindowFooter()
{
}
public virtual void Toggle()
{
IsVisible = !IsVisible;
}
}
}
namespace DistributeSpray
{
[BepInPlugin("com.Valoneu.DistributeSpray", "DistributeSpray", "1.0.1")]
[BepInProcess("DSPGAME.exe")]
public class DistributeSprayPlugin : BaseUnityPlugin
{
public const string GUID = "com.Valoneu.DistributeSpray";
public const string NAME = "DistributeSpray";
public const string VERSION = "1.0.1";
public static ConfigEntry<bool> ModEnabled;
private void Awake()
{
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Expected O, but got Unknown
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
//IL_00f2: Expected O, but got Unknown
//IL_00f2: Expected O, but got Unknown
//IL_017d: Unknown result type (might be due to invalid IL or missing references)
//IL_0192: Unknown result type (might be due to invalid IL or missing references)
//IL_019f: Expected O, but got Unknown
//IL_019f: Expected O, but got Unknown
//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
//IL_0213: Unknown result type (might be due to invalid IL or missing references)
//IL_0220: Expected O, but got Unknown
//IL_0220: Expected O, but got Unknown
Log.Init(((BaseUnityPlugin)this).Logger);
ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable/disable automatic spraying.");
Harmony val = new Harmony("com.Valoneu.DistributeSpray");
val.PatchAll(typeof(GameBeginPatch));
MethodInfo methodInfo = AccessTools.Method(typeof(PlanetFactory), "InsertInto", new Type[6]
{
typeof(uint),
typeof(int),
typeof(int),
typeof(byte),
typeof(byte),
typeof(byte).MakeByRefType()
}, (Type[])null);
if (methodInfo != null)
{
val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(InsertPatches), "InsertInto_Prefix", (Type[])null), new HarmonyMethod(typeof(InsertPatches), "InsertInto_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
MethodInfo methodInfo2 = AccessTools.Method(typeof(PlanetFactory), "InsertInto", new Type[6]
{
typeof(int),
typeof(int),
typeof(int),
typeof(byte),
typeof(byte),
typeof(byte).MakeByRefType()
}, (Type[])null);
if (methodInfo2 != null)
{
val.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(InsertPatches), "InsertInto_Prefix", (Type[])null), new HarmonyMethod(typeof(InsertPatches), "InsertInto_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
MethodInfo methodInfo3 = AccessTools.Method(typeof(StationComponent), "AddItem", new Type[3]
{
typeof(int),
typeof(int),
typeof(int)
}, (Type[])null);
if (methodInfo3 != null)
{
val.Patch((MethodBase)methodInfo3, new HarmonyMethod(typeof(StationPatches), "AddItem_Prefix", (Type[])null), new HarmonyMethod(typeof(StationPatches), "AddItem_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
Log.Info("DistributeSpray v1.0.1 loaded!");
}
}
public static class StationPatches
{
public static void AddItem_Prefix(StationComponent __instance, int itemId, int count, ref int inc, ref bool __state)
{
if (!DistributeSprayPlugin.ModEnabled.Value || count <= 0 || itemId <= 0 || GameMain.data == null)
{
return;
}
PlanetFactory val = null;
if (GameMain.data.factories != null)
{
PlanetFactory[] factories = GameMain.data.factories;
foreach (PlanetFactory val2 in factories)
{
if (val2 != null && val2.planetId == __instance.planetId)
{
val = val2;
break;
}
}
}
if (val == null)
{
return;
}
SprayStatus status = SprayLogic.GetStatus(val.index);
if (status != null && status.incLevel > 0)
{
int num = count * status.incLevel - inc;
if (num > 0)
{
__state = true;
inc += num;
}
}
}
public static void AddItem_Postfix(StationComponent __instance, int __result, bool __state)
{
if (!__state || __result <= 0)
{
return;
}
PlanetFactory val = null;
if (GameMain.data != null && GameMain.data.factories != null)
{
PlanetFactory[] factories = GameMain.data.factories;
foreach (PlanetFactory val2 in factories)
{
if (val2 != null && val2.planetId == __instance.planetId)
{
val = val2;
break;
}
}
}
if (val != null)
{
SprayStatus status = SprayLogic.GetStatus(val.index);
if (status != null)
{
Interlocked.Add(ref status.incDebt, __result);
}
}
}
}
public class SprayStatus
{
public int incLevel;
public volatile int incDebt;
public int bestProlIdx;
}
[HarmonyPatch]
public static class GameBeginPatch
{
private static bool _subscribed;
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), "Begin")]
public static void OnGameBegin()
{
try
{
SprayLogic.InitStatusArray(GameMain.data.factories.Length);
if (_subscribed)
{
GameMain.logic.onFactoryFrameEnd -= SprayLogic.OnFactoryFrameEnd;
_subscribed = false;
}
GameMain.logic.onFactoryFrameEnd += SprayLogic.OnFactoryFrameEnd;
_subscribed = true;
}
catch
{
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), "End")]
public static void OnGameEnd()
{
try
{
if (_subscribed)
{
GameMain.logic.onFactoryFrameEnd -= SprayLogic.OnFactoryFrameEnd;
_subscribed = false;
}
}
catch
{
}
}
}
public static class SprayLogic
{
private static SprayStatus[] _statusArr;
public static readonly int[] ProlIds = new int[3] { 1143, 1142, 1141 };
public static readonly int[] ProlInc = new int[3] { 4, 2, 1 };
public static readonly int[] ProlSpray = new int[3] { 60, 24, 12 };
public static void InitStatusArray(int size)
{
_statusArr = new SprayStatus[size];
for (int i = 0; i < size; i++)
{
_statusArr[i] = new SprayStatus();
}
}
public static SprayStatus GetStatus(int fi)
{
if (_statusArr == null || fi < 0 || fi >= _statusArr.Length)
{
return null;
}
return _statusArr[fi];
}
public static void OnFactoryFrameEnd()
{
try
{
if (!DistributeSprayPlugin.ModEnabled.Value || GameMain.data == null || _statusArr == null)
{
return;
}
NexusLogic.Init();
GameData data = GameMain.data;
HashSet<int> hashSet = new HashSet<int>();
for (int i = 0; i < data.factoryCount; i++)
{
PlanetFactory val = data.factories[i];
if (val?.transport == null || i >= _statusArr.Length)
{
continue;
}
SprayStatus sprayStatus = _statusArr[i];
if (GameMain.instance.timei % 60 == 0)
{
int num = 0;
int bestProlIdx = -1;
for (int j = 1; j < val.transport.stationCursor; j++)
{
StationComponent val2 = val.transport.stationPool[j];
if (val2 == null || val2.id != j || val2.isCollector)
{
continue;
}
for (int k = 0; k < val2.storage.Length; k++)
{
int itemId = val2.storage[k].itemId;
int count = val2.storage[k].count;
if (itemId <= 0 || count <= 0)
{
continue;
}
for (int l = 0; l < ProlIds.Length; l++)
{
if (itemId == ProlIds[l] && ProlInc[l] > num)
{
num = ProlInc[l];
bestProlIdx = l;
}
}
if (sprayStatus.incLevel > 0)
{
int num2 = count * sprayStatus.incLevel;
if (val2.storage[k].inc < num2)
{
int value = (num2 - val2.storage[k].inc + sprayStatus.incLevel - 1) / sprayStatus.incLevel;
val2.storage[k].inc = num2;
Interlocked.Add(ref sprayStatus.incDebt, value);
}
}
}
}
sprayStatus.incLevel = num;
sprayStatus.bestProlIdx = bestProlIdx;
}
if (sprayStatus.incLevel > 0)
{
int num3 = val.planetId / 100 - 1;
if (hashSet.Add(num3))
{
int incDebt = 0;
NexusLogic.SprayNexus(num3, sprayStatus.incLevel, ref incDebt);
if (incDebt > 0)
{
Interlocked.Add(ref sprayStatus.incDebt, incDebt);
}
}
}
if (sprayStatus.incDebt <= 0 || sprayStatus.bestProlIdx < 0)
{
continue;
}
_ = sprayStatus.incDebt;
int num4 = ProlIds[sprayStatus.bestProlIdx];
int num5 = ProlSpray[sprayStatus.bestProlIdx];
GameStatData statistics = GameMain.statistics;
object obj;
if (statistics == null)
{
obj = null;
}
else
{
ProductionStatistics production = statistics.production;
if (production == null)
{
obj = null;
}
else
{
FactoryProductionStat[] factoryStatPool = production.factoryStatPool;
obj = ((factoryStatPool == null) ? null : factoryStatPool[val.index]?.consumeRegister);
}
}
int[] array = (int[])obj;
for (int m = 1; m < val.transport.stationCursor; m++)
{
if (sprayStatus.incDebt <= 0)
{
break;
}
StationComponent val3 = val.transport.stationPool[m];
if (val3 == null || val3.id != m || val3.isCollector)
{
continue;
}
for (int n = 0; n < val3.storage.Length; n++)
{
if (sprayStatus.incDebt <= 0)
{
break;
}
if (val3.storage[n].itemId != num4 || val3.storage[n].count <= 0)
{
continue;
}
int num6 = Math.Min((sprayStatus.incDebt + num5 - 1) / num5, val3.storage[n].count);
val3.storage[n].count -= num6;
Interlocked.Add(ref sprayStatus.incDebt, -num6 * num5);
if (array != null)
{
lock (array)
{
array[num4] += num6;
}
}
}
}
}
}
catch
{
}
}
}
public static class NexusLogic
{
private static Type _starAssemblyType;
private static FieldInfo _productStorageField;
private static FieldInfo _productStorageIncField;
private static bool _initialized;
public static void Init()
{
if (!_initialized)
{
_starAssemblyType = AccessTools.TypeByName("MoreMegaStructure.StarAssembly");
if (_starAssemblyType != null)
{
_productStorageField = AccessTools.Field(_starAssemblyType, "productStorage");
_productStorageIncField = AccessTools.Field(_starAssemblyType, "productStorageInc");
}
_initialized = true;
}
}
public static void SprayNexus(int starIndex, int incLevel, ref int incDebt)
{
if (_starAssemblyType == null || _productStorageField == null || _productStorageIncField == null)
{
return;
}
Dictionary<int, Dictionary<int, int>> dictionary = _productStorageField.GetValue(null) as Dictionary<int, Dictionary<int, int>>;
Dictionary<int, Dictionary<int, int>> dictionary2 = _productStorageIncField.GetValue(null) as Dictionary<int, Dictionary<int, int>>;
if (dictionary == null || !dictionary.TryGetValue(starIndex, out var value) || !dictionary2.TryGetValue(starIndex, out var value2))
{
return;
}
int[] array = value.Keys.System_Collections_Generic_ICollection_int_ToArray();
foreach (int num in array)
{
int num2 = value[num];
if (num > 0 && num2 > 0)
{
int num3 = num2 * incLevel;
value2.TryGetValue(num, out var value3);
if (value3 < num3)
{
int num4 = num3 - value3;
value2[num] = num3;
incDebt += (num4 + incLevel - 1) / incLevel;
}
}
}
}
}
internal static class Extensions
{
public static T[] System_Collections_Generic_ICollection_int_ToArray<T>(this ICollection<T> collection)
{
T[] array = new T[collection.Count];
collection.CopyTo(array, 0);
return array;
}
}
public static class InsertPatches
{
public static void InsertInto_Prefix(PlanetFactory __instance, byte itemCount, ref byte itemInc, ref bool __state)
{
if (!DistributeSprayPlugin.ModEnabled.Value)
{
return;
}
SprayStatus status = SprayLogic.GetStatus(__instance.index);
if (status != null && status.incLevel > 0)
{
int num = itemCount * status.incLevel - itemInc;
if (num > 0)
{
num = Math.Min(num, 255 - itemInc);
__state = true;
itemInc += (byte)num;
}
}
}
public static void InsertInto_Postfix(PlanetFactory __instance, int __result, bool __state)
{
if (__state && __result > 0)
{
SprayStatus status = SprayLogic.GetStatus(__instance.index);
if (status != null)
{
Interlocked.Add(ref status.incDebt, __result);
}
}
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "com.Valoneu.DistributeSpray";
public const string PLUGIN_NAME = "DistributeSpray";
public const string PLUGIN_VERSION = "1.0.1";
}
}