From 0c74eb19f6df9f0d99026c049b836cce45e4b4cb Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 12 Sep 2023 16:27:07 -0600 Subject: [PATCH] add ability to specify cache dir in settings file closes #97 --- CHANGELOG.md | 4 ++++ README.md | 1 + src/MinEdLauncher/App.fs | 9 ++++++--- src/MinEdLauncher/Settings.fs | 11 ++++++++--- src/MinEdLauncher/Types.fs | 3 ++- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc7b71b..ab8753a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### New Features +- Add ability to specify cache directory in settings file since Windows users don't have an environment variable + like `XDG_CACHE_DIR` on Linux. Add `cacheDir` to your [settings file]. + ### Bug Fixes - Fix launcher not shutting down when a startup process doesn't properly shutdown or takes too long to shutdown. The timeout for taking too long can be configured via the new `shutdownTimeout` property in the [settings file]. diff --git a/README.md b/README.md index f1ef55d..0841a56 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,7 @@ double backslash (`\\`) instead of a single backslash (`\`). "checkForLauncherUpdates": true, "maxConcurrentDownloads": 4, "forceUpdate": "PUBLIC_TEST_SERVER_OD", + "cacheDir": "C:\\path\\to\\dir", "processes": [ { "fileName": "C:\\path\\to\\app.exe", diff --git a/src/MinEdLauncher/App.fs b/src/MinEdLauncher/App.fs index a5ff159..e5c0493 100644 --- a/src/MinEdLauncher/App.fs +++ b/src/MinEdLauncher/App.fs @@ -222,6 +222,7 @@ let updateProduct downloader (hashProgress: ISampledProgress) (cacheProgres type AppError = | Version of string | ProductsDirectory of string + | CacheDirectory of string | MachineId of string | AuthorizedProducts of string | Login of LoginError @@ -233,6 +234,7 @@ module AppError = let toDisplayString = function | Version m -> $"Unable to get version: %s{m}" | ProductsDirectory m -> $"Unable to get products directory: %s{m}" + | CacheDirectory m -> $"Unable to get cache directory: %s{m}" | MachineId m -> $"Couldn't get machine id: %s{m}" | AuthorizedProducts m -> $"Couldn't get available products: %s{m}" | Login (ActionRequired m) -> $"Unsupported login action required: %s{m}" @@ -350,6 +352,7 @@ let run settings launcherVersion cancellationToken = taskResult { let messages = failedManifests |> List.map (fun (name, error) -> $"%s{name} - %s{error}") |> String.join separator Log.error $"Unable to update the following products. Failed to get their manifests:%s{separator}%s{messages}" + let! cacheDir = settings.CacheDir |> FileIO.ensureDirExists |> Result.mapError CacheDirectory let! updated = let productsDir = Path.Combine(settings.CbLauncherDir, "Products") @@ -357,11 +360,11 @@ let run settings launcherVersion cancellationToken = taskResult { |> List.chooseTasksSequential (fun (product, manifest) -> task { Log.info $"Updating %s{product.Name}" let productDir = Path.Combine(productsDir, product.Directory) - let productCacheDir = Path.Combine(Environment.cacheDir, $"%s{manifest.Title}%s{manifest.Version}") + let productCacheDir = Path.Combine(cacheDir, $"%s{manifest.Title}%s{manifest.Version}") let pathInfo = { ProductDir = productDir ProductCacheDir = productCacheDir CacheHashMap = Path.Combine(productCacheDir, "hashmap.txt") - ProductHashMap = Path.Combine(Environment.cacheDir, $"hashmap.%s{Path.GetFileName(productDir)}.txt") } + ProductHashMap = Path.Combine(cacheDir, $"hashmap.%s{Path.GetFileName(productDir)}.txt") } let barLength = 30 let downloadProgress = Console.Progress(Console.productDownloadIndicator barLength) let totalFiles = manifest.Files.Length @@ -379,7 +382,7 @@ let run settings launcherVersion cancellationToken = taskResult { Console.CursorVisible <- true File.Delete(pathInfo.CacheHashMap) FileIO.mergeDirectories productDir productCacheDir - Log.debug $"Moved downloaded files from '%s{Environment.cacheDir}' to '%s{productDir}'" + Log.debug $"Moved downloaded files from '%s{cacheDir}' to '%s{productDir}'" Log.info $"Finished updating %s{product.Name}" return Some product | Error e -> diff --git a/src/MinEdLauncher/Settings.fs b/src/MinEdLauncher/Settings.fs index 8bab652..cac2284 100644 --- a/src/MinEdLauncher/Settings.fs +++ b/src/MinEdLauncher/Settings.fs @@ -30,7 +30,8 @@ let defaults = FilterOverrides = OrdinalIgnoreCaseMap.empty AdditionalProducts = List.empty DryRun = false - ShutdownTimeout = TimeSpan.FromSeconds(10) } + ShutdownTimeout = TimeSpan.FromSeconds(10) + CacheDir = "" } [] type FrontierCredResult = Found of string * string * string option | NotFound of string | UnexpectedFormat of string | Error of string @@ -174,7 +175,8 @@ type Config = FilterOverrides: FilterConfig list AdditionalProducts: AuthorizedProduct list [] - ShutdownTimeout: int } + ShutdownTimeout: int + CacheDir: string option } let parseConfig fileName = let configRoot = ConfigurationBuilder() .AddJsonFile(fileName, false) @@ -258,4 +260,7 @@ let getSettings args appDir fileConfig = task { FilterOverrides = filterOverrides WatchForCrashes = fileConfig.WatchForCrashes AdditionalProducts = fileConfig.AdditionalProducts - ShutdownTimeout = TimeSpan.FromSeconds(fileConfig.ShutdownTimeout) }) } \ No newline at end of file + ShutdownTimeout = TimeSpan.FromSeconds(fileConfig.ShutdownTimeout) + CacheDir = fileConfig.CacheDir |> Option.defaultValue Environment.cacheDir + }) +} \ No newline at end of file diff --git a/src/MinEdLauncher/Types.fs b/src/MinEdLauncher/Types.fs index aeb3fad..676cbd9 100644 --- a/src/MinEdLauncher/Types.fs +++ b/src/MinEdLauncher/Types.fs @@ -126,7 +126,8 @@ type LauncherSettings = FilterOverrides: OrdinalIgnoreCaseMap AdditionalProducts: AuthorizedProduct list DryRun: bool - ShutdownTimeout: TimeSpan } + ShutdownTimeout: TimeSpan + CacheDir: string } type ProductMode = Online | Offline type VersionInfo = { Name: string