2020-12-05 13:36:23 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2020-12-08 03:49:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tracks information about where the <see cref="SAV"/> originated from, and provides logic for saving to a file.
|
|
|
|
|
/// </summary>
|
2020-12-05 13:36:23 +00:00
|
|
|
|
public class SaveFileMetadata
|
|
|
|
|
{
|
|
|
|
|
private readonly SaveFile SAV;
|
|
|
|
|
|
2020-12-08 03:49:04 +00:00
|
|
|
|
/// <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; }
|
|
|
|
|
|
2020-12-05 13:36:23 +00:00
|
|
|
|
private byte[] Footer = Array.Empty<byte>(); // .dsv
|
|
|
|
|
private byte[] Header = Array.Empty<byte>(); // .gci
|
|
|
|
|
|
2020-12-08 03:49:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Simple summary of the save file, to help differentiate it from other save files with the same filename.
|
|
|
|
|
/// </summary>
|
2020-12-05 13:36:23 +00:00
|
|
|
|
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;
|
2020-12-08 03:49:04 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// File Dialog filter to help save the file.
|
|
|
|
|
/// </summary>
|
2020-12-05 13:36:23 +00:00
|
|
|
|
public string Filter => $"{SAV.GetType().Name}|{GetSuggestedExtension()}|All Files|*.*";
|
|
|
|
|
|
2020-12-08 03:49:04 +00:00
|
|
|
|
/// <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>
|
2020-12-05 13:36:23 +00:00
|
|
|
|
public byte[] Finalize(byte[] data, ExportFlags flags)
|
|
|
|
|
{
|
|
|
|
|
if (Footer.Length > 0 && flags.HasFlagFast(ExportFlags.IncludeFooter))
|
|
|
|
|
return data.Concat(Footer).ToArray();
|
|
|
|
|
if (Header.Length > 0 && flags.HasFlagFast(ExportFlags.IncludeHeader))
|
|
|
|
|
return Header.Concat(data).ToArray();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the details of any trimmed header and footer arrays to a <see cref="SaveFile"/> object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetExtraInfo(byte[] header, byte[] footer)
|
|
|
|
|
{
|
|
|
|
|
Header = header;
|
|
|
|
|
Footer = footer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the details of a path to a <see cref="SaveFile"/> object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Full Path of the file</param>
|
|
|
|
|
public void SetExtraInfo(string path)
|
|
|
|
|
{
|
|
|
|
|
var sav = SAV;
|
2020-12-08 03:49:04 +00:00
|
|
|
|
if (!sav.State.Exportable || string.IsNullOrWhiteSpace(path)) // Blank save file
|
2020-12-05 13:36:23 +00:00
|
|
|
|
{
|
2020-12-08 03:49:04 +00:00
|
|
|
|
sav.Metadata.SetAsBlank();
|
2020-12-05 13:36:23 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 03:49:04 +00:00
|
|
|
|
SetAsLoadedFile(path);
|
2020-12-05 13:36:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 03:49:04 +00:00
|
|
|
|
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>
|
2020-12-05 13:36:23 +00:00
|
|
|
|
public string GetSuggestedExtension()
|
|
|
|
|
{
|
|
|
|
|
var sav = SAV;
|
|
|
|
|
var fn = sav.Metadata.FileName;
|
|
|
|
|
if (fn != null)
|
|
|
|
|
return Path.GetExtension(fn);
|
|
|
|
|
|
|
|
|
|
if ((sav.Generation == 4 || sav.Generation == 5) && sav.Metadata.HasFooter)
|
|
|
|
|
return ".dsv";
|
|
|
|
|
return sav.Extension;
|
|
|
|
|
}
|
2020-12-08 03:49:04 +00:00
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2020-12-05 13:36:23 +00:00
|
|
|
|
}
|
2020-12-08 03:49:04 +00:00
|
|
|
|
}
|