mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
Return feature-only helpers to their owners, remove inactive scaffolding and duplicate state, and reuse existing calculation and UI work without changing public contracts or gameplay. Record repository-wide coverage, justified retentions, independent reviews and validation, including baseline dashboard fixture failures. Assisted-by: Cindy (https://cindy.app)
194 lines
6.5 KiB
C#
194 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace Dustbin;
|
|
|
|
public static class BeltSignal
|
|
{
|
|
private static HashSet<int>[] _signalBelts;
|
|
private static bool _initialized;
|
|
private static Harmony _patch;
|
|
|
|
public static void Enable(bool on)
|
|
{
|
|
if (on)
|
|
{
|
|
_patch ??= Harmony.CreateAndPatchAll(typeof(BeltSignal));
|
|
InitSignalBelts();
|
|
}
|
|
else
|
|
{
|
|
_patch?.UnpatchSelf();
|
|
_patch = null;
|
|
_signalBelts = null;
|
|
}
|
|
}
|
|
|
|
private static Sprite LoadDustbinSprite()
|
|
{
|
|
using var stream = Assembly.GetExecutingAssembly()
|
|
.GetManifestResourceStream("Dustbin.assets.icon.signal-410.png")!;
|
|
var buffer = new byte[stream.Length];
|
|
_ = stream.Read(buffer, 0, buffer.Length);
|
|
var tex = new Texture2D(2, 2);
|
|
tex.LoadImage(buffer);
|
|
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPriority(Priority.Last)]
|
|
[HarmonyPatch(typeof(VFPreload), nameof(VFPreload.InvokeOnLoadWorkEnded))]
|
|
private static void VFPreload_InvokeOnLoadWorkEnded_Postfix()
|
|
{
|
|
if (_initialized) return;
|
|
var signals = LDB._signals;
|
|
var index = signals.dataArray.Length;
|
|
var p = new SignalProto
|
|
{
|
|
ID = 410,
|
|
Name = "DUSTBIN",
|
|
GridIndex = 3110,
|
|
IconPath = "Assets/signal-410.png",
|
|
_iconSprite = LoadDustbinSprite(),
|
|
SID = ""
|
|
};
|
|
p.name = p.Name.Translate();
|
|
signals.dataArray = [.. signals.dataArray.AddItem(p)];
|
|
signals.dataIndices[p.ID] = index;
|
|
_initialized = true;
|
|
}
|
|
|
|
private static void InitSignalBelts()
|
|
{
|
|
if (!GameMain.isRunning) return;
|
|
_signalBelts = new HashSet<int>[64];
|
|
|
|
var factories = GameMain.data?.factories;
|
|
if (factories == null) return;
|
|
foreach (var factory in factories)
|
|
{
|
|
var entitySignPool = factory?.entitySignPool;
|
|
if (entitySignPool == null) continue;
|
|
var cargoTraffic = factory.cargoTraffic;
|
|
var beltPool = cargoTraffic.beltPool;
|
|
for (var i = cargoTraffic.beltCursor - 1; i > 0; i--)
|
|
{
|
|
if (beltPool[i].id != i) continue;
|
|
ref var signal = ref entitySignPool[beltPool[i].entityId];
|
|
var signalId = signal.iconId0;
|
|
if (signalId != 410U) continue;
|
|
SetSignalBelt(factory.index, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void SetSignalBelt(int factory, int beltId)
|
|
{
|
|
var signalBelts = GetOrCreateSignalBelts(factory);
|
|
signalBelts?.Add(beltId);
|
|
}
|
|
|
|
private static HashSet<int> GetOrCreateSignalBelts(int index)
|
|
{
|
|
if (index < 0 || _signalBelts == null) return null;
|
|
if (index >= _signalBelts.Length)
|
|
{
|
|
var newCapacity = _signalBelts.Length * 2;
|
|
while (newCapacity <= index) newCapacity *= 2;
|
|
Array.Resize(ref _signalBelts, newCapacity);
|
|
}
|
|
return _signalBelts[index] ??= [];
|
|
}
|
|
|
|
private static HashSet<int> GetSignalBelts(int index)
|
|
{
|
|
return _signalBelts != null && index >= 0 && index < _signalBelts.Length ? _signalBelts[index] : null;
|
|
}
|
|
|
|
private static void RemoveSignalBelt(int factory, int beltId)
|
|
{
|
|
GetSignalBelts(factory)?.Remove(beltId);
|
|
}
|
|
|
|
private static void RemovePlanetSignalBelts(int factory)
|
|
{
|
|
GetSignalBelts(factory)?.Clear();
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(DigitalSystem), MethodType.Constructor, typeof(PlanetData))]
|
|
private static void DigitalSystem_Constructor_Postfix(PlanetData _planet)
|
|
{
|
|
var player = GameMain.mainPlayer;
|
|
if (player == null) return;
|
|
var factory = _planet?.factory;
|
|
if (factory == null) return;
|
|
RemovePlanetSignalBelts(factory.index);
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(GameMain), nameof(GameMain.Begin))]
|
|
private static void GameMain_Begin_Postfix()
|
|
{
|
|
InitSignalBelts();
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
[HarmonyPatch(typeof(CargoTraffic), nameof(CargoTraffic.RemoveBeltComponent))]
|
|
public static void CargoTraffic_RemoveBeltComponent_Prefix(CargoTraffic __instance, int id)
|
|
{
|
|
RemoveSignalBelt(__instance.factory.index, id);
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(CargoTraffic), nameof(CargoTraffic.SetBeltSignalIcon))]
|
|
public static void CargoTraffic_SetBeltSignalIcon_Postfix(CargoTraffic __instance, int signalId, int entityId)
|
|
{
|
|
var factory = __instance.factory;
|
|
var factoryIndex = factory.index;
|
|
var beltId = factory.entityPool[entityId].beltId;
|
|
if (signalId == 410)
|
|
{
|
|
SetSignalBelt(factoryIndex, beltId);
|
|
}
|
|
else
|
|
{
|
|
RemoveSignalBelt(factoryIndex, beltId);
|
|
}
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic.OnFactoryFrameBegin))]
|
|
public static void GameLogic_OnFactoryFrameBegin_Postfix(GameLogic __instance)
|
|
{
|
|
// TrashSystemGameTick overlaps worker-thread inserters; mutate cargo before factory workers start.
|
|
var data = __instance.data;
|
|
var factories = data?.factories;
|
|
if (factories == null || _signalBelts == null) return;
|
|
for (var i = 0; i < data.factoryCount; i++)
|
|
{
|
|
var factory = factories[i];
|
|
if (factory == null) continue;
|
|
var belts = GetSignalBelts(factory.index);
|
|
if (belts == null || belts.Count == 0) continue;
|
|
var consumeRegister = data.statistics.production.factoryStatPool[factory.index].consumeRegister;
|
|
var cargoTraffic = factory.cargoTraffic;
|
|
foreach (var beltId in belts)
|
|
{
|
|
if (beltId <= 0 || beltId >= cargoTraffic.beltCursor) continue;
|
|
ref var belt = ref cargoTraffic.beltPool[beltId];
|
|
if (belt.id != beltId) continue;
|
|
var cargoPath = cargoTraffic.GetCargoPath(belt.segPathId);
|
|
if (cargoPath == null) continue;
|
|
var itemId = cargoPath.TryPickItem(belt.segIndex + belt.segPivotOffset - 5, 12, out var stack, out _);
|
|
if (itemId <= 0) continue;
|
|
consumeRegister[itemId] += stack;
|
|
Dustbin.CalcGetSands(itemId, stack, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|