private void UpdateFullPathTree(FullPathNode fullPathTree, string oldSaveLocation, string newSaveLocation) { fullPathTree.FullPath = fullPathTree.FullPath.Replace(oldSaveLocation, newSaveLocation); foreach (var child in fullPathTree.Nodes) { UpdateFullPathTree(child, oldSaveLocation, newSaveLocation); } }
private FullPathNode CreateFullPathTree(KoreFileInfo kfi) { var newNode = new FullPathNode(kfi.FullPath); if (kfi.ChildKfi != null) { foreach (var child in kfi.ChildKfi) { newNode.Nodes.Add(CreateFullPathTree(child)); } } return(newNode); }
private KoreFileInfo SaveFile(KoreSaveInfo ksi, bool firstIteration) { var kfi = ksi.Kfi; var tempFolder = ksi.TempFolder; if (!string.IsNullOrEmpty(ksi.NewSaveFile) && File.Exists(ksi.NewSaveFile) && File.GetAttributes(ksi.NewSaveFile).HasFlag(FileAttributes.Directory)) { throw new InvalidOperationException($"{nameof(ksi.NewSaveFile)} needs to be a file path."); } var guid = Guid.NewGuid().ToString(); // Get FullPath tree FullPathNode fullPathTree = null; if (firstIteration) { fullPathTree = CreateFullPathTree(ksi.Kfi); } // Save all children first, if they exist SaveChildren(ksi); // Save data with the adapter var fs = new PhysicalDirectoryNode(guid, Path.GetFullPath(ksi.TempFolder)); SaveWithAdapter(ksi, fs); // Close KFIs CloseFile(kfi, kfi.ParentKfi != null, firstIteration); // Replace data in parent KFI or physical folder fs = new PhysicalDirectoryNode("", Path.Combine(Path.GetFullPath(ksi.TempFolder), guid)); if (kfi.ParentKfi != null) { ReplaceFilesInAdapter(kfi.ParentKfi.Adapter as IArchiveAdapter, fs, fs.RootPath); } else { var newLocation = string.IsNullOrEmpty(ksi.NewSaveFile) ? kfi.FullPath : ksi.NewSaveFile; var newSaveDir = Path.GetDirectoryName(newLocation); ReplaceFilesInFolder(newSaveDir, fs, fs.RootPath); UpdateFullPathTree(fullPathTree, Path.GetDirectoryName(kfi.FullPath), newSaveDir); } // Reopen files recursively from parent to child return(firstIteration ? ReopenFiles(kfi.ParentKfi ?? kfi, fullPathTree, tempFolder, kfi.ParentKfi != null) : null); }
private KoreFileInfo ReopenFiles(KoreFileInfo kfi, FullPathNode fullPathTree, string tempFolder, bool isChild) { var guid = Guid.NewGuid().ToString(); // Open this file KoreFileInfo newKfi; if (isChild) { var parentFiles = (kfi.Adapter as IArchiveAdapter)?.Files; var foundAfi = parentFiles.FirstOrDefault(x => x.FileName == fullPathTree.FullPath.Remove(0, kfi.FullPath.Length + 1)); if (foundAfi == null) { throw new InvalidOperationException($"While reopening files, the ArchiveFileInfo with FullPath \"{fullPathTree.FullPath}\" couldn't be found."); } newKfi = LoadFile(new KoreLoadInfo(foundAfi.FileData, foundAfi.FileName) { LeaveOpen = true, Adapter = kfi.Adapter, FileSystem = NodeFactory.FromArchiveFileInfos((kfi.Adapter as IArchiveAdapter).Files) }); // new VirtualFileSystem(kfi.Adapter as IArchiveAdapter, tempFolder) newKfi.ParentKfi = kfi; newKfi.ChildKfi = new List <KoreFileInfo>(); } else { var openedFile = File.Open(fullPathTree.FullPath, FileMode.Open); newKfi = LoadFile(new KoreLoadInfo(openedFile, fullPathTree.FullPath) { LeaveOpen = true, Adapter = kfi.Adapter, FileSystem = NodeFactory.FromDirectory(Path.GetDirectoryName(fullPathTree.FullPath)) }); newKfi.ChildKfi = new List <KoreFileInfo>(); } // Open Childs foreach (var child in fullPathTree.Nodes) { newKfi.ChildKfi.Add(ReopenFiles(newKfi, child, tempFolder, true)); } return(newKfi); }