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 PhysicalFileSystem(Path.Combine(Path.GetFullPath(ksi.TempFolder), guid)); SaveWithAdapter(ksi, fs); // Close KFIs CloseFile(kfi, kfi.ParentKfi != null, firstIteration); // Replace data in parent KFI or physical folder fs = new PhysicalFileSystem(Path.Combine(Path.GetFullPath(ksi.TempFolder), guid)); if (kfi.ParentKfi != null) { ReplaceFilesInAdapter(kfi.ParentKfi.Adapter as IArchiveAdapter, fs, fs.RootDirectory); } else { var newLocation = string.IsNullOrEmpty(ksi.NewSaveFile) ? kfi.FullPath : ksi.NewSaveFile; var newSaveDir = Path.GetDirectoryName(newLocation); ReplaceFilesInFolder(newSaveDir, fs, fs.RootDirectory); UpdateFullPathTree(fullPathTree, newSaveDir, newSaveDir); } // Reopen files recursively from parent to child return(firstIteration ? ReopenFiles(kfi.ParentKfi != null ? kfi.ParentKfi : kfi, fullPathTree, tempFolder, kfi.ParentKfi != null) : null); }
private void SaveChildren(KoreSaveInfo ksi) { var kfi = ksi.Kfi; if (kfi.ChildKfi != null && kfi.ChildKfi.Count > 0 && kfi.HasChanges) { foreach (var child in kfi.ChildKfi) { if (child.HasChanges) { SaveFile(new KoreSaveInfo(child, ksi.TempFolder) { Version = ksi.Version }, false); } } } }
private void SaveWithAdapter(KoreSaveInfo ksi, IFileSystem fs) { var kfi = ksi.Kfi; if (kfi.Adapter is IMultipleFiles multFileAdapter) { multFileAdapter.FileSystem = fs; } kfi.Adapter.LeaveOpen = false; var newFilename = string.IsNullOrEmpty(ksi.NewSaveFile) ? Path.GetFileName(kfi.StreamFileInfo.FileName) : Path.GetFileName(ksi.NewSaveFile); var streaminfo = new StreamInfo { FileData = fs.CreateFile(newFilename), FileName = newFilename }; (kfi.Adapter as ISaveFiles).Save(streaminfo, ksi.Version); if (streaminfo.FileData.CanRead) { streaminfo.FileData.Dispose(); } }
/* - 1. Create filename tree * - 2. Save all files from child to parent * - 3. Close all KFI Streams * - 4. Reset KFI.StreamFileInfo * - 5. Execute LoadFile of KFI.Adapter on new KFI.StreamFileInfo * - 6. Reopen dependent files from parent to child */ public KoreFileInfo SaveFile(KoreSaveInfo ksi) => SaveFile(ksi, true);