using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using static PKHeX.Core.MessageStrings; namespace PKHeX.Core { public static class SaveExtensions { /// /// Checks if the is compatible with the input , and makes any necessary modifications to force compatibility. /// /// Should only be used when forcing a backwards conversion to sanitize the PKM fields to the target format. /// If the PKM is compatible, some properties may be forced to sanitized values. /// Save File target that the PKM will be injected. /// PKM input that is to be injected into the Save File. /// Indication whether or not the PKM is compatible. public static bool IsPKMCompatibleWithModifications(this SaveFile SAV, PKM pk) => PKMConverter.IsPKMCompatibleWithModifications(pk, SAV); /// /// Sets the details of a path to a object. /// /// Save File to set path details to. /// Full Path of the file public static void SetFileInfo(this SaveFile sav, string path) { if (!sav.Exportable) // Blank save file { sav.FileFolder = sav.FilePath = string.Empty; sav.FileName = "Blank Save File"; return; } sav.FilePath = path; sav.FileFolder = Path.GetDirectoryName(path); sav.FileName = string.Empty; var bakName = Util.CleanFileName(sav.BAKName); sav.FileName = Path.GetFileName(path); if (sav.FileName?.EndsWith(bakName) == true) sav.FileName = sav.FileName.Substring(0, sav.FileName.Length - bakName.Length); } /// /// Gets suggested export options for the savefile. /// /// /// /// public static ExportFlags GetSuggestedFlags(this SaveFile sav, string ext) { var flags = ExportFlags.None; if (ext == ".dsv") flags |= ExportFlags.IncludeFooter; if (ext == ".gci" || (sav is IGCSaveFile gc && !gc.IsMemoryCardSave)) flags |= ExportFlags.IncludeHeader; return flags; } /// /// Checks a file for compatibility to the . /// /// that is being checked. /// that is being tested for compatibility. /// public static IReadOnlyList IsPKMCompatible(this SaveFile SAV, PKM pkm) { IBasicStrings strings = GameInfo.Strings; return SAV.GetSaveFileErrata(pkm, strings); } private static IReadOnlyList GetSaveFileErrata(this SaveFile SAV, PKM pkm, IBasicStrings strings) { var errata = new List(); if (SAV.Generation > 1) { ushort held = (ushort)pkm.HeldItem; var itemstr = GameInfo.Strings.GetItemStrings(pkm.Format, (GameVersion) pkm.Version); if (held > itemstr.Count) errata.Add($"{MsgIndexItemRange} {held}"); else if (held > SAV.MaxItemID) errata.Add($"{MsgIndexItemGame} {itemstr[held]}"); else if (!pkm.CanHoldItem(SAV.HeldItems)) errata.Add($"{MsgIndexItemHeld} {itemstr[held]}"); } if (pkm.Species > strings.Species.Count) errata.Add($"{MsgIndexSpeciesRange} {pkm.Species}"); else if (SAV.MaxSpeciesID < pkm.Species) errata.Add($"{MsgIndexSpeciesGame} {strings.Species[pkm.Species]}"); if (!SAV.Personal[pkm.Species].IsFormeWithinRange(pkm.AltForm) && !FormConverter.IsValidOutOfBoundsForme(pkm.Species, pkm.AltForm, pkm.GenNumber)) errata.Add(string.Format(LegalityCheckStrings.LFormInvalidRange, Math.Max(0, SAV.Personal[pkm.Species].FormeCount - 1), pkm.AltForm)); if (pkm.Moves.Any(m => m > strings.Move.Count)) errata.Add($"{MsgIndexMoveRange} {string.Join(", ", pkm.Moves.Where(m => m > strings.Move.Count).Select(m => m.ToString()))}"); else if (pkm.Moves.Any(m => m > SAV.MaxMoveID)) errata.Add($"{MsgIndexMoveGame} {string.Join(", ", pkm.Moves.Where(m => m > SAV.MaxMoveID).Select(m => strings.Move[m]))}"); if (pkm.Ability > strings.Ability.Count) errata.Add($"{MsgIndexAbilityRange} {pkm.Ability}"); else if (pkm.Ability > SAV.MaxAbilityID) errata.Add($"{MsgIndexAbilityGame} {strings.Ability[pkm.Ability]}"); return errata; } /// /// Imports compatible data to the , starting at the provided box. /// /// Save File that will receive the data. /// Compatible data that can be set to the without conversion. /// Overwrite existing full slots. If true, will only overwrite empty slots. /// First box to start loading to. All prior boxes are not modified. /// Bypass option to not modify properties when setting to Save File. /// Count of injected . public static int ImportPKMs(this SaveFile SAV, IEnumerable compat, bool overwrite = false, int boxStart = 0, PKMImportSetting noSetb = PKMImportSetting.UseDefault) { int startCount = boxStart * SAV.BoxSlotCount; int maxCount = SAV.SlotCount; int index = startCount; int nonOverwriteImport = 0; foreach (var pk in compat) { if (overwrite) { while (SAV.IsSlotOverwriteProtected(index)) ++index; SAV.SetBoxSlotAtIndex(pk, index, noSetb); } else { index = SAV.NextOpenBoxSlot(index-1); if (index < 0) // Boxes full! break; SAV.SetBoxSlotAtIndex(pk, index, noSetb); nonOverwriteImport++; } if (++index == maxCount) // Boxes full! break; } return (overwrite) ? index - startCount : nonOverwriteImport; // actual imported count } public static IEnumerable GetCompatible(this SaveFile SAV, IEnumerable pks) { var savtype = SAV.PKMType; foreach (var temp in pks) { var pk = PKMConverter.ConvertToType(temp, savtype, out string c); if (pk == null) { Debug.WriteLine(c); continue; } if (PKMConverter.IsIncompatibleGB(pk.Format, SAV.Japanese, pk.Japanese)) { c = PKMConverter.GetIncompatibleGBMessage(pk, SAV.Japanese); Debug.WriteLine(c); continue; } var compat = SAV.IsPKMCompatible(pk); if (compat.Count > 0) continue; yield return pk; } } /// /// Gets a compatible for editing with a new . /// /// SaveFile to receive the compatible /// Current Pokémon being edited /// Current Pokémon, assuming conversion is possible. If conversion is not possible, a blank will be obtained from the . public static PKM GetCompatiblePKM(this SaveFile sav, PKM pk = null) { if (pk == null) return sav.BlankPKM; if (pk.Format < 3 && sav.Generation < 7) { // gen1-2 compatibility check if (pk.Japanese != sav.Japanese) return sav.BlankPKM; if (sav is SAV2 s2 && s2.Korean != pk.Korean) return sav.BlankPKM; } return PKMConverter.ConvertToType(pk, sav.PKMType, out _) ?? sav.BlankPKM; } /// /// Gets a blank file for the save file. If the template path exists, a template load will be attempted. /// /// Save File to fetch a template for /// Path to look for a template in /// Template if it exists, or a blank from the 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 } } }