/// <summary> /// Creates PKSM's bank.bin to individual <see cref="PKM"/> files (v1) /// </summary> /// <param name="dir">Folder to export all dumped files to</param> public static int CreateBank(string dir) { var files = Directory.GetFiles(dir, "*.p??", SearchOption.TopDirectoryOnly); var ver = Enum.GetValues(typeof(PKSMBankVersion)).Cast <int>().Max(); var version = BitConverter.GetBytes(ver + 1); // Latest bank version var pksmsize = GetBankSize((PKSMBankVersion)ver); var boxcount = (files.Length / 30) + 1; var bank = new byte[8 + 4 + 4 + (boxcount * pksmsize * 30)]; var ctr = 0; var magic = new byte[] { 0x50, 0x4B, 0x53, 0x4D, 0x42, 0x41, 0x4E, 0x4B }; // PKSMBANK magic.CopyTo(bank, 0); version.CopyTo(bank, 8); BitConverter.GetBytes(boxcount).CopyTo(bank, 12); // Number of bank boxes. foreach (var f in files) { var prefer = EntityFileExtension.GetContextFromExtension(f, EntityContext.None); var pk = EntityFormat.GetFromBytes(File.ReadAllBytes(f), prefer); if (pk == null) { continue; } if (pk.Species == 0 && pk.Species >= pk.MaxSpeciesID) { continue; } var ofs = 16 + (ctr * pksmsize); BitConverter.GetBytes((int)GetPKSMFormat(pk)).CopyTo(bank, ofs); pk.DecryptedBoxData.CopyTo(bank, ofs + 4); byte[] temp = Enumerable.Repeat((byte)0xFF, pksmsize - pk.DecryptedBoxData.Length - 8).ToArray(); temp.CopyTo(bank, ofs + pk.DecryptedBoxData.Length + 4); temp = Enumerable.Repeat((byte)0x00, 4).ToArray(); temp.CopyTo(bank, ofs + pksmsize - 4); ctr++; } var empty = (boxcount * 30) - files.Length; for (int i = 0; i < empty; i++) { var ofs = 16 + (ctr * pksmsize); byte[] temp = Enumerable.Repeat((byte)0xFF, pksmsize).ToArray(); temp.CopyTo(bank, ofs); ctr++; } File.WriteAllBytes(Path.Combine(dir, "alm.bnk"), bank); return(ctr - empty); }
public static IEnumerable <PKM> GetPKMsFromPaths(IEnumerable <string> files, EntityContext generation) { foreach (var f in files) { var fi = new FileInfo(f); if (!fi.Exists) { continue; } if (!EntityDetection.IsSizePlausible(fi.Length)) { continue; } var data = File.ReadAllBytes(f); var prefer = EntityFileExtension.GetContextFromExtension(fi.Extension, generation); var pk = EntityFormat.GetFromBytes(data, prefer); if (pk?.Species is > 0) { yield return(pk); } } }
/// <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) { if (templatePath == null || !Directory.Exists(templatePath)) { return(LoadTemplateInternal(sav)); } var di = new DirectoryInfo(templatePath); string path = Path.Combine(templatePath, $"{di.Name}.{sav.PKMType.Name.ToLowerInvariant()}"); if (!File.Exists(path) || !EntityDetection.IsSizePlausible(new FileInfo(path).Length)) { return(LoadTemplateInternal(sav)); } var pk = EntityFormat.GetFromBytes(File.ReadAllBytes(path), prefer: sav.Generation); if (pk?.Species is not > 0) { return(LoadTemplateInternal(sav)); } return(EntityConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out _) ?? LoadTemplateInternal(sav)); }
/// <summary> /// Advanced constructor to build formatter options with the minimum number of parameters needed. /// </summary> /// <param name="indentChar">Char used to indent</param> /// <param name="indentSize">Number of chars the indent char is repeated each level</param> /// <param name="maxLineLength">Dermines the forced line break</param> /// <param name="elementCasing"><c>true</c> makes all attributes upper case</param> /// <param name="attributeCasing"><c>true</c> makes all elements upper case</param> /// <param name="makeXhtml"><c>true</c> if XHTML should produced</param> /// <param name="Entities">Determines the formatting of entities.</param> public HtmlFormatterOptions(char indentChar, int indentSize, int maxLineLength, HtmlFormatterCase elementCasing, HtmlFormatterCase attributeCasing, bool makeXhtml, EntityFormat Entities) { _indentChar = indentChar; _indentSize = indentSize; _maxLineLength = maxLineLength; _elementCasing = elementCasing; _attributeCasing = attributeCasing; _makeXhtml = makeXhtml; _Entities = Entities; _preserveWhitespace = false; _reformatStyles = false; }
protected override PKM GetPKM(byte[] data) => EntityFormat.GetFromBytes(data, prefer: Context) ?? blank;
protected override PKM GetPKM(byte[] data) => EntityFormat.GetFromBytes(data, prefer: Generation) ?? blank;