// Copied from OnMechLabDrop() but with inplace replacing of parts removed
            public static bool Prefix(
                MechLabLocationWidget __instance,
                MechLabPanel ___mechLab,
                Localize.Text ____dropErrorMessage,
                PointerEventData eventData,
                MechLabDropTargetType addToType)
            {
                return(_harmonyManager.PrefixLogExceptions(() =>
                {
                    if (!___mechLab.Initialized)
                    {
                        return;
                    }

                    if (___mechLab.DragItem == null)
                    {
                        return;
                    }

                    IMechLabDraggableItem dragItem = ___mechLab.DragItem;
                    bool flag = __instance.ValidateAdd(dragItem.ComponentRef);
                    if (!flag)
                    {
                        ___mechLab.ShowDropErrorMessage(____dropErrorMessage);
                        ___mechLab.OnDrop(eventData);
                        return;
                    }

                    bool clearOriginalItem = __instance.OnAddItem(dragItem, true);
                    if (__instance.Sim != null)
                    {
                        WorkOrderEntry_InstallComponent subEntry =
                            __instance.Sim.CreateComponentInstallWorkOrder(___mechLab.baseWorkOrder.MechID,
                                                                           dragItem.ComponentRef, __instance.loadout.Location, dragItem.MountedLocation);
                        ___mechLab.baseWorkOrder.AddSubEntry(subEntry);
                    }

                    dragItem.MountedLocation = __instance.loadout.Location;
                    ___mechLab.ClearDragItem(clearOriginalItem);
                    __instance.RefreshHardpointData();
                    ___mechLab.ValidateLoadout(false);
                }));
            }
        // @ToDo: Somehow implement that manual Remove/Add of components AFTER this method-call calculates work-orders correctly
        public static void SetEquipment(this MechLabPanel mechLabPanel, List <InventoryItemElement_Simple> equipment, MechLabLocationWidget locationWidget, LocationLoadoutDef loadout)
        {
            locationWidget.loadout = loadout;

            // Nah. This kills fixed equipment!
            //locationWidget.ClearInventory();

            for (int i = 0; i < equipment.Count; i++)
            {
                if (equipment[i].ComponentRef.MountedLocation == loadout.Location)
                {
                    Logger.Debug("[Extensions.SetEquipment] Component: " + equipment[i].ComponentRef.ComponentDefID + " gets installed at: " + loadout.Location.ToString() + " and was fetched from (simulated): " + equipment[i].Origin);

                    // NOTE that creation of items (vs. simulate-grabbing the real ones from inventory) will confuse the work-order-entries
                    // @ToDo: Try setting "copyComponentRef" to true and test somewhen
                    MechLabItemSlotElement mechLabItemSlotElement = mechLabPanel.CreateMechComponentItem(equipment[i].ComponentRef, false, loadout.Location, locationWidget, null);
                    Traverse.Create(mechLabItemSlotElement).Field("originalDropParentType").SetValue(equipment[i].Origin);

                    if (mechLabItemSlotElement != null)
                    {
                        List <MechLabItemSlotElement> ___localInventory = (List <MechLabItemSlotElement>)AccessTools.Field(typeof(MechLabLocationWidget), "localInventory").GetValue(locationWidget);
                        ___localInventory.Add(mechLabItemSlotElement);
                        int ___usedSlots = (int)AccessTools.Field(typeof(MechLabLocationWidget), "usedSlots").GetValue(locationWidget);
                        ___usedSlots += equipment[i].ComponentRef.Def.InventorySize;
                        Transform ___inventoryParent = (Transform)AccessTools.Field(typeof(MechLabLocationWidget), "inventoryParent").GetValue(locationWidget);
                        mechLabItemSlotElement.gameObject.transform.SetParent(___inventoryParent, false);
                        mechLabItemSlotElement.gameObject.transform.localScale = Vector3.one;

                        ReflectionHelper.InvokePrivateMethode(locationWidget, "RefreshMechComponentData", new object[] { mechLabItemSlotElement, false });
                        locationWidget.RefreshHardpointData();

                        // Add WorkOrderEntry
                        WorkOrderEntry_InstallComponent subEntry = locationWidget.Sim.CreateComponentInstallWorkOrder(mechLabPanel.baseWorkOrder.MechID, equipment[i].ComponentRef, loadout.Location, ChassisLocations.None);
                        mechLabPanel.baseWorkOrder.AddSubEntry(subEntry);
                    }
                }
            }
            //mechLabPanel.ValidateLoadout(false);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        public static bool Prefix(MechLabLocationWidget __instance,
                                  MechLabPanel ___mechLab,
                                  PointerEventData eventData)
        {
            try
            {
                var dragItem = ___mechLab.DragItem as MechLabItemSlotElement;


                if (!___mechLab.Initialized)
                {
                    return(false);
                }

                var newComponentDef = dragItem?.ComponentRef?.Def;
                if (newComponentDef == null)
                {
                    return(false);
                }

                Control.LogDebug(DType.ComponentInstall, $"OnMechLabDrop: Adding {newComponentDef.Description.Id}");

                var location_helper = new LocationHelper(__instance);
                var mechlab_helper  = new MechLabHelper(___mechLab);

                bool do_cancel(string error, List <IChange> allchanges)
                {
                    if (string.IsNullOrEmpty(error))
                    {
                        return(false);
                    }

                    Control.LogDebug(DType.ComponentInstall, $"- Canceled: {error}");

                    if (allchanges != null)
                    {
                        foreach (var change in allchanges)
                        {
                            change.CancelChange(mechlab_helper, location_helper);
                        }
                    }

                    ___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_helper, mechlab_helper), null))
                    {
                        return(false);
                    }
                }

                Control.LogDebug(DType.ComponentInstall, $"- replace validation");

                List <IChange> changes = new List <IChange>();
                changes.Add(new AddFromInventoryChange(__instance.loadout.Location, dragItem));

                foreach (var rep_validator in Validator.GetReplace(newComponentDef))
                {
                    if (do_cancel(rep_validator(dragItem, location_helper, changes), 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 AddChange add)
                            {
                                Control.LogDebug(DType.ComponentInstall,
                                                 $"-- add {add.item.ComponentRef.ComponentDefID}");
                            }

                            else if (replace is RemoveChange remove)
                            {
                                Control.LogDebug(DType.ComponentInstall,
                                                 $"-- remove {remove.item.ComponentRef.ComponentDefID}");
                            }
                        }
                    }
                }
