Esempio n. 1
0
        private static CommFileHeaderRecord ReadHeader(byte[] bytes, int headerNum)
        {
            var pCommHeader = headerNum * 14;
            var thisHeader  = new CommFileHeaderRecord
            {
                commHdrNbr = BitConverter.ToUInt16(bytes, pCommHeader)
            };

            pCommHeader        += 2;
            thisHeader.warp     = BitConverter.ToUInt16(bytes, pCommHeader);
            pCommHeader        += 2;
            thisHeader.priority = bytes[pCommHeader];
            pCommHeader++;
            thisHeader.positionElement = (sbyte)bytes[pCommHeader];
            pCommHeader++;
            thisHeader.bullseye      = BitConverter.ToInt16(bytes, pCommHeader);
            pCommHeader             += 2;
            thisHeader.totalElements = bytes[pCommHeader];
            pCommHeader++;
            thisHeader.totalEvals = bytes[pCommHeader];
            pCommHeader++;
            thisHeader.commOffset = BitConverter.ToUInt32(bytes, pCommHeader);
            pCommHeader          += 4;
            thisHeader.data       = new CommFileDataRecord[thisHeader.totalElements];
            var pData = thisHeader.commOffset;

            for (var i = 0; i < thisHeader.totalElements; i++)
            {
                var thisData = new CommFileDataRecord
                {
                    fragIdOrEvalId = BitConverter.ToInt16(bytes, (int)pData)
                };
                pData += 2;
                thisHeader.data[i] = thisData;
            }
            return(thisHeader);
        }
