found;
try { found = saves.FindEmulatorSaves(); } catch (Exception e) { Log.Write("saves: " + e.Message); found = new(); }
savesFound = found;
var options = found.Select((f, i) => ("s" + i, f.Name, (string?)SetLine(f), i == 0)).ToList();
options.Add(("folder", "A folder…", "Choose it yourself", found.Count == 0));
if (into)
Choice("Bring saves in", found.Count > 0 ? "Where are the saves you want to play with?" : "No emulator saves were found on this PC. Choose the folder they are in.",
"saves.from", options, "Next");
else
Choice("Copy saves out", "Where should Triforce's saves go?", "saves.to", options, "Next");
}
/// Step one answered: a found save set, or a folder to pick first.
void SavesChosen(bool into, string? id)
{
if (id == null) return;
if (id == "folder")
{
PickThen(true, into ? "Choose the folder with your saves" : "Choose where the saves go", path => SavesFolderPicked(into, path));
return;
}
if (!id.StartsWith('s') || !int.TryParse(id.AsSpan(1), out var i) || i < 0 || i >= savesFound.Count) return;
SavesAsk(into, savesFound[i]);
}
void SavesFolderPicked(bool into, string path)
{
if (Saves.FindIn(path) is { } found)
{
SavesAsk(into, Saves.Describe(Path.GetFileName(found.TrimEnd('\\', '/')) is { Length: > 0 } n ? "the folder " + n : "that folder", found));
return;
}
if (into) { Toast("There are no saves of this game in that folder.", "error"); return; }
// (out: an empty folder takes the saves as they are; anything else gets a folder of their own inside it, never its own files replaced)
var empty = !Directory.Exists(path) || !Directory.EnumerateFileSystemEntries(path).Any();
var target = empty ? path : Path.Combine(path, "Triforce saves");
if (!empty && Directory.Exists(target) && Directory.EnumerateFileSystemEntries(target).Any() && !Saves.IsSaveSet(target))
{
Toast("That folder already has a \"Triforce saves\" folder with other files in it. Choose an empty folder.", "error");
return;
}
SavesAsk(false, Saves.Describe(empty ? "the folder " + Path.GetFileName(path.TrimEnd('\\', '/')) : "a new folder, Triforce saves", target));
}
/// Step two: a plain question that names what gets replaced, and that it is backed up first.
void SavesAsk(bool into, SaveSet set)
{
var game = GameDir;
if (game == null) return;
var key = (into ? "in:" : "out:") + Guid.NewGuid().ToString("N");
savesPending[key] = set;
var ours = Saves.Describe("Triforce", new Saves(ctx).TriforceNewest(game));
string html;
if (into)
html = $"The saves from {H(set.Name)} ({H(SetLine(set))}) become Triforce's.
" +
(ours.Empty ? "Triforce has no saves yet, so nothing is replaced.
"
: $"Triforce's own saves ({H(SetLine(ours))}) are replaced. They are backed up first, so you can go back to them.
");
else
html = $"Triforce's saves ({H(SetLine(ours))}) are copied to {H(set.Name)}.
" +
(set.Empty ? "Nothing is there yet, so nothing is replaced.
"
: $"What is there now ({H(SetLine(set))}) is replaced. It is backed up first, so you can go back to it.
") +
$"{H(set.Folder)}
";
Ask(into ? "Bring saves in" : "Copy saves out", html, "saves.confirm",
new[] { ("cancel", "Cancel", false), ("go", into ? "Bring saves in" : "Copy saves out", true) }, key);
}
async Task SavesConfirm(string? id, string? key)
{
if (key == null || !savesPending.TryRemove(key, out var set) || id != "go") return;
var game = GameDir;
if (game == null) return;
var into = key.StartsWith("in:", StringComparison.Ordinal);
var saves = new Saves(ctx);
string? backup;
try
{
backup = into ? await saves.ImportAsync(set, game) : await saves.ExportAsync(game, set);
}
catch (Exception e) when (e is SavesException || e is InvalidOperationException || e is IOException || e is UnauthorizedAccessException)
{
Log.Write("saves: " + e.Message);
Toast(H(e.Message), "error");
return;
}
var kept = backup != null ? " The ones they replaced are backed up in save-backups." : "";
Toast((into ? "Your saves are in." : "Your saves are copied to " + H(set.Name) + ".") + kept, "success");
SendSaves();
if (into) RefreshHome();
}
// ---------------- folders and links (shared by the three pages) ----------------
/// A folder in the system's file window. A test run opens no windows: it says so instead.
void OpenPlace(string folder)
{
if (!Directory.Exists(folder)) { Toast("That folder is not there: " + H(folder), "error"); return; }
if (ctx.TestRoot != null) { Toast("Test run: no window opens. " + H(folder)); Log.Write("open folder (test run, not opened): " + folder); return; }
ctx.Platform.OpenFolder(folder);
}
/// A web page in the player's browser (https only). A test run opens nothing: it says so instead.
void OpenLink(string url)
{
if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) return;
if (ctx.TestRoot != null) { Toast("Test run: no browser opens. " + H(url)); Log.Write("open link (test run, not opened): " + url); return; }
try
{
if (OperatingSystem.IsWindows()) System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(url) { UseShellExecute = true })?.Dispose();
else
{
var si = new System.Diagnostics.ProcessStartInfo("xdg-open") { UseShellExecute = false };
si.ArgumentList.Add(url);
System.Diagnostics.Process.Start(si)?.Dispose();
}
}
catch (Exception e) { Log.Write("open " + url + ": " + e.Message); Toast("The page could not be opened: " + H(url), "error"); }
}
}