예제 #1
0
        public Deck Deserialize(FsPath file, FsPath dir)
        {
            State.LastLoadedFile = file;

            Deck deck = Deck.Create();

            deck.File = file;

            int  maxLen = 0x8000000;            // 128 MB
            long length = file.File().Length;

            if (length > maxLen)
            {
                deck.Error = $"File size {length} bytes exceeds maximum of {maxLen} bytes";
                return(deck);
            }

            string serialized;

            try
            {
                serialized = file.ReadAllText();
            }
            catch (IOException ex)
            {
                deck.Error = ex.Message;
                return(deck);
            }

            var format = @"*" + file.Extension();

            var formatter = getFormatter(format, serialized);

            if (formatter == null)
            {
                deck.Error = "Deck format is not supported";
                return(deck);
            }

            deck = LoadSerialized(format, serialized, exact: false);

            deck.File = file;

            if (deck.Name == null)
            {
                string getNestedFileName() =>
                dir.Base().Join(file.RelativeTo(dir)).Value
                .Replace(new string(Path.DirectorySeparatorChar, 1), Environment.NewLine);

                var extension = file.Extension();

                string nameBase = !dir.HasValue()
                                        ? file.Basename()
                                        : getNestedFileName();

                deck.Name = nameBase.Substring(0, nameBase.Length - extension.Length);
            }

            return(deck);
        }
예제 #2
0
        public void RenameWizardsWebpageImages(string htmlFile, string targetSubdir)
        {
            FsPath htmlPath  = HtmlDir.Join(htmlFile);
            FsPath targetDir = DevPaths.GathererOriginalDir.Join(targetSubdir);

            string htmlFileName  = htmlPath.Basename(extension: false);
            FsPath directoryName = htmlPath.Parent();

            if (!directoryName.HasValue())
            {
                throw new ArgumentException(htmlPath.Value, nameof(htmlPath));
            }

            FsPath filesDirectory = directoryName.Join(htmlFileName + "_files");

            string content = htmlPath.ReadAllText();
            var    matches = _imgTagPattern.Matches(content);

            targetDir.CreateDirectory();
            foreach (Match match in matches)
            {
                string originalFileName = match.Groups["file"].Value;
                string ext = Path.GetExtension(originalFileName);

                FsPath filePath = filesDirectory.Join(originalFileName);

                string name = HttpUtility.HtmlDecode(match.Groups["name"].Value)
                              .Replace(" // ", "");

                FsPath defaultTargetPath = targetDir.Join(name + ext);

                bool defaultTargetExists = defaultTargetPath.IsFile();

                if (defaultTargetExists || getTargetPath(1).IsFile())
                {
                    if (defaultTargetExists)
                    {
                        defaultTargetPath.MoveFileTo(getTargetPath(1));
                    }

                    for (int i = 2; i < 12; i++)
                    {
                        FsPath targetPath = getTargetPath(i);
                        if (!targetPath.IsFile())
                        {
                            filePath.CopyFileTo(targetPath, overwrite: false);
                            break;
                        }
                    }
                }
                else
                {
                    filePath.CopyFileTo(defaultTargetPath, overwrite: false);
                }

                FsPath getTargetPath(int num) =>
                targetDir.Join(name + num + ext);
            }
        }
예제 #3
0
        public static void CreateApplicationShortcut(FsPath exePath, FsPath iconPath, FsPath shortcutPath)
        {
            if (shortcutPath.IsFile())
            {
                shortcutPath.DeleteFile();
            }

            var          wsh = new WshShell();
            IWshShortcut shortcut;

            try
            {
                shortcut = wsh.CreateShortcut(shortcutPath.Value) as IWshShortcut;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(
                    "Failed to create shortcut object {0} at {1}: {2}",
                    exePath, shortcutPath, ex);
                return;
            }

            FsPath bin = exePath.Parent();

            if (shortcut == null)
            {
                Console.Error.WriteLine("Failed to create shortcut {0} at {1}: {2}.{3} returned null",
                                        exePath, shortcutPath, nameof(WshShell), nameof(wsh.CreateShortcut));
                return;
            }

            shortcut.Arguments   = "";
            shortcut.TargetPath  = exePath.Value;
            shortcut.WindowStyle = 1;

            shortcut.Description      = "Application to search MTG cards and build decks";
            shortcut.WorkingDirectory = bin.Value;

            if (iconPath.HasValue())
            {
                shortcut.IconLocation = iconPath.Value;
            }

            try
            {
                shortcut.Save();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Failed to create shortcut {0} at {1}: {2}", exePath, shortcutPath, ex);
            }
        }
