diff --git a/Dustbin/BeltSignal.cs b/Dustbin/BeltSignal.cs index 1927a6e..a78e1fe 100644 --- a/Dustbin/BeltSignal.cs +++ b/Dustbin/BeltSignal.cs @@ -225,11 +225,11 @@ public static class BeltSignal 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); + Dustbin.CalcGetSands(itemId, stack, 0); } } }) ); return matcher.InstructionEnumeration(); } -} \ No newline at end of file +} diff --git a/Dustbin/Dustbin.cs b/Dustbin/Dustbin.cs index eb8d15e..81f63ea 100644 --- a/Dustbin/Dustbin.cs +++ b/Dustbin/Dustbin.cs @@ -58,13 +58,31 @@ public class Dustbin : BaseUnityPlugin, IModCanSave, IMultiplayerMod } [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; - 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 addCount = count * sandsPerItem; player.sandCount += addCount; + Logger.LogInfo($"Sands calculation - ItemID: {itemId}, SandsFactor: {sandsPerItem}, Total sands: {addCount}, Inc: {inc} - Returning {count} to indicate item destroyed"); return count; } diff --git a/Dustbin/StoragePatch.cs b/Dustbin/StoragePatch.cs index fd86cc0..8277f27 100644 --- a/Dustbin/StoragePatch.cs +++ b/Dustbin/StoragePatch.cs @@ -148,25 +148,93 @@ public static class StoragePatch _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] [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])] private static IEnumerable StorageComponent_AddItem_HarmonyTranspiler(IEnumerable instructions, ILGenerator generator) { - var matcher = new CodeMatcher(instructions, generator); - var label1 = generator.DefineLabel(); - matcher.Start().MatchForward(false, - new CodeMatch(OpCodes.Stind_I4) - ).Advance(1).InsertAndAdvance( + var codes = new List(instructions); + var matcher = new CodeMatcher(codes, generator); + + // 找到方法开始处 + 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.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_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.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(); }