XLibrary.XNodeIn.ReadNode C# (CSharp) Method

ReadNode() public static method

public static ReadNode ( FileStream stream ) : XNodeIn
stream System.IO.FileStream
return XNodeIn
        public static XNodeIn ReadNode(FileStream stream)
        {
            // total size 4
            // name size 4
            // name x
            // type 4
            // value 4
            // external 1
            // anon 1
            // id 8
            // parent exist? 1
            //      parent id 4
            // dependencies? 1
            //      dependency count 4
            //      dependent ids 4x

            XNodeIn node = new XNodeIn();

            int totalSize = BitConverter.ToInt32(stream.Read(4), 0);

            int packetType = stream.ReadByte();
            Debug.Assert(packetType == (int)XPacketType.Node);

            node.Name = ReadString(stream);

            node.ObjType =(XObjType) BitConverter.ToInt32(stream.Read(4), 0);
            node.Lines = BitConverter.ToInt64(stream.Read(8), 0);
            node.External = BitConverter.ToBoolean(stream.Read(1), 0);
            node.IsAnon = BitConverter.ToBoolean(stream.Read(1), 0);
            node.ID = BitConverter.ToInt32(stream.Read(4), 0);

            // mod name for readability
            node.UnformattedName = node.Name;
            node.Name = Utilities.FormatTemplateName(node.UnformattedName);

            if (node.ObjType == XObjType.Field)
            {
                int pos = node.Name.LastIndexOf(' ');
                if (pos != -1)
                    node.UnformattedName = node.Name.Substring(pos + 1);
            }

            bool hasParent = BitConverter.ToBoolean(stream.Read(1), 0);
            if(hasParent)
                node.ParentID = BitConverter.ToInt32(stream.Read(4), 0);

            node.ReturnID = BitConverter.ToInt32(stream.Read(4), 0);

            int paramCount = BitConverter.ToInt32(stream.Read(4), 0);
            if (paramCount > 0)
            {
                node.ParamIDs = new int[paramCount];
                node.ParamNames = new string[paramCount];

                for (int i = 0; i < paramCount; i++)
                {
                    node.ParamIDs[i] = BitConverter.ToInt32(stream.Read(4), 0);
                    node.ParamNames[i] = ReadString(stream);
                }
            }

            int dependencyCount = BitConverter.ToInt32(stream.Read(4), 0);
            if(dependencyCount > 0)
            {
                node.Dependencies = new int[dependencyCount];

                for (int i = 0; i < dependencyCount; i++)
                    node.Dependencies[i] = BitConverter.ToInt32(stream.Read(4), 0);
            }

            node.CSharpLength = BitConverter.ToInt32(stream.Read(4), 0);
            node.CSharpPos = stream.Position;
            stream.Position += node.CSharpLength;

            node.MsilLines = BitConverter.ToInt32(stream.Read(4), 0);
            if (node.MsilLines > 0)
                node.MsilPos = stream.Position;

            //stream.Position += node.MsilLines; // lines dont translate into bytes

            return node;
        }