2017-05-12 04:34:18 +00:00
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms
|
|
|
|
|
{
|
|
|
|
|
public static class PathUtilWindows
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static string Get3DSLocation()
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] DriveList = Environment.GetLogicalDrives();
|
|
|
|
|
for (int i = 1; i < DriveList.Length; i++) // Skip first drive (some users still have floppy drives and would chew up time!)
|
|
|
|
|
{
|
|
|
|
|
string potentialPath = Path.Combine(DriveList[i], "Nintendo 3DS");
|
|
|
|
|
if (Directory.Exists(potentialPath))
|
|
|
|
|
return potentialPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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-06-18 01:37:19 +00:00
|
|
|
|
public static string[] Get3DSBackupPaths(string root)
|
2017-05-16 23:27:43 +00:00
|
|
|
|
{
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
Path.Combine(root, "saveDataBackup"),
|
|
|
|
|
Path.Combine(root, "filer", "UserSaveData"),
|
|
|
|
|
Path.Combine(root, "JKSV", "Saves"),
|
|
|
|
|
Path.Combine(root, "TWLSaveTool"),
|
|
|
|
|
Path.Combine(root, "fbi", "save"),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 04:34:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Detects a save file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Full path of a save file. Returns null if unable to find any.</returns>
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static bool DetectSaveFile(out string path, params string[] extra)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string path3DS = Path.GetPathRoot(Get3DSLocation());
|
2017-05-12 04:34:18 +00:00
|
|
|
|
List<string> possiblePaths = new List<string>();
|
|
|
|
|
List<string> foldersToCheck = new List<string>(extra.Where(f => f?.Length > 0));
|
|
|
|
|
path = null;
|
|
|
|
|
|
2017-05-16 23:27:43 +00:00
|
|
|
|
if (path3DS != null) // check for Homebrew/CFW backups
|
2017-06-18 01:37:19 +00:00
|
|
|
|
foldersToCheck.AddRange(Get3DSBackupPaths(path3DS));
|
2017-05-12 04:34:18 +00:00
|
|
|
|
|
|
|
|
|
foreach (var p in foldersToCheck)
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (!SaveUtil.GetSavesFromFolder(p, true, out IEnumerable<string> files))
|
2017-05-12 04:34:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (files == null)
|
|
|
|
|
continue;
|
|
|
|
|
path = files.First(); // error
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
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);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var sav = SaveUtil.GetVariantSAV(data);
|
2017-05-12 04:34:18 +00:00
|
|
|
|
if (sav?.ChecksumsValid != true)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
path = file;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
path = e.Message + Environment.NewLine + file;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|