示例#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
        public static NodeAttribute ReadAttribute(NodeAttribute.DataType type, BinaryReader reader)
        {
            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_ULongLong:
                attr.Value = reader.ReadUInt64();
                break;

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

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

            case NodeAttribute.DataType.DT_UUID:
                attr.Value = new Guid(reader.ReadBytes(16));
                break;

            default:
                // Strings are serialized differently for each file format and should be
                // handled by the format-specific ReadAttribute()
                throw new InvalidFormatException(String.Format("ReadAttribute() not implemented for type {0}", type));
            }

            return(attr);
        }
示例#4
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);
        }
示例#5
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();

                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));
            }
        }