コード例 #1
0
        public bool Read(Stream stream)
        {
            try
            {
                BinaryReader reader = new BinaryReader(stream);

                // read header
                ulong  magicNumber     = reader.ReadUInt64();
                byte   formatVersion   = reader.ReadByte();
                byte[] md5hash         = reader.ReadBytes(16);
                uint   reserved        = reader.ReadUInt32();
                uint   fileLength      = reader.ReadUInt32();
                uint   offsetToContent = reader.ReadUInt32();
                uint   numContentItems = reader.ReadUInt32();

                // Signing
                CheckSignature(stream, reader, offsetToContent);

                // Construct BinaryReadInfo
                BinaryReadInfo readInfo = new BinaryReadInfo()
                {
                    FormatVersion        = formatVersion,
                    IsSigned             = isSigned,
                    IsSignatureValid     = validSignature,
                    IsCertificateTrusted = certificateTrusted
                };

                var componentDescriptions = new List <ComponentResource>();

                // Load content
                for (uint contentCounter = 0; contentCounter < numContentItems; contentCounter++)
                {
                    var itemType = (BinaryConstants.ContentItemType)reader.ReadUInt16();

                    if (itemType == BinaryConstants.ContentItemType.Resource)
                    {
                        var item = new BinaryResource();
                        item.Read(reader, readInfo);
                        Items.Add(item);
                        Resources.Add(item);
                    }
                    else if (itemType == BinaryConstants.ContentItemType.Component)
                    {
                        var item = new ComponentResource();
                        item.Read(reader, readInfo);
                        Items.Add(item);
                        componentDescriptions.Add(item);
                    }
                    else
                    {
                        // Unknown item type
                        continue;
                    }
                }

                // Process images
                foreach (var resource in Resources)
                {
                    if (resource.ResourceType == BinaryResourceType.BitmapImage ||
                        resource.ResourceType == BinaryResourceType.JPEGImage ||
                        resource.ResourceType == BinaryResourceType.PNGImage ||
                        true)
                    {
                        if (!Images.ContainsKey(resource.ID))
                        {
                            Images.Add(resource.ID, new MultiResolutionImage());
                        }

                        Images[resource.ID].Add(new SingleResolutionImage()
                        {
                            MimeType = BinaryConstants.ResourceMimeTypeToString((uint)resource.ResourceType),
                            Data     = resource.Buffer
                        });
                    }
                }

                // Add component descriptions
                foreach (var componentDescription in componentDescriptions)
                {
                    componentDescription.SetIcons(Images);
                    ComponentDescriptions.Add(componentDescription.ComponentDescription);
                }

                return(true);
            }
            catch (Exception ex)
            {
                // Invalid binary file
                _logger.LogError(ex, "Failed to load component.");
                return(false);
            }
        }
コード例 #2
0
        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((uint)(6 + (Settings.WriteExtendedMetadada ? 1 : 0))); // 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.GetDefaultFlag(FlagOptions.NoResize));
                        metadataWriter.Write(description.GetDefaultFlag(FlagOptions.FlipPrimary));
                        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 = 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.Count);
                        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 = 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);
                                    renderWriter.Write(text.Location);
                                    renderWriter.Write((byte)text.Alignment);
                                    renderWriter.Write((byte)text.Rotation);
                                    renderWriter.Write((byte)0);
                                    renderWriter.Write((byte)0);

                                    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

                    #region ExtendedMetadata
                    if (Settings.WriteExtendedMetadada)
                    {
                        using (var extendedMetadataStream = new MemoryStream())
                        {
                            var extendedMetadataWriter = new BW(extendedMetadataStream);
                            extendedMetadataWriter.Write((byte)BinaryConstants.ExtendedMetadataField.SemanticVersion);
                            extendedMetadataWriter.WriteType(new Circuit.PropertyValue(description.Metadata.Version.ToString()));

                            writer.Write((ushort)BinaryConstants.ComponentSectionType.ExtendedMetadata);
                            writer.Write((uint)extendedMetadataStream.Length);
                            writer.Write(extendedMetadataStream.ToArray());
                        }
                    }
                    #endregion

                    mainWriter.Write((ushort)BinaryConstants.ContentItemType.Component);
                    mainWriter.Write((uint)tempStream.Length);
                    mainWriter.Write(tempStream.ToArray());
                }
            }
        }