LSLib.LS.NodeAttribute.FromString C# (CSharp) Méthode

FromString() public méthode

public FromString ( string str ) : void
str string
Résultat void
        public void FromString(string str)
        {
            switch (this.type)
            {
                case DataType.DT_None:
                    // This is a null type, cannot have a value
                    break;

                case DataType.DT_Byte:
                    value = Convert.ToByte(str);
                    break;

                case DataType.DT_Short:
                    value = Convert.ToInt16(str);
                    break;

                case DataType.DT_UShort:
                    value = Convert.ToUInt16(str);
                    break;

                case DataType.DT_Int:
                    value = Convert.ToInt32(str);
                    break;

                case DataType.DT_UInt:
                    value = Convert.ToUInt32(str);
                    break;

                case DataType.DT_Float:
                    value = Convert.ToSingle(str);
                    break;

                case DataType.DT_Double:
                    value = Convert.ToDouble(str);
                    break;

                case DataType.DT_IVec2:
                case DataType.DT_IVec3:
                case DataType.DT_IVec4:
                    {
                        string[] nums = str.Split(' ');
                        int length = GetColumns();
                        if (length != nums.Length)
                            throw new FormatException(String.Format("A vector of length {0} was expected, got {1}", length, nums.Length));

                        int[] vec = new int[length];
                        for (int i = 0; i < length; i++)
                            vec[i] = int.Parse(nums[i]);

                        value = vec;
                        break;
                    }

                case DataType.DT_Vec2:
                case DataType.DT_Vec3:
                case DataType.DT_Vec4:
                    {
                        string[] nums = str.Split(' ');
                        int length = GetColumns();
                        if (length != nums.Length)
                            throw new FormatException(String.Format("A vector of length {0} was expected, got {1}", length, nums.Length));

                        float[] vec = new float[length];
                        for (int i = 0; i < length; i++)
                            vec[i] = float.Parse(nums[i]);

                        value = vec;
                        break;
                    }

                case DataType.DT_Mat2:
                case DataType.DT_Mat3:
                case DataType.DT_Mat3x4:
                case DataType.DT_Mat4x3:
                case DataType.DT_Mat4:
                    var mat = Matrix.Parse(str);
                    if (mat.cols != GetColumns() || mat.rows != GetRows())
                        throw new FormatException("Invalid column/row count for matrix");
                    value = mat;
                    break;

                case DataType.DT_Bool:
                    if (str == "0") value = false;
                    else if (str == "1") value = true;
                    else value = Convert.ToBoolean(str);
                    break;

                case DataType.DT_String:
                case DataType.DT_Path:
                case DataType.DT_FixedString:
                case DataType.DT_LSString:
                case DataType.DT_WString:
                case DataType.DT_LSWString:
                    value = str;
                    break;

                case DataType.DT_TranslatedString:
                    // We'll only set the value part of the translated string, not the TranslatedStringKey / Handle part
                    // That can be changed separately via attribute.Value.Handle
                    if (value == null)
                        value = new TranslatedString();

                    ((TranslatedString)value).Value = str;
                    break;

                case DataType.DT_ULongLong:
                    value = Convert.ToUInt64(str);
                    break;

                case DataType.DT_ScratchBuffer:
                    value = Convert.FromBase64String(str);
                    break;

                case DataType.DT_Long:
                    value = Convert.ToInt64(str);
                    break;

                case DataType.DT_Int8:
                    value = Convert.ToSByte(str);
                    break;

                default:
                    // This should not happen!
                    throw new NotImplementedException(String.Format("FromString() not implemented for type {0}", this.type));
            }
        }
    }

Usage Example

