예제 #1
0
        private NodeAttribute ReadAttribute(NodeAttribute.DataType type, BinaryReader reader, uint length)
        {
            // LSF and LSB serialize the buffer types differently, so specialized
            // code is added to the LSB and LSf serializers, and the common code is
            // available in BinUtils.ReadAttribute()
            switch (type)
            {
            case NodeAttribute.DataType.DT_String:
            case NodeAttribute.DataType.DT_Path:
            case NodeAttribute.DataType.DT_FixedString:
            case NodeAttribute.DataType.DT_LSString:
            case NodeAttribute.DataType.DT_WString:
            case NodeAttribute.DataType.DT_LSWString:
            {
                var attr = new NodeAttribute(type);
                attr.Value = ReadString(reader, (int)length);
                return(attr);
            }

            case NodeAttribute.DataType.DT_TranslatedString:
            {
                var attr = new NodeAttribute(type);
                var str  = new TranslatedString();

                if (Version >= (uint)FileVersion.VerBG3)
                {
                    str.Version = reader.ReadUInt16();
                }
                else
                {
                    str.Version = 0;
                    var valueLength = reader.ReadInt32();
                    str.Value = ReadString(reader, valueLength);
                }

                var handleLength = reader.ReadInt32();
                str.Handle = ReadString(reader, handleLength);

                attr.Value = str;
                return(attr);
            }

            case NodeAttribute.DataType.DT_TranslatedFSString:
            {
                var attr = new NodeAttribute(type);
                attr.Value = ReadTranslatedFSString(reader);
                return(attr);
            }

            case NodeAttribute.DataType.DT_ScratchBuffer:
            {
                var attr = new NodeAttribute(type);
                attr.Value = reader.ReadBytes((int)length);
                return(attr);
            }

            default:
                return(BinUtils.ReadAttribute(type, reader));
            }
        }
예제 #2
0
        private NodeAttribute ReadAttribute(NodeAttribute.DataType type)
        {
            switch (type)
            {
            case NodeAttribute.DataType.DT_String:
            case NodeAttribute.DataType.DT_Path:
            case NodeAttribute.DataType.DT_FixedString:
            case NodeAttribute.DataType.DT_LSString:
            {
                var attr = new NodeAttribute(type);
                attr.Value = ReadString(true);
                return(attr);
            }

            case NodeAttribute.DataType.DT_WString:
            case NodeAttribute.DataType.DT_LSWString:
            {
                var attr = new NodeAttribute(type);
                attr.Value = ReadWideString(true);
                return(attr);
            }

            case NodeAttribute.DataType.DT_TranslatedString:
            {
                var attr = new NodeAttribute(type);
                var str  = new TranslatedString();
                str.Value  = ReadString(true);
                str.Handle = ReadString(true);
                attr.Value = str;
                return(attr);
            }

            case NodeAttribute.DataType.DT_ScratchBuffer:
            {
                var attr         = new NodeAttribute(type);
                var bufferLength = reader.ReadInt32();
                attr.Value = reader.ReadBytes(bufferLength);
                return(attr);
            }

            // DT_TranslatedFSString not supported in LSB
            default:
                return(BinUtils.ReadAttribute(type, reader));
            }
        }
예제 #3
0
파일: LSBReader.cs 프로젝트: Norbyte/lslib
        private NodeAttribute ReadAttribute(NodeAttribute.DataType type)
        {
            switch (type)
            {
                case NodeAttribute.DataType.DT_String:
                case NodeAttribute.DataType.DT_Path:
                case NodeAttribute.DataType.DT_FixedString:
                case NodeAttribute.DataType.DT_LSString:
                    {
                        var attr = new NodeAttribute(type);
                        attr.Value = ReadString(true);
                        return attr;
                    }

                case NodeAttribute.DataType.DT_WString:
                case NodeAttribute.DataType.DT_LSWString:
                    {
                        var attr = new NodeAttribute(type);
                        attr.Value = ReadWideString(true);
                        return attr;
                    }

                case NodeAttribute.DataType.DT_TranslatedString:
                    {
                        var attr = new NodeAttribute(type);
                        var str = new TranslatedString();
                        str.Value = ReadString(true);
                        str.Handle = ReadString(true);
                        attr.Value = str;
                        return attr;
                    }

                case NodeAttribute.DataType.DT_ScratchBuffer:
                    {
                        var attr = new NodeAttribute(type);
                        var bufferLength = reader.ReadInt32();
                        attr.Value = reader.ReadBytes(bufferLength);
                        return attr;
                    }

                default:
                    return BinUtils.ReadAttribute(type, reader);
            }
        }
