Пример #1
0
 /// <summary>
 /// Override to serialize the object to a byte array that can be stored
 /// in the GFF's binary data.  It serializes the data according to 
 /// BioWare's GFF file specification.  Simple data items are returned
 /// in the return value, complex items are stored in the stream and 0
 /// is returned.
 /// </summary>
 /// <param name="rawData">The raw data in which to store the field's
 /// data.</param>
 /// <returns>For simple items the return value contains the item's
 /// data and the stream is uneffected.  For complex items, the
 /// return value is the offset of the written data and the items's 
 /// data is added to the end of the stream.</returns>
 UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
 {
     byte[] bytes = BitConverter.GetBytes((uint) Value.Length);
     uint offset = rawData.WriteComplexData(bytes, bytes.Length);
     rawData.WriteComplexData(Value.GetBuffer(), (int) Value.Length);
     return offset;
 }
Пример #2
0
 /// <summary>
 /// Override to serialize the object to a byte array that can be stored
 /// in the GFF's binary data.  It serializes the data according to 
 /// BioWare's GFF file specification.  Simple data items are returned
 /// in the return value, complex items are stored in the stream and 0
 /// is returned.
 /// </summary>
 /// <param name="rawData">The raw data in which to store the field's
 /// data.</param>
 /// <returns>For simple items the return value contains the item's
 /// data and the stream is uneffected.  For complex items, the
 /// return value is the offset of the written data and the items's 
 /// data is added to the end of the stream.</returns>
 UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
 {
     return SaveFieldStruct(structureType, Value, rawData);
 }
Пример #3
0
        /// <summary>
        /// Override to deserialize the object from the GFF file's binary data.
        /// </summary>
        /// <param name="rawField">The raw field from the GFF</param>
        /// <param name="rawData">The GFF's raw file data</param>
        void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
        {
            // Determine the length of the bob.
            byte[] complexData = rawData.GetComplexDataBuffer();
            uint length = BitConverter.ToUInt32(complexData, (int) rawField.DataOrDataOffset);

            // Copy the data from the complex data byte array to a local byte array.
            byte[] bytes = new Byte[length];
            for (int i = 0; i < length; i++)
                bytes[i] = complexData[rawField.DataOrDataOffset + 4 + i];

            // Save the data in a memory stream.
            Value = new MemoryStream(bytes, 0, bytes.Length, true, true);
        }
Пример #4
0
 /// <summary>
 /// Override to serialize the object to a byte array that can be stored
 /// in the GFF's binary data.  It serializes the data according to 
 /// BioWare's GFF file specification.  Simple data items are returned
 /// in the return value, complex items are stored in the stream and 0
 /// is returned.
 /// </summary>
 /// <param name="rawData">The raw data in which to store the field's
 /// data.</param>
 /// <returns>For simple items the return value contains the item's
 /// data and the stream is uneffected.  For complex items, the
 /// return value is the offset of the written data and the items's 
 /// data is added to the end of the stream.</returns>
 UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
 {
     byte[] bytes = Value.Serialize();
     return rawData.WriteComplexData(bytes, bytes.Length);
 }
Пример #5
0
        /// <summary>
        /// Override to deserialize the object from the GFF file's binary data.
        /// </summary>
        /// <param name="rawField">The raw field from the GFF</param>
        /// <param name="rawData">The GFF's raw file data</param>
        void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
        {
            // Save the structure type.
            RawGffData.RawGffStruct rawStruct = rawData.GetStruct(rawField.DataOrDataOffset);
            structureType = rawStruct.Type;

            // Fill in the field dictionary and assign it to our value.
            Value = GetFieldStruct(rawStruct, rawData);
        }
Пример #6
0
 /// <summary>
 /// Override to deserialize the object from the GFF file's binary data.
 /// </summary>
 /// <param name="rawField">The raw field from the GFF</param>
 /// <param name="rawData">The GFF's raw file data</param>
 void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
 {
     Value = BitConverter.ToUInt64(rawData.GetComplexDataBuffer(),
         (int) rawField.DataOrDataOffset);
 }
Пример #7
0
 /// <summary>
 /// Override to deserialize the object from the GFF file's binary data.
 /// </summary>
 /// <param name="rawField">The raw field from the GFF</param>
 /// <param name="rawData">The GFF's raw file data</param>
 void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
 {
     Value = (uint) rawField.DataOrDataOffset;
 }