Exemple #1
0
        public Resource Read()
        {
            using (this.reader = XmlReader.Create(stream))
            {
                Resource    rsrc          = new Resource();
                Region      currentRegion = null;
                List <Node> stack         = new List <Node>();

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                        case "save":
                            // Root element
                            if (stack.Count() > 0)
                            {
                                throw new InvalidFormatException("Node <save> was unexpected.");
                            }
                            break;

                        case "header":
                            // LSX metadata part 1
                            rsrc.Metadata.Timestamp = Convert.ToUInt64(reader["time"]);
                            break;

                        case "version":
                            // LSX metadata part 2
                            rsrc.Metadata.MajorVersion = Convert.ToUInt32(reader["major"]);
                            rsrc.Metadata.MinorVersion = Convert.ToUInt32(reader["minor"]);
                            rsrc.Metadata.Revision     = Convert.ToUInt32(reader["revision"]);
                            rsrc.Metadata.BuildNumber  = Convert.ToUInt32(reader["build"]);
                            Version = rsrc.Metadata.MajorVersion;
                            break;

                        case "region":
                            if (currentRegion != null)
                            {
                                throw new InvalidFormatException("A <region> can only start at the root level of a resource.");
                            }

                            Debug.Assert(!reader.IsEmptyElement);
                            var region = new Region();
                            region.RegionName = reader["id"];
                            Debug.Assert(region.RegionName != null);
                            rsrc.Regions.Add(region.RegionName, region);
                            currentRegion = region;
                            break;

                        case "node":
                            if (currentRegion == null)
                            {
                                throw new InvalidFormatException("A <node> must be located inside a region.");
                            }

                            Node node;
                            if (stack.Count() == 0)
                            {
                                // The node is the root node of the region
                                node = currentRegion;
                            }
                            else
                            {
                                // New node under the current parent
                                node        = new Node();
                                node.Parent = stack.Last();
                            }

                            node.Name = reader["id"];
                            Debug.Assert(node.Name != null);
                            if (node.Parent != null)
                            {
                                node.Parent.AppendChild(node);
                            }

                            if (!reader.IsEmptyElement)
                            {
                                stack.Add(node);
                            }
                            break;

                        case "attribute":
                            UInt32 attrTypeId;
                            if (Version >= 4)
                            {
                                attrTypeId = (uint)TypeNames[reader["type"]];
                            }
                            else
                            {
                                attrTypeId = Convert.ToUInt32(reader["type"]);
                            }

                            var attrName = reader["id"];
                            if (attrTypeId > (int)NodeAttribute.DataType.DT_Max)
                            {
                                throw new InvalidFormatException(String.Format("Unsupported attribute data type: {0}", attrTypeId));
                            }

                            Debug.Assert(attrName != null);
                            var attr = new NodeAttribute((NodeAttribute.DataType)attrTypeId);

                            var attrValue = reader["value"];
                            if (attrValue != null)
                            {
                                attr.FromString(attrValue);
                            }

                            if (attr.Type == NodeAttribute.DataType.DT_TranslatedString)
                            {
                                if (attr.Value == null)
                                {
                                    attr.Value = new TranslatedString();
                                }

                                var ts = ((TranslatedString)attr.Value);
                                ts.Handle = reader["handle"];
                                Debug.Assert(ts.Handle != null);

                                if (attrValue == null)
                                {
                                    ts.Version = UInt16.Parse(reader["version"]);
                                }
                            }
                            else if (attr.Type == NodeAttribute.DataType.DT_TranslatedFSString)
                            {
                                var fs = ((TranslatedFSString)attr.Value);
                                ReadTranslatedFSString(fs);
                            }

                            stack.Last().Attributes.Add(attrName, attr);
                            break;

                        case "children":
                            // Child nodes are handled in the "node" case
                            break;

                        default:
                            throw new InvalidFormatException(String.Format("Unknown element encountered: {0}", reader.Name));
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        switch (reader.Name)
                        {
                        case "save":
                        case "header":
                        case "version":
                        case "attribute":
                        case "children":
                            // These elements don't change the stack, just discard them
                            break;

                        case "region":
                            Debug.Assert(stack.Count == 0);
                            Debug.Assert(currentRegion != null);
                            Debug.Assert(currentRegion.Name != null);
                            currentRegion = null;
                            break;

                        case "node":
                            stack.RemoveAt(stack.Count - 1);
                            break;

                        default:
                            throw new InvalidFormatException(String.Format("Unknown element encountered: {0}", reader.Name));
                        }
                    }
                }

                return(rsrc);
            }
        }
All Usage Examples Of LSLib.LS.NodeAttribute::FromString