PKHeX/PKHeX.Core/Saves/SaveFileMetadata.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

135 lines
4.4 KiB
C#

using System;
using System.IO;
namespace PKHeX.Core;
/// <summary>
/// Tracks information about where the <see cref="SAV"/> originated from, and provides logic for saving to a file.
/// </summary>
public sealed record SaveFileMetadata(SaveFile SAV)
{
public SaveFile SAV { private get; init; } = SAV;
/// <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
private string BAKSuffix => $" [{SAV.ShortSummary}].bak";
/// <summary>
/// Simple summary of the save file, to help differentiate it from other save files with the same filename.
/// </summary>
public string BAKName => FileName + BAKSuffix;
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="setting">Toggle flags </param>
/// <returns>Final save file data.</returns>
public byte[] Finalize(byte[] data, BinaryExportSetting setting)
{
if (Footer.Length > 0 && setting.HasFlag(BinaryExportSetting.IncludeFooter))
return ArrayUtil.ConcatAll(data, Footer);
if (Header.Length > 0 && setting.HasFlag(BinaryExportSetting.IncludeHeader))
return ArrayUtil.ConcatAll(Header, data);
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;
if (!sav.State.Exportable || string.IsNullOrWhiteSpace(path)) // Blank save file
{
sav.Metadata.SetAsBlank();
return;
}
SetAsLoadedFile(path);
}
private void SetAsLoadedFile(string path)
{
FilePath = path;
FileFolder = Path.GetDirectoryName(path);
FileName = GetFileName(path, BAKSuffix);
}
private static string GetFileName(string path, string bak)
{
var bakName = Util.CleanFileName(bak);
var fn = Path.GetFileName(path);
return fn.EndsWith(bakName, StringComparison.Ordinal) ? fn[..^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;
var fn = sav.Metadata.FileName;
if (fn != null)
return Path.GetExtension(fn);
if ((sav.Generation is 4 or 5) && sav.Metadata.HasFooter)
return ".dsv";
return sav.Extension;
}
/// <summary>
/// Gets suggested export options for the save file.
/// </summary>
/// <param name="ext">Selected export extension</param>
public BinaryExportSetting GetSuggestedFlags(string? ext = null)
{
var flags = BinaryExportSetting.None;
if (ext == ".dsv")
flags |= BinaryExportSetting.IncludeFooter;
if (ext == ".gci" || SAV is IGCSaveFile {MemoryCard: null})
flags |= BinaryExportSetting.IncludeHeader;
return flags;
}
}