Esempio n. 2
0
        public static CommFile LoadFromXml(string commXmlFilePath)
        {
            var toReturn = new CommFile();
            var headers  = new CommFileHeaderRecord[0];

            using (var fs = new FileStream(commXmlFilePath, FileMode.Open, FileAccess.Read))
                using (XmlReader xr = new XmlTextReader(fs))
                {
                    var thisHeader  = new CommFileHeaderRecord();
                    var dataRecords = new CommFileDataRecord[0];
                    while (xr.Read())
                    {
                        long   val;
                        string attribValString;
                        bool   parsed;
                        if (xr.NodeType == XmlNodeType.Element && xr.Name == "CommFile")
                        {
                            //attribValString = xr.GetAttribute("numComms");
                            //parsed = Int64.TryParse(attribValString, out val);
                            //if (parsed)
                            //{
                            // headers = new CommFileHeaderRecord[val];
                            //}
                            //else
                            //{
                            //    throw new IOException(string.Format("Could not parse {0}, bad or missing @numComms attribute in /CommFile root element.", commXmlFilePath));
                            //}
                            headers = new CommFileHeaderRecord[0];
                        }
                        if (xr.NodeType == XmlNodeType.Element && xr.Name == "Comm")
                        {
                            thisHeader = new CommFileHeaderRecord();

                            attribValString = xr.GetAttribute("id");
                            parsed          = Int64.TryParse(attribValString, out val);
                            if (parsed)
                            {
                                thisHeader.commHdrNbr = (ushort)val;
                            }
                            else
                            {
                                throw new IOException(
                                          string.Format(
                                              "Could not parse {0}, bad or missing @id attribute in /CommFile/Comm element.",
                                              commXmlFilePath));
                            }

                            attribValString = xr.GetAttribute("warp");
                            parsed          = Int64.TryParse(attribValString, out val);
                            if (parsed)
                            {
                                thisHeader.warp = (ushort)val;
                            }
                            else
                            {
                                throw new IOException(
                                          string.Format(
                                              "Could not parse {0}, bad or missing @warp attribute in /CommFile/Comm element.",
                                              commXmlFilePath));
                            }

                            attribValString = xr.GetAttribute("priority");
                            parsed          = Int64.TryParse(attribValString, out val);
                            if (parsed)
                            {
                                thisHeader.priority = (byte)val;
                            }
                            else
                            {
                                throw new IOException(
                                          string.Format(
                                              "Could not parse {0}, bad or missing @priority attribute in /CommFile/Comm element.",
                                              commXmlFilePath));
                            }

                            attribValString = xr.GetAttribute("positionElement");
                            parsed          = Int64.TryParse(attribValString, out val);
                            if (parsed)
                            {
                                thisHeader.positionElement = (sbyte)val;
                            }
                            else
                            {
                                throw new IOException(
                                          string.Format(
                                              "Could not parse {0}, bad or missing @positionElement attribute in /CommFile/Comm element.",
                                              commXmlFilePath));
                            }

                            attribValString = xr.GetAttribute("bullseye");
                            parsed          = Int64.TryParse(attribValString, out val);
                            if (parsed)
                            {
                                thisHeader.bullseye = (short)val;
                            }
                            else
                            {
                                throw new IOException(
                                          string.Format(
                                              "Could not parse {0}, bad or missing @bullseye attribute in /CommFile/Comm element.",
                                              commXmlFilePath));
                            }

                            //attribValString = xr.GetAttribute("totalElements");
                            //parsed = Int64.TryParse(attribValString, out val);
                            //if (parsed)
                            //{
                            //    thisHeader.totalElements = (byte)val;
                            //    dataRecords = new CommFileDataRecord[thisHeader.totalElements];
                            //}
                            //else
                            //{
                            //    throw new IOException(string.Format("Could not parse {0}, bad or missing @totalElements attribute in /CommFile/Comm element.", commXmlFilePath));
                            //}
                            dataRecords = new CommFileDataRecord[0];

                            //attribValString = xr.GetAttribute("totalEvals");
                            //parsed = Int64.TryParse(attribValString, out val);
                            //if (parsed)
                            //{
                            //    thisHeader.totalEvals = (byte)val;
                            //}
                            //else
                            //{
                            //    throw new IOException(string.Format("Could not parse {0}, bad or missing @totalEvals attribute in /CommFile/Comm element.", commXmlFilePath));
                            //}
                        }
                        else if (xr.NodeType == XmlNodeType.Element && (xr.Name == "CommElement"))
                        {
                            var thisElementDataRecord = new CommFileDataRecord();
                            attribValString = xr.GetAttribute("fragId");
                            if (!string.IsNullOrEmpty(attribValString))
                            {
                                parsed = Int64.TryParse(attribValString, out val);
                                if (parsed)
                                {
                                    thisElementDataRecord.fragIdOrEvalId = (short)val;
                                }
                                else
                                {
                                    throw new IOException(
                                              string.Format(
                                                  "Could not parse {0}, bad or missing @fragId attribute in /CommFile/Comm/CommElement element.",
                                                  commXmlFilePath));
                                }
                            }
                            else
                            {
                                attribValString = xr.GetAttribute("evalId");
                                if (string.IsNullOrEmpty(attribValString))
                                {
                                    throw new IOException(
                                              string.Format(
                                                  "Could not parse {0}, missing @fragId or @evalId attribute in /CommFile/Comm/CommElement element.",
                                                  commXmlFilePath));
                                }
                                parsed = Int64.TryParse(attribValString, out val);
                                if (parsed)
                                {
                                    thisHeader.totalEvals++;
                                    thisElementDataRecord.fragIdOrEvalId = (short)-val;
                                }
                                else
                                {
                                    throw new IOException(
                                              string.Format(
                                                  "Could not parse {0}, bad or missing @evalId attribute in /CommFile/Comm/CommElement element.",
                                                  commXmlFilePath));
                                }
                            }

                            attribValString = xr.GetAttribute("index");
                            parsed          = Int64.TryParse(attribValString, out val);
                            if (parsed)
                            {
                                var index = (int)val;
                                if (index > dataRecords.Length - 1)
                                {
                                    //throw new IOException(string.Format("Could not parse {0}, @index attribute value in /CommFile/Comm/CommElement element exceeds (@totalElements-1) value declared in parent /CommFile/Comm element.", commXmlFilePath));
                                    Array.Resize(ref dataRecords, index + 1);
                                }
                                dataRecords[index] = thisElementDataRecord;
                                thisHeader.totalElements++;
                            }
                            else
                            {
                                throw new IOException(
                                          string.Format(
                                              "Could not parse {0}, bad or missing @index attribute in /CommFile/Comm/CommElement element.",
                                              commXmlFilePath));
                            }
                        }
                        else if (xr.NodeType == XmlNodeType.EndElement && xr.Name == "Comm")
                        {
                            thisHeader.data = dataRecords;
                            if (thisHeader.commHdrNbr > headers.Length - 1)
                            {
                                //throw new IOException(string.Format("Could not parse {0}, @id attribute value in /CommFile/Comm element exceeds (@numComms-1) attribute value declared in /CommFile root element.", commXmlFilePath));
                                Array.Resize(ref headers, thisHeader.commHdrNbr + 1);
                            }
                            headers[thisHeader.commHdrNbr] = thisHeader;
                        }
                    }
                }
            toReturn.Headers = headers;
            toReturn.FixupOffsets();
            return(toReturn);
        }