/// <summary> /// Function to load in the meta data. /// </summary> /// <remarks>Use this method to retrieve any stored meta data. If no meta data exists, then this function will do nothing.</remarks> /// <exception cref="GorgonLibrary.GorgonException">Thrown when the meta data is corrupted.</exception> public static void Load() { _metaDataFile = ScratchArea.ScratchFiles.GetFile(_path); // If the file doesn't exist yet, then move on. if (_metaDataFile == null) { Reset(); return; } // Otherwise, load it up and parse it. using (Stream stream = _metaDataFile.OpenStream(false)) { _metaData = XDocument.Load(stream); } // Validate the file. XElement rootNode = _metaData.Element(MetaDataRootName); GetWriterSettings(rootNode); GetFiles(rootNode); ScratchArea.AddBlockedFile(_metaDataFile); }
/// <summary> /// Function to load dependencies for a file. /// </summary> /// <param name="content">The content that is being loaded.</param> /// <param name="file">The file that contains the dependencies.</param> /// <param name="missing">A list of dependencies that are missing.</param> private static void LoadDependencies(ContentObject content, EditorFile file, ICollection <string> missing) { foreach (Dependency dependencyFile in file.DependsOn) { GorgonFileSystemFileEntry externalFile = OnGetDependency(dependencyFile.EditorFile.FilePath); if (externalFile == null) { throw new FileNotFoundException(string.Format(APIResources.GOREDIT_ERR_CANNOT_FIND_DEPENDENCY_FILE, dependencyFile.EditorFile.FilePath)); } // If the dependency file contains any dependencies, then we need to load it them prior to loading the actual dependency. if (dependencyFile.EditorFile.DependsOn.Count > 0) { LoadDependencies(content, dependencyFile.EditorFile, missing); } // Read the external content. using (Stream dependencyStream = externalFile.OpenStream(false)) { DependencyLoadResult result = content.LoadDependencyFile(dependencyFile, dependencyStream); switch (result.State) { case DependencyLoadState.FatalError: throw new GorgonException(GorgonResult.CannotRead, string.Format(APIResources.GOREDIT_ERR_CANNOT_LOAD_DEPENDENCY, dependencyFile.EditorFile.FilePath, result.Message)); case DependencyLoadState.ErrorContinue: missing.Add(string.Format(APIResources.GOREDIT_DLG_CANNOT_LOAD_DEPENDENCY, dependencyFile.EditorFile.FilePath, result.Message)); break; } } } }
/// <summary> /// Function to add a file to the tree. /// </summary> /// <param name="nodes">Source collection.</param> /// <param name="file">File to add.</param> /// <returns>The new node.</returns> public static TreeNodeFile AddFile(this TreeNodeCollection nodes, GorgonFileSystemFileEntry file) { if (file == null) { throw new ArgumentNullException("file"); } // Find check to ensure the node is unique. TreeNodeFile result = (from node in nodes.Cast <TreeNode>() let dirNode = node as TreeNodeFile where dirNode != null && string.Equals(node.Name, file.FullPath, StringComparison.OrdinalIgnoreCase) select dirNode).FirstOrDefault(); if (result != null) { return(result); } result = new TreeNodeFile(); result.UpdateFile(file); nodes.Add(result); if ((result.Parent != null) && (!result.Parent.IsExpanded)) { result.Parent.Expand(); } return(result); }
/// <summary> /// Function to include a file in the editor. /// </summary> /// <param name="file">File entry to include.</param> /// <returns>An editor file object linking the file to the the system.</returns> public static EditorFile IncludeItem(GorgonFileSystemFileEntry file) { ContentPlugIn plugIn = GetContentPlugInForFile(file.Extension); string plugInType = string.Empty; if (plugIn != null) { plugInType = plugIn.GetType().FullName; } var fileItem = new EditorFile(file.FullPath) { PlugInType = plugInType }; if (plugIn != null) { using (Stream fileStream = file.OpenStream(false)) { plugIn.GetEditorFileAttributes(fileStream, fileItem.Attributes); } } EditorMetaDataFile.Files[fileItem.FilePath] = fileItem; return(fileItem); }
/// <summary> /// Function called when a file is opened as a file stream. /// </summary> /// <param name="file">File to open.</param> /// <returns> /// The open <see cref="GorgonFileSystemStream" /> file stream object. /// </returns> protected override GorgonFileSystemStream OnOpenFileStream(GorgonFileSystemFileEntry file) { return(new GorgonGorPackFileStream(file, File.Open(file.MountPoint, FileMode.Open, FileAccess.Read, FileShare.Read), (_compressedFiles.ContainsKey(file.FullPath) ? new CompressedFileEntry?(_compressedFiles[file.FullPath]) : null))); }
/// <summary> /// Function to retrieve the node associated with the current content. /// </summary> /// <param name="file">File associated with the node.</param> /// <returns>The node associated with the current content.</returns> public TreeNodeFile GetCurrentContentNode(GorgonFileSystemFileEntry file) { // Find our node that corresponds to this content. return((from treeNode in AllNodes() let fileNode = treeNode as TreeNodeFile where fileNode != null && fileNode.NodeType == NodeType.File && fileNode.File == file select fileNode).FirstOrDefault()); }
/// <summary> /// Function to create a new file node. /// </summary> /// <param name="file">File to retrieve information from.</param> /// <param name="position">Position of the file in the packed data.</param> /// <param name="size">Size of the compressed file in the packed data.</param> /// <param name="compressedSize">Compressed size of the file.</param> /// <returns>A new node element with the file information.</returns> private static XElement CreateFileNode(GorgonFileSystemFileEntry file, long position, long size, long compressedSize) { return(new XElement("File", new XElement("Filename", file.BaseFileName), new XElement("Extension", file.Extension), new XElement("Offset", position), new XElement("Size", size), new XElement("CompressedSize", compressedSize), new XElement("FileDate", file.CreateDate.ToString(CultureInfo.InvariantCulture.DateTimeFormat)), new XElement("Encrypted", false), new XElement("Comment", "Gorgon.Editor"))); }
/// <summary> /// Function to update the file information. /// </summary> /// <param name="file">File system file entry to use.</param> public void UpdateFile(GorgonFileSystemFileEntry file) { Name = file.FullPath; Text = file.Name; GetFileData(); // We have dependencies, so update. if ((_editorFile != null) && (_editorFile.DependsOn.Count > 0)) { Nodes.Add(new TreeNode("DummyNode")); } }
/// <summary> /// Initializes a new instance of the <see cref="GorgonGorPackFileStream"/> class. /// </summary> /// <param name="file">The file.</param> /// <param name="stream">The stream.</param> /// <param name="compressionInfo">Compression information for the file.</param> internal GorgonGorPackFileStream(GorgonFileSystemFileEntry file, Stream stream, GorgonGorPackProvider.CompressedFileEntry?compressionInfo) : base(file, stream) { stream.Position = file.Offset; // Set the offset here. if (compressionInfo.HasValue) { _bzipStream = new BZip2InputStream(stream); _length = compressionInfo.Value.Size; } else { _bzipStream = stream; _length = file.Size; } _basePosition = stream.Position; }
/// <summary> /// Function to open a file stream to the first selected file. /// </summary> /// <returns>A read-only stream to the first select file.</returns> /// <exception cref="System.IO.FileNotFoundException">Thrown if no files were selected, or the file was not found in the file system.</exception> public Stream OpenFile() { if ((Files.Length == 0) || (Files[0] == null)) { throw new FileNotFoundException(APIResources.GOREDIT_ERR_NO_FILES_SELECTED); } GorgonFileSystemFileEntry file = ScratchArea.ScratchFiles.GetFile(Files[0].FilePath); if (file == null) { throw new FileNotFoundException(string.Format(APIResources.GOREDIT_ERR_FILE_NOT_FOUND, Files[0].FilePath)); } return(file.OpenStream(false)); }
/// <summary> /// Function to store the meta data. /// </summary> /// <remarks>Use this to store the meta data in the file system.</remarks> public static void Save() { if (_metaData == null) { return; } XElement root = _metaData.Element(MetaDataRootName); if (root == null) { root = new XElement(MetaDataRootName); _metaData.Add(root); } // Add or update the elements to the XML document. XElement writerPlugInElement = AddOrUpdateNode(root, WriterPlugInNode); XAttribute writerTypeAttr = AddOrUpdateAttribute(writerPlugInElement, TypeNameAttr, WriterPlugInType); if (string.IsNullOrWhiteSpace(WriterPlugInType)) { writerTypeAttr.Remove(); } // Add files. if (Files.Count > 0) { XElement fileList = AddOrUpdateNode(root, EditorFilesNode); fileList.Add(Files.Serialize()); } _metaDataFile = ScratchArea.ScratchFiles.WriteFile(_path, null); using (Stream stream = _metaDataFile.OpenStream(true)) { _metaData.Save(stream); } // Add to the block list if this file should not show up. ScratchArea.AddBlockedFile(_metaDataFile); }
/// <summary> /// Function to get the zip entry stream. /// </summary> /// <param name="file">File in the zip file to read.</param> /// <param name="stream">Stream to the zip file.</param> private void GetZipEntryStream(GorgonFileSystemFileEntry file, Stream stream) { ZipEntry entry; _zipStream = new ZipInputStream(stream); while ((entry = _zipStream.GetNextEntry()) != null) { if (!entry.IsFile) { continue; } string newPath = entry.Name; string filePath = file.PhysicalFileSystemPath.Substring(file.PhysicalFileSystemPath.LastIndexOf(':') + 1); if (!newPath.StartsWith("/")) { newPath = "/" + newPath; } if (!string.Equals(newPath, filePath, StringComparison.OrdinalIgnoreCase)) { continue; } _length = _zipStream.Length; _basePosition = _zipStream.Position; return; } if (_zipStream != null) { _zipStream.Dispose(); } throw new FileNotFoundException(string.Format(Resources.GORFS_FILE_NOT_FOUND, file.PhysicalFileSystemPath)); }
/// <summary> /// Initializes a new instance of the <see cref="GorgonZipFileStream"/> class. /// </summary> /// <param name="file">The file.</param> /// <param name="stream">The stream.</param> internal GorgonZipFileStream(GorgonFileSystemFileEntry file, Stream stream) : base(file, stream) { GetZipEntryStream(file, stream); }
/// <summary> /// Function called when a file is opened as a file stream. /// </summary> /// <param name="file">File to open.</param> /// <returns> /// The open <see cref="GorgonFileSystemStream" /> file stream object. /// </returns> protected override GorgonFileSystemStream OnOpenFileStream(GorgonFileSystemFileEntry file) { return(new GorgonZipFileStream(file, File.Open(file.MountPoint, FileMode.Open, FileAccess.Read, FileShare.Read))); }
/// <summary> /// Function to load content data from the file system. /// </summary> /// <param name="editorFile">The editor file data.</param> /// <param name="file">The file system file that contains the content data.</param> /// <param name="plugIn">The plug-in used to open the file.</param> /// <param name="reload">TRUE to just reload the file, FALSE to do a complete load of the content.</param> public static void Load(EditorFile editorFile, GorgonFileSystemFileEntry file, ContentPlugIn plugIn, bool reload = false) { ContentObject content; if (file == null) { throw new ArgumentNullException("file"); } if (!reload) { ContentSettings settings = plugIn.GetContentSettings(); // Get default settings. if (settings != null) { // Assign the name from the file. settings.Name = file.Name; settings.CreateContent = false; } content = CreateContentObjectInstance(plugIn, settings, editorFile, false); Debug.Assert(_currentContentObject != null, "Content should not be NULL!"); } else { content = Current; } // Load the content dependencies if any exist. // Check for dependencies. if ((editorFile != null) && (editorFile.DependsOn.Count > 0)) { var missingDependencies = new List <string>(); try { LoadDependencies(content, content.EditorFile, missingDependencies); } catch { content.Dependencies.Clear(); throw; } if ((missingDependencies.Count > 0) && (DependencyNotFound != null)) { DependencyNotFound(file.Name, missingDependencies); } } if (!reload) { LoadContentPane(content); } // Load in the content data. using (Stream stream = file.OpenStream(false)) { content.Read(stream); } ContentFile = file; content.OnContentReady(); }
/// <summary> /// Initializes a new instance of the <see cref="FileWriteEventArgs"/> class. /// </summary> /// <param name="file">The file.</param> public FileWriteEventArgs(GorgonFileSystemFileEntry file) { File = file; }