Пример #8
0
        /// <summary>
        /// Override to serialize the object to a byte array that can be stored
        /// in the GFF's binary data.  It serializes the data according to 
        /// BioWare's GFF file specification.  Simple data items are returned
        /// in the return value, complex items are stored in the stream and 0
        /// is returned.
        /// </summary>
        /// <param name="rawData">The raw data in which to store the field's
        /// data.</param>
        /// <returns>For simple items the return value contains the item's
        /// data and the stream is uneffected.  For complex items, the
        /// return value is the offset of the written data and the items's 
        /// data is added to the end of the stream.</returns>
        UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
        {
            // Create an array to hold all of the structure indeces,
            // then loop through all of the structures, serializing
            // them and saving the returned offsets.
            uint[] indeces = new uint[Value.Count];
            for (int i = 0; i < Value.Count; i++)
            {
                IGffFieldSerialize serialize = (IGffFieldSerialize) Value[i];
                indeces[i] = serialize.Serialize(rawData);
            }

            // Add the index array to the list indeces and return that offset.
            return rawData.AddListIndeces(indeces);
        }
Пример #9
0
 /// <summary>
 /// Override to deserialize the object from the GFF file's binary data.
 /// </summary>
 /// <param name="rawField">The raw field from the GFF</param>
 /// <param name="rawData">The GFF's raw file data</param>
 void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
 {
     byte[] bytes = rawData.GetComplexDataBuffer();
     byte length = bytes[rawField.DataOrDataOffset];
     Value = RawGffData.DeserializeString(bytes,
         (int) rawField.DataOrDataOffset + 1, (int) length);
 }
Пример #10
0
 /// <summary>
 /// Override to serialize the object to a byte array that can be stored
 /// in the GFF's binary data.  It serializes the data according to 
 /// BioWare's GFF file specification.  Simple data items are returned
 /// in the return value, complex items are stored in the stream and 0
 /// is returned.
 /// </summary>
 /// <param name="rawData">The raw data in which to store the field's
 /// data.</param>
 /// <returns>For simple items the return value contains the item's
 /// data and the stream is uneffected.  For complex items, the
 /// return value is the offset of the written data and the items's 
 /// data is added to the end of the stream.</returns>
 UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
 {
     byte[] bytes = BitConverter.GetBytes(Value);
     return BitConverter.ToUInt32(bytes, 0);
 }
Пример #11
0
        /// <summary>
        /// Override to deserialize the object from the GFF file's binary data.
        /// </summary>
        /// <param name="rawField">The raw field from the GFF</param>
        /// <param name="rawData">The GFF's raw file data</param>
        void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
        {
            // Get the list of structure indeces for the items in the list
            uint[] indeces = rawData.GetListIndeces(rawField.DataOrDataOffset);

            // Create a field collection for the structures, and loop through
            // the index array.
            GffFieldCollection fields = new GffFieldCollection();
            for (int i = 0; i < indeces.Length; i++)
            {
                // Get the raw structure data for this structure.
                RawGffData.RawGffStruct rawStruct = rawData.GetStruct(indeces[i]);

                // Create a GffStructField object for the structure and set it's
                // structure type.
                GffStructField field = (GffStructField)
                    GffFieldFactory.CreateField(GffFieldType.Struct);
                field.StructureType = rawStruct.Type;

                // Create a dummy field object so we can deserialize the structure,
                // set it's DataOrDataOffset to the structure index.
                RawGffData.RawGffField dummyField = new RawGffData.RawGffField(GffFieldType.Struct);
                dummyField.LabelIndex = 0;
                dummyField.DataOrDataOffset = indeces[i];

                // Deserialize the structure.
                ((IGffFieldSerialize) field).Deserialize(dummyField, rawData);

                // Add the structure to the collection.
                fields.Add(field);
            }

            Value = fields;
        }
Пример #12
0
 /// <summary>
 /// Override to deserialize the object from the GFF file's binary data.
 /// </summary>
 /// <param name="rawField">The raw field from the GFF</param>
 /// <param name="rawData">The GFF's raw file data</param>
 void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
 {
     byte[] bytes = BitConverter.GetBytes(rawField.DataOrDataOffset);
     Value = BitConverter.ToSingle(bytes, 0);
 }
Пример #13
0
        /// <summary>
        /// Creates a GffField derived object and deserializes the field's data
        /// into the created object.
        /// </summary>
        /// <param name="rawField">The raw field data for the object to create</param>
        /// <param name="rawData">The raw GFF file data</param>
        /// <returns>The created GffField derived object</returns>
        public static GffField CreateField(RawGffData.RawGffField rawField, 
			RawGffData rawData)
        {
            // Create an empty GffField object.
            GffField field = CreateField((GffFieldType) rawField.Type);

            // Get the field's IGffFieldSerialize implementation.
            IGffFieldSerialize serialize = field as IGffFieldSerialize;
            if (null == serialize) throw new InvalidCastException("IGffSerialize not implemented");

            // Deserialize the object.
            serialize.Deserialize(rawField, rawData);
            return field;
        }
