Beispiel #1
0
        public void SignFiles(FsPath packagePath, FsPath output, string setCodes)
        {
            FsPath parentDir = output.Parent();

            parentDir.CreateDirectory();

            if (packagePath.IsDirectory())
            {
                var sets = setCodes?.Split(';', ',', '|')
                           // to remove duplicates with different set name casing
                           .ToHashSet(StringComparer.OrdinalIgnoreCase);

                var prevSignatureByPath = sets != null && output.IsFile()
                                        ? Signer.ReadFromFile(output, internPath: false)
                                          .Where(_ => !sets.Contains(_.Path.Parent().Value))
                                          .ToDictionary(_ => _.Path)
                                        : new Dictionary <FsPath, FileSignature>();

                var signatures = Signer.CreateSignatures(packagePath, precalculated: prevSignatureByPath);
                Signer.WriteToFile(output, signatures);
            }
            else if (packagePath.IsFile())
            {
                var metadata = Signer.CreateSignature(packagePath);
                Signer.WriteToFile(output, Sequence.Array(metadata));
            }
            else
            {
                Console.WriteLine("Specified path {0} does not exist", packagePath);
            }
        }
Beispiel #2
0
        public void MoveFormHistoryToEnd(FormRoot form)
        {
            var idBeforeClosing = GetId(form);

            FsPath tempDir = AppDir.History.Join("temp");

            if (tempDir.IsDirectory())
            {
                tempDir.DeleteDirectory(recursive: true);
            }

            AppDir.History.Join(idBeforeClosing.ToString()).MoveDirectoryTo(tempDir);

            int lastId = _instances.Count - 1;

            for (int i = idBeforeClosing + 1; i <= lastId; i++)
            {
                AppDir.History.Join(i.ToString()).MoveDirectoryTo(AppDir.History.Join((i - 1).ToString()));
            }

            tempDir.MoveDirectoryTo(AppDir.History.Join(lastId.ToString()));

            _instances.RemoveAt(idBeforeClosing);
            _instances.Add(form);
        }
Beispiel #3
0
 private static void moveDirectoryToBackup(FsPath dir, FsPath dirBak)
 {
     if (dirBak.IsDirectory())
     {
         dirBak.DeleteDirectory(recursive: true);
     }
     dirBak.Parent().CreateDirectory();
     dir.MoveDirectoryTo(dirBak);
 }
Beispiel #4
0
        public void RemoveObsoleteIndexes()
        {
            if (!_root.IsDirectory())
            {
                return;
            }

            foreach (var subdir in _root.EnumerateDirectories())
            {
                if (subdir != IndexDirectory)
                {
                    subdir.DeleteDirectory(recursive: true);
                }
            }
        }
Beispiel #5
0
        public void MigrateHistoryFiles()
        {
            FsPath firstFormDirectory = AppDir.History.Join(0.ToString());

            if (firstFormDirectory.IsDirectory())
            {
                return;
            }

            firstFormDirectory.CreateDirectory();

            foreach (FsPath file in AppDir.History.EnumerateFiles())
            {
                file.CopyFileTo(firstFormDirectory.Join(file.Basename()));
            }
        }
Beispiel #6
0
        public async Task DownloadGathererImages(string setCodesStr, bool nonToken, bool token)
        {
            var clients = new List <ImageDownloaderBase>(2)
            {
                // new GathererClient(),
                new ScryfallClient(),
            };

            var setCodes = setCodesStr?.Split(',').ToHashSet(StringComparer.OrdinalIgnoreCase);
            var repo     = new CardRepository(new CardFormatter(), () => null)
            {
                FilterSetCode = setCode => setCodes?.Contains(setCode) != false,
            };

            repo.LoadFile();
            repo.Load();

            foreach (Set set in repo.SetsByCode.Values)
            {
                if (setCodes?.Contains(set.Code) == false)
                {
                    continue;
                }

                foreach ((bool isToken, FsPath typeSubdir, _) in getIsToken(nonToken, token))
                {
                    var cards = set.List(isToken);
                    if (cards.Count == 0)
                    {
                        continue;
                    }

                    var missingCardsByClient = clients.ToDictionary(_ => _, _ => 0);

                    FsPath setSubdir        = new FsPath(set.Code + (Str.Equals(set.Code, "con") ? " escape" : string.Empty));
                    FsPath downloadRootDir  = DevPaths.MtgContentDir.Join(_createZoom ? OriginalSubdir : PreProcessedSubdir);
                    FsPath rootDirZoom      = DevPaths.MtgContentDir.Join(PreProcessedSubdir);
                    FsPath setDirectory     = downloadRootDir.Join(typeSubdir, setSubdir);
                    FsPath setDirectoryZoom = rootDirZoom.Join(typeSubdir, setSubdir);
                    FsPath setDirectoryPng  = setDirectory.Concat(".png");

                    bool dirExisted = setDirectoryPng.IsDirectory();
                    if (!dirExisted)
                    {
                        setDirectoryPng.CreateDirectory();
                    }

                    foreach (var card in cards)
                    {
                        FsPath targetFile        = setDirectoryPng.Join(card.ImageName + ".png");
                        FsPath processedFile     = setDirectory.Join(card.ImageName + ".jpg");
                        FsPath processedFileZoom = setDirectoryZoom.Join(card.ImageName + ".jpg");
                        if (targetFile.IsFile() || processedFile.IsFile() && processedFileZoom.IsFile())
                        {
                            continue;
                        }

                        if (targetFile.Basename(extension: false).EndsWith("1"))
                        {
                            var unnumbered = targetFile.WithName(_ => _.Replace("1.png", ".png"));
                            if (unnumbered.IsFile())
                            {
                                unnumbered.MoveFileTo(targetFile);
                                continue;
                            }
                        }

                        foreach (ImageDownloaderBase client in clients)
                        {
                            int attempts = 5;

                            for (int i = 0; i < attempts; i++)
                            {
                                var cancellation = new CancellationTokenSource();
                                var time         = DateTime.UtcNow;

                                var downloadTask = client.DownloadCardImage(card, targetFile, cancellation.Token);
                                var waitTask     = Task.Delay(TimeSpan.FromSeconds(5), cancellation.Token);

                                await Task.WhenAny(downloadTask, waitTask);

                                cancellation.Cancel();

                                var elapsed = DateTime.UtcNow - time;
                                var delta   = TimeSpan.FromSeconds(0.5) - elapsed;
                                if (delta.TotalSeconds > 0)
                                {
                                    await Task.Delay(delta);
                                }

                                if (targetFile.IsFile())
                                {
                                    break;
                                }
                            }

                            if (targetFile.IsFile())
                            {
                                break;
                            }

                            missingCardsByClient[client]++;
                        }
                    }

                    if (!dirExisted && !setDirectoryPng.EnumerateFiles().Any())
                    {
                        setDirectoryPng.DeleteDirectory();
                    }
                }
            }
        }