예제 #4
0
        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));
            }
        }
예제 #5
0
        private NodeAttribute ReadAttribute(NodeAttribute.DataType type)
        {
            var attr = new NodeAttribute(type);

            switch (type)
            {
            case NodeAttribute.DataType.DT_None:
                break;

            case NodeAttribute.DataType.DT_Byte:
                attr.Value = reader.ReadByte();
                break;

            case NodeAttribute.DataType.DT_Short:
                attr.Value = reader.ReadInt16();
                break;

            case NodeAttribute.DataType.DT_UShort:
                attr.Value = reader.ReadUInt16();
                break;

            case NodeAttribute.DataType.DT_Int:
                attr.Value = reader.ReadInt32();;
                break;

            case NodeAttribute.DataType.DT_UInt:
                attr.Value = reader.ReadUInt32();
                break;

            case NodeAttribute.DataType.DT_Float:
                attr.Value = reader.ReadSingle();
                break;

            case NodeAttribute.DataType.DT_Double:
                attr.Value = reader.ReadDouble();
                break;

            case NodeAttribute.DataType.DT_IVec2:
            case NodeAttribute.DataType.DT_IVec3:
            case NodeAttribute.DataType.DT_IVec4:
            {
                int columns = attr.GetColumns();
                var vec     = new int[columns];
                for (int i = 0; i < columns; i++)
                {
                    vec[i] = reader.ReadInt32();
                }
                attr.Value = vec;
                break;
            }

            case NodeAttribute.DataType.DT_Vec2:
            case NodeAttribute.DataType.DT_Vec3:
            case NodeAttribute.DataType.DT_Vec4:
            {
                int columns = attr.GetColumns();
                var vec     = new float[columns];
                for (int i = 0; i < columns; i++)
                {
                    vec[i] = reader.ReadSingle();
                }
                attr.Value = vec;
                break;
            }

            case NodeAttribute.DataType.DT_Mat2:
            case NodeAttribute.DataType.DT_Mat3:
            case NodeAttribute.DataType.DT_Mat3x4:
            case NodeAttribute.DataType.DT_Mat4x3:
            case NodeAttribute.DataType.DT_Mat4:
            {
                int columns = attr.GetColumns();
                int rows    = attr.GetRows();
                var mat     = new Matrix(rows, columns);
                attr.Value = mat;

                for (int col = 0; col < columns; col++)
                {
                    for (int row = 0; row < rows; row++)
                    {
                        mat[row, col] = reader.ReadSingle();
                    }
                }
                break;
            }

            case NodeAttribute.DataType.DT_Bool:
                attr.Value = reader.ReadByte() != 0;
                break;

            case NodeAttribute.DataType.DT_String:
            case NodeAttribute.DataType.DT_Path:
            case NodeAttribute.DataType.DT_FixedString:
            case NodeAttribute.DataType.DT_LSString:
                attr.Value = ReadString(true);
                break;

            case NodeAttribute.DataType.DT_WString:
            case NodeAttribute.DataType.DT_LSWString:
                attr.Value = ReadWideString(true);
                break;

            case NodeAttribute.DataType.DT_TranslatedString:
                var str = new TranslatedString();
                str.Value  = ReadString(true);
                str.Handle = ReadString(true);
                attr.Value = str;
                break;

            case NodeAttribute.DataType.DT_ULongLong:
                attr.Value = reader.ReadUInt64();
                break;

            case NodeAttribute.DataType.DT_ScratchBuffer:
                var bufferLength = reader.ReadInt32();
                attr.Value = reader.ReadBytes(bufferLength);
                break;

            case NodeAttribute.DataType.DT_Long:
                attr.Value = reader.ReadInt64();
                break;

            case NodeAttribute.DataType.DT_Int8:
                attr.Value = reader.ReadSByte();
                break;

            default:
                throw new InvalidFormatException(String.Format("ReadAttribute() not implemented for type {0}", type));
            }

            return(attr);
        }
