Extract extension->prefer format method

fails for 'pkx' (returned 8 instead of 6); establish standard behavior
This commit is contained in:
Kurt
2018-04-21 14:38:18 -07:00
parent 7a245278ea
commit e91cb806e5
6 changed files with 34 additions and 7 deletions

View File

@@ -823,6 +823,31 @@ public static string[] GetPKMExtensions(int MaxGeneration = Generation)
return result.ToArray();
}
/// <summary>
/// Roughly detects the PKM format from the file's extension.
/// </summary>
/// <param name="ext">File extension.</param>
/// <param name="prefer">Preference if not a valid extension, usually the highest acceptable format.</param>
/// <returns>Format hint that the file is.</returns>
public static int GetPKMFormatFromExtension(string ext, int prefer)
{
return ext?.Length > 1
? GetPKMFormatFromExtension(ext[ext.Length - 1], prefer)
: prefer;
}
/// <summary>
/// Roughly detects the PKM format from the file's extension.
/// </summary>
/// <param name="last">Last character of the file's extensio.n</param>
/// <param name="prefer">Preference if not a valid extension, usually the highest acceptable format.</param>
/// <returns>Format hint that the file is.</returns>
public static int GetPKMFormatFromExtension(char last, int prefer)
{
if ('1' <= last && last <= '9')
return last - '0';
return last == 'x' ? 6 : prefer;
}
// Extensions
/// <summary>
/// Gets the Location Name for the <see cref="PKM"/>

View File

@@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using PKHeX.Core;
@@ -235,8 +234,9 @@ private bool TryLoadFiles(string[] files, DragEventArgs e, bool noEgg)
byte[] data = File.ReadAllBytes(file);
MysteryGift mg = MysteryGift.GetMysteryGift(data, fi.Extension);
PKM temp = mg?.ConvertToPKM(SAV) ?? PKMConverter.GetPKMfromBytes(data,
prefer: fi.Extension.Length > 0 ? (fi.Extension.Last() - '0') & 0xF : SAV.Generation);
if (fi.Extension.Length > 0 || !int.TryParse(fi.Extension[fi.Extension.Length - 1].ToString(), out var prefer))
prefer = SAV.Generation;
PKM temp = mg?.ConvertToPKM(SAV) ?? PKMConverter.GetPKMfromBytes(data, prefer: prefer);
PKM pk = PKMConverter.ConvertToType(temp, SAV.PKMType, out string c);
if (pk == null)

View File

@@ -576,7 +576,7 @@ private bool TryLoadMemoryCard(byte[] input, string path)
}
private bool TryLoadPKM(byte[] input, string ext)
{
var pk = PKMConverter.GetPKMfromBytes(input, prefer: ext.Length > 0 ? (ext.Last() - '0') & 0xF : C_SAV.SAV.Generation);
var pk = PKMConverter.GetPKMfromBytes(input, prefer: PKX.GetPKMFormatFromExtension(ext, C_SAV.SAV.Generation));
if (pk == null)
return false;

View File

@@ -276,7 +276,7 @@ private void ProcessFolder(IReadOnlyList<string> files, IList<StringInstruction>
continue;
}
int format = fi.Extension.Length > 0 ? (fi.Extension.Last() - '0') & 0xF : SAV.Generation;
int format = PKX.GetPKMFormatFromExtension(fi.Extension, SAV.Generation);
byte[] data = File.ReadAllBytes(file);
var pkm = PKMConverter.GetPKMfromBytes(data, prefer: format);
if (ProcessPKM(pkm, Filters, Instructions))

View File

@@ -338,7 +338,9 @@ private void LoadDatabase()
{
FileInfo fi = new FileInfo(file);
if (!fi.Extension.Contains(".pk") || !PKX.IsPKM(fi.Length)) return;
var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(file), file, prefer: (fi.Extension.Last() - '0') & 0xF);
var data = File.ReadAllBytes(file);
var prefer = PKX.GetPKMFormatFromExtension(fi.Extension, SAV.Generation);
var pk = PKMConverter.GetPKMfromBytes(data, file, prefer);
if (pk != null)
dbTemp.Add(pk);
});

View File

@@ -49,7 +49,7 @@ private static void VerifyAll(string folder, string name, bool IsValid)
Assert.IsTrue(PKX.IsPKM(fi.Length), $"Invalid file in {fi.Directory.Name} folder.");
var data = File.ReadAllBytes(file);
var format = file[file.Length - 1] - '0';
var format = PKX.GetPKMFormatFromExtension(file[file.Length - 1], -1);
if (format > 10)
format = 6;
var pkm = PKMConverter.GetPKMfromBytes(data, prefer: format);