mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-03-22 10:13:26 +08:00
修复喷涂增产剂的物品会卡住储物仓垃圾桶的问题
Fix issue where items with productivity spray get stuck in storage dustbin
This commit is contained in:
@@ -225,11 +225,11 @@ public static class BeltSignal
|
|||||||
int itemId;
|
int itemId;
|
||||||
if ((itemId = cargoPath.TryPickItem(belt.segIndex + belt.segPivotOffset - 5, 12, out var stack, out _)) <= 0) continue;
|
if ((itemId = cargoPath.TryPickItem(belt.segIndex + belt.segPivotOffset - 5, 12, out var stack, out _)) <= 0) continue;
|
||||||
consumeRegister[itemId] += stack;
|
consumeRegister[itemId] += stack;
|
||||||
Dustbin.CalcGetSands(itemId, stack);
|
Dustbin.CalcGetSands(itemId, stack, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return matcher.InstructionEnumeration();
|
return matcher.InstructionEnumeration();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,13 +58,31 @@ public class Dustbin : BaseUnityPlugin, IModCanSave, IMultiplayerMod
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int CalcGetSands(int itemId, int count)
|
public static int CalcGetSands(int itemId, int count, int inc)
|
||||||
{
|
{
|
||||||
var sandsPerItem = itemId <= 12000 ? Dustbin.SandsFactors[itemId] : 0;
|
var sandsPerItem = itemId <= 12000 ? Dustbin.SandsFactors[itemId] : 0;
|
||||||
if (sandsPerItem <= 0) return count;
|
|
||||||
|
// Log item information for debugging
|
||||||
|
var itemProto = LDB.items.Select(itemId);
|
||||||
|
if (itemProto != null)
|
||||||
|
{
|
||||||
|
Logger.LogInfo($"Item destroyed - ID: {itemId}, Name: {itemProto.name}, Type: {itemProto.Type}, StackSize: {itemProto.StackSize}, Grade: {itemProto.Grade}, Count: {count}, Inc: {inc}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger.LogInfo($"Item destroyed - ID: {itemId}, Name: Unknown, Count: {count}, Inc: {inc}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sandsPerItem <= 0)
|
||||||
|
{
|
||||||
|
Logger.LogInfo($"No sands generated for item {itemId} (SandsFactor: {sandsPerItem}, Inc: {inc}) - Returning {count} to indicate item destroyed");
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
var player = GameMain.mainPlayer;
|
var player = GameMain.mainPlayer;
|
||||||
var addCount = count * sandsPerItem;
|
var addCount = count * sandsPerItem;
|
||||||
player.sandCount += addCount;
|
player.sandCount += addCount;
|
||||||
|
Logger.LogInfo($"Sands calculation - ItemID: {itemId}, SandsFactor: {sandsPerItem}, Total sands: {addCount}, Inc: {inc} - Returning {count} to indicate item destroyed");
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -148,25 +148,93 @@ public static class StoragePatch
|
|||||||
_lastStorageId = 0; // Refresh UI to reposition button on client side
|
_lastStorageId = 0; // Refresh UI to reposition button on client side
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void LogTranspilerInfo(string stage, int itemId, int count, int inc, bool isDustbin, bool enteringDustbinLogic)
|
||||||
|
{
|
||||||
|
Dustbin.Logger.LogInfo($"Transpiler {stage} - ItemID: {itemId}, Count: {count}, Inc: {inc}, IsDustbin: {isDustbin}, EnteringDustbinLogic: {enteringDustbinLogic}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void LogReturnValue(int returnValue, string context)
|
||||||
|
{
|
||||||
|
Dustbin.Logger.LogInfo($"Return Value - Context: {context}, Value: {returnValue}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void LogInserterState(string operation, int itemId, int count, int inc, int storageId)
|
||||||
|
{
|
||||||
|
Dustbin.Logger.LogInfo($"分拣器状态 - 操作: {operation}, 物品ID: {itemId}, 数量: {count}, 增产剂: {inc}, 储物仓ID: {storageId}");
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyTranspiler]
|
[HarmonyTranspiler]
|
||||||
[HarmonyPatch(typeof(StorageComponent), nameof(StorageComponent.AddItem), [typeof(int), typeof(int), typeof(int), typeof(int), typeof(bool)],
|
[HarmonyPatch(typeof(StorageComponent), nameof(StorageComponent.AddItem), [typeof(int), typeof(int), typeof(int), typeof(int), typeof(bool)],
|
||||||
[ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Out, ArgumentType.Normal])]
|
[ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Out, ArgumentType.Normal])]
|
||||||
private static IEnumerable<CodeInstruction> StorageComponent_AddItem_HarmonyTranspiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
|
private static IEnumerable<CodeInstruction> StorageComponent_AddItem_HarmonyTranspiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
|
||||||
{
|
{
|
||||||
var matcher = new CodeMatcher(instructions, generator);
|
var codes = new List<CodeInstruction>(instructions);
|
||||||
var label1 = generator.DefineLabel();
|
var matcher = new CodeMatcher(codes, generator);
|
||||||
matcher.Start().MatchForward(false,
|
|
||||||
new CodeMatch(OpCodes.Stind_I4)
|
// 找到方法开始处
|
||||||
).Advance(1).InsertAndAdvance(
|
matcher.Start();
|
||||||
|
|
||||||
|
// 定义标签用于跳转
|
||||||
|
var skipDustbinLogic = generator.DefineLabel();
|
||||||
|
|
||||||
|
// 在方法开始处插入安全检查
|
||||||
|
matcher.InsertAndAdvance(
|
||||||
|
// 安全检查:确保StorageComponent实例不为null
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_0),
|
||||||
|
new CodeInstruction(OpCodes.Brfalse, skipDustbinLogic),
|
||||||
|
|
||||||
|
// 检查是否是垃圾桶
|
||||||
new CodeInstruction(OpCodes.Ldarg_0),
|
new CodeInstruction(OpCodes.Ldarg_0),
|
||||||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StorageComponent), nameof(StorageComponent.IsDustbin))),
|
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StorageComponent), nameof(StorageComponent.IsDustbin))),
|
||||||
new CodeInstruction(OpCodes.Brfalse, label1),
|
new CodeInstruction(OpCodes.Brfalse, skipDustbinLogic),
|
||||||
|
|
||||||
|
// 检查count是否大于0
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_2),
|
||||||
|
new CodeInstruction(OpCodes.Ldc_I4_0),
|
||||||
|
new CodeInstruction(OpCodes.Ble, skipDustbinLogic),
|
||||||
|
|
||||||
|
// 记录进入垃圾桶处理逻辑
|
||||||
|
new CodeInstruction(OpCodes.Ldstr, "Processing Dustbin"),
|
||||||
new CodeInstruction(OpCodes.Ldarg_1),
|
new CodeInstruction(OpCodes.Ldarg_1),
|
||||||
new CodeInstruction(OpCodes.Ldarg_2),
|
new CodeInstruction(OpCodes.Ldarg_2),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_3),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_0),
|
||||||
|
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StorageComponent), nameof(StorageComponent.IsDustbin))),
|
||||||
|
new CodeInstruction(OpCodes.Ldc_I4_1),
|
||||||
|
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(StoragePatch), nameof(LogTranspilerInfo))),
|
||||||
|
|
||||||
|
// 记录分拣器状态
|
||||||
|
new CodeInstruction(OpCodes.Ldstr, "Before CalcGetSands"),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_1),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_2),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_3),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_0),
|
||||||
|
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StorageComponent), nameof(StorageComponent.id))),
|
||||||
|
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(StoragePatch), nameof(LogInserterState))),
|
||||||
|
|
||||||
|
// 调用CalcGetSands处理物品销毁和沙子生成
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_1),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_2),
|
||||||
|
new CodeInstruction(OpCodes.Ldarg_3),
|
||||||
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Dustbin), nameof(Dustbin.CalcGetSands))),
|
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Dustbin), nameof(Dustbin.CalcGetSands))),
|
||||||
new CodeInstruction(OpCodes.Ret)
|
|
||||||
|
// 记录返回值
|
||||||
|
new CodeInstruction(OpCodes.Dup),
|
||||||
|
new CodeInstruction(OpCodes.Ldstr, "CalcGetSands Return"),
|
||||||
|
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(StoragePatch), nameof(LogReturnValue))),
|
||||||
|
|
||||||
|
// 设置remainInc为0(第5个参数,索引4)
|
||||||
|
new CodeInstruction(OpCodes.Ldarg, 4),
|
||||||
|
new CodeInstruction(OpCodes.Ldc_I4_0),
|
||||||
|
new CodeInstruction(OpCodes.Stind_I4),
|
||||||
|
|
||||||
|
// 返回count表示成功销毁
|
||||||
|
new CodeInstruction(OpCodes.Ret),
|
||||||
|
|
||||||
|
// 跳过垃圾桶逻辑的标签
|
||||||
|
new CodeInstruction(OpCodes.Nop).WithLabels(skipDustbinLogic)
|
||||||
);
|
);
|
||||||
matcher.Labels.Add(label1);
|
|
||||||
return matcher.InstructionEnumeration();
|
return matcher.InstructionEnumeration();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user