/// <summary> /// Serializes a structure into a temporary memory block. /// </summary> /// <param name="context">The serialization context to use.</param> /// <param name="tagStream">The stream to write completed blocks of tag data to.</param> /// <param name="block">The temporary block to write incomplete tag data to.</param> /// <param name="info">Information about the tag structure type.</param> /// <param name="structure">The structure to serialize.</param> /// <exception cref="System.InvalidOperationException">Structure type must have TagStructureAttribute</exception> private void SerializeStruct(ISerializationContext context, MemoryStream tagStream, IDataBlock block, TagStructureInfo info, object structure) { var baseOffset = block.Stream.Position; var enumerator = new TagFieldEnumerator(info); while (enumerator.Next()) { SerializeProperty(context, tagStream, block, structure, enumerator, baseOffset); } // Honor the struct size if it's defined if (info.TotalSize > 0) { block.Stream.Position = baseOffset + info.TotalSize; if (block.Stream.Position > block.Stream.Length) { block.Stream.SetLength(block.Stream.Position); } } // Honor alignment if (info.Structure.Align > 0) { block.SuggestAlignment(info.Structure.Align); } }
/// <summary> /// Deserializes a structure. /// </summary> /// <param name="reader">The reader.</param> /// <param name="context">The serialization context to use.</param> /// <param name="info">Information about the structure to deserialize.</param> /// <returns>The deserialized structure.</returns> /// <exception cref="System.InvalidOperationException">Target type must have TagStructureAttribute</exception> private object DeserializeStruct(BinaryReader reader, ISerializationContext context, TagStructureInfo info) { var baseOffset = reader.BaseStream.Position; var instance = Activator.CreateInstance(info.Types[0]); var enumerator = new TagFieldEnumerator(info); while (enumerator.Next()) { DeserializeProperty(reader, context, instance, enumerator, baseOffset); } if (enumerator.Info.TotalSize > 0) { reader.BaseStream.Position = baseOffset + enumerator.Info.TotalSize; } return(instance); }
public override bool Execute(List<string> args) { if (args.Count > 1) return false; var match = (args.Count == 1); var token = match ? args[0].ToLower() : ""; var enumerator = new TagFieldEnumerator(Structure); while (enumerator.Next()) { var nameString = enumerator.Field.Name; if (match && !nameString.ToLower().Contains(token)) continue; var fieldType = enumerator.Field.FieldType; var fieldValue = enumerator.Field.GetValue(Value); var typeString = fieldType.IsGenericType ? $"{fieldType.Name}<{fieldType.GenericTypeArguments[0].Name}>" : fieldType.Name; string valueString; if (fieldValue == null) valueString = "null"; else if (fieldType.GetInterface(typeof(IList).Name) != null) valueString = ((IList)fieldValue).Count != 0 ? $"{{...}}[{((IList)fieldValue).Count}]" : "null"; else if (fieldType == typeof(StringID)) valueString = Info.StringIDs.GetString((StringID)fieldValue); else if (fieldType == typeof(TagInstance)) valueString = $"[0x{((TagInstance)fieldValue).Index:X4}] {Info.TagNames[((TagInstance)fieldValue).Index]}.{Info.StringIDs.GetString(((TagInstance)fieldValue).Group.Name)}"; else valueString = fieldValue.ToString(); Console.WriteLine("{0}: {1} = {2}", nameString, typeString, valueString); } return true; }
/// <summary> /// Serializes a structure into a temporary memory block. /// </summary> /// <param name="context">The serialization context to use.</param> /// <param name="tagStream">The stream to write completed blocks of tag data to.</param> /// <param name="block">The temporary block to write incomplete tag data to.</param> /// <param name="info">Information about the tag structure type.</param> /// <param name="structure">The structure to serialize.</param> /// <exception cref="System.InvalidOperationException">Structure type must have TagStructureAttribute</exception> private void SerializeStruct(ISerializationContext context, MemoryStream tagStream, IDataBlock block, TagStructureInfo info, object structure) { var baseOffset = block.Stream.Position; var enumerator = new TagFieldEnumerator(info); while (enumerator.Next()) SerializeProperty(context, tagStream, block, structure, enumerator, baseOffset); // Honor the struct size if it's defined if (info.TotalSize > 0) { block.Stream.Position = baseOffset + info.TotalSize; if (block.Stream.Position > block.Stream.Length) block.Stream.SetLength(block.Stream.Position); } // Honor alignment if (info.Structure.Align > 0) block.SuggestAlignment(info.Structure.Align); }
private object CreateElement(Type elementType) { var element = Activator.CreateInstance(elementType); var isTagStructure = Attribute.IsDefined(elementType, typeof(TagStructureAttribute)); if (isTagStructure) { var enumerator = new TagFieldEnumerator( new TagStructureInfo(elementType)); while (enumerator.Next()) { var fieldType = enumerator.Field.FieldType; if (fieldType.IsArray && enumerator.Attribute.Count > 0) { var array = (IList)Activator.CreateInstance(enumerator.Field.FieldType, new object[] { enumerator.Attribute.Count }); for (var i = 0; i < enumerator.Attribute.Count; i++) array[i] = CreateElement(fieldType.GetElementType()); } else { try { enumerator.Field.SetValue(element, CreateElement(enumerator.Field.FieldType)); } catch { enumerator.Field.SetValue(element, null); } } } } return element; }