public override bool Iterate(EsfNode fromNode) { ParentNode parent = fromNode.Parent as ParentNode; bool result = (parent != null); if (result) { bool pastNode = false; foreach (EsfNode candidate in parent.AllNodes) { if (pastNode) { if (!Visit(candidate)) { result = false; break; } } else { pastNode = (candidate == fromNode); } } } return(result); }
// encodes the given node public void Encode(BinaryWriter writer, EsfNode node) { try { //NamedNode named = node as NamedNode; if (node.TypeCode == EsfType.RECORD_BLOCK_ENTRY) { EncodeSized(writer, (node as ParentNode).AllNodes); } else if (node.TypeCode < EsfType.BOOL_ARRAY) { WriteValueNode(writer, node); } else if (node.TypeCode < EsfType.RECORD) { WriteArrayNode(writer, node); } else if (node.TypeCode == EsfType.RECORD) { WriteRecordNode(writer, node); } else if (node.TypeCode == EsfType.RECORD_BLOCK) { WriteRecordArrayNode(writer, node); } else { throw new NotImplementedException(string.Format("Cannot write type code {0:x} at {1:x}", node.TypeCode)); } } catch { Console.WriteLine(string.Format("failed to write node {0}", node)); throw; } }
// public T[] ReadArrayItems<T>(BinaryReader reader, ValueReader<T> ReadValue) { // long targetOffset = ReadSize(reader); // List<T> items = new List<T>(); // while (reader.BaseStream.Position < targetOffset) { // items.Add(ReadValue(reader)); // } // return items.ToArray(); // } protected void WriteArrayNode(BinaryWriter writer, EsfNode arrayNode) { // writer.Write((byte) arrayNode.TypeCode); #if DEBUG Console.WriteLine("writing array node type {0} at {1}", arrayNode.TypeCode, writer.BaseStream.Position); #endif (arrayNode as ICodecNode).Encode(writer); }
// read public EsfNode ReadValueNode(BinaryReader reader, EsfType typeCode) { try { EsfNode result = CreateValueNode(typeCode); (result as ICodecNode).Decode(reader, typeCode); return(result); } catch (Exception e) { throw new InvalidDataException(string.Format("{0} at {1:x}", e.Message, reader.BaseStream.Position)); } }
public override bool Iterate(EsfNode node) { bool result = (node != null && Visit(node)); if (result) { Iterate(node.Parent as ParentNode); } return(result); }
public static string CreatePath(EsfNode node, string separator = "/") { NodePathCreator creator = new NodePathCreator { PathSeparator = separator }; new ParentIterator { Visit = creator.Visit }.Iterate(node); return(creator.Path); }
public virtual void WriteValueNode(BinaryWriter writer, EsfNode node) { if (node is ICodecNode) { (node as ICodecNode).Encode(writer); } else { throw new InvalidDataException(string.Format("Invalid type code {0:x} at {1:x}", node.TypeCode, writer.BaseStream.Position)); } }
/* * Invalidate until the first memory-mapped node is found. */ private bool Invalidate(EsfNode node) { MemoryMappedRecordNode mapped = node as MemoryMappedRecordNode; bool continuteIteration = mapped == null; if (mapped != null) { mapped.Invalid = true; } // don't continue when a node was invalidated return(!continuteIteration); }
// protected virtual void WriteRecordArrayNode(BinaryWriter writer, EsfNode node) { // NamedNode recordBlockNode = node as NamedNode; // ushort nameIndex = (ushort)nodeNames.IndexOfValue(recordBlockNode.Name); // WriteRecordInfo(writer, (byte) EsfType.RECORD_BLOCK, nameIndex, recordBlockNode.Version); // long beforePosition = writer.BaseStream.Position; // writer.Seek(4, SeekOrigin.Current); // // write number of entries // WriteSize(writer, recordBlockNode.AllNodes.Count); // foreach (EsfNode child in recordBlockNode.AllNodes) { // EncodeSized(writer, (child as ParentNode).AllNodes); // } //// EncodeSized(writer, recordBlockNode.AllNodes); // long afterPosition = writer.BaseStream.Position; // writer.BaseStream.Seek(beforePosition, SeekOrigin.Begin); // WriteSize(writer, afterPosition); // writer.BaseStream.Seek(afterPosition, SeekOrigin.Begin); // } protected void WriteRecordArrayNode(BinaryWriter writer, EsfNode node) { RecordArrayNode recordBlockNode = node as RecordArrayNode; if (recordBlockNode != null) { recordBlockNode.Encode(writer); } else { throw new InvalidOperationException(); } }
public override bool Iterate(EsfNode node) { bool result = Visit(node); if (result) { ParentNode parent = node as ParentNode; if (parent != null) { parent.AllNodes.ForEach(n => Iterate(n)); } } return(result); }
public void EncodeRootNode(BinaryWriter writer, EsfNode rootNode) { WriteHeader(writer); long currentPosition = writer.BaseStream.Position; writer.Write(0); WriteRecordNode(writer, rootNode); // remember the offset of the node name list long nodeNamePosition = writer.BaseStream.Position; WriteNodeNames(writer); writer.BaseStream.Seek(currentPosition, SeekOrigin.Begin); writer.Write((uint)nodeNamePosition); }
public EsfNode Parse(BinaryReader reader) { reader.BaseStream.Seek(0, SeekOrigin.Begin); Header = ReadHeader(reader); uint nodeNameOffset = reader.ReadUInt32(); long restorePosition = reader.BaseStream.Position; reader.BaseStream.Seek(nodeNameOffset, SeekOrigin.Begin); ReadNodeNames(reader); reader.BaseStream.Seek(restorePosition, SeekOrigin.Begin); EsfNode result = Decode(reader); result.Codec = this; return(result); }
public bool Visit(EsfNode node) { INamedNode named = node as INamedNode; if (named is CompressedNode) { path = path.Substring(path.IndexOf(PathSeparator) + 1); } if (!(named is MemoryMappedRecordNode) || string.IsNullOrEmpty(path)) { path = string.Format("{0}{1}{2}", named.GetName(), PathSeparator, path); #if DEBUG Console.WriteLine("node {0} - {1}", named.GetName(), node.GetType()); #endif } return(true); }
// reads a node from the reader at its current position public EsfNode Decode(BinaryReader reader) { // read type code byte typeCode = reader.ReadByte(); if (NodeReadStarting != null) { NodeReadStarting(typeCode, reader.BaseStream.Position - 1); } EsfNode result = Decode(reader, typeCode); if (NodeReadFinished != null) { NodeReadFinished(result, reader.BaseStream.Position); } // Console.WriteLine(string.Format("decoded {0} at {1:x}", result.TypeCode, reader.BaseStream.Position)); return(result); }
protected virtual EsfNode CreateArrayNode(EsfType typeCode) { EsfNode result = null; switch (typeCode) { case EsfType.BOOL_ARRAY: result = new EsfArrayNode <bool>(this, typeCode); // CreateArrayNode<bool>(reader), ItemReader = ReadBool); break; case EsfType.INT8_ARRAY: result = new EsfArrayNode <sbyte>(this, typeCode); break; case EsfType.INT16_ARRAY: result = new EsfArrayNode <short>(this, typeCode); break; case EsfType.INT32_ARRAY: result = new EsfArrayNode <int>(this, typeCode); break; case EsfType.INT64_ARRAY: result = new EsfArrayNode <long>(this, typeCode); break; case EsfType.UINT8_ARRAY: result = new EsfArrayNode <byte>(this, typeCode); break; case EsfType.UINT16_ARRAY: result = new EsfArrayNode <ushort>(this, typeCode); break; case EsfType.UINT32_ARRAY: result = new EsfArrayNode <uint>(this, typeCode); break; case EsfType.UINT64_ARRAY: result = new EsfArrayNode <ulong>(this, typeCode); break; case EsfType.SINGLE_ARRAY: result = new EsfArrayNode <float>(this, typeCode); break; case EsfType.DOUBLE_ARRAY: result = new EsfArrayNode <double>(this, typeCode); break; case EsfType.COORD2D_ARRAY: result = new EsfArrayNode <Coordinates2D>(this, typeCode) { Separator = "-" }; break; case EsfType.COORD3D_ARRAY: result = new EsfArrayNode <Coordinates2D>(this, typeCode) { Separator = "-" }; break; case EsfType.UTF16_ARRAY: result = new EsfArrayNode <string>(this, EsfType.UTF16_ARRAY); break; case EsfType.ASCII_ARRAY: result = new EsfArrayNode <string>(this, EsfType.ASCII_ARRAY); break; case EsfType.ANGLE_ARRAY: result = new EsfArrayNode <ushort>(this, typeCode); break; default: throw new InvalidDataException(string.Format("Unknown array type code {0}", typeCode)); } result.TypeCode = typeCode; return(result); }
public EsfFile(EsfNode rootNode, EsfCodec codec) { Codec = codec; RootNode = rootNode; }
protected virtual void WriteRecordNode(BinaryWriter writer, EsfNode node) { RecordNode recordNode = node as RecordNode; recordNode.Encode(writer); }
public static void LogReadNode(EsfNode node, long position) { Console.WriteLine("{1:x}: read {0}", node, position); }
/* * Invalidate all memory-mapped nodes we come across. */ private bool InvalidateAll(EsfNode node) { Invalidate(node); return(true); }
private void ModifiedDelegate(EsfNode node) { Modified = node.Modified; }
public abstract bool Iterate(EsfNode node);