Exemplo n.º 1
0
        // only allow one engine part per specific location
        public static bool Prefix(
            MechLabLocationWidget __instance,
            PointerEventData eventData,
            MechLabDropTargetType addToType,
            MechLabPanel ___mechLab,
            List <MechLabItemSlotElement> ___localInventory,
            int ___usedSlots,
            int ___maxSlots,
            ref Text ____dropErrorMessage)
        {
            try
            {
                if (___mechLab == null)
                {
                    return(false);
                }

                var dragItem = ___mechLab.DragItem as MechLabItemSlotElement;
                if (dragItem == null)
                {
                    return(false);
                }

                var result = EngineHeat.Shared.ValidateDrop(dragItem, __instance);

                if (result is MechLabDropRemoveDragItemResult)
                {
                    // remove item and delete it
                    dragItem.thisCanvasGroup.blocksRaycasts = true;
                    dragItem.MountedLocation = ChassisLocations.None;
                    ___mechLab.dataManager.PoolGameObject(MechLabPanel.MECHCOMPONENT_ITEM_PREFAB, dragItem.gameObject);
                    ___mechLab.ClearDragItem(true);
                    return(false);
                }

                if (result is MechLabDropErrorResult error)
                {
                    ____dropErrorMessage = new Text(error.errorMessage);
                    ___mechLab.ShowDropErrorMessage(____dropErrorMessage);
                    ___mechLab.OnDrop(eventData);
                    return(false);
                }
            }
            catch (Exception e)
            {
                Control.mod.Logger.LogError(e);
            }

            return(true);
        }
Exemplo n.º 2
0
        public static bool Prefix(MechLabLocationWidget __instance, PointerEventData eventData, MechLabDropTargetType addToType, MechLabPanel ___mechLab, Text ____dropErrorMessage)
        {
            var sim = UnityGameInstance.BattleTechGame.Simulation;

            if (sim == null)
            {
                return(true);
            }

            if (!ModState.IsComponentValidForRefit)
            {
                ___mechLab.ShowDropErrorMessage(____dropErrorMessage);
                ___mechLab.OnDrop(eventData);
                return(false);
            }

            return(true);
        }
            // 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);
                }));
            }
Exemplo n.º 4
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);
        }
Exemplo n.º 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);
        }