private static string GetPreMarks(ChunkBase tree) { var parent = tree.Parent; if (parent == null) return string.Empty; List<bool> lstline = new List<bool>(); while (parent != null) { var pp = parent.Parent; if (pp != null) { lstline.Add(pp.Children.IndexOf(parent) < pp.Children.Count - 1); } parent = pp; } StringBuilder builder = new StringBuilder(); for (int i = lstline.Count - 1; i >= 0; i--) { if (lstline[i]) builder.Append("│ "); else builder.Append(" "); } parent = tree.Parent; if (parent.Children.IndexOf(tree) < parent.Children.Count - 1) builder.Append("├─"); else builder.Append("└─"); return builder.ToString(); }
internal void ChunkBaseProcess(ParsingContext context) { var chunk = this; var reader = context.reader; while (chunk.BytesRead + ChunkBaseHelper.HeaderLength < chunk.Length)//如果还能读出一个子chunk。 { ChunkBase child = reader.ReadChunk(); child.Parent = this; this.Children.Add(child); child.Process(context); chunk.BytesRead += child.BytesRead; } { var parent = this.Parent; uint length = this.Length - this.BytesRead; if (length > 0) { if ((parent != null)) { var another = parent.Length - parent.BytesRead - this.BytesRead; length = Math.Min(length, another); } reader.BaseStream.Position += length; chunk.BytesRead += length; } } }
public static ChunkBase ReadChunk(this BinaryReader reader) { long position = reader.BaseStream.Position; // 2 byte ID ushort id = reader.ReadUInt16(); // 4 byte length uint length = reader.ReadUInt32(); // 2 + 4 = 6 uint bytesRead = HeaderLength; Type type; if (chunkIDDict.TryGetValue(id, out type)) { object obj = Activator.CreateInstance(type); ChunkBase result = obj as ChunkBase; //result.ID = id;//不再需要记录ID,此对象的类型就指明了它的ID。 result.Position = position; result.Length = length; result.BytesRead = bytesRead; return(result); } else { return(new UndefinedChunk() { Position = position, ID = id, Length = length, BytesRead = bytesRead, }); } }
public static string DumpToText(this ChunkBase chunk) { StringBuilder builder = new StringBuilder(); int tabSpace = 0; GetBuilder(builder, chunk, ref tabSpace); return(builder.ToString()); }
private static void GetBuilder(StringBuilder builder, ChunkBase tree, ref int tabSpace) { //builder.AppendLine(GetSpace(tabSpace) + tree.NodeValue.ToString()); //builder.AppendLine(GetBrantch(tabSpace)/*GetSpace(tabSpace)*/ + tree.NodeValue.ToString()); //builder.AppendLine(GetPreMarks(tree)/*GetSpace(tabSpace)*/ + tree.NodeValue.ToString()); builder.AppendLine(GetPreMarks(tree)/*GetSpace(tabSpace)*/ + tree.ToString()); tabSpace++; foreach (var item in tree.Children) { GetBuilder(builder, item, ref tabSpace); } tabSpace--; }
private static void GetBuilder(StringBuilder builder, ChunkBase tree, ref int tabSpace) { //builder.AppendLine(GetSpace(tabSpace) + tree.NodeValue.ToString()); //builder.AppendLine(GetBrantch(tabSpace)/*GetSpace(tabSpace)*/ + tree.NodeValue.ToString()); //builder.AppendLine(GetPreMarks(tree)/*GetSpace(tabSpace)*/ + tree.NodeValue.ToString()); builder.AppendLine(GetPreMarks(tree) /*GetSpace(tabSpace)*/ + tree.ToString()); tabSpace++; foreach (var item in tree.Children) { GetBuilder(builder, item, ref tabSpace); } tabSpace--; }
public static ushort GetID(this ChunkBase chunk) { ushort value; if (chunk is UndefinedChunk) { value = (chunk as UndefinedChunk).ID; } else { Type type = chunk.GetType(); value = chunkTypeDict[type];//如果此处不存在此type的key,说明static构造函数需要添加此类型的字典信息。 } return(value); }
private TreeNode BuildTreeNode(ChunkBase chunk) { TreeNode node = new TreeNode(chunk.ToString()); node.Tag = chunk; node.ToolTipText = chunk.ToString(); if (chunk.Length != chunk.BytesRead) { node.BackColor = Color.Red; } foreach (var item in chunk.Children) { var itemNode = BuildTreeNode(item); node.Nodes.Add(itemNode); } return node; }
private ChunkBase BuildSimpleTree(ChunkBase chunk) { var newChunk = ((ICloneable)chunk).Clone() as ChunkBase; newChunk.Parent = null; newChunk.Children = new List<ChunkBase>(); foreach (var item in chunk.Children) { if (item.GetType() != typeof(UndefinedChunk)) { var newItem = BuildSimpleTree(item); newItem.Parent = newChunk; newChunk.Children.Add(newItem); } } return newChunk; }
private static string GetPreMarks(ChunkBase tree) { var parent = tree.Parent; if (parent == null) { return(string.Empty); } List <bool> lstline = new List <bool>(); while (parent != null) { var pp = parent.Parent; if (pp != null) { lstline.Add(pp.Children.IndexOf(parent) < pp.Children.Count - 1); } parent = pp; } StringBuilder builder = new StringBuilder(); for (int i = lstline.Count - 1; i >= 0; i--) { if (lstline[i]) { builder.Append("│ "); } else { builder.Append(" "); } } parent = tree.Parent; if (parent.Children.IndexOf(tree) < parent.Children.Count - 1) { builder.Append("├─"); } else { builder.Append("└─"); } return(builder.ToString()); }