コード例 #1
0
ファイル: CommFile.cs プロジェクト: karpiyon/lightningstools
        public static CommFile LoadFromBinary(string commFilePath)
        {
            var fi = new FileInfo(commFilePath);

            if (!fi.Exists)
            {
                throw new FileNotFoundException(commFilePath);
            }

            var commFile = new CommFile();
            var bytes    = new byte[fi.Length];

            using (var fs = new FileStream(commFilePath, FileMode.Open, FileAccess.Read))
            {
                fs.Seek(0, SeekOrigin.Begin);
                fs.Read(bytes, 0, (int)fi.Length);
            }


            var firstHeader = ReadHeader(bytes, 0);
            var numHeaders  = firstHeader.commOffset / 14;

            commFile.Headers    = new CommFileHeaderRecord[numHeaders];
            commFile.Headers[0] = firstHeader;
            for (var i = 1; i < numHeaders; i++)
            {
                commFile.Headers[i] = ReadHeader(bytes, i);
            }

            return(commFile);
        }
コード例 #2
0
ファイル: CommFile.cs プロジェクト: karpiyon/lightningstools
        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);
        }