예제 #6
0
        private NodeAttribute ReadAttribute(JsonReader reader)
        {
            string key = "", handle = null;
            List <TranslatedFSStringArgument> fsStringArguments = null;
            NodeAttribute attribute = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }
                else if (reader.TokenType == JsonToken.PropertyName)
                {
                    key = reader.Value.ToString();
                }
                else if (reader.TokenType == JsonToken.String ||
                         reader.TokenType == JsonToken.Integer ||
                         reader.TokenType == JsonToken.Float ||
                         reader.TokenType == JsonToken.Boolean)
                {
                    if (key == "type")
                    {
                        var type = (NodeAttribute.DataType)Convert.ToUInt32(reader.Value);
                        attribute = new NodeAttribute(type);
                    }
                    else if (key == "value")
                    {
                        switch (attribute.Type)
                        {
                        case NodeAttribute.DataType.DT_Byte:
                            attribute.Value = Convert.ToByte(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_Short:
                            attribute.Value = Convert.ToInt16(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_UShort:
                            attribute.Value = Convert.ToUInt16(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_Int:
                            attribute.Value = Convert.ToInt32(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_UInt:
                            attribute.Value = Convert.ToUInt32(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_Float:
                            attribute.Value = Convert.ToSingle(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_Double:
                            attribute.Value = Convert.ToDouble(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_Bool:
                            attribute.Value = Convert.ToBoolean(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_String:
                        case NodeAttribute.DataType.DT_Path:
                        case NodeAttribute.DataType.DT_FixedString:
                        case NodeAttribute.DataType.DT_LSString:
                        case NodeAttribute.DataType.DT_WString:
                        case NodeAttribute.DataType.DT_LSWString:
                            attribute.Value = reader.Value.ToString();
                            break;

                        case NodeAttribute.DataType.DT_ULongLong:
                            if (reader.Value.GetType() == typeof(System.Int64))
                            {
                                attribute.Value = Convert.ToUInt64((long)reader.Value);
                            }
                            else if (reader.Value.GetType() == typeof(BigInteger))
                            {
                                attribute.Value = (ulong)((BigInteger)reader.Value);
                            }
                            else
                            {
                                attribute.Value = (ulong)reader.Value;
                            }
                            break;

                        // TODO: Not sure if this is the correct format
                        case NodeAttribute.DataType.DT_ScratchBuffer:
                            attribute.Value = Convert.FromBase64String(reader.Value.ToString());
                            break;

                        case NodeAttribute.DataType.DT_Long:
                            attribute.Value = Convert.ToInt64(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_Int8:
                            attribute.Value = Convert.ToSByte(reader.Value);
                            break;

                        case NodeAttribute.DataType.DT_TranslatedString:
                        {
                            var translatedString = new TranslatedString();
                            translatedString.Value  = reader.Value.ToString();
                            translatedString.Handle = handle;
                            attribute.Value         = translatedString;
                            break;
                        }

                        case NodeAttribute.DataType.DT_TranslatedFSString:
                        {
                            var fsString = new TranslatedFSString();
                            fsString.Value     = reader.Value.ToString();
                            fsString.Handle    = handle;
                            fsString.Arguments = fsStringArguments;
                            attribute.Value    = fsString;
                            break;
                        }

                        case NodeAttribute.DataType.DT_UUID:
                            attribute.Value = new Guid(reader.Value.ToString());
                            break;

                        case NodeAttribute.DataType.DT_IVec2:
                        case NodeAttribute.DataType.DT_IVec3:
                        case NodeAttribute.DataType.DT_IVec4:
                        {
                            string[] nums   = reader.Value.ToString().Split(' ');
                            int      length = attribute.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]);
                            }

                            attribute.Value = vec;
                            break;
                        }

                        case NodeAttribute.DataType.DT_Vec2:
                        case NodeAttribute.DataType.DT_Vec3:
                        case NodeAttribute.DataType.DT_Vec4:
                        {
                            string[] nums   = reader.Value.ToString().Split(' ');
                            int      length = attribute.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]);
                            }

                            attribute.Value = vec;
                            break;
                        }

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

                        case NodeAttribute.DataType.DT_None:
                        default:
                            throw new NotImplementedException("Don't know how to unserialize type " + attribute.Type.ToString());
                        }
                    }
                    else if (key == "handle")
                    {
                        if (attribute != null)
                        {
                            var ts = ((TranslatedString)attribute.Value);
                            ts.Handle = reader.Value.ToString();
                        }
                        else
                        {
                            handle = reader.Value.ToString();
                        }
                    }
                    else
                    {
                        throw new InvalidDataException("Unknown property encountered during attribute parsing: " + key);
                    }
                }
                else if (reader.TokenType == JsonToken.StartArray && key == "arguments")
                {
                    var args = ReadFSStringArguments(reader);

                    if (attribute.Value != null)
                    {
                        var fs = ((TranslatedFSString)attribute.Value);
                        fs.Arguments = args;
                    }
                    else
                    {
                        fsStringArguments = args;
                    }
                }
                else
                {
                    throw new InvalidDataException("Unexpected JSON token during parsing of attribute: " + reader.TokenType);
                }
            }

            return(attribute);
        }
예제 #7
0
파일: LSBReader.cs 프로젝트: slimlime/lslib
        private NodeAttribute ReadAttribute(NodeAttribute.DataType type)
        {
            switch (type)
            {
            case NodeAttribute.DataType.DT_String:
            case NodeAttribute.DataType.DT_Path:
            case NodeAttribute.DataType.DT_FixedString:
            case NodeAttribute.DataType.DT_LSString:
            {
                var attr = new NodeAttribute(type);
                attr.Value = ReadString(true);
                return(attr);
            }

            case NodeAttribute.DataType.DT_WString:
            case NodeAttribute.DataType.DT_LSWString:
            {
                var attr = new NodeAttribute(type);
                attr.Value = ReadWideString(true);
                return(attr);
            }

            case NodeAttribute.DataType.DT_TranslatedString:
            {
                var attr = new NodeAttribute(type);
                var str  = new TranslatedString();

                if (IsBG3)
                {
                    str.Version = reader.ReadUInt16();

                    // Sometimes BG3 string keys still contain the value?
                    // Weird heuristic to find these cases
                    var test = reader.ReadUInt16();
                    if (test == 0)
                    {
                        stream.Seek(-4, SeekOrigin.Current);
                        str.Version = 0;
                        str.Value   = ReadString(true);
                    }
                    else
                    {
                        stream.Seek(-2, SeekOrigin.Current);
                        str.Value = null;
                    }
                }
                else
                {
                    str.Version = 0;
                    str.Value   = ReadString(true);
                }

                str.Handle = ReadString(true);
                attr.Value = str;
                return(attr);
            }

            case NodeAttribute.DataType.DT_ScratchBuffer:
            {
                var attr         = new NodeAttribute(type);
                var bufferLength = reader.ReadInt32();
                attr.Value = reader.ReadBytes(bufferLength);
                return(attr);
            }

            // DT_TranslatedFSString not supported in LSB
            default:
                return(BinUtils.ReadAttribute(type, reader));
            }
        }