Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to Save/load last folder in file pickers across restarts #72

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Shared.Core/KoikatuAPIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static bool EnableDebugLogging

private static ConfigEntry<bool> EnableDebugLoggingSetting { get; set; }
internal static ConfigEntry<bool> RememberFilePickerFolder { get; set; }
internal static ConfigEntry<bool> RememberFilePickerSaveLoad { get; set; }

internal static KoikatuAPI Instance { get; private set; }
internal static new ManualLogSource Logger { get; private set; }
Expand Down Expand Up @@ -86,6 +87,7 @@ private void BaseAwake()

EnableDebugLoggingSetting = Config.Bind("Debug", "Show debug messages", false, "Enables display of additional log messages when certain events are triggered within KKAPI. Useful for plugin devs to understand when controller messages are fired. Changes take effect after game restart.");
RememberFilePickerFolder = Config.Bind("General", "Remember last folder in file pickers", true, "If true, file picker dialogs will remember the last opened folder and open already inside of it. If false, the dialogs will always open in the default folder.\n\nWarning: This setting only applies to plugins that use the file picker through this API.");
RememberFilePickerSaveLoad = Config.Bind("General", "Save remembered file picker folder between restarts", true, "Save and load last remembered file picker folders across game restarts");

Logger.LogDebug($"Game version {GetGameVersion()} running under {System.Threading.Thread.CurrentThread.CurrentCulture.Name} culture");

Expand Down Expand Up @@ -159,6 +161,7 @@ void PrintFileIfExists(string fileName)
}
});
}
OpenFileDialog.LoadFilePickerStates();

#if QUITTING_EVENT_AVAILABLE
UnityEngine.Application.quitting += () => OnQuitting(EventArgs.Empty);
Expand Down
45 changes: 44 additions & 1 deletion src/Shared.Core/Utilities/OpenFileDialog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using BepInEx;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;

Expand Down Expand Up @@ -30,6 +33,7 @@ public class OpenFileDialog
/// Key is the initialDir, value is the last opened path.
/// </summary>
private static readonly Dictionary<string, string> LastOpenedPaths = new Dictionary<string, string>();
private static readonly string lastOpenedPathsCachePath = Path.Combine(Paths.CachePath, "ModdingAPI.lastOpenedPaths");

/// <inheritdoc cref="ShowDialog(string,string,string,string,OpenSaveFileDialgueFlags,string,IntPtr)"/>
public static string[] ShowDialog(string title, string initialDir, string filter, string defaultExt, OpenSaveFileDialgueFlags flags, IntPtr owner = default)
Expand Down Expand Up @@ -133,6 +137,7 @@ public static string[] ShowDialog(string title, string initialDir, string filter
}

LastOpenedPaths[initialDir] = Path.GetDirectoryName(selectedFilesList[0]);
SaveFilePickerStates();
if (selectedFilesList.Count == 1)
{
// Only one file selected with full path
Expand Down Expand Up @@ -202,6 +207,44 @@ private static class NativeMethods
public static extern IntPtr GetActiveWindow();
}

internal static void SaveFilePickerStates()
{
try
{
using (var fs = File.Create(lastOpenedPathsCachePath))
using (var wr = new StreamWriter(fs))
foreach (var path in LastOpenedPaths)
wr.WriteLine($"{path.Key}, {path.Value}");
}
catch (Exception ex)
{
KoikatuAPI.Logger.LogError($"Failed saving folder states to '{lastOpenedPathsCachePath}' because of error: {ex}");
}
}

internal static void LoadFilePickerStates()
{
try
{
if (KoikatuAPI.RememberFilePickerSaveLoad.Value && File.Exists(lastOpenedPathsCachePath))
{
var lines = File.ReadAllLines(lastOpenedPathsCachePath);
foreach (var line in lines)
{
if (string.IsNullOrEmpty(line)) continue;

var s = line.Split(',');
if (s.Length != 2) continue;
LastOpenedPaths[s[0]] = s[1];
}
}
}
catch (Exception ex)
{
KoikatuAPI.Logger.LogError($"Failed loading folder states from '{lastOpenedPathsCachePath}' because of error: {ex}");
}
}

#pragma warning disable 1591
[Flags]
public enum OpenSaveFileDialgueFlags
Expand Down