2017-09-16 18:38:58 +00:00
|
|
|
|
using System;
|
2017-05-12 04:34:18 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2018-07-21 17:23:15 +00:00
|
|
|
|
namespace PKHeX.Core
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static class SaveDetection
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2017-09-16 18:38:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the 3DS's root folder, usually from an inserted SD card.
|
|
|
|
|
/// </summary>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
/// <param name="drives">List of drives on the host machine.</param>
|
2017-09-16 18:38:58 +00:00
|
|
|
|
/// <param name="skipFirstDrive">Optional parameter to skip the first drive.
|
|
|
|
|
/// The first drive is usually the system hard drive, or can be a floppy disk drive (slower to check, never has expected data).</param>
|
|
|
|
|
/// <returns>Folder path pointing to the Nintendo 3DS folder.</returns>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static string Get3DSLocation(IEnumerable<string> drives, bool skipFirstDrive = true) =>
|
|
|
|
|
FindConsoleRootFolder(drives, "Nintendo 3DS", skipFirstDrive);
|
2018-05-22 01:20:08 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Switch's root folder, usually from an inserted SD card.
|
|
|
|
|
/// </summary>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
/// <param name="drives">List of drives on the host machine.</param>
|
2018-05-22 01:20:08 +00:00
|
|
|
|
/// <param name="skipFirstDrive">Optional parameter to skip the first drive.
|
|
|
|
|
/// The first drive is usually the system hard drive, or can be a floppy disk drive (slower to check, never has expected data).</param>
|
|
|
|
|
/// <returns>Folder path pointing to the Nintendo folder.</returns>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static string GetSwitchLocation(IEnumerable<string> drives, bool skipFirstDrive = true) =>
|
|
|
|
|
FindConsoleRootFolder(drives, "Nintendo", skipFirstDrive);
|
2018-05-22 01:20:08 +00:00
|
|
|
|
|
2018-07-21 17:23:15 +00:00
|
|
|
|
private static string FindConsoleRootFolder(IEnumerable<string> drives, string path, bool skipFirstDrive)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2018-07-21 17:23:15 +00:00
|
|
|
|
if (skipFirstDrive)
|
|
|
|
|
drives = drives.Skip(1);
|
|
|
|
|
|
|
|
|
|
var paths = drives.Select(drive => Path.Combine(drive, path));
|
|
|
|
|
return paths.FirstOrDefault(Directory.Exists);
|
2017-05-12 04:34:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 23:27:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of 3DS save backup paths for the storage device.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="root">Root location of device</param>
|
|
|
|
|
/// <returns>List of possible 3DS save backup paths.</returns>
|
2017-09-16 18:38:58 +00:00
|
|
|
|
public static IEnumerable<string> Get3DSBackupPaths(string root)
|
2017-05-16 23:27:43 +00:00
|
|
|
|
{
|
2017-09-16 18:38:58 +00:00
|
|
|
|
yield return Path.Combine(root, "saveDataBackup");
|
|
|
|
|
yield return Path.Combine(root, "filer", "UserSaveData");
|
|
|
|
|
yield return Path.Combine(root, "JKSV", "Saves");
|
|
|
|
|
yield return Path.Combine(root, "TWLSaveTool");
|
|
|
|
|
yield return Path.Combine(root, "fbi", "save");
|
|
|
|
|
yield return Path.Combine(root, "gm9", "out");
|
2017-10-05 16:40:41 +00:00
|
|
|
|
yield return Path.Combine(root, "3ds", "Checkpoint", "saves");
|
2017-05-16 23:27:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 01:20:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of Switch save backup paths for the storage device.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="root">Root location of device</param>
|
|
|
|
|
/// <returns>List of possible 3DS save backup paths.</returns>
|
|
|
|
|
public static IEnumerable<string> GetSwitchBackupPaths(string root)
|
|
|
|
|
{
|
|
|
|
|
yield return Path.Combine(root, "switch", "Checkpoint", "saves");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 04:34:18 +00:00
|
|
|
|
/// <summary>
|
2017-09-16 18:38:58 +00:00
|
|
|
|
/// Finds a compatible save file that was most recently saved (by file write time).
|
2017-05-12 04:34:18 +00:00
|
|
|
|
/// </summary>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
/// <param name="drives">List of drives on the host machine.</param>
|
2018-07-15 20:35:58 +00:00
|
|
|
|
/// <param name="error">If this function does not return a save file, this parameter will be set to the error message.</param>
|
2017-06-21 01:38:33 +00:00
|
|
|
|
/// <param name="extra">Paths to check in addition to the default paths</param>
|
2018-07-15 20:35:58 +00:00
|
|
|
|
/// <returns>Reference to a valid save file, if any.</returns>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static SaveFile DetectSaveFile(IReadOnlyList<string> drives, ref string error, params string[] extra)
|
2018-07-15 20:35:58 +00:00
|
|
|
|
{
|
2018-07-21 17:23:15 +00:00
|
|
|
|
var foldersToCheck = GetFoldersToCheck(drives, extra);
|
2018-07-15 20:35:58 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 00:48:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all detectable save files ordered by most recently saved (by file write time).
|
|
|
|
|
/// </summary>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
/// <param name="drives">List of drives on the host machine.</param>
|
2018-07-19 04:26:18 +00:00
|
|
|
|
/// <param name="detect">Detect save files stored in common SD card homebrew locations.</param>
|
2018-07-16 00:48:31 +00:00
|
|
|
|
/// <param name="extra">Paths to check in addition to the default paths</param>
|
|
|
|
|
/// <returns>Valid save files, if any.</returns>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static IEnumerable<SaveFile> GetSaveFiles(IReadOnlyList<string> drives, bool detect, params string[] extra) => GetSaveFiles(drives, detect, (IEnumerable<string>)extra);
|
2018-07-16 00:48:31 +00:00
|
|
|
|
|
2018-07-15 20:35:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all detectable save files ordered by most recently saved (by file write time).
|
|
|
|
|
/// </summary>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
/// <param name="drives">List of drives on the host machine.</param>
|
2018-07-19 04:26:18 +00:00
|
|
|
|
/// <param name="detect">Detect save files stored in common SD card homebrew locations.</param>
|
2018-07-15 20:35:58 +00:00
|
|
|
|
/// <param name="extra">Paths to check in addition to the default paths</param>
|
|
|
|
|
/// <returns>Valid save files, if any.</returns>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static IEnumerable<SaveFile> GetSaveFiles(IReadOnlyList<string> drives, bool detect, IEnumerable<string> extra)
|
2018-07-15 20:35:58 +00:00
|
|
|
|
{
|
2018-07-21 17:23:15 +00:00
|
|
|
|
var paths = detect ? GetFoldersToCheck(drives, extra) : extra;
|
2018-07-19 04:26:18 +00:00
|
|
|
|
var result = GetSaveFilePathsFromFolders(paths, out var possiblePaths);
|
2018-07-15 20:35:58 +00:00
|
|
|
|
if (!result)
|
|
|
|
|
return Enumerable.Empty<SaveFile>();
|
|
|
|
|
|
|
|
|
|
var byMostRecent = possiblePaths.OrderByDescending(f => new FileInfo(f).LastWriteTime);
|
|
|
|
|
return byMostRecent.Select(SaveUtil.GetVariantSAV);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-21 17:23:15 +00:00
|
|
|
|
public static IEnumerable<string> GetFoldersToCheck(IReadOnlyList<string> drives, IEnumerable<string> extra)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2017-09-16 18:38:58 +00:00
|
|
|
|
var foldersToCheck = extra.Where(f => f?.Length > 0);
|
2017-05-12 04:34:18 +00:00
|
|
|
|
|
2018-07-21 17:23:15 +00:00
|
|
|
|
string path3DS = Path.GetPathRoot(Get3DSLocation(drives));
|
2017-05-16 23:27:43 +00:00
|
|
|
|
if (path3DS != null) // check for Homebrew/CFW backups
|
2017-09-16 18:38:58 +00:00
|
|
|
|
foldersToCheck = foldersToCheck.Concat(Get3DSBackupPaths(path3DS));
|
2017-05-12 04:34:18 +00:00
|
|
|
|
|
2018-07-21 17:23:15 +00:00
|
|
|
|
string pathNX = Path.GetPathRoot(GetSwitchLocation(drives));
|
2018-05-22 01:20:08 +00:00
|
|
|
|
if (pathNX != null) // check for Homebrew/CFW backups
|
|
|
|
|
foldersToCheck = foldersToCheck.Concat(GetSwitchBackupPaths(pathNX));
|
|
|
|
|
|
2018-07-15 20:35:58 +00:00
|
|
|
|
return foldersToCheck;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool GetSaveFilePathsFromFolders(IEnumerable<string> foldersToCheck, out IEnumerable<string> possible)
|
|
|
|
|
{
|
|
|
|
|
var possiblePaths = new List<string>();
|
2017-09-16 18:38:58 +00:00
|
|
|
|
foreach (var folder in foldersToCheck)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2017-09-16 18:38:58 +00:00
|
|
|
|
if (!SaveUtil.GetSavesFromFolder(folder, true, out IEnumerable<string> files))
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2018-07-15 20:35:58 +00:00
|
|
|
|
if (files == null)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
continue;
|
2018-07-15 20:35:58 +00:00
|
|
|
|
possible = files;
|
2017-05-12 04:34:18 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-15 20:35:58 +00:00
|
|
|
|
if (files != null)
|
|
|
|
|
possiblePaths.AddRange(files);
|
2017-05-12 04:34:18 +00:00
|
|
|
|
}
|
2018-07-15 20:35:58 +00:00
|
|
|
|
possible = possiblePaths;
|
2017-05-12 04:34:18 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|