Example #1
0
 public MarshalComponentParameter(ComponentFamily family, ComponentParameter parameter)
 {
     Family    = family;
     Parameter = parameter;
 }
        public void LoadFromXml(XElement templateElement)
        {
            Name = templateElement.Attribute("name").Value;

            IEnumerable <XElement> tComponents = templateElement.Element("Components").Elements();

            //Parse components
            foreach (XElement tComponent in tComponents)
            {
                string componentname = tComponent.Attribute("name").Value;
                _components.Add(componentname);
                _parameters.Add(componentname, new List <ComponentParameter>());
                IEnumerable <XElement> tComponentParameters = from tParam in tComponent.Descendants("Parameter")
                                                              select tParam;
                //Parse component parameters
                foreach (XElement tComponentParameter in tComponentParameters)
                {
                    if (tComponentParameter.Attribute("type").Value == "" ||
                        tComponentParameter.Attribute("name").Value == "")
                    {
                        throw new ArgumentException("Component Parameter name or type not set.");
                    }

                    //Get the specified type
                    Type paramType = TranslateType(tComponentParameter.Attribute("type").Value);

                    //Get the raw value
                    string paramRawValue = tComponentParameter.Attribute("value").Value;

                    //Validate
                    string paramName = tComponentParameter.Attribute("name").Value;
                    if (paramType == null)
                    {
                        throw new TemplateLoadException("Invalid parameter type specified.");
                    }
                    if (paramName == "")
                    {
                        throw new TemplateLoadException("Invalid parameter name specified.");
                    }

                    //Convert the raw value to the proper type
                    object paramValue; // = Convert.ChangeType(tComponentParameter.Attribute("value").Value, paramType);
                    if (paramType == typeof(int))
                    {
                        int pval;
                        if (!int.TryParse(paramRawValue, out pval))
                        {
                            throw new ArgumentException("Could not parse parameter " + paramName + " as int. Value: " +
                                                        paramRawValue);
                        }
                        paramValue = pval;
                    }
                    else if (paramType == typeof(float))
                    {
                        float pval;
                        if (!float.TryParse(paramRawValue, NumberStyles.Float, CultureInfo.InvariantCulture, out pval))
                        {
                            throw new ArgumentException("Could not parse parameter " + paramName + " as float. Value: " +
                                                        paramRawValue);
                        }
                        paramValue = pval;
                    }
                    else if (paramType == typeof(bool))
                    {
                        bool pval;
                        if (!bool.TryParse(paramRawValue, out pval))
                        {
                            throw new ArgumentException("Could not parse parameter " + paramName + " as bool. Value: " +
                                                        paramRawValue);
                        }
                        paramValue = pval;
                    }
                    else if (paramType == typeof(string))
                    {
                        paramValue = paramRawValue;
                    }
                    else if (paramType == typeof(Vector2))
                    {
                        var args = paramRawValue.Split(',');
                        if (args.Length != 2)
                        {
                            throw new ArgumentException("Could not parse parameter " + paramName +
                                                        " as Vector2. Value: " + paramRawValue);
                        }
                        paramValue = new Vector2(float.Parse(args[0]), float.Parse(args[1]));
                    }
                    else if (paramType == typeof(Vector3))
                    {
                        var args = paramRawValue.Split(',');
                        if (args.Length != 3)
                        {
                            throw new ArgumentException("Could not parse parameter " + paramName +
                                                        " as Vector3. Value: " + paramRawValue);
                        }
                        paramValue = new Vector3(float.Parse(args[0]), float.Parse(args[1]), float.Parse(args[2]));
                    }
                    else if (paramType == typeof(Vector4))
                    {
                        var args = paramRawValue.Split(',');
                        if (args.Length != 4)
                        {
                            throw new ArgumentException("Could not parse parameter " + paramName +
                                                        " as Vector4. Value: " + paramRawValue);
                        }
                        paramValue = new Vector4(float.Parse(args[0]), float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
                    }
                    else
                    {
                        throw new ArgumentException("Could not parse parameter " + paramName +
                                                    ". Type not recognized. Value: " + paramRawValue);
                    }

                    var cparam = new ComponentParameter(paramName, paramValue);
                    _parameters[componentname].Add(cparam);
                }

                if (tComponent.Element("ExtendedParameters") != null)
                {
                    _parameters[componentname].Add(new ComponentParameter("ExtendedParameters",
                                                                          tComponent.Element("ExtendedParameters")));
                }
            }

            XElement t_placementprops = templateElement.Element("PlacementProperties");

            //Load Placement properties.
            if (t_placementprops != null)
            {
                LoadPlacementProperties(t_placementprops);
            }
            else
            {
                PlacementMode = "AlignNone";
            }

            XElement tDescription = templateElement.Element("Description");

            if (tDescription != null)
            {
                Description = tDescription.Attribute("string").Value;
            }
        }