Пример #14
0
 /// <summary>
 /// Override to deserialize the object from the GFF file's binary data.
 /// </summary>
 /// <param name="rawField">The raw field from the GFF</param>
 /// <param name="rawData">The GFF's raw file data</param>
 void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
 {
     byte[] bytes = rawData.GetComplexDataBuffer();
     uint length = BitConverter.ToUInt32(bytes, (int) rawField.DataOrDataOffset);
     Value = RawGffData.DeserializeString(bytes,
         (int) rawField.DataOrDataOffset + 4, (int) length);
 }
Пример #15
0
        /// <summary>
        /// Saves the GFF, using a new file name.
        /// </summary>
        /// <param name="fileName">The new file name for the GFF</param>
        public void SaveAs(string fileName)
        {
            // Save the top level structure to the raw data.
            RawGffData rawData = new RawGffData();
            GffStructField.SaveFieldStruct(0xffffffff, topLevel, rawData);

            // Create a header for the GFF file.
            string type = Path.GetExtension(fileName);
            type = type.Substring(1, type.Length - 1);
            GffHeader header = new GffHeader(type);
            rawData.InitializeHeader(ref header);

            // Create the disk file and save the header and raw data.
            using (FileStream writer = new FileStream(fileName, FileMode.Create,
                       FileAccess.Write, FileShare.Write))
            {
                header.Serialize(writer);
                rawData.Save(writer);
                writer.Close();
            }
        }
Пример #16
0
        /// <summary>
        /// Override to serialize the object to a byte array that can be stored
        /// in the GFF's binary data.  It serializes the data according to 
        /// BioWare's GFF file specification.  Simple data items are returned
        /// in the return value, complex items are stored in the stream and 0
        /// is returned.
        /// </summary>
        /// <param name="rawData">The raw data in which to store the field's
        /// data.</param>
        /// <returns>For simple items the return value contains the item's
        /// data and the stream is uneffected.  For complex items, the
        /// return value is the offset of the written data and the items's 
        /// data is added to the end of the stream.</returns>
        UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
        {
            // Write the string length first, saving the offset of the written
            // position.
            byte[] bytes = new Byte[1];
            bytes[0] = (byte) Value.Length;
            uint offset = rawData.WriteComplexData(bytes, bytes.Length);

            // Now write the string data.
            bytes = new byte[Value.Length];
            for (int i = 0; i < Value.Length; i++)
                bytes[i] = (byte) Value[i];
            rawData.WriteComplexData(bytes, bytes.Length);

            // return the offset of our data.
            return offset;
        }
Пример #17
0
        /// <summary>
        /// Loads the GFF file from the specified stream.
        /// </summary>
        /// <param name="stream">The stream to load the GFF file from.s</param>
        private void LoadStream(Stream stream)
        {
            // Read the header.
            NWNLogger.Log(0, "Gff.LoadStream loading header");
            GffHeader header = new GffHeader(stream);
            NWNLogger.Log(1, "Gff.LoadStream version {0}", header.VersionText);
            if ("V3.2" != header.VersionText)
                throw new NWNException("Version {0} GFF files are unsupported", header.VersionText);

            NWNLogger.Log(0, "Gff.LoadStream reading raw GFF data");
            RawGffData rawData = new RawGffData(stream, header);
            topLevel = GffStructField.GetFieldStruct(rawData.GetStruct(0), rawData);
        }
Пример #18
0
        public static GffFieldDictionary GetFieldStruct(RawGffData.RawGffStruct gstruct, 
			RawGffData rawData)
        {
            // Loop through all of the fields in the struct adding them to the
            // collection.
            GffFieldDictionary fields = new GffFieldDictionary();
            for (int i = 0; i < gstruct.FieldCount; i++)
            {
                // Get the index of the current field.  If the structure has 1
                // member then the offset is in DataOrDataOffset directly, if not
                // then DataOrDataOffset points to an array of DWORD indeces in
                // the raw field indeces block.
                uint fieldIndex = 1 == gstruct.FieldCount ?
                    gstruct.DataOrDataOffset :
                    rawData.GetFieldIndex((uint) (gstruct.DataOrDataOffset + (i * 4)));

                // Get the data label.
                RawGffData.RawGffField rawField = rawData.GetField(fieldIndex);
                string label = rawData.GetLabel(rawField.LabelIndex);

                // Create a GffField object for the field and add it to the
                // dictionary, using the label as the key.
                GffField field = GffFieldFactory.CreateField(rawField, rawData);
                fields.Add(label, field);
            }

            return fields;
        }
