Beispiel #1
0
        public static Sleeve FromString(string sleeveImageString)
        {
            if (!string.IsNullOrWhiteSpace(sleeveImageString))
            {
                var parts = sleeveImageString.Split(new char[] { ':' }, 3);

                if (parts.Length != 3)
                {
                    throw new SleeveException($"This decks sleeve format is invalid.");
                }

                var name = parts[1];

                switch (parts[0])
                {
                case "custom": {
                    var sleeve = new Sleeve();

                    sleeve.Name = name;

                    try {
                        sleeve.ImageData = Convert.FromBase64String(parts[2]);
                    } catch (FormatException ex) {
                        throw new SleeveException("This decks sleeve data is invalid.", ex);
                    }

                    return(sleeve);
                }

                case "game": {
                    var sleeve = GetSleeves()
                                 .Where(x => x.Source == SleeveSource.Game)
                                 .Where(x => {
                            var dir          = new FileInfo(x.FilePath).Directory;
                            var gameIdString = dir.Parent.Name;
                            return(string.Equals(gameIdString, x.GameId.ToString(), StringComparison.InvariantCultureIgnoreCase));
                        })
                                 .Where(x => x.Name == name)
                                 .FirstOrDefault();

                    return(sleeve ?? throw new SleeveException($"Unable to find Game sleeve "));
                }

                case "octgn": {
                    var sleeve = GetSleeves()
                                 .Where(x => x.Source == SleeveSource.OCTGN)
                                 .Where(x => x.Name == name)
                                 .FirstOrDefault();

                    return(sleeve ?? throw new SleeveException($"Unable to find Game sleeve "));
                }

                default: throw new SleeveException($"Sleeve type {parts[0]} is not valid.");
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        private static IEnumerable <Sleeve> GetFromFolder(DirectoryInfo dir)
        {
            if (dir == null)
            {
                throw new ArgumentNullException(nameof(dir));
            }

            FileInfo[] files = null;

            try {
                if (!dir.Exists)
                {
                    yield break;
                }

                files = dir.GetFiles();
            } catch (Exception ex) {
                Log.Warn($"Error loading sleeves in {dir.FullName}", ex);

                yield break;
            }

            foreach (var file in files)
            {
                Sleeve sleeve = null;

                try {
                    sleeve           = new Sleeve();
                    sleeve.Name      = Path.GetFileNameWithoutExtension(file.Name);
                    sleeve.ImageData = File.ReadAllBytes(file.FullName);
                    sleeve.FilePath  = file.FullName;

                    sleeve.VerifyIsValid();
                } catch (Exception ex) {
                    Log.Warn($"Error loading sleeve {file.FullName}", ex);

                    continue;
                }

                yield return(sleeve);
            }
        }