예제 #4
0
        private void export(
            FsPath directory,
            string setCodesStr,
            ISet <FsPath> exportedSmall,
            ISet <FsPath> exportedZoomed,
            bool small,
            bool zoomed,
            FsPath smallSubdir,
            FsPath zoomedSubdir,
            bool matchingSet,
            bool forceRemoveCorner,
            bool token)
        {
            var setCodes = setCodesStr?.Split(',').ToHashSet(StringComparer.OrdinalIgnoreCase);

            foreach ((string setCode, Set set) in _cardRepo.SetsByCode)
            {
                Console.WriteLine(setCode);

                if (setCodes?.Contains(setCode) == false)
                {
                    continue;
                }

                FsPath smallSetSubdir  = FsPath.None;
                FsPath zoomedSetSubdir = FsPath.None;

                if (small)
                {
                    if (smallSubdir.HasValue())
                    {
                        smallSetSubdir = directory.Join(smallSubdir).Join(setCode);
                    }
                    else
                    {
                        smallSetSubdir = directory.Join(setCode);
                    }

                    smallSetSubdir = ensureSetSubdirectory(smallSetSubdir);
                }

                if (zoomed)
                {
                    if (zoomedSubdir.HasValue())
                    {
                        zoomedSetSubdir = directory.Join(zoomedSubdir).Join(setCode);
                    }
                    else
                    {
                        zoomedSetSubdir = directory.Join(setCode);
                    }

                    zoomedSetSubdir = ensureSetSubdirectory(zoomedSetSubdir);
                }

                foreach (var card in set.Cards)
                {
                    if (card.IsSingleSide() && card.Faces.Main != card)
                    {
                        continue;
                    }

                    if (card.IsToken != token)
                    {
                        continue;
                    }

                    Bitmap     original   = null;
                    ImageModel modelSmall = null;

                    if (small)
                    {
                        modelSmall = _imageRepo.GetSmallImage(card, _cardRepo.GetReleaseDateSimilarity);

                        if (modelSmall != null &&
                            Str.Equals(card.SetCode, modelSmall.ImageFile.SetCode) == matchingSet &&
                            exportedSmall.Add(modelSmall.ImageFile.FullPath))
                        {
                            FsPath smallPath = getTargetPath(modelSmall.ImageFile, smallSetSubdir);

                            if (!smallPath.IsFile() || card.Faces.Count > 1)
                            {
                                original = ImageLoader.Open(modelSmall);
                                addFile(original, modelSmall.ImageFile, smallPath, small: true, forceRemoveCorner);
                            }
                        }
                    }

                    if (zoomed)
                    {
                        var modelZoom = _imageRepo.GetImagePrint(card, _cardRepo.GetReleaseDateSimilarity);

                        if (modelZoom != null &&
                            Str.Equals(card.SetCode, modelZoom.ImageFile.SetCode) == matchingSet &&
                            exportedZoomed.Add(modelZoom.ImageFile.FullPath))
                        {
                            FsPath zoomedPath = getTargetPath(modelZoom.ImageFile, zoomedSetSubdir);

                            if (!zoomedPath.IsFile() || card.Faces.Count > 1)
                            {
                                if (original == null || modelSmall.ImageFile.FullPath != modelZoom.ImageFile.FullPath)
                                {
                                    original?.Dispose();
                                    original = ImageLoader.Open(modelZoom);
                                }

                                addFile(original, modelZoom.ImageFile, zoomedPath, small: false, forceRemoveCorner);
                            }
                        }
                    }

                    original?.Dispose();
                }

                smallSetSubdir.DeleteEmptyDirectory();
                zoomedSetSubdir.DeleteEmptyDirectory();
            }
        }