private void ScanPropertyNode(XElement propertyElement, LoadContext lc) { string propertyName = propertyElement.Attribute("name").Value; string type = propertyElement.Attribute("type").Value; string defaultValue = propertyElement.Attribute("default").Value; PropertyType propertyType; switch (type.ToLowerInvariant()) { case "double": propertyType = PropertyType.Decimal; break; case "decimal": propertyType = PropertyType.Decimal; break; case "int": propertyType = PropertyType.Integer; break; case "bool": propertyType = PropertyType.Boolean; break; default: propertyType = PropertyType.String; break; } PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType); lc.ParseContext.PropertyTypes.Add(propertyName, propertyDefaultValue.InternalType); }
private static string ApplySpecialFormatting(PropertyUnion property, string formatting) { string[] formatTasks = formatting.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); foreach (string formatTask in formatTasks) { if (string.IsNullOrEmpty(formatTask)) continue; string[] parameters = formatTask.Split('_'); string task = parameters[0]; string option = parameters[1]; switch (task) { case "div": if (property.InternalType == PropertyUnionType.Double) property = new PropertyUnion((double)property.Value / double.Parse(option)); break; case "mul": if (property.InternalType == PropertyUnionType.Double) property = new PropertyUnion((double)property.Value * double.Parse(option)); break; case "round": if (property.InternalType == PropertyUnionType.Double) property = new PropertyUnion(Math.Round((double)property.Value, int.Parse(option))); break; } } return property.ToString(); }
public ConditionTreeLeaf(ConditionType type, string name, ConditionComparison comparison, PropertyUnion compareTo) { Type = type; VariableName = name; Comparison = comparison; CompareTo = compareTo; }
public static void WriteType(this System.IO.BinaryWriter writer, PropertyUnion value, bool isEnum = false) { var valueType = value.InternalType; if (valueType == PropertyUnionType.String && !isEnum) { writer.Write((int)BinaryType.String); writer.Write(value.Value as string); } else if (valueType == PropertyUnionType.Double) { writer.Write((int)BinaryType.Double); writer.Write((double)value.Value); } else if (valueType == PropertyUnionType.Boolean) { writer.Write((int)BinaryType.Bool); writer.Write((bool)value.Value); } else if (valueType == PropertyUnionType.String && isEnum) { writer.Write((int)BinaryType.Enum); writer.Write(value.Value as string); } else { writer.Write((int)BinaryType.Unknown); } }
public ComponentProperty(string name, string serializedName, string displayName, PropertyType type, PropertyUnion defaultValue, ComponentPropertyFormat[] formatRules, Dictionary<PropertyOtherConditionType, IConditionTreeItem> otherConditions, string[] enumOptions = null) { Name = name; SerializedName = serializedName; DisplayName = displayName; Type = type; Default = defaultValue; FormatRules = formatRules; EnumOptions = enumOptions; OtherConditions = otherConditions; }
public ComponentProperty(string name, string serializedName, string displayName, PropertyType type, PropertyUnion defaultValue, ComponentPropertyFormat[] formatRules, Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions, string[] enumOptions = null) { Name = name; SerializedName = serializedName; DisplayName = displayName; Type = type; Default = defaultValue; FormatRules = formatRules; EnumOptions = enumOptions; OtherConditions = otherConditions; }
private static string ApplySpecialFormatting(PropertyUnion property, string formatting) { string[] formatTasks = formatting.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); foreach (string formatTask in formatTasks) { if (string.IsNullOrEmpty(formatTask)) { continue; } string[] parameters = formatTask.Split('_'); string task = parameters[0]; string option = parameters[1]; switch (task) { case "div": if (property.InternalType == PropertyUnionType.Double) { property = new PropertyUnion((double)property.Value / double.Parse(option)); } break; case "mul": if (property.InternalType == PropertyUnionType.Double) { property = new PropertyUnion((double)property.Value * double.Parse(option)); } break; case "round": if (property.InternalType == PropertyUnionType.Double) { property = new PropertyUnion(Math.Round((double)property.Value, int.Parse(option))); } break; } } return(property.ToString()); }
internal override void Read(System.IO.BinaryReader reader, BinaryReadInfo readInfo) { uint length = reader.ReadUInt32(); ID = reader.ReadUInt32(); uint numSections = reader.ReadUInt32(); string componentName = null; bool canResize = false; bool canFlip = false; double minSize = ComponentHelper.GridSize; List <ComponentProperty> properties = new List <ComponentProperty>(); List <ConnectionGroup> connections = new List <ConnectionGroup>(); List <RenderDescription> renderDescriptions = new List <RenderDescription>(); List <Conditional <FlagOptions> > flagOptions = new List <Conditional <FlagOptions> >(); ComponentDescriptionMetadata descriptionMetadata = new ComponentDescriptionMetadata(); uint?iconResourceId = null; for (uint sectionCounter = 0; sectionCounter < numSections; sectionCounter++) { ushort sectionType = reader.ReadUInt16(); uint sectionLength = reader.ReadUInt32(); #region Metadata if (sectionType == (uint)BinaryConstants.ComponentSectionType.Metadata) { componentName = reader.ReadString(); canResize = reader.ReadBoolean(); canFlip = reader.ReadBoolean(); minSize = reader.ReadDouble(); descriptionMetadata.Type = String.Format("Binary r{0} (*.cdcom)", readInfo.FormatVersion); descriptionMetadata.GUID = new Guid(reader.ReadBytes(16)); descriptionMetadata.Author = reader.ReadString(); if (readInfo.IsSignatureValid && readInfo.Certificate != null && readInfo.IsCertificateTrusted) { descriptionMetadata.Author = readInfo.Certificate.GetNameInfo(X509NameType.EmailName, false); } descriptionMetadata.Version = new Version(reader.ReadUInt16(), reader.ReadUInt16()); descriptionMetadata.AdditionalInformation = reader.ReadString(); descriptionMetadata.ImplementSet = reader.ReadString(); descriptionMetadata.ImplementItem = reader.ReadString(); descriptionMetadata.Signature.IsHashValid = readInfo.IsSignatureValid; descriptionMetadata.Signature.Certificate = readInfo.Certificate; descriptionMetadata.Signature.IsCertificateTrusted = readInfo.IsCertificateTrusted; int iconResource = reader.ReadInt32(); if (iconResource != -1) { iconResourceId = (uint)iconResource; } long created = reader.ReadInt64(); } #endregion #region Flags else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Flags) { uint numFlagGroups = reader.ReadUInt32(); for (uint j = 0; j < numFlagGroups; j++) { IConditionTreeItem conditions; if (readInfo.FormatVersion > 1) { conditions = reader.ReadConditionTree(); } else { conditions = reader.ReadConditionCollection(); } FlagOptions value = (FlagOptions)reader.ReadUInt32(); flagOptions.Add(new Conditional <FlagOptions>(value, conditions)); } } #endregion #region Properties else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Properties) { uint numProperties = reader.ReadUInt32(); for (uint j = 0; j < numProperties; j++) { string propertyName = reader.ReadString(); string serializedName = reader.ReadString(); string displayName = reader.ReadString(); BinaryType propType; object rawDefaultValue = reader.ReadType(out propType); PropertyUnion defaultValue = propType.ToPropertyUnion(rawDefaultValue); string[] enumOptions = null; if (propType == BinaryType.Enum) { enumOptions = new string[reader.ReadInt32()]; for (int k = 0; k < enumOptions.Length; k++) { enumOptions[k] = reader.ReadString(); } } // Format rules List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>(); uint numFormatRules = reader.ReadUInt32(); for (uint k = 0; k < numFormatRules; k++) { IConditionTreeItem conditions; if (readInfo.FormatVersion > 1) { conditions = reader.ReadConditionTree(); } else { conditions = reader.ReadConditionCollection(); } string formatRule = reader.ReadString(); formatRules.Add(new ComponentPropertyFormat(formatRule, conditions)); } // Other conditions uint numOtherConditions = reader.ReadUInt32(); Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>((int)numOtherConditions); for (uint k = 0; k < numOtherConditions; k++) { uint uintConditionType = reader.ReadUInt32(); IConditionTreeItem conditions; if (readInfo.FormatVersion > 1) { conditions = reader.ReadConditionTree(); } else { conditions = reader.ReadConditionCollection(); } PropertyOtherConditionType conditionType = (PropertyOtherConditionType)uintConditionType; otherConditions.Add(conditionType, conditions); } properties.Add(new ComponentProperty(propertyName, serializedName, displayName, BinaryIOExtentions.BinaryTypeToPropertyType(propType), defaultValue, formatRules.ToArray(), otherConditions, enumOptions)); } } #endregion #region Configurations else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Configurations) { uint numConfigurations = reader.ReadUInt32(); for (int j = 0; j < numConfigurations; j++) { string configurationName = reader.ReadString(); string implementationName = reader.ReadString(); int numSetters = reader.ReadInt32(); var setters = new Dictionary <string, PropertyUnion>(numSetters); for (int k = 0; k < numSetters; k++) { BinaryType tempType; string name = reader.ReadString(); var setterValue = reader.ReadType(out tempType); setters.Add(name, tempType.ToPropertyUnion(setterValue)); } int iconID = reader.ReadInt32(); var configuration = new ComponentConfiguration(implementationName, configurationName, setters); descriptionMetadata.Configurations.Add(configuration); if (iconID != -1) { iconResources.Add(configuration, (uint)iconID); } } } #endregion #region Connections else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Connections) { uint numConnectionGroups = reader.ReadUInt32(); List <ConnectionGroup> connectionGroups = new List <ConnectionGroup>(); for (int j = 0; j < numConnectionGroups; j++) { IConditionTreeItem conditions; if (readInfo.FormatVersion > 1) { conditions = reader.ReadConditionTree(); } else { conditions = reader.ReadConditionCollection(); } List <ConnectionDescription> tConnections = new List <ConnectionDescription>(); uint numConnections = reader.ReadUInt32(); for (uint k = 0; k < numConnections; k++) { tConnections.Add(new ConnectionDescription(reader.ReadComponentPoint(), reader.ReadComponentPoint(), (ConnectionEdge)reader.ReadInt32(), reader.ReadString())); } connections.Add(new ConnectionGroup(conditions, tConnections.ToArray())); } } #endregion #region Render else if (sectionType == (uint)BinaryConstants.ComponentSectionType.Render) { uint numRenderGroups = reader.ReadUInt32(); for (uint j = 0; j < numRenderGroups; j++) { IConditionTreeItem conditions; if (readInfo.FormatVersion > 1) { conditions = reader.ReadConditionTree(); } else { conditions = reader.ReadConditionCollection(); } int numRenderCommands = (int)reader.ReadUInt32(); List <IRenderCommand> renderCommands = new List <IRenderCommand>(numRenderCommands); for (int k = 0; k < numRenderCommands; k++) { RenderCommandType commandType = (RenderCommandType)reader.ReadUInt32(); switch (commandType) { case RenderCommandType.Line: { ComponentPoint start = reader.ReadComponentPoint(); ComponentPoint end = reader.ReadComponentPoint(); double thickness = reader.ReadDouble(); renderCommands.Add(new Line(start, end, thickness)); } continue; case RenderCommandType.Rect: { ComponentPoint location = reader.ReadComponentPoint(); double width = reader.ReadDouble(); double height = reader.ReadDouble(); double thickness = reader.ReadDouble(); bool fill = (reader.ReadUInt32() == 0 ? false : true); renderCommands.Add(new Rectangle(location, width, height, thickness, fill)); } continue; case RenderCommandType.Ellipse: { ComponentPoint centre = reader.ReadComponentPoint(); double radiusX = reader.ReadDouble(); double radiusY = reader.ReadDouble(); double thickness = reader.ReadDouble(); bool fill = (reader.ReadUInt32() == 0 ? false : true); renderCommands.Add(new Ellipse(centre, radiusX, radiusY, thickness, fill)); } continue; case RenderCommandType.Path: { ComponentPoint start = reader.ReadComponentPoint(); double thickness = reader.ReadDouble(); bool fill = (reader.ReadUInt32() == 0 ? false : true); int numCommands = reader.ReadInt32(); List <IPathCommand> pathCommands = new List <IPathCommand>(numCommands); for (int l = 0; l < numCommands; l++) { CommandType pType = (CommandType)reader.ReadInt32(); IPathCommand theCommand = null; switch (pType) { case CommandType.MoveTo: theCommand = new MoveTo(); break; case CommandType.LineTo: theCommand = new LineTo(); break; case CommandType.CurveTo: theCommand = new CurveTo(); break; case CommandType.EllipticalArcTo: theCommand = new EllipticalArcTo(); break; case CommandType.QuadraticBeizerCurveTo: theCommand = new QuadraticBeizerCurveTo(); break; case CommandType.SmoothCurveTo: theCommand = new SmoothCurveTo(); break; case CommandType.SmoothQuadraticBeizerCurveTo: theCommand = new SmoothQuadraticBeizerCurveTo(); break; default: theCommand = new ClosePath(); break; } theCommand.Read(reader); pathCommands.Add(theCommand); } renderCommands.Add(new RenderPath(start, thickness, fill, pathCommands)); } continue; case RenderCommandType.Text: { byte formattedTextVersion = reader.ReadByte(); ComponentPoint location = reader.ReadComponentPoint(); TextAlignment alignment = (TextAlignment)reader.ReadUInt32(); uint numTextRuns = reader.ReadUInt32(); List <TextRun> textRuns = new List <TextRun>((int)numTextRuns); for (uint l = 0; l < numTextRuns; l++) { TextRunFormattingType formattingType = (TextRunFormattingType)reader.ReadUInt32(); double runSize = reader.ReadDouble(); string runText = reader.ReadString(); textRuns.Add(new TextRun(runText, new TextRunFormatting(formattingType, runSize))); } renderCommands.Add(new Text(location, alignment, textRuns)); } continue; } } renderDescriptions.Add(new RenderDescription(conditions, renderCommands.ToArray())); } } #endregion #region Skip else { // Unknown type - skip reader.BaseStream.Seek(sectionLength, SeekOrigin.Current); } #endregion } ComponentDescription = new ComponentDescription(ID.ToString(), componentName, canResize, canFlip, minSize, properties.ToArray(), connections.ToArray(), renderDescriptions.ToArray(), flagOptions.ToArray(), descriptionMetadata); if (iconResourceId.HasValue) { mainIconResource = iconResourceId.Value; } }
private ComponentProperty ReadPropertyNode(XElement propertyElement, LoadContext lc) { IXmlLineInfo elementLine = propertyElement as IXmlLineInfo; string propertyName = propertyElement.Attribute("name").Value; string type = propertyElement.Attribute("type").Value; string defaultValue = propertyElement.Attribute("default").Value; string serializeAs = propertyElement.Attribute("serialize").Value; string display = propertyElement.Attribute("display").Value; PropertyType propertyType; switch (type.ToLowerInvariant()) { case "double": propertyType = PropertyType.Decimal; if (lc.FormatVersion >= new Version(1, 2)) { lc.Errors.Add(new LoadError(lc.FileName, elementLine.LineNumber, elementLine.LinePosition, LoadErrorCategory.Warning, "Property type 'double' is deprecated, use 'decimal' instead")); } break; case "decimal": propertyType = PropertyType.Decimal; break; case "int": propertyType = PropertyType.Integer; break; case "bool": propertyType = PropertyType.Boolean; break; default: propertyType = PropertyType.String; break; } PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType); List <string> propertyOptions = null; if (type == "enum") { propertyOptions = new List <string>(); var optionNodes = propertyElement.Elements(ns + "option"); foreach (var optionNode in optionNodes) { propertyOptions.Add(optionNode.Value); } } List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>(); if (propertyElement.Attribute("format") != null) { formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty)); } else { var formatRuleNodes = propertyElement.XPathSelectElements("cd:formatting/cd:format", lc.NamespaceManager); foreach (var formatNode in formatRuleNodes) { IXmlLineInfo line = formatNode as IXmlLineInfo; IConditionTreeItem conditionCollection = ConditionTree.Empty; if (formatNode.Attribute("conditions") != null) { try { conditionCollection = lc.ConditionParser.Parse(formatNode.Attribute("conditions").Value, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + formatNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return(null); } } formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection)); } } Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>(); var otherConditionsNodes = propertyElement.XPathSelectElements("cd:other/cd:conditions", lc.NamespaceManager); foreach (var otherConditionNode in otherConditionsNodes) { if (otherConditionNode != null && otherConditionNode.Attribute("for") != null && otherConditionNode.Attribute("value") != null) { IXmlLineInfo line = otherConditionNode as IXmlLineInfo; string conditionsFor = otherConditionNode.Attribute("for").Value; string conditionsString = otherConditionNode.Attribute("value").Value; IConditionTreeItem conditionCollection; try { conditionCollection = lc.ConditionParser.Parse(conditionsString, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + otherConditionNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return(null); } if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor)) { otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection); } } } ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray())); return(property); }
private ComponentProperty ReadPropertyNode(XElement propertyElement, LoadContext lc) { IXmlLineInfo elementLine = propertyElement as IXmlLineInfo; string propertyName = propertyElement.Attribute("name").Value; string type = propertyElement.Attribute("type").Value; string defaultValue = propertyElement.Attribute("default").Value; string serializeAs = propertyElement.Attribute("serialize").Value; string display = propertyElement.Attribute("display").Value; PropertyType propertyType; switch (type.ToLowerInvariant()) { case "double": propertyType = PropertyType.Decimal; if (lc.FormatVersion >= new Version(1, 2)) lc.Errors.Add(new LoadError(lc.FileName, elementLine.LineNumber, elementLine.LinePosition, LoadErrorCategory.Warning, "Property type 'double' is deprecated, use 'decimal' instead")); break; case "decimal": propertyType = PropertyType.Decimal; break; case "int": propertyType = PropertyType.Integer; break; case "bool": propertyType = PropertyType.Boolean; break; default: propertyType = PropertyType.String; break; } PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType); List<string> propertyOptions = null; if (type == "enum") { propertyOptions = new List<string>(); var optionNodes = propertyElement.Elements(ns + "option"); foreach (var optionNode in optionNodes) propertyOptions.Add(optionNode.Value); } List<ComponentPropertyFormat> formatRules = new List<ComponentPropertyFormat>(); if (propertyElement.Attribute("format") != null) formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty)); else { var formatRuleNodes = propertyElement.XPathSelectElements("cd:formatting/cd:format", lc.NamespaceManager); foreach (var formatNode in formatRuleNodes) { IXmlLineInfo line = formatNode as IXmlLineInfo; IConditionTreeItem conditionCollection = ConditionTree.Empty; if (formatNode.Attribute("conditions") != null) { try { conditionCollection = lc.ConditionParser.Parse(formatNode.Attribute("conditions").Value, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + formatNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return null; } } formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection)); } } Dictionary<PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary<PropertyOtherConditionType, IConditionTreeItem>(); var otherConditionsNodes = propertyElement.XPathSelectElements("cd:other/cd:conditions", lc.NamespaceManager); foreach (var otherConditionNode in otherConditionsNodes) { if (otherConditionNode != null && otherConditionNode.Attribute("for") != null && otherConditionNode.Attribute("value") != null) { IXmlLineInfo line = otherConditionNode as IXmlLineInfo; string conditionsFor = otherConditionNode.Attribute("for").Value; string conditionsString = otherConditionNode.Attribute("value").Value; IConditionTreeItem conditionCollection; try { conditionCollection = lc.ConditionParser.Parse(conditionsString, lc.ParseContext); } catch (ConditionFormatException ex) { lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + otherConditionNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message)); return null; } if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor)) otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection); } } ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray())); return property; }
public void SetProperty(ComponentProperty property, PropertyUnion value) { if (m_propertyValues.ContainsKey(property)) { m_propertyValues[property] = value; } }