private List <TOCEntry> ReadTOC() { var entries = new List <TOCEntry>(); try { byte[] buffer = new byte[0xA]; // Read in the offset of the PSAR file //stream.Seek(HEADER_PSAR_OFFSET, SeekOrigin.Begin); //var psar_offset = stream.ReadInteger(); stream.Seek(psar_offset + PSAR_TOC_OFFSET, SeekOrigin.Begin); stream.Read(buffer, 0, 0xA); if (buffer[2] != 0xA0) { throw new Exception("Invalid TOC!"); } int startTrack = TOCHelper.FromBinaryDecimal(buffer[7]); stream.Read(buffer, 0, 0xA); if (buffer[2] != 0xA1) { throw new Exception("Invalid TOC!"); } int endTrack = TOCHelper.FromBinaryDecimal(buffer[7]); stream.Read(buffer, 0, 0xA); if (buffer[2] != 0xA2) { throw new Exception("Invalid TOC!"); } int mm = TOCHelper.FromBinaryDecimal(buffer[7]); int ss = TOCHelper.FromBinaryDecimal(buffer[8]); int ff = TOCHelper.FromBinaryDecimal(buffer[9]); //var frames = mm * 60 * 75 + ss * 75 + ff; //var size = 2352 * frames; for (var c = startTrack; c <= endTrack; c++) { stream.Read(buffer, 0, 0xA); var trackNo = TOCHelper.FromBinaryDecimal(buffer[2]); if (trackNo != c) { throw new Exception("Invalid TOC!"); } var entry = new TOCEntry { TrackType = (TrackTypeEnum)buffer[0], TrackNo = trackNo, Minutes = TOCHelper.FromBinaryDecimal(buffer[3]), Seconds = TOCHelper.FromBinaryDecimal(buffer[4]), Frames = TOCHelper.FromBinaryDecimal(buffer[5]) }; entries.Add(entry); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(entries); }
static MergedBin MergeBins(string file, CueFile cueFilex, string tempPath) { var mergedBin = new MergedBin(); mergedBin.CueFile = new CueFile(); var cueFilePath = Path.GetDirectoryName(file); long currentFrame = 0; var mergedFilename = Path.GetFileNameWithoutExtension(file) + " - MERGED.bin"; mergedBin.Path = Path.Combine(tempPath, mergedFilename); var mcueFile = new CueFileEntry() { FileName = mergedFilename, FileType = "BINARY", Tracks = new List <CueTrack>() }; mergedBin.CueFile.FileEntries.Add(mcueFile); using (var joinedFile = new FileStream(mergedBin.Path, FileMode.Create)) { foreach (var cueFileEntry in cueFilex.FileEntries) { var binPath = cueFileEntry.FileName; if (Path.GetDirectoryName(binPath) == "" || Path.GetDirectoryName(binPath).StartsWith("..") || Path.GetDirectoryName(binPath).StartsWith(".")) { binPath = Path.Combine(cueFilePath, cueFileEntry.FileName); } using (var srcStream = new FileStream(binPath, FileMode.Open)) { srcStream.CopyTo(joinedFile); foreach (var item in cueFileEntry.Tracks) { var indexes = new List <CueIndex>(); foreach (var idx in item.Indexes) { var newIndex = new CueIndex { Number = idx.Number, Position = idx.Position + TOCHelper.PositionFromFrames(currentFrame) }; indexes.Add(newIndex); } var newTrack = new CueTrack() { DataType = item.DataType, Number = item.Number, Indexes = indexes }; mcueFile.Tracks.Add(newTrack); } var frames = srcStream.Length / 2352; currentFrame += frames; } } } return(mergedBin); }