Пример #19
0
 /// <summary>
 /// Override to serialize the object to a byte array that can be stored
 /// in the GFF's binary data.  It serializes the data according to 
 /// BioWare's GFF file specification.  Simple data items are returned
 /// in the return value, complex items are stored in the stream and 0
 /// is returned.
 /// </summary>
 /// <param name="rawData">The raw data in which to store the field's
 /// data.</param>
 /// <returns>For simple items the return value contains the item's
 /// data and the stream is uneffected.  For complex items, the
 /// return value is the offset of the written data and the items's 
 /// data is added to the end of the stream.</returns>
 UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
 {
     byte[] bytes = BitConverter.GetBytes(Value);
     return rawData.WriteComplexData(bytes, bytes.Length);
 }
Пример #20
0
        /// <summary>
        /// Saves all of the data associated with the given structure.
        /// </summary>
        /// <param name="type">The type ID of the structure</param>
        /// <param name="dict">The structure's dictionary</param>
        /// <param name="rawData">The raw data in which to save the structure</param>
        /// <returns>The index of the structure in the raw data</returns>
        public static uint SaveFieldStruct(uint type, GffFieldDictionary dict, 
			RawGffData rawData)
        {
            // Create the structure and fill in the data we can then add it.  Note
            // that we can't fill in the DataOrDataOffset yet since we don't know
            // what that value is until after all of the fields have been added.
            // We will update the structure when we are done.
            RawGffData.RawGffStruct rawStruct = new RawGffData.RawGffStruct(type);
            rawStruct.FieldCount = (uint) dict.Count;
            rawStruct.DataOrDataOffset = 0;
            uint structureIndex = rawData.AddStruct(rawStruct);

            // Create an array to hold all of the field index values and loop through
            // the dictionary to store all of the structure elements.
            uint[] indeces = new uint[dict.Count];
            uint i = 0;
            foreach (DictionaryEntry entry in dict)
            {
                // Get the label and field from the entry.
                string label = (string) entry.Key;
                GffField field = (GffField) entry.Value;

                // Create a raw field for the field.
                RawGffData.RawGffField rawField = new RawGffData.RawGffField(field.Type);

                // Get the index of the label, adding it if it's not in the raw data.
                rawField.LabelIndex = (uint) rawData.GetLabelIndex(label);
                if (0xffffffff == rawField.LabelIndex)
                    rawField.LabelIndex = (uint) rawData.AddLabel(label);

                // Serialize the field's data and save the offset to the serialized
                // data (for some fields the offset may be the data itself).
                IGffFieldSerialize serialize = (IGffFieldSerialize) field;
                rawField.DataOrDataOffset = serialize.Serialize(rawData);

                // Add the field to the raw data, saving the index of the added
                // field in our index array.
                indeces[i++] = rawData.AddField(rawField);
            }

            // If we have multiple fields, we have to add the index array to the
            // field indeces and save the offset, otherwise we can just save the
            // field index as our data offset.  Once we do this we have to update
            // the structure to store the offset.
            rawStruct.DataOrDataOffset = 1 == dict.Count ?
                indeces[0] : rawData.AddFieldIndeces(indeces);
            rawData.UpdateStruct(structureIndex, rawStruct);

            // Return the structure's index.
            return structureIndex;
        }
Пример #21
0
 /// <summary>
 /// Override to serialize the object to a byte array that can be stored
 /// in the GFF's binary data.  It serializes the data according to 
 /// BioWare's GFF file specification.  Simple data items are returned
 /// in the return value, complex items are stored in the stream and 0
 /// is returned.
 /// </summary>
 /// <param name="rawData">The raw data in which to store the field's
 /// data.</param>
 /// <returns>For simple items the return value contains the item's
 /// data and the stream is uneffected.  For complex items, the
 /// return value is the offset of the written data and the items's 
 /// data is added to the end of the stream.</returns>
 UInt32 IGffFieldSerialize.Serialize(RawGffData rawData)
 {
     return (UInt32) Value;
 }
Пример #22
0
 /// <summary>
 /// Override to deserialize the object from the GFF file's binary data.
 /// </summary>
 /// <param name="rawField">The raw field from the GFF</param>
 /// <param name="rawData">The GFF's raw file data</param>
 void IGffFieldSerialize.Deserialize(RawGffData.RawGffField rawField, RawGffData rawData)
 {
     Value = new ExoLocString(rawData.GetComplexDataBuffer(),
         (int) rawField.DataOrDataOffset);
 }