diff --git a/StabilityMatrix.Avalonia/App.axaml.cs b/StabilityMatrix.Avalonia/App.axaml.cs index 30a6b229..9b01279a 100644 --- a/StabilityMatrix.Avalonia/App.axaml.cs +++ b/StabilityMatrix.Avalonia/App.axaml.cs @@ -258,6 +258,8 @@ public sealed class App : Application var settingsManager = Services.GetRequiredService(); + settingsManager.LibraryDirOverride = Program.Args.DataDirectoryOverride; + if (settingsManager.TryFindLibrary()) { Cultures.SetSupportedCultureOrDefault(settingsManager.Settings.Language); diff --git a/StabilityMatrix.Avalonia/Models/AppArgs.cs b/StabilityMatrix.Avalonia/Models/AppArgs.cs index 03ff8b5a..f1624103 100644 --- a/StabilityMatrix.Avalonia/Models/AppArgs.cs +++ b/StabilityMatrix.Avalonia/Models/AppArgs.cs @@ -58,18 +58,18 @@ public class AppArgs public bool DisableGpuRendering { get; set; } /// - /// Override global app data directory + /// Override global app home directory /// Defaults to (%APPDATA%|~/.config)/StabilityMatrix /// - [Option("global-dir", HelpText = "Override global app data directory")] - public string? GlobalDirectory { get; set; } + [Option("home-dir", HelpText = "Override global app home directory")] + public string? HomeDirectoryOverride { get; set; } /// /// Override data directory /// This takes precedence over relative portable directory and global directory /// [Option("data-dir", HelpText = "Override data directory")] - public string? DataDirectory { get; set; } + public string? DataDirectoryOverride { get; set; } /// /// Custom Uri protocol handler diff --git a/StabilityMatrix.Avalonia/Program.cs b/StabilityMatrix.Avalonia/Program.cs index 16a667f2..616b6346 100644 --- a/StabilityMatrix.Avalonia/Program.cs +++ b/StabilityMatrix.Avalonia/Program.cs @@ -65,6 +65,11 @@ public static class Program Args = parseResult.Value; + if (Args.HomeDirectoryOverride is { } homeDir) + { + Compat.SetAppDataHome(homeDir); + } + // Launched for custom URI scheme, handle and exit if (Args.Uri is { } uriArg) { diff --git a/StabilityMatrix.Core/Helper/Compat.cs b/StabilityMatrix.Core/Helper/Compat.cs index 9ddb79f9..9a014424 100644 --- a/StabilityMatrix.Core/Helper/Compat.cs +++ b/StabilityMatrix.Core/Helper/Compat.cs @@ -51,7 +51,7 @@ public static class Compat /// /// Set AppDataHome to a custom path. Used for testing. /// - internal static void SetAppDataHome(string path) + public static void SetAppDataHome(string path) { AppDataHome = path; } @@ -136,17 +136,7 @@ public static class Compat AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); - if ( - Environment.GetEnvironmentVariable("STABILITY_MATRIX_APPDATAHOME") is - { } appDataOverride - ) - { - AppDataHome = appDataOverride; - } - else - { - AppDataHome = AppData + AppName; - } + AppDataHome = AppData + AppName; } /// diff --git a/StabilityMatrix.Core/Services/ISettingsManager.cs b/StabilityMatrix.Core/Services/ISettingsManager.cs index 4b8a92d3..52e9c3e8 100644 --- a/StabilityMatrix.Core/Services/ISettingsManager.cs +++ b/StabilityMatrix.Core/Services/ISettingsManager.cs @@ -9,6 +9,7 @@ namespace StabilityMatrix.Core.Services; public interface ISettingsManager { bool IsPortableMode { get; } + string? LibraryDirOverride { set; } string LibraryDir { get; } bool IsLibraryDirSet { get; } string DatabasePath { get; } diff --git a/StabilityMatrix.Core/Services/SettingsManager.cs b/StabilityMatrix.Core/Services/SettingsManager.cs index f1dfb5db..0d96f8e8 100644 --- a/StabilityMatrix.Core/Services/SettingsManager.cs +++ b/StabilityMatrix.Core/Services/SettingsManager.cs @@ -22,10 +22,9 @@ public class SettingsManager : ISettingsManager private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly ReaderWriterLockSlim FileLock = new(); - private static readonly string GlobalSettingsPath = Path.Combine( - Compat.AppDataHome, - "global.json" - ); + private static string GlobalSettingsPath => Path.Combine(Compat.AppDataHome, "global.json"); + + public string? LibraryDirOverride { private get; set; } private readonly string? originalEnvPath = Environment.GetEnvironmentVariable( "PATH", @@ -274,6 +273,16 @@ public class SettingsManager : ISettingsManager if (IsLibraryDirSet && !forceReload) 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 var appDir = Compat.AppCurrentDir; IsPortableMode = File.Exists(Path.Combine(appDir, "Data", ".sm-portable"));