#endif


                Control.LogDebug(DType.ComponentInstall, $"- adjusting {changes.Count} changes");

                int num = changes.Count;
                for (int i = 0; i < num; i++)
                {
                    if (changes[i] is AddChange add)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"-- add: {add.item.ComponentRef.ComponentDefID}");

                        foreach (var adj_validator in add.item.ComponentRef.GetComponents <IAdjustValidateDrop>())
                        {
                            changes.AddRange(adj_validator.ValidateDropOnAdd(add.item, location_helper, mechlab_helper,
                                                                             changes));
                        }
                    }
                    else if (changes[i] is RemoveChange remove)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"-- add: {remove.item.ComponentRef.ComponentDefID}");

                        foreach (var adj_validator in remove.item.ComponentRef.GetComponents <IAdjustValidateDrop>())
                        {
                            changes.AddRange(adj_validator.ValidateDropOnRemove(remove.item, location_helper,
                                                                                mechlab_helper, changes));
                        }
                    }
                }

                List <InvItem> new_inventory = ___mechLab.activeMechInventory
                                               .Select(i => new InvItem {
                    item = i, location = i.MountedLocation
                }).ToList();

                new_inventory.AddRange(changes.OfType <AddChange>()
                                       .Select(i => new InvItem {
                    item = i.item.ComponentRef, location = i.location
                }));

                foreach (var remove in changes.OfType <RemoveChange>())
                {
                    new_inventory.RemoveAll(i => i.item == remove.item.ComponentRef);
                }
                Control.LogDebug(DType.ComponentInstall, $"- post validation");

                foreach (var pst_validator in Validator.GetPost(newComponentDef))
                {
                    if (do_cancel(pst_validator(dragItem, ___mechLab.activeMechDef, new_inventory, changes), changes))
                    {
                        return(false);
                    }
                }

                Control.LogDebug(DType.ComponentInstall, $"- apply changes");

                foreach (var change in changes)
                {
                    change.DoChange(mechlab_helper, location_helper);
                }

                ___mechLab.ClearDragItem(true);
                __instance.RefreshHardpointData();
                ___mechLab.ValidateLoadout(false);

                return(false);
            }
            catch (Exception e)
            {
                Control.LogError(e);
            }

            return(true);
        }