public Updater([NotNull] IReadOnlyList <ToUpdatePreview> entries, [NotNull] DarkPreviewsOptions options, [CanBeNull] string presetName, [CanBeNull] DarkPreviewsUpdater updater) { _entries = entries; _options = options; _presetName = presetName; if (updater == null) { _localUpdater = true; _updater = new DarkPreviewsUpdater(AcRootDirectory.Instance.RequireValue, options); } else { _updater = updater; _updater.SetOptions(options); } }
private static async Task <IReadOnlyList <UpdatePreviewError> > UpdatePreview(IReadOnlyList <ToUpdatePreview> entries, DarkPreviewsOptions options, string presetName = null, DarkPreviewsUpdater updater = null) { var localUpdater = updater == null; if (localUpdater) { updater = new DarkPreviewsUpdater(AcRootDirectory.Instance.RequireValue, options); } else { updater.SetOptions(options); } var errors = new List <UpdatePreviewError>(); try { if (options.Showroom != null && ShowroomsManager.Instance.GetById(options.Showroom) == null) { if (options.Showroom == "at_previews" && MissingShowroomHelper != null) { await MissingShowroomHelper.OfferToInstall("Kunos Previews Showroom (AT Previews Special)", "at_previews", "http://www.assettocorsa.net/assetto-corsa-v1-5-dev-diary-part-33/"); if (ShowroomsManager.Instance.GetById(options.Showroom) != null) { goto Action; } } throw new InformativeException("Can’t update preview", $"Showroom “{options.Showroom}” is missing"); } Action: var checksum = options.GetChecksum(); var finished = false; var j = 0; using (var waiting = new WaitingDialog()) { var cancellation = waiting.CancellationToken; var singleMode = entries.Count == 1; var verySingleMode = singleMode && entries[0].Skins?.Count == 1; var recycled = 0; if (!verySingleMode) { waiting.SetImage(null); if (SettingsHolder.CustomShowroom.PreviewsRecycleOld) { waiting.SetMultiline(true); } } var step = 1d / entries.Count; var postfix = string.Empty; var started = Stopwatch.StartNew(); var approximateSkinsPerCarCars = 1; var approximateSkinsPerCarSkins = 10; Action <int> updateApproximate = skinsPerCar => { approximateSkinsPerCarCars++; approximateSkinsPerCarSkins += skinsPerCar; }; Func <int, int> leftSkins = currentEntry => { var skinsPerCar = (double)approximateSkinsPerCarSkins / approximateSkinsPerCarCars; var result = 0d; for (var k = currentEntry; k < entries.Count; k++) { var entry = entries[k]; result += entry.Skins?.Count ?? skinsPerCar; } return(result.RoundToInt()); }; var shotSkins = 0; var recyclingWarning = false; Func <CarObject, CarSkinObject, int?, IEnumerable <string> > getDetails = (car, skin, currentEntrySkinsLeft) => { var left = leftSkins(j) + (currentEntrySkinsLeft ?? approximateSkinsPerCarSkins / approximateSkinsPerCarCars); // ReSharper disable once AccessToModifiedClosure var speed = shotSkins / started.Elapsed.TotalMinutes; var remainingTime = speed < 0.0001 ? "Unknown" : $"About {TimeSpan.FromMinutes(left / speed).ToReadableTime()}"; var remainingItems = $"About {left} {PluralizingConverter.Pluralize(left, ControlsStrings.CustomShowroom_SkinHeader).ToSentenceMember()}"; return(new[] { $"Car: {car?.DisplayName}", $"Skin: {skin?.DisplayName ?? "?"}", $"Speed: {speed:F2} {PluralizingConverter.Pluralize(10, ControlsStrings.CustomShowroom_SkinHeader).ToSentenceMember()}/{"min"}", $"Time remaining: {remainingTime}", $"Items remaining: {remainingItems}", // ReSharper disable once AccessToModifiedClosure recyclingWarning ? "[i]Recycling seems to take too long? If so, it can always be disabled in Settings.[/i]" : null }.NonNull()); }; for (j = 0; j < entries.Count; j++) { if (cancellation.IsCancellationRequested) { goto Cancel; } var entry = entries[j]; var progress = step * j; var car = entry.Car; var skins = entry.Skins; if (skins == null) { waiting.Report(new AsyncProgressEntry("Loading skins…" + postfix, verySingleMode ? 0d : progress)); waiting.SetDetails(getDetails(car, null, null)); await car.SkinsManager.EnsureLoadedAsync(); if (cancellation.IsCancellationRequested) { goto Cancel; } skins = car.EnabledOnlySkins.ToList(); updateApproximate(skins.Count); } var halfstep = step * 0.5 / skins.Count; for (var i = 0; i < skins.Count; i++) { if (cancellation.IsCancellationRequested) { goto Cancel; } var skin = skins[i]; waiting.SetDetails(getDetails(car, skin, skins.Count - i)); var subprogress = progress + step * (0.1 + 0.8 * i / skins.Count); if (SettingsHolder.CustomShowroom.PreviewsRecycleOld && File.Exists(skin.PreviewImage)) { if (++recycled > 5) { recyclingWarning = true; } waiting.Report(new AsyncProgressEntry($"Recycling current preview for {skin.DisplayName}…" + postfix, verySingleMode ? 0d : subprogress)); await Task.Run(() => FileUtils.Recycle(skin.PreviewImage)); } waiting.Report(new AsyncProgressEntry($"Updating skin {skin.DisplayName}…" + postfix, verySingleMode ? 0d : subprogress + halfstep)); try { await updater.ShotAsync(car.Id, skin.Id, skin.PreviewImage, car.AcdData, GetInformation(car, skin, presetName, checksum), () => { if (!verySingleMode) { ActionExtension.InvokeInMainThreadAsync(() => { // ReSharper disable once AccessToModifiedClosure if (!finished) { // ReSharper disable once AccessToDisposedClosure waiting.SetImage(skin.PreviewImage); } }); } }); shotSkins++; } catch (Exception e) { if (errors.All(x => x.ToUpdate != entry)) { errors.Add(new UpdatePreviewError(entry, e.Message, null)); } } } } waiting.Report(new AsyncProgressEntry("Saving…" + postfix, verySingleMode ? 0d : 0.999999d)); await updater.WaitForProcessing(); finished = true; } if (errors.Count > 0) { NonfatalError.Notify("Can’t update previews:\n" + errors.Select(x => @"• " + x.Message.ToSentence()).JoinToString(";" + Environment.NewLine)); } goto End; Cancel: for (; j < entries.Count; j++) { errors.Add(new UpdatePreviewError(entries[j], ControlsStrings.Common_Cancelled, null)); } End: return(errors); } catch (Exception e) { NonfatalError.Notify("Can’t update preview", e); return(null); } finally { if (localUpdater) { updater.Dispose(); GC.Collect(); } } }