mirror of
https://github.com/kwsch/PKHeX
synced 2025-02-16 21:38:40 +00:00
Add xmldoc for save meta/state tracking
Move some logic around to the more appropriate spot
This commit is contained in:
parent
15823f6f31
commit
98ef0299b6
5 changed files with 119 additions and 61 deletions
|
@ -5,19 +5,6 @@ using System.Linq;
|
|||
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
public class SaveFileState
|
||||
{
|
||||
public bool Edited { get; set; }
|
||||
public readonly bool Exportable;
|
||||
public readonly byte[] BAK;
|
||||
|
||||
public SaveFileState(byte[] bak, bool exportable = true)
|
||||
{
|
||||
BAK = bak;
|
||||
Exportable = exportable;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base Class for Save Files
|
||||
/// </summary>
|
||||
|
|
|
@ -4,24 +4,53 @@ using System.Linq;
|
|||
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Tracks information about where the <see cref="SAV"/> originated from, and provides logic for saving to a file.
|
||||
/// </summary>
|
||||
public class SaveFileMetadata
|
||||
{
|
||||
private readonly SaveFile SAV;
|
||||
|
||||
public string? FileName;
|
||||
public string? FilePath;
|
||||
public string? FileFolder;
|
||||
/// <summary>
|
||||
/// Full path where the <see cref="SAV"/> originated from.
|
||||
/// </summary>
|
||||
public string? FilePath { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// File Name of the <see cref="SAV"/>.
|
||||
/// </summary>
|
||||
/// <remarks>This is not always the original file name. We try to strip out the Backup name markings to get the original filename.</remarks>
|
||||
public string? FileName { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Directory in which the <see cref="SAV"/> was saved in.
|
||||
/// </summary>
|
||||
public string? FileFolder { get; private set; }
|
||||
|
||||
private byte[] Footer = Array.Empty<byte>(); // .dsv
|
||||
private byte[] Header = Array.Empty<byte>(); // .gci
|
||||
|
||||
/// <summary>
|
||||
/// Simple summary of the save file, to help differentiate it from other save files with the same filename.
|
||||
/// </summary>
|
||||
public string BAKName => $"{FileName} [{SAV.ShortSummary}].bak";
|
||||
|
||||
public SaveFileMetadata(SaveFile sav) => SAV = sav;
|
||||
|
||||
public bool HasHeader => Header.Length != 0;
|
||||
public bool HasFooter => Footer.Length != 0;
|
||||
|
||||
/// <summary>
|
||||
/// File Dialog filter to help save the file.
|
||||
/// </summary>
|
||||
public string Filter => $"{SAV.GetType().Name}|{GetSuggestedExtension()}|All Files|*.*";
|
||||
|
||||
/// <summary>
|
||||
/// Writes the input <see cref="data"/> and appends the <see cref="Header"/> and <see cref="Footer"/> if requested.
|
||||
/// </summary>
|
||||
/// <param name="data">Finalized save file data (with fixed checksums) to be written to a file</param>
|
||||
/// <param name="flags">Toggle flags </param>
|
||||
/// <returns></returns>
|
||||
public byte[] Finalize(byte[] data, ExportFlags flags)
|
||||
{
|
||||
if (Footer.Length > 0 && flags.HasFlagFast(ExportFlags.IncludeFooter))
|
||||
|
@ -47,22 +76,40 @@ namespace PKHeX.Core
|
|||
public void SetExtraInfo(string path)
|
||||
{
|
||||
var sav = SAV;
|
||||
if (!sav.State.Exportable) // Blank save file
|
||||
if (!sav.State.Exportable || string.IsNullOrWhiteSpace(path)) // Blank save file
|
||||
{
|
||||
sav.Metadata.FileFolder = sav.Metadata.FilePath = string.Empty;
|
||||
sav.Metadata.FileName = "Blank Save File";
|
||||
sav.Metadata.SetAsBlank();
|
||||
return;
|
||||
}
|
||||
|
||||
sav.Metadata.FilePath = path;
|
||||
sav.Metadata.FileFolder = Path.GetDirectoryName(path);
|
||||
sav.Metadata.FileName = string.Empty;
|
||||
var bakName = Util.CleanFileName(sav.Metadata.BAKName);
|
||||
sav.Metadata.FileName = Path.GetFileName(path);
|
||||
if (sav.Metadata.FileName?.EndsWith(bakName) == true)
|
||||
sav.Metadata.FileName = sav.Metadata.FileName.Substring(0, sav.Metadata.FileName.Length - bakName.Length);
|
||||
SetAsLoadedFile(path);
|
||||
}
|
||||
|
||||
private void SetAsLoadedFile(string path)
|
||||
{
|
||||
FilePath = path;
|
||||
FileFolder = Path.GetDirectoryName(path);
|
||||
FileName = GetFileName(path, BAKName);
|
||||
}
|
||||
|
||||
private static string? GetFileName(string path, string bak)
|
||||
{
|
||||
var bakName = Util.CleanFileName(bak);
|
||||
var fn = Path.GetFileName(path);
|
||||
if (fn == null)
|
||||
return null;
|
||||
return fn.EndsWith(bakName) ? fn.Substring(0, fn.Length - bakName.Length) : fn;
|
||||
}
|
||||
|
||||
private void SetAsBlank()
|
||||
{
|
||||
FileFolder = FilePath = string.Empty;
|
||||
FileName = "Blank Save File";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the suggested file extension when writing to a saved file.
|
||||
/// </summary>
|
||||
public string GetSuggestedExtension()
|
||||
{
|
||||
var sav = SAV;
|
||||
|
@ -74,5 +121,19 @@ namespace PKHeX.Core
|
|||
return ".dsv";
|
||||
return sav.Extension;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets suggested export options for the save file.
|
||||
/// </summary>
|
||||
/// <param name="ext">Selected export extension</param>
|
||||
public ExportFlags GetSuggestedFlags(string? ext = null)
|
||||
{
|
||||
var flags = ExportFlags.None;
|
||||
if (ext == ".dsv")
|
||||
flags |= ExportFlags.IncludeFooter;
|
||||
if (ext == ".gci" || (SAV is IGCSaveFile gc && !gc.IsMemoryCardSave))
|
||||
flags |= ExportFlags.IncludeHeader;
|
||||
return flags;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
32
PKHeX.Core/Saves/SaveFileState.cs
Normal file
32
PKHeX.Core/Saves/SaveFileState.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Tracks information about modifications made to a <see cref="SaveFile"/>
|
||||
/// </summary>
|
||||
public class SaveFileState
|
||||
{
|
||||
/// <summary>
|
||||
/// Mutable value tracking if the save file has been changed. This is set manually by modifications, and not for all modifications.
|
||||
/// </summary>
|
||||
public bool Edited { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Toggle determining if the save file can be exported.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is always true, unless the save file is a "fake" save file with blank data. Blank Save Files are essentially zeroed out buffers.
|
||||
/// </remarks>
|
||||
public readonly bool Exportable;
|
||||
|
||||
/// <summary>
|
||||
/// Original Binary Data the save file was loaded with.
|
||||
/// </summary>
|
||||
public readonly byte[] BAK;
|
||||
|
||||
public SaveFileState(byte[] bak, bool exportable = true)
|
||||
{
|
||||
BAK = bak;
|
||||
Exportable = exportable;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,37 +13,11 @@ namespace PKHeX.Core
|
|||
/// </summary>
|
||||
public static class SaveExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if the <see cref="PKM"/> is compatible with the input <see cref="SaveFile"/>, and makes any necessary modifications to force compatibility.
|
||||
/// </summary>
|
||||
/// <remarks>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.</remarks>
|
||||
/// <param name="sav">Save File target that the PKM will be injected.</param>
|
||||
/// <param name="pk">PKM input that is to be injected into the Save File.</param>
|
||||
/// <returns>Indication whether or not the PKM is compatible.</returns>
|
||||
public static bool IsPKMCompatibleWithModifications(this SaveFile sav, PKM pk) => PKMConverter.IsPKMCompatibleWithModifications(pk, sav);
|
||||
|
||||
/// <summary>
|
||||
/// Gets suggested export options for the save file.
|
||||
/// </summary>
|
||||
/// <param name="sav">SaveFile to be exported</param>
|
||||
/// <param name="ext">Selected export extension</param>
|
||||
public static ExportFlags GetSuggestedFlags(this SaveFile sav, string? ext = null)
|
||||
{
|
||||
var flags = ExportFlags.None;
|
||||
if (ext == ".dsv")
|
||||
flags |= ExportFlags.IncludeFooter;
|
||||
if (ext == ".gci" || (sav is IGCSaveFile gc && !gc.IsMemoryCardSave))
|
||||
flags |= ExportFlags.IncludeHeader;
|
||||
return flags;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks a <see cref="PKM"/> file for compatibility to the <see cref="SaveFile"/>.
|
||||
/// </summary>
|
||||
/// <param name="sav"><see cref="SaveFile"/> that is being checked.</param>
|
||||
/// <param name="pkm"><see cref="PKM"/> that is being tested for compatibility.</param>
|
||||
/// <returns></returns>
|
||||
public static IReadOnlyList<string> IsPKMCompatible(this SaveFile sav, PKM pkm)
|
||||
{
|
||||
return sav.GetSaveFileErrata(pkm, GameInfo.Strings);
|
||||
|
@ -111,6 +85,10 @@ namespace PKHeX.Core
|
|||
while (sav.IsSlotOverwriteProtected(index))
|
||||
++index;
|
||||
|
||||
// The above will return false if out of range. We need to double-check.
|
||||
if (index >= maxCount) // Boxes full!
|
||||
break;
|
||||
|
||||
sav.SetBoxSlotAtIndex(pk, index, noSetb);
|
||||
}
|
||||
else
|
||||
|
@ -180,7 +158,7 @@ namespace PKHeX.Core
|
|||
/// </summary>
|
||||
/// <param name="sav">Save File to fetch a template for</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) => sav.BlankPKM;
|
||||
private static PKM LoadTemplateInternal(this SaveFile sav) => sav.BlankPKM;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a blank file for the save file. If the template path exists, a template load will be attempted.
|
||||
|
@ -188,22 +166,22 @@ namespace PKHeX.Core
|
|||
/// <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)
|
||||
public static PKM LoadTemplate(this SaveFile sav, string? templatePath = null)
|
||||
{
|
||||
if (!Directory.Exists(templatePath))
|
||||
return LoadTemplate(sav);
|
||||
if (templatePath == null || !Directory.Exists(templatePath))
|
||||
return LoadTemplateInternal(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);
|
||||
return LoadTemplateInternal(sav);
|
||||
|
||||
var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(path), prefer: sav.Generation);
|
||||
if (pk == null)
|
||||
return LoadTemplate(sav);
|
||||
return LoadTemplateInternal(sav);
|
||||
|
||||
return PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out _) ?? LoadTemplate(sav);
|
||||
return PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out _) ?? LoadTemplateInternal(sav);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -372,7 +372,7 @@ namespace PKHeX.WinForms
|
|||
private static void ExportSAV(SaveFile sav, string path)
|
||||
{
|
||||
var ext = Path.GetExtension(path).ToLower();
|
||||
var flags = sav.GetSuggestedFlags(ext);
|
||||
var flags = sav.Metadata.GetSuggestedFlags(ext);
|
||||
|
||||
try
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue