Browse Source

Add AppDataHome and LibraryDir overrides from args

pull/324/head
Ionite 1 year ago
parent
commit
ac8c7a0e9f
No known key found for this signature in database
  1. 2
      StabilityMatrix.Avalonia/App.axaml.cs
  2. 8
      StabilityMatrix.Avalonia/Models/AppArgs.cs
  3. 5
      StabilityMatrix.Avalonia/Program.cs
  4. 14
      StabilityMatrix.Core/Helper/Compat.cs
  5. 1
      StabilityMatrix.Core/Services/ISettingsManager.cs
  6. 17
      StabilityMatrix.Core/Services/SettingsManager.cs

2
StabilityMatrix.Avalonia/App.axaml.cs

@ -258,6 +258,8 @@ public sealed class App : Application
var settingsManager = Services.GetRequiredService<ISettingsManager>(); var settingsManager = Services.GetRequiredService<ISettingsManager>();
settingsManager.LibraryDirOverride = Program.Args.DataDirectoryOverride;
if (settingsManager.TryFindLibrary()) if (settingsManager.TryFindLibrary())
{ {
Cultures.SetSupportedCultureOrDefault(settingsManager.Settings.Language); Cultures.SetSupportedCultureOrDefault(settingsManager.Settings.Language);

8
StabilityMatrix.Avalonia/Models/AppArgs.cs

@ -58,18 +58,18 @@ public class AppArgs
public bool DisableGpuRendering { get; set; } public bool DisableGpuRendering { get; set; }
/// <summary> /// <summary>
/// Override global app data directory /// Override global app home directory
/// Defaults to (%APPDATA%|~/.config)/StabilityMatrix /// Defaults to (%APPDATA%|~/.config)/StabilityMatrix
/// </summary> /// </summary>
[Option("global-dir", HelpText = "Override global app data directory")] [Option("home-dir", HelpText = "Override global app home directory")]
public string? GlobalDirectory { get; set; } public string? HomeDirectoryOverride { get; set; }
/// <summary> /// <summary>
/// Override data directory /// Override data directory
/// This takes precedence over relative portable directory and global directory /// This takes precedence over relative portable directory and global directory
/// </summary> /// </summary>
[Option("data-dir", HelpText = "Override data directory")] [Option("data-dir", HelpText = "Override data directory")]
public string? DataDirectory { get; set; } public string? DataDirectoryOverride { get; set; }
/// <summary> /// <summary>
/// Custom Uri protocol handler /// Custom Uri protocol handler

5
StabilityMatrix.Avalonia/Program.cs

@ -65,6 +65,11 @@ public static class Program
Args = parseResult.Value; Args = parseResult.Value;
if (Args.HomeDirectoryOverride is { } homeDir)
{
Compat.SetAppDataHome(homeDir);
}
// Launched for custom URI scheme, handle and exit // Launched for custom URI scheme, handle and exit
if (Args.Uri is { } uriArg) if (Args.Uri is { } uriArg)
{ {

14
StabilityMatrix.Core/Helper/Compat.cs

@ -51,7 +51,7 @@ public static class Compat
/// <summary> /// <summary>
/// Set AppDataHome to a custom path. Used for testing. /// Set AppDataHome to a custom path. Used for testing.
/// </summary> /// </summary>
internal static void SetAppDataHome(string path) public static void SetAppDataHome(string path)
{ {
AppDataHome = path; AppDataHome = path;
} }
@ -136,17 +136,7 @@ public static class Compat
AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if ( AppDataHome = AppData + AppName;
Environment.GetEnvironmentVariable("STABILITY_MATRIX_APPDATAHOME") is
{ } appDataOverride
)
{
AppDataHome = appDataOverride;
}
else
{
AppDataHome = AppData + AppName;
}
} }
/// <summary> /// <summary>

1
StabilityMatrix.Core/Services/ISettingsManager.cs

@ -9,6 +9,7 @@ namespace StabilityMatrix.Core.Services;
public interface ISettingsManager public interface ISettingsManager
{ {
bool IsPortableMode { get; } bool IsPortableMode { get; }
string? LibraryDirOverride { set; }
string LibraryDir { get; } string LibraryDir { get; }
bool IsLibraryDirSet { get; } bool IsLibraryDirSet { get; }
string DatabasePath { get; } string DatabasePath { get; }

17
StabilityMatrix.Core/Services/SettingsManager.cs

@ -22,10 +22,9 @@ public class SettingsManager : ISettingsManager
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static readonly ReaderWriterLockSlim FileLock = new(); private static readonly ReaderWriterLockSlim FileLock = new();
private static readonly string GlobalSettingsPath = Path.Combine( private static string GlobalSettingsPath => Path.Combine(Compat.AppDataHome, "global.json");
Compat.AppDataHome,
"global.json" public string? LibraryDirOverride { private get; set; }
);
private readonly string? originalEnvPath = Environment.GetEnvironmentVariable( private readonly string? originalEnvPath = Environment.GetEnvironmentVariable(
"PATH", "PATH",
@ -274,6 +273,16 @@ public class SettingsManager : ISettingsManager
if (IsLibraryDirSet && !forceReload) if (IsLibraryDirSet && !forceReload)
return true; return true;
// 0. Check Override
if (!string.IsNullOrEmpty(LibraryDirOverride))
{
Logger.Info("Using library override path: {Path}", LibraryDirOverride);
LibraryDir = LibraryDirOverride;
SetStaticLibraryPaths();
LoadSettings();
return true;
}
// 1. Check portable mode // 1. Check portable mode
var appDir = Compat.AppCurrentDir; var appDir = Compat.AppCurrentDir;
IsPortableMode = File.Exists(Path.Combine(appDir, "Data", ".sm-portable")); IsPortableMode = File.Exists(Path.Combine(appDir, "Data", ".sm-portable"));

Loading…
Cancel
Save