mirror of
https://github.com/kwsch/PKHeX
synced 2025-02-16 21:38:40 +00:00
Rework recent savefile detection
return a reference to the savefile rather than the path, since a SaveFile has a FilePath property.
This commit is contained in:
parent
f5bed410b4
commit
99673706a0
4 changed files with 72 additions and 40 deletions
|
@ -416,6 +416,15 @@ namespace PKHeX.Core
|
|||
|
||||
private static bool GetIsBank7(byte[] data) => data.Length == SIZE_G7BANK && data[0] != 0;
|
||||
|
||||
public static SaveFile GetVariantSAV(string path)
|
||||
{
|
||||
var data = File.ReadAllBytes(path);
|
||||
var sav = GetVariantSAV(data);
|
||||
if (sav != null)
|
||||
sav.FilePath = path;
|
||||
return sav;
|
||||
}
|
||||
|
||||
/// <summary>Creates an instance of a SaveFile using the given save data.</summary>
|
||||
/// <param name="data">Save data from which to create a SaveFile.</param>
|
||||
/// <returns>An appropriate type of save file for the given data, or null if the save data is invalid.</returns>
|
||||
|
@ -567,7 +576,7 @@ namespace PKHeX.Core
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the full path of the most recent file based on <see cref="FileInfo.LastWriteTime"/>.
|
||||
/// Retrieves possible save file paths from the provided <see cref="folderPath"/>.
|
||||
/// </summary>
|
||||
/// <param name="folderPath">Folder to look within</param>
|
||||
/// <param name="deep">Search all subfolders</param>
|
||||
|
|
|
@ -1164,9 +1164,13 @@ namespace PKHeX.WinForms
|
|||
string pathCache = CyberGadgetUtil.GetCacheFolder();
|
||||
if (Directory.Exists(pathCache))
|
||||
cgse = Path.Combine(pathCache);
|
||||
if (!PathUtilWindows.DetectSaveFile(out path, cgse))
|
||||
return false;
|
||||
|
||||
string msg = null;
|
||||
var sav = PathUtilWindows.DetectSaveFile(ref msg, cgse);
|
||||
if (sav == null && !string.IsNullOrWhiteSpace(msg))
|
||||
WinFormsUtil.Error(msg);
|
||||
|
||||
path = sav?.FileName;
|
||||
return path != null && File.Exists(path);
|
||||
}
|
||||
|
||||
|
|
|
@ -71,10 +71,42 @@ namespace PKHeX.WinForms
|
|||
/// <summary>
|
||||
/// Finds a compatible save file that was most recently saved (by file write time).
|
||||
/// </summary>
|
||||
/// <param name="path">If this function returns true, full path of a save file or null if no path could be found. If this function returns false, this parameter will be set to the error message.</param>
|
||||
/// <param name="error">If this function does not return a save file, this parameter will be set to the error message.</param>
|
||||
/// <param name="extra">Paths to check in addition to the default paths</param>
|
||||
/// <returns>A boolean indicating whether or not a file was detected</returns>
|
||||
public static bool DetectSaveFile(out string path, params string[] extra)
|
||||
/// <returns>Reference to a valid save file, if any.</returns>
|
||||
public static SaveFile DetectSaveFile(ref string error, params string[] extra)
|
||||
{
|
||||
var foldersToCheck = GetFoldersToCheck(extra);
|
||||
var result = GetSaveFilePathsFromFolders(foldersToCheck, out var possiblePaths);
|
||||
if (!result)
|
||||
{
|
||||
error = string.Join(Environment.NewLine, possiblePaths); // `possiblePaths` contains the error message
|
||||
return null;
|
||||
}
|
||||
|
||||
// return newest save file path that is valid
|
||||
var byMostRecent = possiblePaths.OrderByDescending(f => new FileInfo(f).LastWriteTime);
|
||||
var saves = byMostRecent.Select(SaveUtil.GetVariantSAV);
|
||||
return saves.FirstOrDefault(z => z?.ChecksumsValid == true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all detectable save files ordered by most recently saved (by file write time).
|
||||
/// </summary>
|
||||
/// <param name="extra">Paths to check in addition to the default paths</param>
|
||||
/// <returns>Valid save files, if any.</returns>
|
||||
public static IEnumerable<SaveFile> GetSaveFiles(params string[] extra)
|
||||
{
|
||||
var foldersToCheck = GetFoldersToCheck(extra);
|
||||
var result = GetSaveFilePathsFromFolders(foldersToCheck, out var possiblePaths);
|
||||
if (!result)
|
||||
return Enumerable.Empty<SaveFile>();
|
||||
|
||||
var byMostRecent = possiblePaths.OrderByDescending(f => new FileInfo(f).LastWriteTime);
|
||||
return byMostRecent.Select(SaveUtil.GetVariantSAV);
|
||||
}
|
||||
|
||||
private static IEnumerable<string> GetFoldersToCheck(IEnumerable<string> extra)
|
||||
{
|
||||
var foldersToCheck = extra.Where(f => f?.Length > 0);
|
||||
|
||||
|
@ -86,41 +118,25 @@ namespace PKHeX.WinForms
|
|||
if (pathNX != null) // check for Homebrew/CFW backups
|
||||
foldersToCheck = foldersToCheck.Concat(GetSwitchBackupPaths(pathNX));
|
||||
|
||||
path = null;
|
||||
List<string> possiblePaths = new List<string>();
|
||||
return foldersToCheck;
|
||||
}
|
||||
|
||||
private static bool GetSaveFilePathsFromFolders(IEnumerable<string> foldersToCheck, out IEnumerable<string> possible)
|
||||
{
|
||||
var possiblePaths = new List<string>();
|
||||
foreach (var folder in foldersToCheck)
|
||||
{
|
||||
if (!SaveUtil.GetSavesFromFolder(folder, true, out IEnumerable<string> files))
|
||||
{
|
||||
if (files != null) // can be null if folder doesn't exist
|
||||
{
|
||||
path = string.Join(Environment.NewLine, files); // `files` contains the error message
|
||||
return false;
|
||||
}
|
||||
if (files == null)
|
||||
continue;
|
||||
possible = files;
|
||||
return false;
|
||||
}
|
||||
if (files != null)
|
||||
possiblePaths.AddRange(files);
|
||||
}
|
||||
|
||||
// return newest save file path that is valid
|
||||
foreach (var file in possiblePaths.OrderByDescending(f => new FileInfo(f).LastWriteTime))
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = File.ReadAllBytes(file);
|
||||
var sav = SaveUtil.GetVariantSAV(data);
|
||||
if (sav?.ChecksumsValid != true)
|
||||
continue;
|
||||
|
||||
path = file;
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
path = e.Message + Environment.NewLine + file;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
possible = possiblePaths;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -161,17 +161,20 @@ namespace PKHeX.WinForms
|
|||
string pathCache = CyberGadgetUtil.GetCacheFolder();
|
||||
if (Directory.Exists(pathCache))
|
||||
cgse = Path.Combine(pathCache);
|
||||
if (!PathUtilWindows.DetectSaveFile(out path, cgse) && !string.IsNullOrEmpty(path))
|
||||
{
|
||||
Error(path); // `path` contains the error message
|
||||
path = null;
|
||||
}
|
||||
|
||||
if (path != null)
|
||||
ofd.FileName = path;
|
||||
string msg = null;
|
||||
var sav = PathUtilWindows.DetectSaveFile(ref msg, cgse);
|
||||
if (sav == null && !string.IsNullOrWhiteSpace(msg))
|
||||
Error(msg);
|
||||
|
||||
if (sav != null)
|
||||
ofd.FileName = sav.FileName;
|
||||
|
||||
if (ofd.ShowDialog() != DialogResult.OK)
|
||||
{
|
||||
path = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
path = ofd.FileName;
|
||||
return true;
|
||||
|
|
Loading…
Add table
Reference in a new issue