using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
namespace PKHeX.Core
{
///
/// Utility logic for detecting a from various locations on the host machine.
///
public static class SaveFinder
{
///
/// Searches the provided to find a valid 3DS drive, usually from an inserted SD card.
///
/// List of drives on the host machine.
/// 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).
/// Folder path pointing to the Nintendo 3DS folder.
public static string? Get3DSLocation(IEnumerable drives, bool skipFirstDrive = true) =>
FindConsoleRootFolder(drives, "Nintendo 3DS", skipFirstDrive);
///
/// Searches the provided to find a valid Switch drive, usually from an inserted SD card.
///
/// List of drives on the host machine.
/// 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).
/// Folder path pointing to the Nintendo folder.
public static string? GetSwitchLocation(IEnumerable drives, bool skipFirstDrive = true) =>
FindConsoleRootFolder(drives, "Nintendo", skipFirstDrive);
private static string? FindConsoleRootFolder(IEnumerable drives, string path, bool skipFirstDrive)
{
if (skipFirstDrive)
drives = drives.Skip(1);
var paths = drives.Select(drive => Path.Combine(drive, path));
return paths.FirstOrDefault(Directory.Exists);
}
///
/// Gets a list of 3DS save backup paths for the storage device.
///
/// Root location of device
/// List of possible 3DS save backup paths.
public static IEnumerable Get3DSBackupPaths(string root)
{
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");
yield return Path.Combine(root, "3ds", "Checkpoint", "saves");
}
///
/// Gets a list of Switch save backup paths for the storage device.
///
/// Root location of device
/// List of possible 3DS save backup paths.
public static IEnumerable GetSwitchBackupPaths(string root)
{
yield return Path.Combine(root, "switch", "Checkpoint", "saves");
}
///
/// Extra list of Backup Paths used for detecting a save file.
///
public static readonly List CustomBackupPaths = new();
///
/// Finds a compatible save file that was most recently saved (by file write time).
///
/// List of drives on the host machine.
/// Paths to check in addition to the default paths
/// Reference to a valid save file, if any.
public static SaveFile? FindMostRecentSaveFile(IReadOnlyList drives, params string[] extra)
=> FindMostRecentSaveFile(drives, (IEnumerable)extra);
///
/// Finds a compatible save file that was most recently saved (by file write time).
///
/// List of drives on the host machine.
/// Paths to check in addition to the default paths
/// Reference to a valid save file, if any.
public static SaveFile? FindMostRecentSaveFile(IReadOnlyList drives, IEnumerable extra)
{
var foldersToCheck = GetFoldersToCheck(drives, extra);
var result = GetSaveFilePathsFromFolders(foldersToCheck, out var possiblePaths);
if (!result)
throw new Exception(string.Join(Environment.NewLine, possiblePaths)); // `possiblePaths` contains the error message
// return newest save file path that is valid
var byMostRecent = possiblePaths.OrderByDescending(File.GetLastWriteTimeUtc);
var saves = byMostRecent.Select(SaveUtil.GetVariantSAV);
return saves.FirstOrDefault(z => z?.ChecksumsValid == true);
}
///
/// Gets all detectable save files ordered by most recently saved (by file write time).
///
/// List of drives on the host machine.
/// Detect save files stored in common SD card homebrew locations.
/// Paths to check in addition to the default paths
/// Valid save files, if any.
public static IEnumerable GetSaveFiles(IReadOnlyList drives, bool detect, IEnumerable extra)
{
var paths = detect ? GetFoldersToCheck(drives, extra) : extra;
var result = GetSaveFilePathsFromFolders(paths, out var possiblePaths);
if (!result)
yield break;
var byMostRecent = possiblePaths.OrderByDescending(File.GetLastWriteTimeUtc);
foreach (var s in byMostRecent)
{
var sav = SaveUtil.GetVariantSAV(s);
if (sav != null)
yield return sav;
}
}
public static IEnumerable GetFoldersToCheck(IReadOnlyList drives, IEnumerable extra)
{
var foldersToCheck = extra.Where(f => !string.IsNullOrWhiteSpace(f)).Concat(CustomBackupPaths);
string? path3DS = Path.GetPathRoot(Get3DSLocation(drives));
if (!string.IsNullOrEmpty(path3DS)) // check for Homebrew/CFW backups
foldersToCheck = foldersToCheck.Concat(Get3DSBackupPaths(path3DS));
string? pathNX = Path.GetPathRoot(GetSwitchLocation(drives));
if (!string.IsNullOrEmpty(pathNX)) // check for Homebrew/CFW backups
foldersToCheck = foldersToCheck.Concat(GetSwitchBackupPaths(pathNX));
return foldersToCheck;
}
private static bool GetSaveFilePathsFromFolders(IEnumerable foldersToCheck, out IEnumerable possible)
{
var possiblePaths = new List();
foreach (var folder in foldersToCheck)
{
if (!SaveUtil.GetSavesFromFolder(folder, true, out IEnumerable files))
{
if (files is not string[] msg) // should always return string[]
continue;
if (msg.Length == 0) // folder doesn't exist
continue;
possible = msg;
return false;
}
possiblePaths.AddRange(files);
}
possible = possiblePaths;
return true;
}
///
public static SaveFile? FindMostRecentSaveFile() => FindMostRecentSaveFile(Environment.GetLogicalDrives(), CustomBackupPaths);
///
public static IEnumerable DetectSaveFiles() => GetSaveFiles(Environment.GetLogicalDrives(), true, CustomBackupPaths);
///
public static bool TryDetectSaveFile([NotNullWhen(true)] out SaveFile? sav) => TryDetectSaveFile(Environment.GetLogicalDrives(), out sav);
public static bool TryDetectSaveFile(IReadOnlyList drives, [NotNullWhen(true)] out SaveFile? sav)
{
var result = FindMostRecentSaveFile(drives, CustomBackupPaths);
if (result == null)
{
sav = null;
return false;
}
var path = result.Metadata.FilePath!;
sav = result;
return File.Exists(path);
}
}
}