Exemplo n.º 1
0
        /// <summary>
        /// resolve conflict as an asynchronous operation.
        /// </summary>
        /// <param name="resolve">if set to <c>true</c> [resolve].</param>
        protected virtual async Task ResolveConflictAsync(bool resolve)
        {
            if (ResolvingConflict)
            {
                return;
            }
            ResolvingConflict = true;
            if (ModCompareSelector.VirtualDefinitions?.Count() > 0)
            {
                IHierarchicalDefinitions conflictParent = null;
                int?conflictParentIdx = null;
                int parentIdx         = HierarchalConflicts.ToList().IndexOf(SelectedParentConflict);
                foreach (var item in HierarchalConflicts)
                {
                    if (item.Children.Contains(SelectedConflict))
                    {
                        conflictParent = item;
                        var conflictParentChildren = new List <IHierarchicalDefinitions>(item.Children);
                        conflictParentIdx = conflictParentChildren.IndexOf(SelectedConflict);
                        break;
                    }
                }
                var id = idGenerator.GetNextId();
                await TriggerOverlayAsync(id, true, localizationManager.GetResource(LocalizationResources.Conflict_Solver.OverlayResolve));

                IDefinition patchDefinition = null;
                if (!IsBinaryConflict)
                {
                    patchDefinition = ModCompareSelector.VirtualDefinitions.FirstOrDefault(p => modPatchCollectionService.IsPatchMod(p.ModName));
                }
                else
                {
                    if (resolve)
                    {
                        patchDefinition = takeLeftBinary ? ModCompareSelector.LeftSelectedDefinition : ModCompareSelector.RightSelectedDefinition;
                    }
                    else
                    {
                        patchDefinition = ModCompareSelector.Definitions.FirstOrDefault();
                    }
                }
                if (patchDefinition != null)
                {
                    var generatedFileNames = patchDefinition.GeneratedFileNames;
                    foreach (var fileNames in ModCompareSelector.Definitions.Select(p => p.GeneratedFileNames))
                    {
                        foreach (var item in fileNames)
                        {
                            generatedFileNames.Add(item);
                        }
                    }
                    patchDefinition.GeneratedFileNames = generatedFileNames;
                    SyncCode(patchDefinition);
                    try
                    {
                        if (resolve ?
                            await modPatchCollectionService.ApplyModPatchAsync(Conflicts, patchDefinition, SelectedModCollection.Name) :
                            await modPatchCollectionService.IgnoreModPatchAsync(Conflicts, patchDefinition, SelectedModCollection.Name))
                        {
                            await FilterHierarchalConflictsAsync(Conflicts);

                            IHierarchicalDefinitions selectedConflict = null;
                            if (conflictParentIdx.HasValue && HierarchalConflicts.Any())
                            {
                                foreach (var item in HierarchalConflicts)
                                {
                                    if (item.Name.Equals(conflictParent.Name))
                                    {
                                        if (conflictParentIdx.Value > (item.Children.Count - 1))
                                        {
                                            conflictParentIdx = item.Children.Count - 1;
                                        }
                                        else if (conflictParentIdx.Value < 0)
                                        {
                                            conflictParentIdx = 0;
                                        }
                                        selectedConflict = item.Children.Select(p => p).ToList()[conflictParentIdx.GetValueOrDefault()];
                                        break;
                                    }
                                }
                            }
                            SelectedConflict = selectedConflict;
                            ResetConflicts.Refresh();
                        }
                    }
                    catch (Exception ex)
                    {
                        var title   = localizationManager.GetResource(LocalizationResources.SavingError.Title);
                        var message = localizationManager.GetResource(LocalizationResources.SavingError.Message);
                        logger.Error(ex);
                        notificationAction.ShowNotification(title, message, NotificationType.Error, 30);
                    }
                }
                if (SelectedConflict == null)
                {
                    if (parentIdx > (HierarchalConflicts.Count() - 1))
                    {
                        parentIdx = HierarchalConflicts.Count() - 1;
                    }
                    else if (parentIdx < 0)
                    {
                        parentIdx = 0;
                    }
                    if (HierarchalConflicts.Any())
                    {
                        SelectedParentConflict = HierarchalConflicts.ElementAt(parentIdx);
                    }
                }
                await TriggerOverlayAsync(id, false);
            }
            ResolvingConflict = false;
        }