Add alternate config location (fixes #768)

This commit is contained in:
Matt Nadareski
2024-12-06 23:55:27 -05:00
parent 3d96379aba
commit b8e923207f
3 changed files with 79 additions and 6 deletions

View File

@@ -61,6 +61,7 @@
- Migrate to using publish script for GHA
- Update README to remove AppVeyor references
- Remove unused gated using statement
- Add alternate config location
### 3.2.4 (2024-11-24)

View File

@@ -9,14 +9,26 @@ namespace MPF.Frontend.Tools
{
public static class OptionsLoader
{
/// <summary>
/// Configuration file name
/// </summary>
private const string ConfigurationFileName = "config.json";
/// <summary>
/// Full path to the configuration file used by the program
/// </summary>
#if NET20 || NET35 || NET40 || NET452
private static string ConfigurationPath => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json");
#else
private static string ConfigurationPath => Path.Combine(AppContext.BaseDirectory, "config.json");
#endif
private static string ConfigurationPath
{
get
{
if (_configPath != null)
return _configPath;
_configPath = GetConfigurationPath();
return _configPath;
}
}
private static string? _configPath = null;
#region Arguments
@@ -281,6 +293,11 @@ namespace MPF.Frontend.Tools
/// </summary>
public static Options LoadFromConfig()
{
// If no options path can be found
if (string.IsNullOrEmpty(ConfigurationPath))
return new Options();
// Ensure the file exists
if (!File.Exists(ConfigurationPath))
{
File.Create(ConfigurationPath).Dispose();
@@ -300,6 +317,10 @@ namespace MPF.Frontend.Tools
/// </summary>
public static void SaveToConfig(Options options, bool saveDefault = false)
{
// If no options path can be found
if (string.IsNullOrEmpty(ConfigurationPath))
return;
// If default values should be saved as well
if (saveDefault)
{
@@ -329,6 +350,53 @@ namespace MPF.Frontend.Tools
serializer.Serialize(writer, options.Settings, typeof(Dictionary<string, string>));
}
/// <summary>
/// Attempt to determine the configuration path
/// </summary>
private static string GetConfigurationPath()
{
// User home directory
#if NET20 || NET35
string homeDir = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
homeDir = Path.Combine(Path.Combine(homeDir, ".config"), "mpf");
#else
string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
homeDir = Path.Combine(homeDir, ".config", "mpf");
#endif
if (File.Exists(Path.Combine(homeDir, ConfigurationFileName)))
return Path.Combine(homeDir, ConfigurationFileName);
// Local folder
#if NET20 || NET35 || NET40 || NET452
string runtimeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#else
string runtimeDir = AppContext.BaseDirectory;
#endif
if (File.Exists(Path.Combine(runtimeDir, ConfigurationFileName)))
return Path.Combine(runtimeDir, ConfigurationFileName);
// Attempt to use local folder
try
{
Directory.CreateDirectory(runtimeDir);
File.Create(Path.Combine(runtimeDir, ConfigurationFileName)).Dispose();
return Path.Combine(runtimeDir, ConfigurationFileName);
}
catch { }
// Attempt to use home directory
try
{
Directory.CreateDirectory(homeDir);
File.Create(Path.Combine(homeDir, ConfigurationFileName)).Dispose();
return Path.Combine(homeDir, ConfigurationFileName);
}
catch { }
// This should not happen
return string.Empty;
}
#endregion
}
}

View File

@@ -16,6 +16,8 @@ For the latest WIP build here: [Rolling Release](https://github.com/SabreTools/M
MPF is the main, UI-centric application of the MPF suite. This program allows users to use Redumper, Aaru, or DiscImageCreator in a more user-friendly way. Each backend dumping program is supported as fully as possible to ensure that all information is captured on output. There are many customization options and quality of life settings that can be access through the Options menu.
Configuration files are stored by default next to the application as `config.json`. This contains all settings for the UI including credentials. In addition to this, the configuration can also be located at `%userprofile%\.config\mpf\config.json` on Windows or `~/.config/mpf/config.json` on Unix-like systems. The second path will be used by default if running from a read-only device or folder. If neither directory is readable, no options will be loaded or saved.
### UI Support Limitations
The main UI has some known limitations that are documented in code and in some prior support tickets:
@@ -32,7 +34,9 @@ The main UI has some known limitations that are documented in code and in some p
## Media Preservation Frontend CLI (MPF.CLI)
MPF.CLI is a commandline-only program that allows users to use Redumper, Aaru, or DiscImageCreator in a more user-friendly way. Each backend dumping program is supported as fully as possible to ensure that all information is captured on output. There are many customization options and quality of life settings that can be access through the `config.json` file.
MPF.CLI is a commandline-only program that allows users to use Redumper, Aaru, or DiscImageCreator in a more user-friendly way. Each backend dumping program is supported as fully as possible to ensure that all information is captured on output. All options are manually configured through the configuration file. See below for details.
Configuration files are stored by default next to the application as `config.json`. This contains all settings for the UI including credentials. In addition to this, the configuration can also be located at `%userprofile%\.config\mpf\config.json` on Windows or `~/.config/mpf/config.json` on Unix-like systems. The second path will be used by default if running from a read-only device or folder. If neither directory is readable, no options will be loaded or saved.
### CLI Support Limitations