コード例 #1
1
 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();
 }
コード例 #2
0
        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;
                }
            }
        }
コード例 #3
0
        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,
                });
            }
        }
コード例 #4
0
        public static string DumpToText(this ChunkBase chunk)
        {
            StringBuilder builder  = new StringBuilder();
            int           tabSpace = 0;

            GetBuilder(builder, chunk, ref tabSpace);
            return(builder.ToString());
        }
コード例 #5
0
 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--;
 }
コード例 #6
0
 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--;
 }
コード例 #7
0
        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);
        }
コード例 #8
0
        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;
        }
コード例 #9
0
        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;
        }
コード例 #10
0
        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());
        }