Skip to content

Commit 72ad34a

Browse files
author
Nick Daniels
committed
Modification to HOME migration for lInux
1 parent 57d14ac commit 72ad34a

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

Nabu.Lib/Network/LocationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using Gry.Settings;using Lgc;using Microsoft.Extensions.Logging;using Nabu.Settings;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using YamlDotNet.Serialization;namespace Nabu.Network;public class LocationService : ILocationService, ISingletonDependency{ public LocationService( ILogger<LocationService>? logger = null, GlobalSettings? settings = null, bool? useClassic = null ) { StorageNames.Home = ".nns"; if (settings?.UseHome is true || useClassic is false) { UseClassic = false; } FromHome = PathSet.FromHome(); FromOrigin = OriginPaths(settings?.StoragePath, settings?.LocalProgramPath); if (!UseClassic) { EnsureLocations(); } Logger = logger; Settings = settings!; } ILogger<LocationService>? Logger { get; } public GlobalSettings Settings { get; } protected bool UseClassic { get; } = true; public PathSet FromHome { get; } public PathSet FromOrigin { get; } public PathSet Of => UseClassic ? FromOrigin : FromHome; public string Home => Of.Home; public string Storage => Of.Storage; public string Cache => Of.Cache;
22
public string Programs => Of.Programs;
33
public string Logs => Of.Logs;
4-
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))) { Directory.Move( Path.Combine(Storage, NNSFolderNames.ClassicStorageSource), Path.Combine(Storage, StorageNames.StorageSource) ); } } 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); } } } }}
4+
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

Comments
 (0)