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); }
public ImageFile( FsPath fileName, FsPath rootPath, string setCode = null, string artist = null, bool isArt = false, int?customPriority = null) { var fileNameWithoutExtension = fileName.Basename(extension: false); FsPath directoryName = fileName.Parent(); FullPath = fileName; string[] parts = fileNameWithoutExtension.Split(Sequence.Array('.'), StringSplitOptions.None); var lastNamePart = Enumerable.Range(0, parts.Length) .Last(i => i == 0 || // Richard Garfield, Ph.D..xlhq.jpg // S.N.O.T..xlhq.jpg // Our Market Research....xlhq.jpg parts[i].Length <= 1 || // Sarpadian Empires, Vol. VII.xlhq.jpg // Но Thoughtseize.[Size 16x20].jpg parts[i].Contains(' ') && !(parts[i].StartsWith("[") && parts[i].EndsWith("]"))); Type = string.Join(".", parts.Skip(1 + lastNamePart)); string imageNameRaw = string.Join(".", parts.Take(1 + lastNamePart)); var imageName = _patternToRemove.Replace(imageNameRaw, string.Empty); var replacedName = _nameReplacements.TryGet(imageName) ?? imageName; ImageName = string.Intern(replacedName); var nameParts = ImageName.SplitTailingNumber(); Name = string.Intern(nameParts.Item1); VariantNumber = nameParts.Item2; rootPath = rootPath.ToAppRootedPath(); if (setCode != null) { SetCode = string.Intern(setCode); SetCodeIsFromAttribute = true; } else { var setCodeMatch = _setCodeRegex.Match(directoryName.RelativeTo(rootPath).Value); if (setCodeMatch.Success) { SetCode = string.Intern(setCodeMatch.Value.ToUpperInvariant()); } else { SetCode = string.Empty; } } Priority = customPriority ?? getPriority(); if (artist != null) { Artist = string.Intern(artist); } IsArt = isArt; IsToken = directoryName.Value.IndexOf("Token", Str.Comparison) >= 0; }