private static FragFileHeaderRecord ReadFragHeader(byte[] bytes, int hdrNum) { var pFragHeader = hdrNum * 8; var thisHeader = new FragFileHeaderRecord(); thisHeader.fragHdrNbr = BitConverter.ToUInt16(bytes, pFragHeader); pFragHeader += 2; thisHeader.totalSpeakers = BitConverter.ToUInt16(bytes, pFragHeader); pFragHeader += 2; thisHeader.fragOffset = BitConverter.ToUInt32(bytes, pFragHeader); pFragHeader += 4; thisHeader.data = new FragFileDataRecord[thisHeader.totalSpeakers]; var pSpeakerData = thisHeader.fragOffset; var thisDataCount = 0; for (var i = 0; i < thisHeader.totalSpeakers; i++) { var thisData = new FragFileDataRecord(); thisData.speaker = BitConverter.ToUInt16(bytes, (int)pSpeakerData); pSpeakerData += 2; thisData.fileNbr = BitConverter.ToUInt16(bytes, (int)pSpeakerData); pSpeakerData += 2; thisHeader.data[thisDataCount] = thisData; thisDataCount++; } return(thisHeader); }
public static FragFile LoadFromXml(string fragXmlFilePath) { var toReturn = new FragFile(); var headers = new FragFileHeaderRecord[0]; var thisHeader = new FragFileHeaderRecord(); var parsed = false; long val = 0; using (var fs = new FileStream(fragXmlFilePath, FileMode.Open, FileAccess.Read)) using (XmlReader xr = new XmlTextReader(fs)) { while (xr.Read()) { if (xr.NodeType == XmlNodeType.Element && xr.Name == "FragFile") { //string numFragsString= xr.GetAttribute("numFrags"); //parsed = Int64.TryParse(numFragsString, out val); //if (parsed) //{ // headers = new FragFileHeaderRecord[val]; //} //else //{ // throw new IOException(string.Format("Could not parse {0}, bad or missing @numFrags attribute in /FragFile root element.", fragXmlFilePath)); //} headers = new FragFileHeaderRecord[0]; } if (xr.NodeType == XmlNodeType.Element && xr.Name == "Frag") { thisHeader = new FragFileHeaderRecord(); var fragIdString = xr.GetAttribute("id"); parsed = Int64.TryParse(fragIdString, out val); if (parsed) { thisHeader.fragHdrNbr = (ushort)val; } else { throw new IOException( string.Format( "Could not parse {0}, bad or missing @id attribute in /FragFile/Frag element.", fragXmlFilePath)); } //string totalSpeakersString= xr.GetAttribute("numSpeakers"); //parsed = Int64.TryParse(totalSpeakersString, out val); //if (parsed) //{ // thisHeader.data = new FragFileDataRecord[thisHeader.totalSpeakers]; // thisHeader.totalSpeakers = (ushort)val; //} //else //{ // throw new IOException(string.Format("Could not parse {0}, bad or missing @numSpeakers attribute in /FragFile/Frag element.", fragXmlFilePath)); //} thisHeader.data = new FragFileDataRecord[0]; } else if (xr.NodeType == XmlNodeType.Element && xr.Name == "Speaker") { var data = new FragFileDataRecord(); var voiceNumString = xr.GetAttribute("voice"); parsed = Int64.TryParse(voiceNumString, out val); if (parsed) { data.speaker = (ushort)val; } else { throw new IOException( string.Format( "Could not parse {0}, bad or missing @voice attribute in /FragFile/Frag/Speaker element.", fragXmlFilePath)); } var tlkId = xr.GetAttribute("tlkId"); parsed = Int64.TryParse(tlkId, out val); if (parsed) { data.fileNbr = (ushort)val; } else { throw new IOException( string.Format( "Could not parse {0}, bad or missing @tlkId attribute in /FragFile/Frag/Speaker element.", fragXmlFilePath)); } Array.Resize(ref thisHeader.data, thisHeader.data.Length + 1); thisHeader.data[thisHeader.data.Length - 1] = data; thisHeader.totalSpeakers++; } else if (xr.NodeType == XmlNodeType.EndElement && xr.Name == "Frag") { if (thisHeader.fragHdrNbr > headers.Length - 1) { //throw new IOException(string.Format("Could not parse {0}, @id attribute value in /FragFile/Frag element exceeds (@numFrags-1) declared in the /FragFile root element.", fragXmlFilePath)); Array.Resize(ref headers, thisHeader.fragHdrNbr + 1); } //else //{ if (thisHeader.totalSpeakers > 0) { headers[thisHeader.fragHdrNbr] = thisHeader; } //} } } } toReturn.headers = headers; toReturn.FixupOffsets(); return(toReturn); }