internal static bool OnRemoveItemStrip(this MechLabLocationWidget widget, IMechLabDraggableItem item, bool validate) { var component = item.ComponentRef.Def; strip_widget = widget; Control.LogDebug(DType.DefaultHandle, $"Removing {component.Description.Id} "); var mechlab = widget.parentDropTarget as MechLabPanel; strip_state = true; foreach (var validator in component.GetComponents <IOnItemGrab>()) { Control.LogDebug(DType.DefaultHandle, $"- {validator.GetType()}"); strip_state = validator.OnItemGrab(item, mechlab, out _); if (!strip_state) { Control.LogDebug(DType.DefaultHandle, $"-- Canceled"); return(true); } } return(widget.OnRemoveItem(item, validate)); }
internal static bool OnRemoveItemRepair(this MechLabLocationWidget widget, IMechLabDraggableItem item, bool validate) { var component = item.ComponentRef.Def; repair_state = true; repair_widget = widget; if (component.Is <Flags>(out var f) && f.AutoRepair) { Control.LogDebug(DType.DefaultHandle, $"AutoRepair: {component.Description.Id} "); item.ComponentRef.DamageLevel = ComponentDamageLevel.Penalized; var t = Traverse.Create(item).Method("RefreshDamageOverlays").GetValue(); item.RepairComponent(true); repair_state = false; return(true); } repair_state = true; var mechlab = widget.parentDropTarget as MechLabPanel; foreach (var validator in component.GetComponents <IOnItemGrab>()) { repair_state = validator.OnItemGrab(item, mechlab, out _); if (!repair_state) { return(true); } } return(widget.OnRemoveItem(item, validate)); }
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); } } }
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, 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); }