using System.IO; using Chronos.Core.Windows.Shell; namespace Chronos.App.Infrastructure; /// /// Where the client keeps its data (BRIEF ยง6.2, ): %LOCALAPPDATA%\Chronos, or .\data /// next to the executable when portable.txt exists. Never inside a folder OneDrive synchronises (SQLite corrupts /// there): a portable copy in one keeps its data in %LOCALAPPDATA%\Chronos, and Settings tells the user so /// (, ). When that folder itself lies in one, nowhere /// is safe: Chronos starts all the same and Settings warns (). /// public sealed class AppEnvironment { private AppEnvironment(string dataDirectory, bool portable, bool isCapture, string? syncedFolder = null, string? portableSyncRoot = null, string? earlierPortableData = null) { DataDirectory = dataDirectory; IsPortable = portable; IsCapture = isCapture; SyncedFolder = syncedFolder; PortableSyncRoot = portableSyncRoot; EarlierPortableData = earlierPortableData; Directory.CreateDirectory(DataDirectory); Directory.CreateDirectory(LogDirectory); Directory.CreateDirectory(CacheDirectory); } public string DataDirectory { get; } public bool IsPortable { get; } /// A throw-away environment for --capture: nothing of the user's data is read or written. public bool IsCapture { get; } /// The OneDrive folder itself lies in, where the index may be damaged; null when it is safe. public string? SyncedFolder { get; } /// The OneDrive folder this portable copy runs from; its data is in %LOCALAPPDATA%\Chronos instead of .\data. Null otherwise. public string? PortableSyncRoot { get; } /// The .\data folder this portable copy used before it ran from OneDrive, left as it was; null when there is none. public string? EarlierPortableData { get; } public string LogDirectory => Path.Combine(DataDirectory, "logs"); public string CacheDirectory => Path.Combine(DataDirectory, "cache"); public string BackupDirectory => Path.Combine(DataDirectory, "backups"); /// The private folder of the server this PC runs while it shares its library: certificate, settings, uploads, logs. public string ShareDirectory => Path.Combine(DataDirectory, "server"); /// The PC's name as the screens show it: a capture shows . public string PcName => IsCapture ? SamplePcName : Environment.MachineName; /// The PC name captures show. public const string SamplePcName = "STUDIO-PC"; public string DatabasePath => Path.Combine(DataDirectory, AppPaths.DatabaseFileName); public string SettingsPath => Path.Combine(DataDirectory, AppPaths.SettingsFileName); /// The environment of this process: its executable's folder and the user's OneDrive folders decide. public static AppEnvironment Detect() => From(AppPaths.Resolve()); /// The environment at (its folders are created). public static AppEnvironment From(AppPaths paths) { ArgumentNullException.ThrowIfNull(paths); return new(paths.DataRoot, paths.IsPortable, isCapture: false, paths.SyncedFolder, paths.PortableSyncRoot, paths.EarlierPortableData); } /// /// The data folder a capture shows in place of its temporary one (): the default data folder of a /// sample user, so the screenshots show no path of the machine (its temporary folder, its user name). /// public const string SampleDataDirectory = @"C:\Users\Studio\AppData\Local\Chronos"; /// The folder a capture shows for its sample archive (archive in its data folder), as the sample settings name it. public const string SampleArchiveDirectory = @"D:\ChronosArchive"; /// The temporary data folder of the capture this process runs (the last one made); null outside a capture. private static string? _captureData; /// A fresh temporary data folder for screenshots and smoke tests; delete it with . public static AppEnvironment ForCapture() { var environment = new AppEnvironment(Path.Combine(Path.GetTempPath(), "chronos-capture-" + Guid.CreateVersion7().ToString("N")), portable: false, isCapture: true); _captureData = environment.DataDirectory; return environment; } /// /// A path as the screens show it: one under a capture's temporary data folder as under /// (its sample archive as ), so the screenshots show nothing of the machine; any /// other as it is. Only for text on screen: a path typed there goes through . /// public static string Shown(string path) => Shown(path, _captureData); /// A path typed or shown on screen as the one it stands for: undone, so a capture never writes outside its temporary folder. public static string Actual(string shown) => Actual(shown, _captureData); /// for the capture whose data folder is (none: the path as it is). internal static string Shown(string path, string? captureData) { foreach (var (actual, shown) in CaptureFolders(captureData)) { if (Swap(path, actual, shown) is { } swapped) return swapped; } return path; } /// for the capture whose data folder is . internal static string Actual(string shown, string? captureData) { foreach (var (actual, sample) in CaptureFolders(captureData)) { if (Swap(shown, sample, actual) is { } swapped) return swapped; } return shown; } /// A capture's folders and what the screens show for each, the more specific first. private static (string Actual, string Shown)[] CaptureFolders(string? captureData) => captureData is null ? [] : [(Path.Combine(captureData, "archive"), SampleArchiveDirectory), (captureData, SampleDataDirectory)]; /// with its root replaced by ; null when it is not under it. private static string? Swap(string path, string from, string to) { if (string.IsNullOrEmpty(path)) return null; var root = from.TrimEnd(Path.DirectorySeparatorChar); if (path.TrimEnd(Path.DirectorySeparatorChar).Equals(root, StringComparison.OrdinalIgnoreCase)) return to; return path.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) ? to + path[root.Length..] : null; } /// Removes the temporary data folder of a capture environment (never a real one). public void DeleteCaptureData() { if (!IsCapture) return; try { Directory.Delete(DataDirectory, recursive: true); } catch (IOException) { // Files still open (log, database) are left to the OS temp cleanup. } catch (UnauthorizedAccessException) { } } }