+ public string Packages => Of.Packages; public string StorageSource => Of.StorageSource; public string ClientPrefix => Of.ClientPrefix; public static string AppPath { get; } = AppContext.BaseDirectory; public static string StorageOrigin { get; } = Path.Combine( AppPath, NNSFolderNames.ClassicStorage, NNSFolderNames.ClassicStorageSource ); public string GetOriginPath(string folderName, string? suffix = null) { return folderName switch { StorageNames.Storage => Storage, StorageNames.Cache => Cache, StorageNames.Programs => Programs, StorageNames.Logs => Logs, StorageNames.Packages => Packages, NNSFolderNames.ClassicStorageSource => StorageOrigin, NNSFolderNames.ClassicClientPrefix => Path.Combine( Storage, $"{ClientPrefix}.{suffix}" ), _ => Home }; } public string GetPath(string folderName, string? suffix = null) { return folderName switch { StorageNames.Storage => Storage, StorageNames.Cache => Cache, StorageNames.Programs => Programs, StorageNames.Logs => Logs, StorageNames.Packages => Packages, NNSFolderNames.ClassicStorageSource => StorageOrigin, StorageNames.StorageSource => StorageSource, StorageNames.ClientPrefix => Path.Combine( Storage, $"{ClientPrefix}.{suffix}" ), _ => Home }; } public static PathSet OriginPaths(string? storagePath, string? localProgramPath) { var root = AppContext.BaseDirectory; var storage = !string.IsNullOrWhiteSpace(storagePath) ? ( //Storage path is set Path.IsPathRooted(storagePath) || Path.IsPathFullyQualified(storagePath) //Is the path absolute? ) ? storagePath : //Take the path as is Path.Join(root, storagePath) : //Set the path relative to the base path Path.Combine(root, NNSFolderNames.ClassicStorage); //Default storage path return new PathSet( root, storage, Path.Combine(root, NNSFolderNames.ClassicCache), !string.IsNullOrWhiteSpace(localProgramPath) ? ( //Program path is set Path.IsPathRooted(localProgramPath) || Path.IsPathFullyQualified(localProgramPath) //Is the path absolute? ) ? localProgramPath : //Take the path as is Path.Join(root, localProgramPath) : //Set the path relative to the base path Path.Combine(root, NNSFolderNames.ClassicPrograms), //Default program path Path.Combine(root, NNSFolderNames.ClassicLogs), Path.Combine(root, NNSFolderNames.ClassicPackages), Path.Combine(storage, NNSFolderNames.ClassicStorageSource), NNSFolderNames.ClassicClientPrefix ); } void EnsureLocations() { if (!Directory.Exists(Cache)) { Directory.CreateDirectory(Cache); } if (!Directory.Exists(Logs)) { Directory.CreateDirectory(Logs); } if (UseClassic) return; if (!File.Exists(FromHome.SettingsPath) || File.GetLastWriteTime(FromHome.SettingsPath) < File.GetLastWriteTime(FromOrigin.SettingsPath) ) { File.Copy(FromOrigin.SettingsPath, FromHome.SettingsPath, true); } if (FromOrigin.Cache != Cache && Path.Exists(FromOrigin.Cache)) { foreach (var file in Directory.GetFiles(FromOrigin.Cache, "*", SearchOption.AllDirectories)) { var relative = Path.GetRelativePath(FromOrigin.Cache, file); var subFolder = Path.GetDirectoryName(relative); if (!string.IsNullOrWhiteSpace(subFolder)) { Directory.CreateDirectory(Path.Combine(Cache, subFolder)); } var path = Path.Combine(Cache, Path.GetRelativePath(FromOrigin.Cache, file)); if (!File.Exists(path) || File.GetLastWriteTime(path) < File.GetLastWriteTime(file)) { File.Copy(file, path, true); } } } if (FromOrigin.Logs != Logs && Path.Exists(FromOrigin.Logs)) { foreach (var file in Directory.GetFiles(FromOrigin.Logs)) { var path = Path.Combine(Logs, Path.GetRelativePath(FromOrigin.Logs, file)); if (!File.Exists(path) || File.GetLastWriteTime(path) < File.GetLastWriteTime(file)) { File.Copy(file, path, true); } } } if (!Directory.Exists(Storage)) { Directory.CreateDirectory(Storage); Logger?.LogInformation("Created storage folder: {}", Storage); } if (Storage != FromOrigin.Storage && Path.Exists(FromOrigin.Storage)) { foreach (var file in Directory.GetFiles(FromOrigin.Storage, "*", SearchOption.AllDirectories)) { var relative = Path.GetRelativePath(FromOrigin.Storage, file); var subFolder = Path.GetDirectoryName(relative); if (!string.IsNullOrWhiteSpace(subFolder)) { Directory.CreateDirectory(Path.Combine(Storage, subFolder)); } var path = Path.Combine(Storage, Path.GetRelativePath(FromOrigin.Storage, file)); if (!File.Exists(path) || File.GetLastWriteTime(path) < File.GetLastWriteTime(file)) { File.Copy(file, path, true); } } if (Directory.Exists(Path.Combine(Storage, NNSFolderNames.ClassicStorageSource))) { if (!Directory.Exists(Path.Combine(Storage, StorageNames.StorageSource))) { Directory.Move( Path.Combine(Storage, NNSFolderNames.ClassicStorageSource), Path.Combine(Storage, StorageNames.StorageSource) ); } else { Directory.Delete(Path.Combine(Storage, NNSFolderNames.ClassicStorageSource), true); } } } if (!Directory.Exists(Programs)) { Directory.CreateDirectory(Programs); Logger?.LogInformation("Created programs folder: {}", Programs); } if (Programs != FromOrigin.Programs && Path.Exists(FromOrigin.Programs)) { foreach (var file in Directory.GetFiles(FromOrigin.Programs, "*.nabu|*.NABU", SearchOption.TopDirectoryOnly)) { var path = Path.Combine(Programs, Path.GetFileName(file)); if (!File.Exists(path) || File.GetLastWriteTime(path) < File.GetLastWriteTime(file)) { File.Copy(file, path, true); } } } if (!Directory.Exists(Packages)) { Directory.CreateDirectory(Packages); Logger?.LogInformation("Created packages folder: {}", Packages); } if (Packages != FromOrigin.Packages && Path.Exists(FromOrigin.Packages)) { foreach (var file in Directory.GetFiles(FromOrigin.Packages, "*", SearchOption.AllDirectories)) { var relative = Path.GetRelativePath(FromOrigin.Packages, file); var subFolder = Path.GetDirectoryName(relative); if (!string.IsNullOrWhiteSpace(subFolder)) { Directory.CreateDirectory(Path.Combine(Packages, subFolder)); } var path = Path.Combine(Packages, Path.GetRelativePath(FromOrigin.Packages, file)); if (!File.Exists(path) || File.GetLastWriteTime(path) < File.GetLastWriteTime(file)) { File.Copy(file, path, true); } } } }}
0 commit comments