/// <summary> /// Reads a file structure from an open stream. /// </summary> /// <param name="stream">Stream to read from.</param> /// <returns>File structure populated from the stream.</returns> public static FileStructure Read(Stream stream) { FileStructure fs = new FileStructure(); using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) using (BinaryReader reader = new BinaryReader(wrapper)) { fs.FileFormat = FileStructure.ReadFileFormat(reader); if (FileFormat.Unknown != fs.FileFormat) { fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader); // Remember the data stream offset, which is right after the embedded files have been written. fs.dataStreamOffset = stream.Position; foreach (long size in fs.embeddedFileSizes) { fs.dataStreamOffset += size; } } } fs.stream = stream; return(fs); }
/// <summary> /// Loads a pdb from a path on disk. /// </summary> /// <param name="path">Path to pdb file saved on disk.</param> /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param> /// <returns>Pdb pdb.</returns> public static Pdb Load(string path, bool suppressVersionCheck) { using (FileStream stream = File.OpenRead(path)) using (FileStructure fs = FileStructure.Read(stream)) { if (FileFormat.Wixpdb != fs.FileFormat) { throw new WixUnexpectedFileFormatException(path, FileFormat.Wixpdb, fs.FileFormat); } Uri uri = new Uri(Path.GetFullPath(path)); using (XmlReader reader = XmlReader.Create(fs.GetDataStream(), null, uri.AbsoluteUri)) { try { reader.MoveToContent(); return(Pdb.Read(reader, suppressVersionCheck)); } catch (XmlException xe) { throw new WixCorruptFileException(path, fs.FileFormat, xe); } } } }
/// <summary> /// Reads a file structure from an open stream. /// </summary> /// <param name="stream">Stream to read from.</param> /// <returns>File structure populated from the stream.</returns> public static FileStructure Read(Stream stream) { var fs = new FileStructure(); using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { fs.FileFormat = FileStructure.ReadFileFormat(reader); if (fs.FileFormat != FileFormat.Unknown) { fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader); // Remember the data stream offset, which is right after the embedded files have been written. fs.dataStreamOffset = stream.Position; foreach (long size in fs.embeddedFileSizes) { fs.dataStreamOffset += size; } } } fs.stream = stream; return(fs); }
/// <summary> /// Loads json form of intermedaite from stream. /// </summary> /// <param name="stream">Stream to intermediate file.</param> /// <param name="baseUri">Path name of intermediate file.</param> /// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param> /// <returns>Returns the loaded json.</returns> private static JsonObject LoadJson(Stream stream, Uri baseUri, bool suppressVersionCheck) { JsonObject jsonObject; using (var fs = FileStructure.Read(stream)) { if (FileFormat.WixIR != fs.FileFormat) { throw new WixUnexpectedFileFormatException(baseUri.LocalPath, FileFormat.WixIR, fs.FileFormat); } var json = fs.GetData(); jsonObject = SimpleJson.DeserializeObject(json) as JsonObject; } if (!suppressVersionCheck) { var versionJson = jsonObject.GetValueOrDefault <string>("version"); if (!Version.TryParse(versionJson, out var version) || !Intermediate.CurrentVersion.Equals(version)) { throw new WixException(ErrorMessages.VersionMismatch(SourceLineNumber.CreateFromUri(baseUri.AbsoluteUri), "intermediate", versionJson, Intermediate.CurrentVersion.ToString())); } } return(jsonObject); }
/// <summary> /// Saves a library to a path on disk. /// </summary> /// <param name="path">Path to save library file to on disk.</param> /// <param name="resolver">The WiX path resolver.</param> public void Save(string path, ILibraryBinaryFileResolver resolver) { List <string> embedFilePaths = new List <string>(); // Resolve paths to files that are to be embedded in the library. if (null != resolver) { foreach (Table table in this.sections.SelectMany(s => s.Tables)) { foreach (Row row in table.Rows) { foreach (ObjectField objectField in row.Fields.Where(f => f is ObjectField)) { if (null != objectField.Data) { string file = resolver.Resolve(row.SourceLineNumbers, table.Name, (string)objectField.Data); if (!String.IsNullOrEmpty(file)) { // File was successfully resolved so track the embedded index as the embedded file index. objectField.EmbeddedFileIndex = embedFilePaths.Count; embedFilePaths.Add(file); } else { Messaging.Instance.OnMessage(WixDataErrors.FileNotFound(row.SourceLineNumbers, (string)objectField.Data, table.Name)); } } else // clear out embedded file id in case there was one there before. { objectField.EmbeddedFileIndex = null; } } } } } // Do not save the library if errors were found while resolving object paths. if (Messaging.Instance.EncounteredError) { return; } // Ensure the location to output the library exists and write it out. Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))); using (FileStream stream = File.Create(path)) using (FileStructure fs = FileStructure.Create(stream, FileFormat.Wixlib, embedFilePaths)) using (XmlWriter writer = XmlWriter.Create(fs.GetDataStream())) { writer.WriteStartDocument(); this.Write(writer); writer.WriteEndDocument(); } }
/// <summary> /// Saves an intermediate to a path on disk. /// </summary> /// <param name="path">Path to save intermediate file to disk.</param> public void Save(string path) { Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))); using (var stream = File.Create(path)) using (var fs = FileStructure.Create(stream, FileFormat.WixIR, this.EmbedFilePaths)) using (var writer = new StreamWriter(fs.GetDataStream())) { var jsonObject = new JsonObject { { "id", this.Id }, { "version", Intermediate.CurrentVersion.ToString() } }; var sectionsJson = new JsonArray(this.Sections.Count); foreach (var section in this.Sections) { var sectionJson = section.Serialize(); sectionsJson.Add(sectionJson); } jsonObject.Add("sections", sectionsJson); var customDefinitions = this.GetCustomDefinitionsInSections(); if (customDefinitions.Count > 0) { var customDefinitionsJson = new JsonArray(customDefinitions.Count); foreach (var kvp in customDefinitions.OrderBy(d => d.Key)) { var customDefinitionJson = kvp.Value.Serialize(); customDefinitionsJson.Add(customDefinitionJson); } jsonObject.Add("definitions", customDefinitionsJson); } if (this.Localizations.Any()) { var localizationsJson = new JsonArray(); foreach (var localization in this.Localizations) { var localizationJson = localization.Serialize(); localizationsJson.Add(localizationJson); } jsonObject.Add("localizations", localizationsJson); } var json = SimpleJson.SerializeObject(jsonObject); writer.Write(json); } }
/// <summary> /// Saves a pdb to a path on disk. /// </summary> /// <param name="path">Path to save pdb file to on disk.</param> public void Save(string path) { Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))); using (FileStream stream = File.Create(path)) using (FileStructure fs = FileStructure.Create(stream, FileFormat.Wixpdb, null)) using (XmlWriter writer = XmlWriter.Create(fs.GetDataStream())) { writer.WriteStartDocument(); this.Write(writer); writer.WriteEndDocument(); } }
/// <summary> /// Creates a new file structure. /// </summary> /// <param name="stream">Stream to write the file structure to.</param> /// <param name="fileFormat">File format for the file structure.</param> /// <param name="embedFilePaths">Paths to files to embedd in the file structure.</param> /// <returns>Newly created file structure.</returns> public static FileStructure Create(Stream stream, FileFormat fileFormat, IEnumerable <string> embedFilePaths) { var fs = new FileStructure(); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { fs.WriteType(writer, fileFormat); fs.WriteEmbeddedFiles(writer, embedFilePaths?.ToArray() ?? Array.Empty <string>()); // Remember the data stream offset, which is right after the embedded files have been written. fs.dataStreamOffset = stream.Position; } fs.stream = stream; return(fs); }
/// <summary> /// Creates a new file structure. /// </summary> /// <param name="stream">Stream to write the file structure to.</param> /// <param name="fileFormat">File format for the file structure.</param> /// <param name="embedFilePaths">Paths to files to embedd in the file structure.</param> /// <returns>Newly created file structure.</returns> public static FileStructure Create(Stream stream, FileFormat fileFormat, List <string> embedFilePaths) { FileStructure fs = new FileStructure(); using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) using (BinaryWriter writer = new BinaryWriter(wrapper)) { fs.WriteType(writer, fileFormat); fs.WriteEmbeddedFiles(writer, embedFilePaths ?? new List <string>()); // Remember the data stream offset, which is right after the embedded files have been written. fs.dataStreamOffset = stream.Position; } fs.stream = stream; return(fs); }
/// <summary> /// Probes a stream to determine the file format. /// </summary> /// <param name="stream">Stream to test.</param> /// <returns>The file format.</returns> public static FileFormat TestFileFormat(Stream stream) { FileFormat format = FileFormat.Unknown; long position = stream.Position; try { using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { format = FileStructure.ReadFileFormat(reader); } } finally { stream.Seek(position, SeekOrigin.Begin); } return(format); }
/// <summary> /// Probes a stream to determine the file format. /// </summary> /// <param name="stream">Stream to test.</param> /// <returns>The file format.</returns> public static FileFormat TestFileFormat(Stream stream) { FileFormat format = FileFormat.Unknown; long position = stream.Position; try { using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) using (BinaryReader reader = new BinaryReader(wrapper)) { format = FileStructure.ReadFileFormat(reader); } } finally { stream.Seek(position, SeekOrigin.Begin); } return(format); }
/// <summary> /// Loads a library from a stream. /// </summary> /// <param name="stream">Stream containing the library file.</param> /// <param name="uri">Uri for finding this stream.</param> /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param> /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param> /// <returns>Returns the loaded library.</returns> public static Library Load(Stream stream, Uri uri, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck) { using (FileStructure fs = FileStructure.Read(stream)) { if (FileFormat.Wixlib != fs.FileFormat) { throw new WixUnexpectedFileFormatException(uri.LocalPath, FileFormat.Wixlib, fs.FileFormat); } using (XmlReader reader = XmlReader.Create(fs.GetDataStream(), null, uri.AbsoluteUri)) { try { reader.MoveToContent(); return(Library.Read(reader, tableDefinitions, suppressVersionCheck)); } catch (XmlException xe) { throw new WixCorruptFileException(uri.LocalPath, fs.FileFormat, xe); } } } }