2022-11-25 01:42:17 +00:00
|
|
|
using System;
|
2018-08-18 06:23:44 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
2021-01-17 08:05:07 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-08-18 06:23:44 +00:00
|
|
|
using System.IO;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logic for detecting supported binary object formats.
|
|
|
|
/// </summary>
|
|
|
|
public static class FileUtil
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Attempts to get a binary object from the provided path.
|
2018-08-18 06:23:44 +00:00
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="path"></param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <param name="reference">Reference SaveFile used for PC Binary compatibility checks.</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <returns>Supported file object reference, null if none found.</returns>
|
|
|
|
public static object? GetSupportedFile(string path, SaveFile? reference = null)
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
try
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var fi = new FileInfo(path);
|
|
|
|
if (!fi.Exists || IsFileTooBig(fi.Length) || IsFileTooSmall(fi.Length))
|
2018-08-18 06:23:44 +00:00
|
|
|
return null;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var data = File.ReadAllBytes(path);
|
2023-07-14 05:18:34 +00:00
|
|
|
var ext = Path.GetExtension(path.AsSpan());
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetSupportedFile(data, ext, reference);
|
|
|
|
}
|
|
|
|
// User input data can be fuzzed; if anything blows up, just fail safely.
|
|
|
|
catch (Exception e)
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Debug.WriteLine(MessageStrings.MsgFileInUse);
|
|
|
|
Debug.WriteLine(e.Message);
|
2018-08-18 06:23:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Attempts to get a binary object from the provided inputs.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data for the file.</param>
|
|
|
|
/// <param name="ext">File extension used as a hint.</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <param name="reference">Reference SaveFile used for PC Binary compatibility checks.</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <returns>Supported file object reference, null if none found.</returns>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static object? GetSupportedFile(byte[] data, ReadOnlySpan<char> ext, SaveFile? reference = null)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (TryGetSAV(data, out var sav))
|
|
|
|
return sav;
|
|
|
|
if (TryGetMemoryCard(data, out var mc))
|
|
|
|
return mc;
|
|
|
|
if (TryGetPKM(data, out var pk, ext))
|
|
|
|
return pk;
|
2024-05-01 05:49:43 +00:00
|
|
|
if (TryGetPCBoxBin(data, out var concat, reference))
|
|
|
|
return concat;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (TryGetBattleVideo(data, out var bv))
|
|
|
|
return bv;
|
|
|
|
if (TryGetMysteryGift(data, out var g, ext))
|
|
|
|
return g;
|
|
|
|
if (TryGetGP1(data, out var gp))
|
|
|
|
return gp;
|
|
|
|
if (TryGetBundle(data, out var bundle))
|
|
|
|
return bundle;
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-19 05:11:13 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsFileLocked(string path)
|
|
|
|
{
|
|
|
|
try { return (File.GetAttributes(path) & FileAttributes.ReadOnly) != 0; }
|
|
|
|
catch { return true; }
|
|
|
|
}
|
2020-09-19 05:11:13 +00:00
|
|
|
|
2023-05-01 23:51:17 +00:00
|
|
|
public static long GetFileSize(string path)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
try
|
2018-11-22 04:48:55 +00:00
|
|
|
{
|
2023-05-01 23:51:17 +00:00
|
|
|
var fi = new FileInfo(path);
|
|
|
|
var size = fi.Length;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (size > int.MaxValue)
|
|
|
|
return -1;
|
2023-05-01 23:51:17 +00:00
|
|
|
return size;
|
2018-11-22 04:48:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
catch { return -1; } // Bad File / Locked
|
|
|
|
}
|
2018-11-22 04:48:55 +00:00
|
|
|
|
2023-05-01 23:51:17 +00:00
|
|
|
public static IEnumerable<T> IterateSafe<T>(this IEnumerable<T> source, int failOut = 10, Action<Exception>? log = null)
|
|
|
|
{
|
|
|
|
using var enumerator = source.GetEnumerator();
|
|
|
|
int ctr = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var next = enumerator.MoveNext();
|
|
|
|
if (!next)
|
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
log?.Invoke(ex);
|
|
|
|
if (++ctr >= failOut)
|
|
|
|
yield break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ctr = 0;
|
|
|
|
yield return enumerator.Current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool TryGetGP1(byte[] data, [NotNullWhen(true)] out GP1? gp1)
|
|
|
|
{
|
|
|
|
gp1 = null;
|
|
|
|
if (data.Length != GP1.SIZE || ReadUInt32LittleEndian(data.AsSpan(0x28)) == 0)
|
2020-11-04 04:12:42 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
gp1 = new GP1(data);
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 04:12:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool TryGetBundle(byte[] data, [NotNullWhen(true)] out IPokeGroup? result)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
if (RentalTeam8.IsRentalTeam(data))
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
result = new RentalTeam8(data);
|
2018-08-18 06:23:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-25 01:42:17 +00:00
|
|
|
if (RentalTeam9.IsRentalTeam(data))
|
|
|
|
{
|
|
|
|
result = new RentalTeam9(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (RentalTeamSet9.IsRentalTeamSet(data))
|
|
|
|
{
|
|
|
|
result = new RentalTeamSet9(data);
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the length is too big to be a detectable file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="length">File size</param>
|
|
|
|
public static bool IsFileTooBig(long length)
|
|
|
|
{
|
|
|
|
if (length <= 0x10_0000) // 1 MB
|
|
|
|
return false;
|
|
|
|
if (length > int.MaxValue)
|
|
|
|
return true;
|
|
|
|
if (SaveUtil.IsSizeValid((int)length))
|
|
|
|
return false;
|
|
|
|
if (SAV3GCMemoryCard.IsMemoryCardSize(length))
|
|
|
|
return false; // pbr/GC have size > 1MB
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the length is too small to be a detectable file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="length">File size</param>
|
|
|
|
public static bool IsFileTooSmall(long length) => length < 0x20; // bigger than PK1
|
|
|
|
|
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Tries to get a <see cref="SaveFile"/> object from the input parameters.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data</param>
|
|
|
|
/// <param name="sav">Output result</param>
|
|
|
|
/// <returns>True if file object reference is valid, false if none found.</returns>
|
|
|
|
public static bool TryGetSAV(byte[] data, [NotNullWhen(true)] out SaveFile? sav)
|
|
|
|
{
|
|
|
|
sav = SaveUtil.GetVariantSAV(data);
|
|
|
|
return sav != null;
|
|
|
|
}
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Tries to get a <see cref="SAV3GCMemoryCard"/> object from the input parameters.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data</param>
|
|
|
|
/// <param name="memcard">Output result</param>
|
|
|
|
/// <returns>True if file object reference is valid, false if none found.</returns>
|
|
|
|
public static bool TryGetMemoryCard(byte[] data, [NotNullWhen(true)] out SAV3GCMemoryCard? memcard)
|
|
|
|
{
|
2024-03-19 00:05:22 +00:00
|
|
|
if (!SAV3GCMemoryCard.IsMemoryCardSize(data) || IsNoDataPresent(data))
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
memcard = null;
|
|
|
|
return false;
|
2018-08-18 06:23:44 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
memcard = new SAV3GCMemoryCard(data);
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2023-05-01 23:51:17 +00:00
|
|
|
/// <inheritdoc cref="TryGetMemoryCard(byte[], out SAV3GCMemoryCard?)"/>
|
|
|
|
public static bool TryGetMemoryCard(string file, [NotNullWhen(true)] out SAV3GCMemoryCard? memcard)
|
|
|
|
{
|
|
|
|
if (!File.Exists(file))
|
2023-05-08 07:03:45 +00:00
|
|
|
{
|
|
|
|
memcard = null;
|
2023-05-01 23:51:17 +00:00
|
|
|
return false;
|
2023-05-08 07:03:45 +00:00
|
|
|
}
|
2023-05-01 23:51:17 +00:00
|
|
|
var data = File.ReadAllBytes(file);
|
|
|
|
return TryGetMemoryCard(data, out memcard);
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Tries to get a <see cref="PKM"/> object from the input parameters.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data</param>
|
|
|
|
/// <param name="pk">Output result</param>
|
|
|
|
/// <param name="ext">Format hint</param>
|
|
|
|
/// <param name="sav">Reference savefile used for PC Binary compatibility checks.</param>
|
|
|
|
/// <returns>True if file object reference is valid, false if none found.</returns>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static bool TryGetPKM(byte[] data, [NotNullWhen(true)] out PKM? pk, ReadOnlySpan<char> ext, ITrainerInfo? sav = null)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (ext == ".pgt") // size collision with pk6
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pk = null;
|
|
|
|
return false;
|
2018-08-18 06:23:44 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
var format = EntityFileExtension.GetContextFromExtension(ext, sav?.Context ?? EntityContext.Gen6);
|
|
|
|
pk = EntityFormat.GetFromBytes(data, prefer: format);
|
|
|
|
return pk != null;
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Tries to get a <see cref="IEnumerable{T}"/> object from the input parameters.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data</param>
|
2024-05-01 05:49:43 +00:00
|
|
|
/// <param name="result">Output result</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <param name="sav">Reference SaveFile used for PC Binary compatibility checks.</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <returns>True if file object reference is valid, false if none found.</returns>
|
2024-05-01 05:49:43 +00:00
|
|
|
public static bool TryGetPCBoxBin(byte[] data, [NotNullWhen(true)] out ConcatenatedEntitySet? result, SaveFile? sav)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-05-01 05:49:43 +00:00
|
|
|
result = null;
|
|
|
|
if (sav is null || IsNoDataPresent(data))
|
2018-08-18 06:23:44 +00:00
|
|
|
return false;
|
2024-05-01 05:49:43 +00:00
|
|
|
|
|
|
|
// Only return if the size is one of the save file's data chunk formats.
|
|
|
|
var expect = sav.SIZE_BOXSLOT;
|
|
|
|
|
|
|
|
// Check if it's the entire PC data.
|
|
|
|
var countPC = sav.SlotCount;
|
|
|
|
if (expect * countPC == data.Length)
|
|
|
|
{
|
|
|
|
result = new(data, countPC);
|
|
|
|
return true;
|
2018-08-18 06:23:44 +00:00
|
|
|
}
|
2024-05-01 05:49:43 +00:00
|
|
|
|
|
|
|
// Check if it's a single box data.
|
|
|
|
var countBox = sav.BoxSlotCount;
|
|
|
|
if (expect * countBox == data.Length)
|
2018-08-18 06:23:44 +00:00
|
|
|
{
|
2024-05-01 05:49:43 +00:00
|
|
|
result = new(data, countBox);
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
2018-08-18 06:23:44 +00:00
|
|
|
}
|
2024-05-01 05:49:43 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-18 06:23:44 +00:00
|
|
|
|
2024-03-19 00:05:22 +00:00
|
|
|
private static bool IsNoDataPresent(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
if (!data.ContainsAnyExcept<byte>(0xFF))
|
|
|
|
return true;
|
|
|
|
if (!data.ContainsAnyExcept<byte>(0x00))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Tries to get a <see cref="BattleVideo"/> object from the input parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data</param>
|
|
|
|
/// <param name="bv">Output result</param>
|
|
|
|
/// <returns>True if file object reference is valid, false if none found.</returns>
|
2024-03-07 06:34:21 +00:00
|
|
|
public static bool TryGetBattleVideo(byte[] data, [NotNullWhen(true)] out IBattleVideo? bv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
bv = BattleVideo.GetVariantBattleVideo(data);
|
|
|
|
return bv != null;
|
|
|
|
}
|
2018-09-16 19:44:00 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Tries to get a <see cref="MysteryGift"/> object from the input parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Binary data</param>
|
|
|
|
/// <param name="mg">Output result</param>
|
|
|
|
/// <param name="ext">Format hint</param>
|
|
|
|
/// <returns>True if file object reference is valid, false if none found.</returns>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static bool TryGetMysteryGift(byte[] data, [NotNullWhen(true)] out MysteryGift? mg, ReadOnlySpan<char> ext)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-05-04 19:48:19 +00:00
|
|
|
mg = ext.Length == 0
|
|
|
|
? MysteryGift.GetMysteryGift(data)
|
|
|
|
: MysteryGift.GetMysteryGift(data, ext);
|
2022-06-18 18:04:24 +00:00
|
|
|
return mg != null;
|
|
|
|
}
|
2018-09-16 19:44:00 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a Temp location File Name for the <see cref="PKM"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pk">Data to be exported</param>
|
|
|
|
/// <param name="encrypt">Data is to be encrypted</param>
|
|
|
|
/// <returns>Path to temporary file location to write to.</returns>
|
|
|
|
public static string GetPKMTempFileName(PKM pk, bool encrypt)
|
|
|
|
{
|
|
|
|
string fn = pk.FileNameWithoutExtension;
|
|
|
|
string filename = fn + (encrypt ? $".ek{pk.Format}" : $".{pk.Extension}");
|
2018-09-16 19:44:00 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a <see cref="PKM"/> from the provided <see cref="file"/> path, which is to be loaded to the <see cref="SaveFile"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="file"><see cref="PKM"/> or <see cref="MysteryGift"/> file path.</param>
|
|
|
|
/// <param name="sav">Generation Info</param>
|
|
|
|
/// <returns>New <see cref="PKM"/> reference from the file.</returns>
|
|
|
|
public static PKM? GetSingleFromPath(string file, ITrainerInfo sav)
|
|
|
|
{
|
|
|
|
var fi = new FileInfo(file);
|
|
|
|
if (!fi.Exists)
|
|
|
|
return null;
|
|
|
|
if (fi.Length == GP1.SIZE && TryGetGP1(File.ReadAllBytes(file), out var gp1))
|
2023-08-12 23:01:16 +00:00
|
|
|
return gp1.ConvertToPKM(sav);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!EntityDetection.IsSizePlausible(fi.Length) && !MysteryGift.IsMysteryGift(fi.Length))
|
|
|
|
return null;
|
|
|
|
var data = File.ReadAllBytes(file);
|
|
|
|
var ext = fi.Extension;
|
|
|
|
var mg = MysteryGift.GetMysteryGift(data, ext);
|
|
|
|
var gift = mg?.ConvertToPKM(sav);
|
|
|
|
if (gift != null)
|
|
|
|
return gift;
|
|
|
|
_ = TryGetPKM(data, out var pk, ext, sav);
|
|
|
|
return pk;
|
2018-08-18 06:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-01 05:49:43 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a set of concatenated <see cref="PKM"/> data.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="Data">Object data</param>
|
|
|
|
/// <param name="Count">Count of objects</param>
|
|
|
|
public sealed record ConcatenatedEntitySet(Memory<byte> Data, int Count)
|
|
|
|
{
|
|
|
|
public int SlotSize => Data.Length / Count;
|
|
|
|
|
|
|
|
public Span<byte> GetSlot(int index)
|
|
|
|
{
|
|
|
|
var size = SlotSize;
|
|
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)size);
|
|
|
|
|
|
|
|
var offset = index * size;
|
|
|
|
return Data.Span.Slice(offset, size);
|
|
|
|
}
|
|
|
|
}
|