using Lumafly.Interfaces; using Lumafly.Models; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO.Abstractions; using System.Linq; using System.Threading.Tasks; using System.IO; using System.Text.Json; using Avalonia.Controls; using MsBox.Avalonia.Dto; using MsBox.Avalonia.Enums; using Lumafly.Util; using System.IO.Compression; using Avalonia.Platform.Storage; using HPackage.Net; using Lumafly.ViewModels; using Lumafly.Views.Windows; namespace Lumafly.Services; public class PackManager : IPackManager { private readonly ISettings _settings; private readonly IInstaller _installer; private readonly IFileSystem _fs; private readonly IModSource _mods; private readonly IOnlineTextStorage _onlineTextStorage; /// /// Private collection to allow constant lookup times of ModItems from name. /// private readonly Dictionary _items; /// /// A list of all the profiles saved by the application. /// private readonly SortableObservableCollection _packList; public SortableObservableCollection PackList => _packList; internal const string packInfoFileName = "packMods.json"; internal const string TempModStorage = "Temp_Mods_Storage"; internal string TempModStorageLocation => Path.Combine(_settings.ManagedFolder, TempModStorage); public PackManager(ISettings settings, IInstaller installer, IModDatabase db, IFileSystem fs, IModSource mods, IOnlineTextStorage onlineTextStorage) { _installer = installer; _fs = fs; _mods = mods; _settings = settings; _onlineTextStorage = onlineTextStorage; _items = db.Items.DistinctBy(x => x.Name).ToDictionary(x => x.Name, x => x); _packList = new SortableObservableCollection(FindAvailablePacks()); _packList.SortBy((pack1, pack2) => string.CompareOrdinal(pack1.Name, pack2.Name)); } /// /// Look through the managed folder for any profiles and add them to the list. /// private IEnumerable FindAvailablePacks() { var packs = new List(); var packFolderLocation = _settings.ManagedFolder; foreach (var folderPath in _fs.Directory.EnumerateDirectories(packFolderLocation)) { var folder = Path.GetFileName(folderPath); if (folder == "Mods") continue; // ignore mod folder var packInfo = Path.Combine(folderPath, packInfoFileName); if (!_fs.File.Exists(packInfo)) continue; try { var pack = JsonSerializer.Deserialize(File.ReadAllText(packInfo)); if (pack?.Name == folder) // only add packs with same name as folder packs.Add(pack); } catch (Exception e) { Trace.TraceError($"Error reading pack info file {e}"); } } Task.Run(() => { if (!_settings.LowStorageMode) return; var managed = new DirectoryInfo(_settings.ManagedFolder); foreach (var dir in managed.EnumerateDirectories()) { if (dir.Name == "Mods") continue; try { if (dir.GetFiles().Any(x => x.Name == packInfoFileName)) { foreach (var subDir in dir.EnumerateDirectories()) { subDir.Delete(true); } dir.EnumerateDirectories(); } } catch (Exception e) { Trace.TraceError($"Error creating pack info file {e}"); } } }); return packs; } /// /// Loads a pack as the main mod list. /// /// The profile to set as current. public async Task LoadPack(string packName, bool additive) { await EnsureGameClosed(); var pack = _packList.FirstOrDefault(x => x.Name == packName); if (pack == null) { await DisplayErrors.DisplayGenericError("Could not set current profile: profile not found!"); return false; } if (additive) return await LoadPackAdditive(pack); else return await LoadPackExclusive(pack); } private async Task CacheOldModsFolder() { // move all the mods to a temp folder so we can revert if the pack enabling fails FileUtil.DeleteDirectory(TempModStorageLocation); FileUtil.CopyDirectory(_settings.ModsFolder, TempModStorageLocation); var tempInstalledMods = Path.Combine(TempModStorageLocation, InstalledMods.FILE_NAME); await using Stream fs = _fs.File.Exists(tempInstalledMods) ? _fs.FileStream.New(tempInstalledMods, FileMode.Truncate) : _fs.File.Create(tempInstalledMods); await JsonSerializer.SerializeAsync(fs, _mods, new JsonSerializerOptions { WriteIndented = true }); fs.Close(); } private async Task LoadPackAdditive(Pack pack) { try { var packFolder = Path.Combine(_settings.ManagedFolder, pack.Name); await CacheOldModsFolder(); foreach (var mod in pack.InstalledMods.Mods.Where(x => x.Value.Enabled)) { if (!_mods.Mods.ContainsKey(mod.Key)) { _mods.Mods.Add(mod.Key, mod.Value); if (Directory.Exists(Path.Combine(packFolder, mod.Key))) { FileUtil.CopyDirectory(Path.Combine(packFolder, mod.Key), Path.Combine(_settings.ModsFolder, mod.Key)); } else { await _installer.Install(_items[mod.Key], _ => { }, true); } } else { if (Directory.Exists(Path.Combine(packFolder, mod.Key))) { FileUtil.CopyDirectory(Path.Combine(packFolder, mod.Key), Path.Combine(_settings.ModsFolder, mod.Key)); } } } await _mods.SetMods(_mods.Mods, _mods.NotInModlinksMods); return true; } catch (Exception e) { await RevertToPreviousModsFolder(); await DisplayErrors.DisplayGenericError("An error occured when activating pack. Lumafly will revert to old mods", e); return false; } } private async Task LoadPackExclusive(Pack pack) { try { var packFolder = Path.Combine(_settings.ManagedFolder, pack.Name); await CacheOldModsFolder(); FileUtil.DeleteDirectory(_settings.ModsFolder); // move the mods from the profile to the mods folder FileUtil.CopyDirectory(packFolder, _settings.ModsFolder); await _mods.SetMods(pack.InstalledMods.Mods, pack.InstalledMods.NotInModlinksMods); var requestedModList = _mods.Mods.Select(x => x.Key) .Concat(_mods.NotInModlinksMods.Select(x => x.Key)) .ToArray(); // get the full dependency list of the pack HashSet fullDependencyList = new(); // ensure all mods that are supposed to be in the pack are installed foreach (string modName in requestedModList) { if (InstalledMods.ModExists(_settings, modName, out _)) continue; if (_items.TryGetValue(modName, out var correspondingMod)) { // to ensure the mod is installed when OnInstall is called correspondingMod.State = new NotInstalledState(); await _installer.Install(correspondingMod, _ => { }, true); // save full dependency list which will be used to decide what to uninstall foreach (var dep in correspondingMod.Dependencies) AddDependency(fullDependencyList, dep); } else // can't install so yeet it from the profile { _mods.Mods.Remove(modName); _mods.NotInModlinksMods.Remove(modName); } } var currentList = _fs.Directory.EnumerateDirectories(_settings.ModsFolder).ToList(); if (_fs.Directory.Exists(_settings.DisabledFolder)) currentList.AddRange(_fs.Directory.EnumerateDirectories(_settings.DisabledFolder)); // uninstall mods that arent in pack foreach (var modNamePath in currentList) { var modName = Path.GetFileName(modNamePath); if (modName == "Disabled") continue; // skip disabled Folder // don't uninstall mods that are said to be in the pack if (_mods.NotInModlinksMods.ContainsKey(modName)) continue; if (_mods.Mods.ContainsKey(modName)) continue; // don't uninstall if the mod is a dependency of a pack mod if (fullDependencyList.Contains(modName)) continue; // since it isn't a listed pack mod or a dependency, uninstall it FileUtil.DeleteDirectory(Path.Combine(_settings.ModsFolder, modName)); FileUtil.DeleteDirectory(Path.Combine(_settings.DisabledFolder, modName)); } // cache result so it is easier to load next time await SavePack(pack.Name, pack.Description, pack.Authors); _fs.File.Delete(Path.Combine(_settings.ModsFolder, packInfoFileName)); return true; } catch (Exception e) { await RevertToPreviousModsFolder(); await DisplayErrors.DisplayGenericError("An error occured when activating pack. Lumafly will revert to old mods", e); return false; } } /// /// Saves the current Mods folder as a pack. Replaces the pack if it already exists. /// public async Task SavePack(string name, string description, string authors) { var packFolder = Path.Combine(_settings.ManagedFolder, name); FileUtil.DeleteDirectory(packFolder); FileUtil.CreateDirectory(packFolder); if (!_settings.LowStorageMode) FileUtil.CopyDirectory(_settings.ModsFolder, packFolder, excludeDir: "Disabled"); var packInfo = Path.Combine(packFolder, packInfoFileName); var packDetails = new Pack(name, description, authors, new InstalledMods() { Mods = _mods.Mods.Where(x => x.Value.Enabled).ToDictionary(x => x.Key, x => x.Value), NotInModlinksMods = _mods.NotInModlinksMods.Where(x => x.Value.Enabled).ToDictionary(x => x.Key, x => x.Value), }); await using Stream fs = _fs.File.Exists(packInfo) ? _fs.FileStream.New(packInfo, FileMode.Truncate) : _fs.File.Create(packInfo); await JsonSerializer.SerializeAsync(fs, packDetails, new JsonSerializerOptions() { WriteIndented = true }); var packInList = PackList.FirstOrDefault(x => x.Name == name); if (packInList != null) // if the pack already exists, replace it packInList.InstalledMods = packDetails.InstalledMods; else // otherwise add it to the list PackList.Add(packDetails); PackList.SortBy((pack1, pack2) => string.CompareOrdinal(pack1.Name, pack2.Name)); return PackList.First(x => x.Name == name); } /// /// Saves the current instance of to disk /// public async Task SaveEditedPack(Pack pack, string? oldPackName = null) { // if the name changed, move the folder to reflect the change if (oldPackName != null && oldPackName != pack.Name) { Directory.Move(Path.Combine(_settings.ManagedFolder, oldPackName), Path.Combine(_settings.ManagedFolder, pack.Name)); } // write the new pack info to disk var packFolder = Path.Combine(_settings.ManagedFolder, pack.Name); FileUtil.CreateDirectory(packFolder); var packInfo = Path.Combine(packFolder, packInfoFileName); await using Stream fs = _fs.File.Exists(packInfo) ? _fs.FileStream.New(packInfo, FileMode.Truncate) : _fs.File.Create(packInfo); await JsonSerializer.SerializeAsync(fs, pack, new JsonSerializerOptions() { WriteIndented = true }); PackList.SortBy((pack1, pack2) => string.CompareOrdinal(pack1.Name, pack2.Name)); } /// /// Remove a profile by object. /// /// The pack to remove. /// The profile to remove was not found in the profile list. public void RemovePack(string packName) { var pack = _packList.FirstOrDefault(x => x.Name == packName); if (pack != null) { _packList.Remove(pack); } FileUtil.DeleteDirectory(Path.Combine(_settings.ManagedFolder, packName)); } /// /// Adds a mods full dependency tree to the dependency list. /// private void AddDependency(ISet fullDependencyList, string depName) { // so we don't unnecessarily go through the dependency list if (fullDependencyList.Contains(depName)) return; fullDependencyList.Add(depName); // add all of its dependency to the dependency list if (_items.TryGetValue(depName, out var depMod)) { foreach (var dep in depMod.Dependencies) { AddDependency(fullDependencyList, dep); } } } private async Task EnsureGameClosed() { static bool IsHollowKnight(Process p) => ( (p.ProcessName.StartsWith("hollow_knight") || p.ProcessName.StartsWith("Hollow Knight")) && p.ProcessName != "Hollow Knight Silksong" ); if (Process.GetProcesses().FirstOrDefault(IsHollowKnight) is { } proc) { var res = await MessageBoxUtil.GetMessageBoxStandardWindow(new MessageBoxStandardParams { ContentTitle = Resources.MLVM_InternalUpdateInstallAsync_Msgbox_W_Title, ContentMessage = Resources.MLVM_InternalUpdateInstallAsync_Msgbox_W_Text, ButtonDefinitions = ButtonEnum.YesNo, MinHeight = 200, SizeToContent = SizeToContent.WidthAndHeight, }).ShowAsPopupAsync(AvaloniaUtils.GetMainWindow()); if (res == ButtonResult.Yes) proc.Kill(); } } /// /// Create a .zip file containing the pack in location specified by user /// /// public async void SavePackToZip(string packName) { await EnsureGameClosed(); var pack = _packList.FirstOrDefault(x => x.Name == packName); if (pack == null) { await DisplayErrors.DisplayGenericError("Could not share profile: profile not found!"); return; } var packFolder = Path.Combine(_settings.ManagedFolder, packName); TopLevel? topLevel = TopLevel.GetTopLevel(AvaloniaUtils.GetMainWindow()); // Necessary for save file picker if (topLevel == null) return; var options = new FilePickerSaveOptions { Title = "Select save location", ShowOverwritePrompt = true, DefaultExtension = "zip", SuggestedFileName = packName, FileTypeChoices = new List() { new ("ZIP Archive") { Patterns = new List() { "*.zip" } } } }; // This crashes on multiple repeated attempts due to avalonia issue IStorageFile? storage_file = await topLevel.StorageProvider.SaveFilePickerAsync(options); string? outputFilePath = storage_file?.TryGetLocalPath(); if (outputFilePath == null) return; // Couldn't get local path for some reason CreateZip(packFolder, outputFilePath); } public static Window? CurrentPackWindow; /// /// Opens a window to edit pack details /// /// The pack to edit public async Task EditPack(Pack pack) { string oldPackName = pack.Name; Pack copiedPack = pack.DeepCopy(); CurrentPackWindow = new EditPackWindow { DataContext = new EditPackWindowViewModel(copiedPack, _items.Values) }; var shouldSave = await CurrentPackWindow.ShowDialog(AvaloniaUtils.GetMainWindow()); if (shouldSave) { pack.Copy(copiedPack); if (!string.IsNullOrEmpty(pack.SharingCode) && pack.IsSame(copiedPack)) pack.SharingCode = null; await SaveEditedPack(copiedPack, oldPackName); } } /// /// Create a .zip file from a directory. Returns if the creation was successful /// /// Path to the directory to be zipped /// Path to the new .zip file private bool CreateZip(string sourcePath, string outputFilePath) { try { if (!Directory.Exists(sourcePath)) { throw new DirectoryNotFoundException("Couldn't find the source directory"); } ZipFile.CreateFromDirectory(sourcePath, outputFilePath); return true; } catch { return false; } } /// /// Uploads pack to an online text storage like pastebin /// public async Task UploadPack(string packName) { var pack = _packList.FirstOrDefault(x => x.Name == packName); if (pack == null) return; try { var code = await _onlineTextStorage.Upload(packName, ConvertToHPackage(pack)); pack.SharingCode = code; await SaveEditedPack(pack); } catch (Exception e) { await DisplayErrors.DisplayGenericError("Failed to upload pack", e); } } /// /// Imports a pack /// /// /// public async Task ImportPack(string code) { try { var packJson = await _onlineTextStorage.Download(code); var pack = ConvertFromHPackage(packJson); pack.SharingCode = code; //since we have it lets save it if (PackList.Any(x => x.Name == pack.Name)) throw new ReadableError("a pack with same name already exists"); PackList.Add(pack); await SaveEditedPack(pack); return pack; } catch (ReadableError re) { await MessageBoxUtil.GetMessageBoxStandardWindow("Failed to import pack", $"Failed to import pack from code: {code} as {re.Message}") .ShowAsPopupAsync(AvaloniaUtils.GetMainWindow()); return null; } catch (Exception e) { await DisplayErrors.DisplayGenericError("Failed to import pack", e); return null; } } /// /// Converts a pack to a valid hpackage format /// /// The hpackage json as string private string ConvertToHPackage(Pack pack) { var packageDef = new HollowKnightPackageDef() { Name = pack.Name, Description = pack.Description, Authors = new List { pack.Authors }, Dependencies = new References() { AnythingMap = pack.InstalledMods.Mods.ToDictionary( x => x.Key, x => new ReferenceVersion() { String = JsonSerializer.Serialize(x.Value) }) } }; return packageDef.ToJson(); } /// /// Converts a hpackage json string to a Pack /// private Pack ConvertFromHPackage(string json) { var packageDef = HollowKnightPackageDef.FromJson(json); var pack = new Pack( packageDef.Name, packageDef.Description, string.Join(',', packageDef.Authors), new InstalledMods() { Mods = packageDef.Dependencies!.Value.AnythingMap.ToDictionary( x => x.Key, x => JsonSerializer.Deserialize(x.Value.String)!) }); return pack; } public async Task RevertToPreviousModsFolder() { FileUtil.DeleteDirectory(_settings.ModsFolder); var installedMods = JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Combine(TempModStorageLocation, InstalledMods.FILE_NAME))); if (installedMods != null) await _mods.SetMods(installedMods.Mods, installedMods.NotInModlinksMods); Directory.Move(TempModStorageLocation, _settings.ModsFolder); } }