Exemple #1
0
        public async Task <Scanned> GetEntriesAsync([NotNull] List <IFileInfo> list, string baseId, string baseName,
                                                    [CanBeNull] IProgress <AsyncProgressEntry> progress, CancellationToken cancellation)
        {
            progress?.Report(AsyncProgressEntry.FromStringIndetermitate("Scanning…"));

            var       result         = new List <ContentEntryBase>();
            var       missingContent = false;
            Exception readException  = null;

            var s    = Stopwatch.StartNew();
            var root = new DirectoryNode(_installationParams.FallbackId ?? baseId, null);

            root.ForceName(baseName);

            foreach (var info in list)
            {
                root.Add(info);
            }

            Logging.Debug($"Building tree: {s.Elapsed.TotalMilliseconds:F1} ms");

            s.Restart();
            var queue = new Queue <DirectoryNode>();

            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                var directory = queue.Dequeue();

                ContentEntryBase found;
                try {
                    found = await CheckDirectoryNode(directory, cancellation).ConfigureAwait(false); // WHY IT DOES NOT WORK?

                    if (cancellation.IsCancellationRequested)
                    {
                        break;
                    }
                } catch (Exception e) when(e.IsCancelled())
                {
                    break;
                } catch (MissingContentException) {
                    missingContent = true;
                    continue;
                } catch (Exception e) {
                    Logging.Warning(e);
                    readException = e;
                    continue;
                }

                if (found != null)
                {
                    result.Add(found);
                }
                else
                {
                    foreach (var value in directory.Directories)
                    {
                        queue.Enqueue(value);
                    }

                    foreach (var value in directory.Files)
                    {
                        try {
                            found = await CheckFileNode(value, cancellation).ConfigureAwait(false);

                            if (cancellation.IsCancellationRequested)
                            {
                                break;
                            }
                        } catch (Exception e) when(e.IsCancelled())
                        {
                            break;
                        } catch (MissingContentException) {
                            missingContent = true;
                            continue;
                        } catch (Exception e) {
                            Logging.Warning(e);
                            readException = e;
                            continue;
                        }

                        if (found != null)
                        {
                            result.Add(found);
                        }
                    }
                }
            }

            Logging.Debug($"Scanning directories: {s.Elapsed.TotalMilliseconds:F1} ms");
            return(new Scanned(result, missingContent, readException));
        }