public static IEnumerable <PKM> GetPKMsFromPaths(IEnumerable <string> files, int generation) { var result = files .Where(file => PKX.IsPKM(new FileInfo(file).Length)) .Select(File.ReadAllBytes) .Select(data => PKMConverter.GetPKMfromBytes(data, prefer: generation)); foreach (var pkm in result) { if (pkm != null) { yield return(pkm); } } }
/// <summary> /// Gets a blank file for the save file. If the template path exists, a template load will be attempted. /// </summary> /// <param name="sav">Save File to fetch a template for</param> /// <param name="templatePath">Path to look for a template in</param> /// <returns>Template if it exists, or a blank <see cref="PKM"/> from the <see cref="sav"/></returns> public static PKM LoadTemplate(this SaveFile sav, string templatePath = null) { var blank = sav.BlankPKM; if (!Directory.Exists(templatePath)) { return(blank); } var di = new DirectoryInfo(templatePath); string path = Path.Combine(templatePath, $"{di.Name}.{blank.Extension}"); if (!File.Exists(path) || !PKX.IsPKM(new FileInfo(path).Length)) { return(blank); } var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(path), prefer: blank.Format); return(PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out path) ?? blank); // no sneaky plz; reuse string }
public static void AddFromLocalFile(string file, ConcurrentBag <SlotCache> db, ITrainerInfo dest, ICollection <string> validExtensions) { var fi = new FileInfo(file); if (!validExtensions.Contains(fi.Extension) || !PKX.IsPKM(fi.Length)) { return; } var data = File.ReadAllBytes(file); var prefer = PKX.GetPKMFormatFromExtension(fi.Extension, dest.Generation); var pk = PKMConverter.GetPKMfromBytes(data, prefer); if (pk?.Species is not > 0) { return; } var info = new SlotInfoFile(file); var entry = new SlotCache(info, pk); db.Add(entry); }
/// <summary> /// Gets a blank file for the save file. If the template path exists, a template load will be attempted. /// </summary> /// <param name="sav">Save File to fetch a template for</param> /// <param name="templatePath">Path to look for a template in</param> /// <returns>Template if it exists, or a blank <see cref="PKM"/> from the <see cref="sav"/></returns> public static PKM LoadTemplate(this SaveFile sav, string templatePath) { if (!Directory.Exists(templatePath)) { return(LoadTemplate(sav)); } var di = new DirectoryInfo(templatePath); string path = Path.Combine(templatePath, $"{di.Name}.{sav.PKMType.Name.ToLower()}"); if (!File.Exists(path) || !PKX.IsPKM(new FileInfo(path).Length)) { return(LoadTemplate(sav)); } var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(path), prefer: sav.Generation); if (pk == null) { return(LoadTemplate(sav)); } return(PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out _) ?? LoadTemplate(sav)); }
protected override PKM GetPKM(byte[] data) => PKMConverter.GetPKMfromBytes(data, prefer: Generation);