Ejemplo n.º 1
0
            public Task EnableMod(GenericMod mod)
            {
                return(_busy.Task(async() => {
                    if (_enabler == null || mod.IsEnabled)
                    {
                        return;
                    }

                    var conflicts = await _enabler.CheckConflictsAsync(mod);
                    if (conflicts.Length > 0 && ModernDialog.ShowMessage(
                            conflicts.Select(x => $@"• “{Path.GetFileName(x.RelativeName)}” has already been altered by the “{x.ModName}” mod;")
                            .JoinToString("\n").ToSentence
                                () + $"\n\nEnabling {BbCodeBlock.Encode(mod.DisplayName)} may have adverse effects. Are you sure you want to enable this mod?",
                            "Conflict", MessageBoxButton.YesNo, "genericMods.conflict") != MessageBoxResult.Yes)
                    {
                        return;
                    }

                    try {
                        using (var waiting = WaitingDialog.Create("Enabling mod…")) {
                            await _enabler.EnableAsync(mod, waiting, waiting.CancellationToken);
                            Changed?.Invoke(this, EventArgs.Empty);

                            if (waiting.CancellationToken.IsCancellationRequested)
                            {
                                waiting.Report(AsyncProgressEntry.FromStringIndetermitate("Cancellation…"));
                                await _enabler.DisableAsync(mod);
                            }
                        }
                    } catch (Exception e) {
                        NonfatalError.Notify("Can’t enable mod", e);
                    }
                }));
            }
            void IUserPresetable.ImportFromPresetData(string data)
            {
                if (_enabler == null || Enabled == null || Disabled == null)
                {
                    return;
                }

                _busy.Task(async() => {
                    await Task.Delay(10);

                    var names   = data.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                    var enabled = Enabled.OfType <GenericMod>().OrderByDescending(x => x.AppliedOrder).ToList();

                    try {
                        using (var waiting = WaitingDialog.Create("Loading mod profile…")) {
                            for (var i = 0; i < enabled.Count; i++)
                            {
                                var mod = enabled[i];
                                waiting.Report(mod.DisplayName, i, enabled.Count);
                                if (waiting.CancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                await _enabler.DisableAsync(mod, null, waiting.CancellationToken);
                                if (waiting.CancellationToken.IsCancellationRequested)
                                {
                                    waiting.Report(AsyncProgressEntry.FromStringIndetermitate("Cancellation…"));
                                    await _enabler.EnableAsync(mod);
                                }
                            }

                            for (var i = 0; i < names.Length; i++)
                            {
                                var mod = _enabler.GetByName(names[i]);
                                if (mod == null)
                                {
                                    continue;
                                }

                                waiting.Report(mod.DisplayName, i, enabled.Count);
                                if (waiting.CancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                await _enabler.EnableAsync(mod, null, waiting.CancellationToken);
                                if (waiting.CancellationToken.IsCancellationRequested)
                                {
                                    waiting.Report(AsyncProgressEntry.FromStringIndetermitate("Cancellation…"));
                                    await _enabler.DisableAsync(mod);
                                }
                            }
                        }
                    } catch (Exception e) {
                        NonfatalError.Notify("Can’t load mod profile", e);
                    }
                }).Forget();
            }