internal static void ForceItemDropStrip(this MechLabPanel mechlab, MechLabItemSlotElement item) { Control.LogDebug(DType.DefaultHandle, $"Dropping {item.ComponentRef.Def.Description.Id} "); if (strip_state) { foreach (var validator in item.ComponentRef.Def.GetComponents <IOnItemGrabbed>()) { validator.OnItemGrabbed(item, mechlab, strip_widget); } mechlab.ForceItemDrop(item); } }
internal static void AdvancedStripping(this MechLabLocationWidget widget, MechLabPanel panel) { if (!panel.Initialized) { return; } var loclInv = Traverse.Create(widget).Field("localInventory").GetValue <List <MechLabItemSlotElement> >(); for (int i = loclInv.Count - 1; i >= 0; i--) { MechLabItemSlotElement mechLabItemSlotElement = loclInv[i]; if (!mechLabItemSlotElement.ComponentRef.IsFixed && (mechLabItemSlotElement.ComponentRef.Def is WeaponDef || mechLabItemSlotElement.ComponentRef.Def is AmmunitionBoxDef)) { widget.OnRemoveItem(mechLabItemSlotElement, true); panel.ForceItemDrop(mechLabItemSlotElement); } } }
internal static void ForceItemDropRepair(this MechLabPanel mechlab, MechLabItemSlotElement item) { try { Control.LogDebug(DType.DefaultHandle, $"Repair-Remove for {item.ComponentRef.ComponentDefID}"); if (repair_state) { foreach (var validator in item.ComponentRef.Def.GetComponents <IOnItemGrabbed>()) { Control.LogDebug(DType.DefaultHandle, $" - {validator.GetType()}"); validator.OnItemGrabbed(item, mechlab, repair_widget); } mechlab.ForceItemDrop(item); } } catch (Exception e) { Control.LogError($"ERROR", e); } }
public void OnItemGrabbed(IMechLabDraggableItem item, MechLabPanel mechLab, MechLabLocationWidget widget) { if (widget.loadout.Location != ChassisLocations.LeftArm && widget.loadout.Location != ChassisLocations.RightArm) { return; } var loc_helper = new LocationHelper(widget); var total_slot = ArmActuatorSlot.None; var mount_loc = widget.loadout.Location; var ml_helper = new MechLabHelper(mechLab); CustomComponents.Control.LogDebug(DType.ComponentInstall, $"- ArmActuator: {Def.Description.Id} {Type}"); void add_default(ArmActuatorSlot slot) { bool add_item(string id) { if (string.IsNullOrEmpty(id)) { return(false); } var r = DefaultHelper.CreateRef(id, ComponentType.Upgrade, mechLab.dataManager, mechLab.Sim); if (r.Is <ArmActuator>(out var actuator) && (actuator.Type & total_slot) == 0) { DefaultHelper.AddMechLab(id, ComponentType.Upgrade, ml_helper, mount_loc); total_slot = total_slot | actuator.Type; return(true); } return(false); } CustomComponents.Control.LogDebug(DType.ComponentInstall, $"--- adding {slot} to {total_slot}"); if (total_slot.HasFlag(slot)) { CustomComponents.Control.LogDebug(DType.ComponentInstall, $"---- already present"); return; } if (add_item(ArmActuatorCC.GetDefaultActuator(mechLab.activeMechDef, mount_loc, slot))) { return; } add_item(ArmActuatorCC.GetDefaultActuator(null, mount_loc, slot)); } for (int i = loc_helper.LocalInventory.Count - 1; i >= 0; i--) { var slotitem = loc_helper.LocalInventory[i]; if (!slotitem.ComponentRef.Is <ArmActuator>()) { continue; } var actuator = slotitem.ComponentRef.GetComponent <ArmActuator>(); if (slotitem.ComponentRef.IsDefault() && !slotitem.ComponentRef.IsModuleFixed(ml_helper.MechLab.activeMechDef)) { CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- removing {slotitem.ComponentRef.ComponentDefID} {actuator.Type}"); DefaultHelper.RemoveMechLab(slotitem.ComponentRef.ComponentDefID, slotitem.ComponentRef.ComponentDefType, ml_helper, mount_loc); } else { CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- checking {slotitem.ComponentRef.ComponentDefID} {actuator.Type}"); total_slot = total_slot | actuator.Type; } } CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- actuators {total_slot}"); add_default(ArmActuatorSlot.PartShoulder); add_default(ArmActuatorSlot.PartUpper); if (ArmActuatorCC.IsIgnoreFullActuators(mechLab.activeMechDef)) { if (total_slot.HasFlag(ArmActuatorSlot.PartHand) && !total_slot.HasFlag(ArmActuatorSlot.PartLower)) { CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- Removing hand from {total_slot}"); var hand = loc_helper.LocalInventory.FirstOrDefault(i => i.ComponentRef.Is <ArmActuator>(out var actuator) && actuator.Type.HasFlag(ArmActuatorSlot.PartHand)); if (hand == null || hand.ComponentRef.IsFixed) { return; } var dragitem = mechLab.DragItem; widget.OnRemoveItem(hand, false); mechLab.ForceItemDrop(hand); ml_helper.SetDragItem(dragitem as MechLabItemSlotElement); } else { CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- hand already removed from {total_slot}"); } } else { var limit = mechLab.activeMechDef.Chassis.Is <ArmActuatorSupport>(out var s) ? s.GetLimit(mount_loc) : ArmActuatorSlot.Hand; if (limit.HasFlag(ArmActuatorSlot.PartLower)) { add_default(ArmActuatorSlot.PartLower); } if (limit.HasFlag(ArmActuatorSlot.PartHand)) { add_default(ArmActuatorSlot.PartHand); } } }
public static bool Prefix(MechLabLocationWidget __instance, MechLabPanel ___mechLab, PointerEventData eventData) { try { var dragItem = ___mechLab.DragItem as MechLabItemSlotElement; var location = __instance.loadout.Location; if (!___mechLab.Initialized) { return(false); } var newComponentDef = dragItem?.ComponentRef?.Def; if (newComponentDef == null) { return(false); } Control.LogDebug(DType.ComponentInstall, $"OnMechLabDrop: Adding {newComponentDef.Description.Id}"); bool do_cancel(string error) { if (string.IsNullOrEmpty(error)) { return(false); } Control.LogDebug(DType.ComponentInstall, $"- Canceled: {error}"); ___mechLab.ForceItemDrop(dragItem); ___mechLab.OnDrop(eventData); ___mechLab.ShowDropErrorMessage(new Text(error)); return(true); } Control.LogDebug(DType.ComponentInstall, $"- pre validation"); foreach (var pre_validator in Validator.GetPre(newComponentDef)) { if (do_cancel(pre_validator(dragItem, location))) { return(false); } } Control.LogDebug(DType.ComponentInstall, $"- replace validation"); var changes = new Queue <IChange>(); changes.Enqueue(new Change_Add(dragItem, __instance.loadout.Location)); foreach (ReplaceValidateDropDelegate rep_validator in Validator.GetReplace(newComponentDef)) { if (do_cancel(rep_validator(dragItem, location, changes))) { return(false); } } #if CCDEBUG if (Control.Settings.DebugInfo.HasFlag(DType.ComponentInstall)) { if (changes.Count == 1) { Control.LogDebug(DType.ComponentInstall, $"-- no replace"); } else { foreach (var replace in changes) { if (replace is Change_Add add) { Control.LogDebug(DType.ComponentInstall, $"-- add {add.ItemID} to {add.Location}"); } else if (replace is Change_Remove remove) { Control.LogDebug(DType.ComponentInstall, $"-- remove {remove.ItemID} from {remove.Location}"); } } } } #endif Control.LogDebug(DType.ComponentInstall, $"- adjusting"); var state = new InventoryOperationState(changes, ___mechLab.activeMechDef); state.DoChanges(); var inv = state.Inventory; Control.LogDebug(DType.ComponentInstall, $"- post validation"); foreach (var pst_validator in Validator.GetPost(newComponentDef)) { int n = changes.Count; if (do_cancel(pst_validator(dragItem, state.Inventory))) { return(false); } } state.ApplyMechlab(); ___mechLab.ClearDragItem(true); __instance.RefreshHardpointData(); ___mechLab.ValidateLoadout(false); return(false); } catch (Exception e) { Control.LogError(e); } return(true); }
public static bool Prefix(MechLabLocationWidget __instance, ref string ___dropErrorMessage, List <MechLabItemSlotElement> ___localInventory, int ___usedSlots, int ___maxSlots, TextMeshProUGUI ___locationName, MechLabPanel ___mechLab) { Control.mod.Logger.Log("Drop on " + ___locationName.text); if (!___mechLab.Initialized) { Control.mod.Logger.Log("not Initialized"); return(false); } if (___mechLab.DragItem == null) { Control.mod.Logger.Log("Dragged item: NULL, exit"); return(false); } var drag_item = ___mechLab.DragItem; if (drag_item.ComponentRef == null) { Control.mod.Logger.Log("Dropped item: NULL, exit"); return(false); } Control.mod.Logger.Log("Dropped item: " + drag_item.ComponentRef.ComponentDefID); UniqueItem new_item_info; if (!drag_item.ComponentRef.Def.IsUnique(out new_item_info)) { Control.mod.Logger.Log("Item not Unique, exit"); return(true); } bool flag = __instance.ValidateAdd(drag_item.ComponentRef); Control.mod.Logger.Log(string.Format("Validation: {0} - {1}", flag, ___dropErrorMessage)); if (!flag && !___dropErrorMessage.EndsWith("Not enough free slots.")) { return(true); } var n = ___localInventory.FindUniqueItem(new_item_info); Control.mod.Logger.Log("index = " + n.ToString()); //if no - continue normal flow(add new or show "not enough slots" message if (n < 0) { return(true); } if (___usedSlots - ___localInventory[n].ComponentRef.Def.InventorySize + drag_item.ComponentRef.Def.InventorySize > ___maxSlots) { return(true); } var old_item = ___localInventory[n]; __instance.OnRemoveItem(old_item, true); ___mechLab.ForceItemDrop(old_item); var clear = __instance.OnAddItem(drag_item, true); if (__instance.Sim != null) { WorkOrderEntry_InstallComponent subEntry = __instance.Sim.CreateComponentInstallWorkOrder( ___mechLab.baseWorkOrder.MechID, drag_item.ComponentRef, __instance.loadout.Location, drag_item.MountedLocation); ___mechLab.baseWorkOrder.AddSubEntry(subEntry); } drag_item.MountedLocation = __instance.loadout.Location; ___mechLab.ClearDragItem(clear); __instance.RefreshHardpointData(); ___mechLab.ValidateLoadout(false); return(false); }