/// <summary> /// Initializes a new instance of the <see cref="ContentArchiveNode"/> class from an archive stream. /// </summary> /// <param name="parent">The node's parent node.</param> /// <param name="reader">A <see cref="BinaryReader"/> on the stream containing the archive data.</param> private ContentArchiveNode(ContentArchiveNode parent, BinaryReader reader) { this.parent = parent; this.name = reader.ReadString(); this.path = BuildPath(parent, name); this.isFile = reader.ReadBoolean(); this.isDirectory = !this.isFile; if (this.isFile) { this.position = reader.ReadInt64(); this.sizeInBytes = reader.ReadInt64(); } else { var childList = new List <ContentArchiveNode>(); var childCount = reader.ReadInt32(); for (int i = 0; i < childCount; i++) { var node = new ContentArchiveNode(this, reader); childList.Add(node); } this.children = childList; } }
/// <summary> /// Initializes a new instance of the <see cref="ContentArchiveNode"/> class from a file or directory in the file system. /// </summary> /// <param name="parent">The node's parent node.</param> /// <param name="path">The path to the file or directory that this node represents.</param> private ContentArchiveNode(ContentArchiveNode parent, String path) { path = System.IO.Path.GetFullPath(path); this.parent = parent; this.path = path; this.name = System.IO.Path.GetFileName(path); this.isFile = File.Exists(path); this.isDirectory = Directory.Exists(path); if (!isFile && !isDirectory) { throw new FileNotFoundException(path); } if (isFile) { using (var stream = File.OpenRead(path)) { this.sizeInBytes = stream.Length; } } else { LoadChildren(path); } }
/// <summary> /// Initializes a new instance of the <see cref="ContentArchiveNode"/> class from an archive stream. /// </summary> /// <param name="parent">The node's parent node.</param> /// <param name="reader">A <see cref="BinaryReader"/> on the stream containing the archive data.</param> private ContentArchiveNode(ContentArchiveNode parent, BinaryReader reader) { this.parent = parent; this.name = reader.ReadString(); this.path = BuildPath(parent, name); this.isFile = reader.ReadBoolean(); this.isDirectory = !this.isFile; if (this.isFile) { this.position = reader.ReadInt64(); this.sizeInBytes = reader.ReadInt64(); } else { var childList = new List<ContentArchiveNode>(); var childCount = reader.ReadInt32(); for (int i = 0; i < childCount; i++) { var node = new ContentArchiveNode(this, reader); childList.Add(node); } this.children = childList; } }
/// <summary> /// Initializes a new instance of the <see cref="ContentArchiveNode"/> class from a file or directory in the file system. /// </summary> /// <param name="parent">The node's parent node.</param> /// <param name="path">The path to the file or directory that this node represents.</param> private ContentArchiveNode(ContentArchiveNode parent, String path) { path = System.IO.Path.GetFullPath(path); this.parent = parent; this.path = path; this.name = System.IO.Path.GetFileName(path); this.isFile = File.Exists(path); this.isDirectory = Directory.Exists(path); if (!isFile && !isDirectory) throw new FileNotFoundException(path); if (isFile) { using (var stream = File.OpenRead(path)) { this.sizeInBytes = stream.Length; } } else { LoadChildren(path); } }
/// <summary> /// Builds an asset path for a node. /// </summary> /// <param name="parent">The node's parent node.</param> /// <param name="name">The node's name.</param> /// <returns>The asset path for a node with the specified parameters.</returns> private static String BuildPath(ContentArchiveNode parent, String name) { var builder = new StringBuilder(name); var current = parent; while (current != null) { builder.Insert(0, '/'); builder.Insert(0, current.Name); current = (ContentArchiveNode)current.Parent; } return(builder.ToString()); }
/// <summary> /// Loads the node's child nodes from the file system. /// </summary> /// <param name="path">The full path that the node represents.</param> private void LoadChildren(String path) { var list = new List <ContentArchiveNode>(); foreach (var directory in Directory.GetDirectories(path)) { var node = new ContentArchiveNode(this, directory); list.Add(node); } foreach (var file in Directory.GetFiles(path)) { var node = new ContentArchiveNode(this, file); list.Add(node); } this.children = list; }
private static void ListArchive(ContentArchiveNode node, Int32 indentation) { var indent = new String(' ', indentation); Console.WriteLine(indent + node.Name); foreach (ContentArchiveNode child in node.Children) { ListArchive(child, indentation + 1); } }
/// <summary> /// Loads the node's child nodes from the file system. /// </summary> /// <param name="path">The full path that the node represents.</param> private void LoadChildren(String path) { var list = new List<ContentArchiveNode>(); foreach (var directory in Directory.GetDirectories(path)) { var node = new ContentArchiveNode(this, directory); list.Add(node); } foreach (var file in Directory.GetFiles(path)) { var node = new ContentArchiveNode(this, file); list.Add(node); } this.children = list; }
/// <summary> /// Builds an asset path for a node. /// </summary> /// <param name="parent">The node's parent node.</param> /// <param name="name">The node's name.</param> /// <returns>The asset path for a node with the specified parameters.</returns> private static String BuildPath(ContentArchiveNode parent, String name) { var builder = new StringBuilder(name); var current = parent; while (current != null) { builder.Insert(0, '/'); builder.Insert(0, current.Name); current = (ContentArchiveNode)current.Parent; } return builder.ToString(); }