mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
fix(dustbin): restore belt disposal on the current game loop
Replace the obsolete GameData.GameTick transpiler with the serial factory-frame hook. Build current preloader-injected references instead of using the bundled 2023 game DLL. Refs #70, #69, #91
This commit is contained in:
1 parent
ac16794f07
commit
2cb351ebd3
6 files changed
+92
-43
No files matched your search
+33
-43
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using HarmonyLib;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -110,16 +109,17 @@ public static class BeltSignal
|
||||
private static void SetSignalBelt(int factory, int beltId)
|
||||
{
|
||||
var signalBelts = GetOrCreateSignalBelts(factory);
|
||||
signalBelts.Add(beltId);
|
||||
signalBelts?.Add(beltId);
|
||||
}
|
||||
|
||||
private static HashSet<int> GetOrCreateSignalBelts(int index)
|
||||
{
|
||||
HashSet<int> obj;
|
||||
if (index < 0) return null;
|
||||
if (index < 0 || _signalBelts == null) return null;
|
||||
if (index >= _signalBeltsCapacity)
|
||||
{
|
||||
var newCapacity = _signalBeltsCapacity * 2;
|
||||
while (newCapacity <= index) newCapacity *= 2;
|
||||
var newSignalBelts = new HashSet<int>[newCapacity];
|
||||
Array.Copy(_signalBelts, newSignalBelts, _signalBeltsCapacity);
|
||||
_signalBelts = newSignalBelts;
|
||||
@@ -138,7 +138,7 @@ public static class BeltSignal
|
||||
|
||||
private static HashSet<int> GetSignalBelts(int index)
|
||||
{
|
||||
return index >= 0 && index < _signalBeltsCapacity ? _signalBelts[index] : null;
|
||||
return _signalBelts != null && index >= 0 && index < _signalBeltsCapacity ? _signalBelts[index] : null;
|
||||
}
|
||||
|
||||
private static void RemoveSignalBelt(int factory, int beltId)
|
||||
@@ -171,21 +171,17 @@ public static class BeltSignal
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(CargoTraffic), nameof(CargoTraffic.RemoveBeltComponent))]
|
||||
public static void CargoTraffic_RemoveBeltComponent_Prefix(int id)
|
||||
public static void CargoTraffic_RemoveBeltComponent_Prefix(CargoTraffic __instance, int id)
|
||||
{
|
||||
var planet = GameMain.localPlanet;
|
||||
if (planet == null) return;
|
||||
RemoveSignalBelt(planet.factoryIndex, 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 planet = GameMain.localPlanet;
|
||||
if (planet == null) return;
|
||||
var factory = __instance.factory;
|
||||
var factoryIndex = planet.factoryIndex;
|
||||
var factoryIndex = factory.index;
|
||||
var beltId = factory.entityPool[entityId].beltId;
|
||||
if (signalId == 410)
|
||||
{
|
||||
@@ -197,39 +193,33 @@ public static class BeltSignal
|
||||
}
|
||||
}
|
||||
|
||||
[HarmonyTranspiler]
|
||||
[HarmonyPatch(typeof(GameData), nameof(GameData.GameTick))]
|
||||
public static IEnumerable<CodeInstruction> GameData_GameTick_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
|
||||
[HarmonyPostfix]
|
||||
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic.OnFactoryFrameBegin))]
|
||||
public static void GameLogic_OnFactoryFrameBegin_Postfix(GameLogic __instance)
|
||||
{
|
||||
var matcher = new CodeMatcher(instructions, generator);
|
||||
matcher.MatchForward(false,
|
||||
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(PerformanceMonitor), nameof(PerformanceMonitor.EndSample)))
|
||||
).Advance(1).Insert(
|
||||
Transpilers.EmitDelegate(() =>
|
||||
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)
|
||||
{
|
||||
var factories = GameMain.data?.factories;
|
||||
if (factories == null) return;
|
||||
foreach (var factory in factories)
|
||||
{
|
||||
if (factory == null) continue;
|
||||
var index = factory.index;
|
||||
var belts = GetSignalBelts(index);
|
||||
if (belts == null || belts.Count == 0) continue;
|
||||
var consumeRegister = GameMain.statistics.production.factoryStatPool[index].consumeRegister;
|
||||
var cargoTraffic = factory.cargoTraffic;
|
||||
foreach (var beltId in belts)
|
||||
{
|
||||
ref var belt = ref cargoTraffic.beltPool[beltId];
|
||||
var cargoPath = cargoTraffic.GetCargoPath(belt.segPathId);
|
||||
if (cargoPath == null) continue;
|
||||
int itemId;
|
||||
if ((itemId = cargoPath.TryPickItem(belt.segIndex + belt.segPivotOffset - 5, 12, out var stack, out _)) <= 0) continue;
|
||||
consumeRegister[itemId] += stack;
|
||||
Dustbin.CalcGetSands(itemId, stack, 0);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
return matcher.InstructionEnumeration();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
## Changelog
|
||||
|
||||
* 1.4.0
|
||||
* `Belt signal dustbin`: Restore item disposal and startup compatibility with the current game simulation loop after DSP 0.10.33/0.10.34 updates.
|
||||
* Refactorying Tank input logic codes, for better performance, and resolve a bug [#53](https://github.com/soarqin/DSP_Mods/issues/53)
|
||||
* Remove use of AssetBundle, move the belt signal icon into `Assembly Resources`, for better flexibility.
|
||||
|
||||
@@ -44,6 +45,7 @@
|
||||
## 更新日志
|
||||
|
||||
* 1.4.0
|
||||
* `传送带信号垃圾桶`:适配当前游戏的模拟循环,修复 DSP 0.10.33/0.10.34 更新后的启动报错和物品无法销毁问题。
|
||||
* 重构储液罐的输入逻辑代码,以提高性能并解决bug [#53](https://github.com/soarqin/DSP_Mods/issues/53)
|
||||
* 移除了AssetBundle的使用,将传送带信号图标移入`Assembly资源`,以获得更好的灵活性
|
||||
|
||||
|
||||
@@ -13,9 +13,28 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DysonSphereProgram.Modding.DSPModSave" Version="1.*" />
|
||||
<PackageReference Include="DysonSphereProgram.Modding.NebulaMultiplayerModApi" Version="2.0.0" />
|
||||
<ProjectReference Include="..\DustbinPreloader\DustbinPreloader.csproj" ReferenceOutputAssembly="false" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="assets/icon/signal-410.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Compile against the current game with the same fields injected at runtime. -->
|
||||
<Target Name="UsePreloaderGameReference" BeforeTargets="CoreCompile" DependsOnTargets="ResolveReferences">
|
||||
<MSBuild Projects="..\DustbinPreloader\DustbinPreloader.csproj" Targets="GetTargetPath" Properties="Configuration=$(Configuration)">
|
||||
<Output TaskParameter="TargetOutputs" ItemName="_DustbinPreloaderTarget" />
|
||||
</MSBuild>
|
||||
<PropertyGroup>
|
||||
<_DustbinGameReference>$(MSBuildProjectDirectory)\$(IntermediateOutputPath)game-reference\Assembly-CSharp.dll</_DustbinGameReference>
|
||||
<_OriginalGameReference>@(ReferencePath->WithMetadataValue('Filename', 'Assembly-CSharp'))</_OriginalGameReference>
|
||||
</PropertyGroup>
|
||||
<Exec Command="powershell.exe -NoProfile -ExecutionPolicy Bypass -File "$(MSBuildProjectDirectory)\GenerateGameReference.ps1" -InputAssembly "$(_OriginalGameReference)" -OutputAssembly "$(_DustbinGameReference)" -PreloaderAssembly "@(_DustbinPreloaderTarget)" -CecilAssembly "@(ReferencePath->WithMetadataValue('Filename', 'Mono.Cecil'))" -BepInExAssembly "@(ReferencePath->WithMetadataValue('Filename', 'BepInEx'))" -UnityAssembly "@(ReferencePath->WithMetadataValue('Filename', 'UnityEngine.CoreModule'))"" />
|
||||
<ItemGroup>
|
||||
<ReferencePath Remove="$(_OriginalGameReference)" />
|
||||
<ReferencePath Include="$(_DustbinGameReference)" Private="false" />
|
||||
<ReferencePathWithRefAssemblies Remove="$(_OriginalGameReference)" />
|
||||
<ReferencePathWithRefAssemblies Include="$(_DustbinGameReference)" Private="false" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
</Project>
|
||||
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$InputAssembly,
|
||||
[Parameter(Mandatory = $true)][string]$OutputAssembly,
|
||||
[Parameter(Mandatory = $true)][string]$PreloaderAssembly,
|
||||
[Parameter(Mandatory = $true)][string]$CecilAssembly,
|
||||
[Parameter(Mandatory = $true)][string]$BepInExAssembly,
|
||||
[Parameter(Mandatory = $true)][string]$UnityAssembly
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
[void][Reflection.Assembly]::LoadFrom($CecilAssembly)
|
||||
[void][Reflection.Assembly]::LoadFrom($BepInExAssembly)
|
||||
$preloader = [Reflection.Assembly]::LoadFrom($PreloaderAssembly)
|
||||
$resolver = [Mono.Cecil.DefaultAssemblyResolver]::new()
|
||||
foreach ($assemblyPath in @($InputAssembly, $UnityAssembly, $BepInExAssembly, $CecilAssembly)) {
|
||||
$resolver.AddSearchDirectory([IO.Path]::GetDirectoryName($assemblyPath))
|
||||
}
|
||||
$parameters = [Mono.Cecil.ReaderParameters]::new()
|
||||
$parameters.AssemblyResolver = $resolver
|
||||
$reference = [Mono.Cecil.AssemblyDefinition]::ReadAssembly($InputAssembly, $parameters)
|
||||
try {
|
||||
$preloader.GetType('DustbinPreloader.Preloader').GetMethod('Patch').Invoke($null, @($reference)) | Out-Null
|
||||
foreach ($typeName in @('StorageComponent', 'TankComponent')) {
|
||||
$fields = $reference.MainModule.GetType($typeName).Fields
|
||||
if (-not ($fields | Where-Object { $_.Name -eq 'IsDustbin' -and $_.FieldType.FullName -eq 'System.Boolean' })) {
|
||||
throw "Dustbin preloader did not inject $typeName.IsDustbin."
|
||||
}
|
||||
}
|
||||
[void][IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($OutputAssembly))
|
||||
$reference.Write($OutputAssembly)
|
||||
} finally {
|
||||
$reference.Dispose()
|
||||
$resolver.Dispose()
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
* A checkbox is added to Storages and Tanks UI, which turns them into dustbins.
|
||||
* Items sent into dustbins are removed immediately.
|
||||
* For a belt dustbin, open a belt's signal picker and select the dustbin icon on the first tab. Items passing that belt segment are destroyed, including stacked and proliferated items.
|
||||
* Can get soil piless from destroyed items, configurable through a json encoded config entry.
|
||||
* You can get item ID list from [dsp-wiki](https://dsp-wiki.com/Modding:Items_IDs).
|
||||
|
||||
@@ -23,6 +24,7 @@
|
||||
|
||||
* 在储物仓和储液罐上增加一个垃圾桶的勾选框。
|
||||
* 送进垃圾桶的物品会立即被移除。
|
||||
* 使用传送带垃圾桶时,打开传送带的信号选择面板,在第一个页签选择垃圾桶图标。经过该段传送带的物品会被销毁,包括堆叠和喷涂增产剂的物品。
|
||||
* 可以从移除的物品中获得沙子,可以通过json编码的设置项进行配置。
|
||||
* 可以在[这里](https://dsp-wiki.com/Modding:Items_IDs)获得物品ID列表。
|
||||
|
||||
|
||||
Reference in new issue
Block a user