private void AddDictNodes(TreeNode parent, GffFieldDictionary dict) { System.Text.StringBuilder b = new System.Text.StringBuilder(1024); foreach (DictionaryEntry entry in dict) { GffField field = (GffField) entry.Value; if (field.IsList) { TreeNode node = parent.Nodes.Add(entry.Key.ToString()); AddCollNodes(node, ((GffListField) field).Value); } else if (field.IsStruct) { TreeNode node = parent.Nodes.Add(entry.Key.ToString()); AddDictNodes(node, ((GffStructField) field).Value); } else { b.Length = 0; b.AppendFormat ("{0} = [{1}]", entry.Key.ToString(), field.ToString()); parent.Nodes.Add(b.ToString()); } } }
/// <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); }
/// <summary> /// Gets the GffField derived object for the given schema. It will check /// the passed field dictionary for the field, if it is not found then it will /// create it and add it to the dictionary. /// </summary> /// <param name="schema">The field's schema</param> /// <param name="dict">The field dictionary to check</param> /// <returns>The GffField derived object for the field.</returns> protected GffField GetField(GffFieldSchema schema, GffFieldDictionary dict) { // Look up the field for the label. If we cannot look it up then // it has not been added to the module info file, we need to add // it ourselves. GffField field = dict[schema.Tag]; if (null == field) { field = schema.CreateField(); dict.Add(schema.Tag, field); } return field; }
/// <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); }
/// <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; }
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; }
/// <summary> /// Class constructor. /// </summary> /// <param name="stream">The memory stream containing the data</param> public GffStructField(GffFieldDictionary dict) : base(GffFieldType.Struct, dict) { structureType = 0; }