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
|
|
|
|
{
|
2019-02-23 22:57:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Utility logic for detecting a <see cref="SaveFile"/> from various locations on the host machine.
|
|
|
|
|
/// </summary>
|
2020-04-14 17:59:16 +00:00
|
|
|
|
public static class SaveFinder
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2017-09-16 18:38:58 +00:00
|
|
|
|
/// <summary>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
/// Searches the provided <see cref="drives"/> to find a valid 3DS drive, usually from an inserted SD card.
|
2017-09-16 18:38:58 +00:00
|
|
|
|
/// </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>
|
2020-04-05 02:30:50 +00:00
|
|
|
|
public static string? Get3DSLocation(IEnumerable<string> drives, bool skipFirstDrive = true) =>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
FindConsoleRootFolder(drives, "Nintendo 3DS", skipFirstDrive);
|
2018-05-22 01:20:08 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
/// Searches the provided <see cref="drives"/> to find a valid Switch drive, usually from an inserted SD card.
|
2018-05-22 01:20:08 +00:00
|
|
|
|
/// </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>
|
2020-04-05 02:30:50 +00:00
|
|
|
|
public static string? GetSwitchLocation(IEnumerable<string> drives, bool skipFirstDrive = true) =>
|
2018-07-21 17:23:15 +00:00
|
|
|
|
FindConsoleRootFolder(drives, "Nintendo", skipFirstDrive);
|
2018-05-22 01:20:08 +00:00
|
|
|
|
|
2020-04-05 02:30:50 +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");
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 17:40:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extra list of Backup Paths used for detecting a save file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static readonly List<string> CustomBackupPaths = new List<string>();
|
|
|
|
|
|
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>
|
2020-04-14 17:59:16 +00:00
|
|
|
|
public static SaveFile? FindMostRecentSaveFile(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
|
2019-04-06 01:45:59 +00:00
|
|
|
|
var byMostRecent = possiblePaths.OrderByDescending(File.GetLastWriteTimeUtc);
|
2018-07-15 20:35:58 +00:00
|
|
|
|
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)
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
yield break;
|
2018-07-15 20:35:58 +00:00
|
|
|
|
|
2019-04-06 01:45:59 +00:00
|
|
|
|
var byMostRecent = possiblePaths.OrderByDescending(File.GetLastWriteTimeUtc);
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
foreach (var s in byMostRecent)
|
|
|
|
|
{
|
|
|
|
|
var sav = SaveUtil.GetVariantSAV(s);
|
|
|
|
|
if (sav != null)
|
|
|
|
|
yield return sav;
|
|
|
|
|
}
|
2018-07-15 20:35:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-01-19 01:33:19 +00:00
|
|
|
|
var foldersToCheck = extra.Where(f => !string.IsNullOrWhiteSpace(f)).Concat(CustomBackupPaths);
|
2017-05-12 04:34:18 +00:00
|
|
|
|
|
2018-07-21 17:23:15 +00:00
|
|
|
|
string path3DS = Path.GetPathRoot(Get3DSLocation(drives));
|
2020-01-19 01:33:19 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(path3DS)) // 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));
|
2020-01-19 01:33:19 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(pathNX)) // check for Homebrew/CFW backups
|
2018-05-22 01:20:08 +00:00
|
|
|
|
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
|
|
|
|
{
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
if (!(files is string[] msg)) // should always return string[]
|
|
|
|
|
continue;
|
|
|
|
|
if (msg.Length == 0) // folder doesn't exist
|
2017-05-12 04:34:18 +00:00
|
|
|
|
continue;
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
possible = msg;
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|