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);
                    }
                }));
            }
Beispiel #2
0
 public static Task TaskDelayAfterwards([NotNull] this Busy busy, Func <Task> a, TimeSpan delay)
 {
     return(busy.Task(async() => {
         await a();
         await Task.Delay(delay);
     }));
 }
Beispiel #3
0
 public static Task TaskDelayAfterwards([NotNull] this Busy busy, Func <Task> a, int millisecondsDelay)
 {
     return(busy.Task(async() => {
         await a();
         await Task.Delay(millisecondsDelay);
     }));
 }
Beispiel #4
0
 public static Task DoDelayAfterwards([NotNull] this Busy busy, Action a, TimeSpan delay)
 {
     return(busy.Task(async() => {
         a();
         await Task.Delay(delay);
     }));
 }
Beispiel #5
0
 public static Task DoDelay([NotNull] this Busy busy, Action a, int millisecondsDelay)
 {
     return(busy.Task(async() => {
         await Task.Delay(millisecondsDelay);
         a();
     }));
 }
Beispiel #6
0
 public static Task TaskYield([NotNull] this Busy busy, Func <Task> a)
 {
     return(busy.Task(async() => {
         await Task.Yield();
         await a();
     }));
 }
Beispiel #7
0
 public static Task Yield([NotNull] this Busy busy, Action a)
 {
     return(busy.Task(async() => {
         await Task.Yield();
         a();
     }));
 }
Beispiel #8
0
 protected override void OnClick()
 {
     if (Command is IAsyncCommand asyncCommand)
     {
         _busy.Task(async() => {
             try {
                 SetValue(IsProcessingPropertyKey, true);
                 using (_cancellation = new CancellationTokenSource())
                     using (_taskbar = TaskbarService.Create(1200)) {
                         await _commandInvoke.Invoke(asyncCommand, this, _cancellation.Token, CommandParameter);
                         if (_commandPercentageProgress)
                         {
                             // Report(new AsyncProgressEntry(Progress.Message, 1d));
                             Report(AsyncProgressEntry.Finished);
                         }
                     }
             } catch (Exception e) when(e.IsCanceled())
             {
             } catch (Exception e) {
                 NonfatalError.Notify("Unhandled error", e);
             } finally {
                 _cancellation = null;
                 SetValue(IsProcessingPropertyKey, false);
             }
         }).Forget();
     }
     else
     {
         base.OnClick();
     }
 }
Beispiel #9
0
 private void OnSlotsCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
 {
     _listBusy.Task(async() => {
         await Task.Delay(1);
         Renderer.CarSlots = Slots.Select(x => x.Slot).Distinct().ToArray();
     });
 }
            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();
            }
 private void OnCommentChange(WorkshopComment comment, string newMessage)
 {
     _busyEdit.Task(async() => {
         try {
             await WorkshopHolder.Client.PatchAsync($"{_url}/{comment.Id}", new { comment = newMessage });
         } catch (Exception e) {
             NonfatalError.Notify("Failed to edit a comment", e);
             return;
         }
         try {
             comment.Message = (await WorkshopHolder.Client.GetAsync <WorkshopComment>($"{_url}/{comment.Id}")).Message;
         } catch (Exception e) {
             Logging.Warning(e);
             comment.Message = newMessage;
         }
         comment.ChangeTo = null;
     });
 }
 private void OnReport(WorkshopComment comment, string message)
 {
     _busyReport.Task(async() => {
         try {
             await WorkshopHolder.Client.PostAsync($"{_reportsUrl}{comment.Id}", new { message });
         } catch (Exception e) {
             NonfatalError.Notify("Failed to send a report", e);
             return;
         }
         try {
             await WorkshopHolder.Client.GetAsync <WorkshopComment>($"{_url}/{comment.Id}");
         } catch (WorkshopException e) when(e.Code == HttpStatusCode.NotFound)
         {
             Comments.Remove(comment);
         } catch (Exception e) {
             Logging.Warning(e);
         }
     });
 }
Beispiel #13
0
        private void RunSafe(Func <Task> fn)
        {
            if (!_isActive)
            {
                return;
            }
            Busy.Task(async() => {
                try {
                    await fn().ConfigureAwait(false);
                } catch (Exception e) {
                    if (!_warn)
                    {
                        _warn = true;
                        Logging.Error(e);
                    }

                    CloseSocket();
                }
            });
        }
 private void OnLikeSet(WorkshopComment comment, bool likeSet)
 {
     if (_innerChange)
     {
         return;
     }
     if (_busyLike.Is)
     {
         _innerChange  = true;
         comment.Liked = !likeSet;
         _innerChange  = false;
     }
     _busyLike.Task(async() => {
         try {
             if (likeSet)
             {
                 await WorkshopHolder.Client.PostAsync($"{_likesUrl}{comment.Id}");
             }
             else
             {
                 await WorkshopHolder.Client.DeleteAsync($"{_likesUrl}{comment.Id}");
             }
         } catch (WorkshopException e) when(e.Code == HttpStatusCode.Conflict || e.Code == HttpStatusCode.NotFound)
         {
             // ignore such cases, as they usually mean desired task is already completed
             _innerChange  = true;
             comment.Likes = likeSet ? comment.Likes - 1 : comment.Likes + 1;
             _innerChange  = false;
         } catch (Exception e) {
             _innerChange  = true;
             comment.Liked = !likeSet;
             _innerChange  = false;
             NonfatalError.Notify(likeSet ? "Failed to send a like" : "Failed to delete a like", e);
         }
     });
 }