private void WriteDescriptions() { BW mainWriter = new BW(m_contentStream); foreach (ComponentDescription description in Descriptions) { using (MemoryStream tempStream = new MemoryStream()) { BW writer = new BW(tempStream); writer.Write(NewResourceID()); // ID writer.Write(6u); // 6 sections #region Metadata // Write METADATA /* Component name (string) * Can resize (bool) * Minimum size (bool) * GUID (byte[16]) * Author (string) * Version (major, minor) (uint, uint) * AdditionalInformation (string) * Implement set (string) * Implement item (string) * IconresourceID (int) * Timestamp (long) */ using (MemoryStream metadatSectionStream = new MemoryStream()) { BW metadataWriter = new BW(metadatSectionStream); metadataWriter.WriteNullString(description.ComponentName); metadataWriter.Write(description.CanResize); metadataWriter.Write(description.CanFlip); metadataWriter.Write(description.MinSize); metadataWriter.Write(description.Metadata.GUID.ToByteArray()); metadataWriter.WriteNullString(description.Metadata.Author); metadataWriter.Write((ushort)description.Metadata.Version.Major); metadataWriter.Write((ushort)description.Metadata.Version.Minor); metadataWriter.WriteNullString(description.Metadata.AdditionalInformation); metadataWriter.WriteNullString(description.Metadata.ImplementSet); metadataWriter.WriteNullString(description.Metadata.ImplementItem); if (description.Metadata.Icon != null) { uint iconResourceID = NewResourceID(); foreach (var icon in description.Metadata.Icon as MultiResolutionImage) { BinaryResource iconResource = new BinaryResource(); iconResource.ID = iconResourceID; // Only PNG images can be written at the moment iconResource.ResourceType = IO.Descriptions.BinaryResourceType.PNGImage; iconResource.Buffer = icon.Data; Resources.Add(iconResource); } metadataWriter.Write((int)iconResourceID); } else { metadataWriter.Write(-1); } metadataWriter.Write(DateTime.Now.ToBinary()); writer.Write((ushort)BinaryConstants.ComponentSectionType.Metadata); writer.Write((uint)metadatSectionStream.Length); writer.Write(metadatSectionStream.ToArray()); } #endregion #region Flags // Write FLAGS using (MemoryStream flagsSectionStream = new MemoryStream()) { BW flagsWriter = new BW(flagsSectionStream); flagsWriter.Write((uint)description.Flags.Length); foreach (Conditional <FlagOptions> flags in description.Flags) { flagsWriter.Write(flags.Conditions); flagsWriter.Write((uint)flags.Value); } writer.Write((ushort)BinaryConstants.ComponentSectionType.Flags); writer.Write((uint)flagsSectionStream.Length); writer.Write(flagsSectionStream.ToArray()); } #endregion #region Properties // Write PROPERTIES using (MemoryStream propertiesSectionStream = new MemoryStream()) { BW propertiesWriter = new BW(propertiesSectionStream); propertiesWriter.Write((uint)description.Properties.Length); foreach (ComponentProperty property in description.Properties) { propertiesWriter.Write(property.Name); propertiesWriter.Write(property.SerializedName.Value); propertiesWriter.Write(property.DisplayName); propertiesWriter.WriteType(property.Default, property.EnumOptions != null); if (property.EnumOptions != null) { propertiesWriter.Write(property.EnumOptions.Length); foreach (string option in property.EnumOptions) { propertiesWriter.Write(option); } } // Format rules propertiesWriter.Write((uint)property.FormatRules.Length); foreach (ComponentPropertyFormat formatRule in property.FormatRules) { propertiesWriter.Write(formatRule.Conditions); propertiesWriter.Write(formatRule.Value); } // Other conditions propertiesWriter.Write((uint)property.OtherConditions.Count); foreach (KeyValuePair <PropertyOtherConditionType, IConditionTreeItem> otherCondition in property.OtherConditions) { propertiesWriter.Write((uint)otherCondition.Key); propertiesWriter.Write(otherCondition.Value); } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Properties); writer.Write((uint)propertiesSectionStream.Length); writer.Write(propertiesSectionStream.ToArray()); } #endregion #region Configurations // Write CONFIGURATIONS using (MemoryStream configurationsStream = new MemoryStream()) { BW configurationWriter = new BW(configurationsStream); configurationWriter.Write((uint)description.Metadata.Configurations.Count); foreach (ComponentConfiguration configuration in description.Metadata.Configurations) { configurationWriter.Write(configuration.Name); configurationWriter.Write((configuration.ImplementationName ?? "")); configurationWriter.Write(configuration.Setters.Count); foreach (var setter in configuration.Setters) { foreach (ComponentProperty property in description.Properties) { if (property.SerializedName == setter.Key) { configurationWriter.Write(property.SerializedName.Value); break; } } configurationWriter.WriteType(setter.Value); } if (!Settings.IgnoreIcons && configuration.Icon != null) { uint iconResourceID = NewResourceID(); foreach (var icon in configuration.Icon as MultiResolutionImage) { BinaryResource iconResource = new BinaryResource(); iconResource.ID = iconResourceID; // Only PNG images can be written at the moment iconResource.ResourceType = IO.Descriptions.BinaryResourceType.PNGImage; iconResource.Buffer = icon.Data; Resources.Add(iconResource); } configurationWriter.Write((int)iconResourceID); } else { configurationWriter.Write(-1); } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Configurations); writer.Write((uint)configurationsStream.Length); writer.Write(configurationsStream.ToArray()); } #endregion #region Connections // Write CONNECTIONS using (MemoryStream connectionsStream = new MemoryStream()) { BW connectionsWriter = new BW(connectionsStream); connectionsWriter.Write((uint)description.Connections.Length); foreach (ConnectionGroup connectionGroup in description.Connections) { connectionsWriter.Write(connectionGroup.Conditions); connectionsWriter.Write((uint)connectionGroup.Value.Length); foreach (ConnectionDescription connection in connectionGroup.Value) { connectionsWriter.Write(connection.Start); connectionsWriter.Write(connection.End); connectionsWriter.Write((int)connection.Edge); connectionsWriter.Write(connection.Name.Value); } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Connections); writer.Write((uint)connectionsStream.Length); writer.Write(connectionsStream.ToArray()); } #endregion #region Render // Write RENDER using (MemoryStream renderStream = new MemoryStream()) { BW renderWriter = new BW(renderStream); renderWriter.Write(description.RenderDescriptions.Length); foreach (RenderDescription renderDescription in description.RenderDescriptions) { renderWriter.Write(renderDescription.Conditions); renderWriter.Write(renderDescription.Value.Length); // number of render commands foreach (IRenderCommand command in renderDescription.Value) { renderWriter.Write((uint)command.Type); // command type switch (command.Type) { case RenderCommandType.Line: { Line line = command as Line; renderWriter.Write(line.Start); renderWriter.Write(line.End); renderWriter.Write(line.Thickness); } break; case RenderCommandType.Rect: { Rectangle rect = command as Rectangle; renderWriter.Write(rect.Location); renderWriter.Write(rect.Width); renderWriter.Write(rect.Height); renderWriter.Write(rect.StrokeThickness); renderWriter.Write((rect.Fill ? (uint)1 : (uint)0)); // 0 for transparent, 1 for filled } break; case RenderCommandType.Ellipse: { Ellipse ellipse = command as Ellipse; renderWriter.Write(ellipse.Centre); renderWriter.Write(ellipse.RadiusX); renderWriter.Write(ellipse.RadiusY); renderWriter.Write(ellipse.Thickness); renderWriter.Write((ellipse.Fill ? (uint)1 : (uint)0)); // 0 for transparent, 1 for filled } break; case RenderCommandType.Path: { RenderPath path = command as RenderPath; renderWriter.Write(path.Start); renderWriter.Write(path.Thickness); renderWriter.Write((path.Fill ? (uint)1 : (uint)0)); // 0 for transparent, 1 for filled renderWriter.Write(path.Commands.Count); foreach (IPathCommand pCommand in path.Commands) { renderWriter.Write((int)pCommand.Type); pCommand.Write(renderWriter); } } break; case RenderCommandType.Text: { var text = command as RenderText; renderWriter.Write(BinaryConstants.FormattedTextVersion); // Formatted text version renderWriter.Write(text.Location); // Text location renderWriter.Write((uint)text.Alignment); // Text alignment renderWriter.Write((uint)text.TextRuns.Count); // Number of text runs foreach (TextRun run in text.TextRuns) { renderWriter.Write((uint)run.Formatting.FormattingType); // Run formatting type renderWriter.Write(run.Formatting.Size); // Run text size renderWriter.Write(run.Text); // Text } } break; } } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Render); writer.Write((uint)renderStream.Length); writer.Write(renderStream.ToArray()); } #endregion mainWriter.Write((ushort)BinaryConstants.ContentItemType.Component); mainWriter.Write((uint)tempStream.Length); mainWriter.Write(tempStream.ToArray()); } } }
private void WriteDescriptions() { BW mainWriter = new BW(m_contentStream); foreach (ComponentDescription description in Descriptions) { using (MemoryStream tempStream = new MemoryStream()) { BW writer = new BW(tempStream); writer.Write(NewResourceID()); // ID writer.Write(6u); // 6 sections #region Metadata // Write METADATA /* Component name (string) * Can resize (bool) * Minimum size (bool) * GUID (byte[16]) * Author (string) * Version (major, minor) (uint, uint) * AdditionalInformation (string) * Implement set (string) * Implement item (string) * IconresourceID (int) * Timestamp (long) */ using (MemoryStream metadatSectionStream = new MemoryStream()) { BW metadataWriter = new BW(metadatSectionStream); metadataWriter.WriteNullString(description.ComponentName); metadataWriter.Write(description.CanResize); metadataWriter.Write(description.CanFlip); metadataWriter.Write(description.MinSize); metadataWriter.Write(description.Metadata.GUID.ToByteArray()); metadataWriter.WriteNullString(description.Metadata.Author); metadataWriter.Write((ushort)description.Metadata.Version.Major); metadataWriter.Write((ushort)description.Metadata.Version.Minor); metadataWriter.WriteNullString(description.Metadata.AdditionalInformation); metadataWriter.WriteNullString(description.Metadata.ImplementSet); metadataWriter.WriteNullString(description.Metadata.ImplementItem); if (description.Metadata.Icon != null) { uint iconResourceID = NewResourceID(); foreach(var icon in description.Metadata.Icon) { BinaryResource iconResource = new BinaryResource(); iconResource.ID = iconResourceID; // Only PNG images can be written at the moment iconResource.ResourceType = IO.Descriptions.BinaryResourceType.PNGImage; iconResource.Buffer = icon.Data; Resources.Add(iconResource); } metadataWriter.Write((int)iconResourceID); } else metadataWriter.Write(-1); metadataWriter.Write(DateTime.Now.ToBinary()); writer.Write((ushort)BinaryConstants.ComponentSectionType.Metadata); writer.Write((uint)metadatSectionStream.Length); writer.Write(metadatSectionStream.ToArray()); } #endregion #region Flags // Write FLAGS using (MemoryStream flagsSectionStream = new MemoryStream()) { BW flagsWriter = new BW(flagsSectionStream); flagsWriter.Write((uint)description.Flags.Length); foreach (Conditional<FlagOptions> flags in description.Flags) { flagsWriter.Write(flags.Conditions); flagsWriter.Write((uint)flags.Value); } writer.Write((ushort)BinaryConstants.ComponentSectionType.Flags); writer.Write((uint)flagsSectionStream.Length); writer.Write(flagsSectionStream.ToArray()); } #endregion #region Properties // Write PROPERTIES using (MemoryStream propertiesSectionStream = new MemoryStream()) { BW propertiesWriter = new BW(propertiesSectionStream); propertiesWriter.Write((uint)description.Properties.Length); foreach (ComponentProperty property in description.Properties) { propertiesWriter.Write(property.Name); propertiesWriter.Write(property.SerializedName); propertiesWriter.Write(property.DisplayName); propertiesWriter.WriteType(property.Default, property.EnumOptions != null); if (property.EnumOptions != null) { propertiesWriter.Write(property.EnumOptions.Length); foreach (string option in property.EnumOptions) propertiesWriter.Write(option); } // Format rules propertiesWriter.Write((uint)property.FormatRules.Length); foreach (ComponentPropertyFormat formatRule in property.FormatRules) { propertiesWriter.Write(formatRule.Conditions); propertiesWriter.Write(formatRule.Value); } // Other conditions propertiesWriter.Write((uint)property.OtherConditions.Count); foreach (KeyValuePair<PropertyOtherConditionType, IConditionTreeItem> otherCondition in property.OtherConditions) { propertiesWriter.Write((uint)otherCondition.Key); propertiesWriter.Write(otherCondition.Value); } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Properties); writer.Write((uint)propertiesSectionStream.Length); writer.Write(propertiesSectionStream.ToArray()); } #endregion #region Configurations // Write CONFIGURATIONS using (MemoryStream configurationsStream = new MemoryStream()) { BW configurationWriter = new BW(configurationsStream); configurationWriter.Write((uint)description.Metadata.Configurations.Count); foreach (ComponentConfiguration configuration in description.Metadata.Configurations) { configurationWriter.Write(configuration.Name); configurationWriter.Write((configuration.ImplementationName != null ? configuration.ImplementationName : "")); configurationWriter.Write(configuration.Setters.Count); foreach (var setter in configuration.Setters) { foreach (ComponentProperty property in description.Properties) if (property.SerializedName == setter.Key) { configurationWriter.Write(property.SerializedName); break; } configurationWriter.WriteType(setter.Value); } if (!Settings.IgnoreIcons && configuration.Icon != null) { uint iconResourceID = NewResourceID(); foreach (var icon in configuration.Icon) { BinaryResource iconResource = new BinaryResource(); iconResource.ID = iconResourceID; // Only PNG images can be written at the moment iconResource.ResourceType = IO.Descriptions.BinaryResourceType.PNGImage; iconResource.Buffer = icon.Data; Resources.Add(iconResource); } configurationWriter.Write((int)iconResourceID); } else configurationWriter.Write(-1); } writer.Write((ushort)BinaryConstants.ComponentSectionType.Configurations); writer.Write((uint)configurationsStream.Length); writer.Write(configurationsStream.ToArray()); } #endregion #region Connections // Write CONNECTIONS using (MemoryStream connectionsStream = new MemoryStream()) { BW connectionsWriter = new BW(connectionsStream); connectionsWriter.Write((uint)description.Connections.Length); foreach (ConnectionGroup connectionGroup in description.Connections) { connectionsWriter.Write(connectionGroup.Conditions); connectionsWriter.Write((uint)connectionGroup.Value.Length); foreach (ConnectionDescription connection in connectionGroup.Value) { connectionsWriter.Write(connection.Start); connectionsWriter.Write(connection.End); connectionsWriter.Write((int)connection.Edge); connectionsWriter.Write(connection.Name); // NEW } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Connections); writer.Write((uint)connectionsStream.Length); writer.Write(connectionsStream.ToArray()); } #endregion #region Render // Write RENDER using (MemoryStream renderStream = new MemoryStream()) { BW renderWriter = new BW(renderStream); renderWriter.Write(description.RenderDescriptions.Length); foreach (RenderDescription renderDescription in description.RenderDescriptions) { renderWriter.Write(renderDescription.Conditions); renderWriter.Write(renderDescription.Value.Length); // number of render commands foreach (IRenderCommand command in renderDescription.Value) { renderWriter.Write((uint)command.Type); // command type switch (command.Type) { case RenderCommandType.Line: { Line line = command as Line; renderWriter.Write(line.Start); renderWriter.Write(line.End); renderWriter.Write(line.Thickness); } break; case RenderCommandType.Rect: { Rectangle rect = command as Rectangle; renderWriter.Write(rect.Location); renderWriter.Write(rect.Width); renderWriter.Write(rect.Height); renderWriter.Write(rect.StrokeThickness); renderWriter.Write((rect.Fill ? (uint)1 : (uint)0)); // 0 for transparent, 1 for filled } break; case RenderCommandType.Ellipse: { Ellipse ellipse = command as Ellipse; renderWriter.Write(ellipse.Centre); renderWriter.Write(ellipse.RadiusX); renderWriter.Write(ellipse.RadiusY); renderWriter.Write(ellipse.Thickness); renderWriter.Write((ellipse.Fill ? (uint)1 : (uint)0)); // 0 for transparent, 1 for filled } break; case RenderCommandType.Path: { RenderPath path = command as RenderPath; renderWriter.Write(path.Start); renderWriter.Write(path.Thickness); renderWriter.Write((path.Fill ? (uint)1 : (uint)0)); // 0 for transparent, 1 for filled renderWriter.Write(path.Commands.Count); foreach (IPathCommand pCommand in path.Commands) { renderWriter.Write((int)pCommand.Type); pCommand.Write(renderWriter); } } break; case RenderCommandType.Text: { Text text = command as Text; renderWriter.Write(BinaryConstants.FormattedTextVersion); // Formatted text version renderWriter.Write(text.Location); // Text location renderWriter.Write((uint)text.Alignment); // Text alignment renderWriter.Write((uint)text.TextRuns.Count); // Number of text runs foreach (TextRun run in text.TextRuns) { renderWriter.Write((uint)run.Formatting.FormattingType); // Run formatting type renderWriter.Write(run.Formatting.Size); // Run text size renderWriter.Write(run.Text); // Text } } break; } } } writer.Write((ushort)BinaryConstants.ComponentSectionType.Render); writer.Write((uint)renderStream.Length); writer.Write(renderStream.ToArray()); } #endregion mainWriter.Write((ushort)BinaryConstants.ContentItemType.Component); mainWriter.Write((uint)tempStream.Length); mainWriter.Write(tempStream.ToArray()); } } }