Files
Aaru/Aaru.Images/CDRWin/Write.cs

676 lines
21 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Write.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Writes CDRWin cuesheets (cue/bin).
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.DiscImages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Structs;
using Schemas;
2022-03-06 13:29:38 +00:00
public sealed partial class CdrWin
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
{
2022-03-06 13:29:38 +00:00
if(options != null)
{
2022-03-06 13:29:38 +00:00
if(options.TryGetValue("separate", out string tmpValue))
{
2022-03-06 13:29:38 +00:00
if(!bool.TryParse(tmpValue, out _separateTracksWriting))
{
2022-03-06 13:29:38 +00:00
ErrorMessage = "Invalid value for split option";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2020-07-22 13:20:25 +01:00
2022-03-06 13:29:38 +00:00
if(_separateTracksWriting)
{
ErrorMessage = "Separate tracks not yet implemented";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
}
2022-03-06 13:29:38 +00:00
}
else
_separateTracksWriting = false;
2022-03-06 13:29:38 +00:00
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupported media format {mediaType}";
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
_imageInfo = new ImageInfo
{
MediaType = mediaType,
SectorSize = sectorSize,
Sectors = sectors
};
2022-03-06 13:29:38 +00:00
// TODO: Separate tracks
try
{
2022-03-07 07:36:44 +00:00
_writingBaseName = Path.Combine(Path.GetDirectoryName(path) ?? "", Path.GetFileNameWithoutExtension(path));
2022-03-06 13:29:38 +00:00
_descriptorStream = new StreamWriter(path, false, Encoding.ASCII);
}
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
_discImage = new CdrWinDisc
{
2022-03-06 13:29:38 +00:00
MediaType = mediaType,
Sessions = new List<Session>(),
Tracks = new List<CdrWinTrack>()
};
2020-01-07 17:59:53 +00:00
2022-03-07 07:36:44 +00:00
var mediaTypeAsInt = (int)_discImage.MediaType;
2022-11-14 01:49:10 +00:00
_isCd = mediaTypeAsInt is >= 10 and <= 39 or 112 or 113 or >= 150 and <= 152 or 154 or 155 or >= 171 and <= 179
or >= 740 and <= 749;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(_isCd)
{
_trackFlags = new Dictionary<byte, byte>();
_trackIsrcs = new Dictionary<byte, string>();
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
IsWriting = true;
ErrorMessage = null;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteMediaTag(byte[] data, MediaTagType tag)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
2022-03-06 13:29:38 +00:00
switch(tag)
{
2022-03-06 13:29:38 +00:00
case MediaTagType.CD_MCN when _isCd:
_discImage.Mcn = Encoding.ASCII.GetString(data);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
case MediaTagType.CD_TEXT when _isCd:
var cdTextStream = new FileStream(_writingBaseName + "_cdtext.bin", FileMode.Create,
FileAccess.ReadWrite, FileShare.None);
2022-03-06 13:29:38 +00:00
cdTextStream.Write(data, 0, data.Length);
_discImage.CdTextFile = Path.GetFileName(cdTextStream.Name);
cdTextStream.Close();
2022-03-06 13:29:38 +00:00
return true;
default:
ErrorMessage = $"Unsupported media tag {tag}";
2020-01-07 17:59:53 +00:00
return false;
2022-03-06 13:29:38 +00:00
}
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteSector(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
Track track =
2022-03-07 07:36:44 +00:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
2022-03-06 13:29:38 +00:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.Sequence).Value;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(track.BytesPerSector != track.RawBytesPerSector)
{
ErrorMessage = "Invalid write mode for this sector";
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
if(data.Length != track.RawBytesPerSector)
{
2022-03-06 13:29:38 +00:00
ErrorMessage = "Incorrect data size";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
trackStream.
2022-03-07 07:36:44 +00:00
Seek((long)(track.FileOffset + (sectorAddress - track.StartSector) * (ulong)track.RawBytesPerSector),
2022-03-06 13:29:38 +00:00
SeekOrigin.Begin);
2022-03-06 13:29:38 +00:00
trackStream.Write(data, 0, data.Length);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
Track track =
2022-03-07 07:36:44 +00:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
2022-03-06 13:29:38 +00:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.Sequence).Value;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(track.BytesPerSector != track.RawBytesPerSector)
{
ErrorMessage = "Invalid write mode for this sector";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(sectorAddress + length > track.EndSector + 1)
{
ErrorMessage = "Can't cross tracks";
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
if(data.Length % track.RawBytesPerSector != 0)
{
2022-03-06 13:29:38 +00:00
ErrorMessage = "Incorrect data size";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
trackStream.
2022-03-07 07:36:44 +00:00
Seek((long)(track.FileOffset + (sectorAddress - track.StartSector) * (ulong)track.RawBytesPerSector),
2022-03-06 13:29:38 +00:00
SeekOrigin.Begin);
2022-03-06 13:29:38 +00:00
trackStream.Write(data, 0, data.Length);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
Track track =
2022-03-07 07:36:44 +00:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
2022-03-06 13:29:38 +00:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.Sequence).Value;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
if(data.Length != track.RawBytesPerSector)
{
2022-03-06 13:29:38 +00:00
ErrorMessage = "Incorrect data size";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
trackStream.
2022-03-07 07:36:44 +00:00
Seek((long)(track.FileOffset + (sectorAddress - track.StartSector) * (ulong)track.RawBytesPerSector),
2022-03-06 13:29:38 +00:00
SeekOrigin.Begin);
2022-03-06 13:29:38 +00:00
trackStream.Write(data, 0, data.Length);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
Track track =
2022-03-07 07:36:44 +00:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
2022-03-06 13:29:38 +00:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.Sequence).Value;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(sectorAddress + length > track.EndSector + 1)
{
ErrorMessage = "Can't cross tracks";
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
if(data.Length % track.RawBytesPerSector != 0)
{
2022-03-06 13:29:38 +00:00
ErrorMessage = "Incorrect data size";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
trackStream.
2022-03-07 07:36:44 +00:00
Seek((long)(track.FileOffset + (sectorAddress - track.StartSector) * (ulong)track.RawBytesPerSector),
2022-03-06 13:29:38 +00:00
SeekOrigin.Begin);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
trackStream.Write(data, 0, data.Length);
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool SetTracks(List<Track> tracks)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(tracks == null ||
tracks.Count == 0)
{
ErrorMessage = "Invalid tracks sent";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
if(_writingTracks != null &&
_writingStreams != null)
foreach(FileStream oldTrack in _writingStreams.Select(t => t.Value).Distinct())
oldTrack.Close();
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
_writingTracks = new List<Track>();
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
foreach(Track track in tracks.OrderBy(t => t.Sequence))
{
Track newTrack = track;
2022-03-06 13:29:38 +00:00
newTrack.File = _separateTracksWriting ? _writingBaseName + $"_track{track.Sequence:D2}.bin"
: _writingBaseName + ".bin";
newTrack.FileOffset = _separateTracksWriting ? 0 : track.StartSector * 2352;
_writingTracks.Add(newTrack);
}
2022-03-06 13:29:38 +00:00
_writingStreams = new Dictionary<uint, FileStream>();
if(_separateTracksWriting)
foreach(Track track in _writingTracks)
_writingStreams.Add(track.Sequence,
new FileStream(track.File, FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None));
else
{
2022-03-06 13:29:38 +00:00
var jointStream = new FileStream(_writingBaseName + ".bin", FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
foreach(Track track in _writingTracks)
_writingStreams.Add(track.Sequence, jointStream);
}
2022-03-06 13:29:38 +00:00
return true;
}
/// <inheritdoc />
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
return false;
}
if(_separateTracksWriting)
foreach(FileStream writingStream in _writingStreams.Values)
{
2022-03-06 13:29:38 +00:00
writingStream.Flush();
writingStream.Close();
}
2022-03-06 13:29:38 +00:00
else
{
_writingStreams.First().Value.Flush();
_writingStreams.First().Value.Close();
}
2022-03-07 07:36:44 +00:00
var currentSession = 0;
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrWhiteSpace(_discImage.Comment))
{
string[] commentLines = _discImage.Comment.Split(new[]
{
2022-03-06 13:29:38 +00:00
'\n'
}, StringSplitOptions.RemoveEmptyEntries);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
foreach(string line in commentLines)
_descriptorStream.WriteLine("REM {0}", line);
}
2022-03-06 13:29:38 +00:00
_descriptorStream.WriteLine("REM ORIGINAL MEDIA-TYPE: {0}", MediaTypeToCdrwinType(_imageInfo.MediaType));
2022-03-06 13:29:38 +00:00
_descriptorStream.WriteLine("REM METADATA AARU MEDIA-TYPE: {0}", _imageInfo.MediaType);
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(_imageInfo.Application))
{
_descriptorStream.WriteLine("REM Ripping Tool: {0}", _imageInfo.Application);
2020-01-09 19:03:46 +00:00
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(_imageInfo.ApplicationVersion))
_descriptorStream.WriteLine("REM Ripping Tool Version: {0}", _imageInfo.ApplicationVersion);
}
2020-01-09 19:03:46 +00:00
2022-03-06 13:29:38 +00:00
if(DumpHardware != null)
foreach(var dumpData in from dump in DumpHardware from extent in dump.Extents.OrderBy(e => e.Start)
select new
{
dump.Manufacturer,
dump.Model,
dump.Firmware,
dump.Serial,
Application = dump.Software.Name,
ApplicationVersion = dump.Software.Version,
dump.Software.OperatingSystem,
extent.Start,
extent.End
})
_descriptorStream.
WriteLine($"REM METADATA DUMP EXTENT: {dumpData.Application} | {dumpData.ApplicationVersion} | {dumpData.OperatingSystem} | {dumpData.Manufacturer} | {dumpData.Model} | {dumpData.Firmware} | {dumpData.Serial} | {dumpData.Start}:{dumpData.End}");
2020-01-09 19:03:46 +00:00
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(_discImage.CdTextFile))
_descriptorStream.WriteLine("CDTEXTFILE \"{0}\"", Path.GetFileName(_discImage.CdTextFile));
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(_discImage.Title))
_descriptorStream.WriteLine("TITLE {0}", _discImage.Title);
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(_discImage.Mcn))
_descriptorStream.WriteLine("CATALOG {0}", _discImage.Mcn);
2022-03-06 13:29:38 +00:00
if(!string.IsNullOrEmpty(_discImage.Barcode))
_descriptorStream.WriteLine("UPC_EAN {0}", _discImage.Barcode);
2022-03-06 13:29:38 +00:00
if(!_separateTracksWriting)
2022-03-07 07:36:44 +00:00
_descriptorStream.WriteLine("FILE \"{0}\" BINARY", Path.GetFileName(_writingStreams.First().Value.Name));
2020-01-09 19:03:46 +00:00
2022-03-06 13:29:38 +00:00
Track trackZero = null;
2022-03-06 13:29:38 +00:00
foreach(Track track in _writingTracks)
{
switch(track.Sequence)
{
2022-03-06 13:29:38 +00:00
// You should not be able to write CD-i Ready as this format, but just in case
case 0 when _imageInfo.MediaType != MediaType.CDIREADY:
trackZero = track;
2022-03-06 13:29:38 +00:00
continue;
case 1 when trackZero != null && !track.Indexes.ContainsKey(0):
track.StartSector = trackZero.StartSector;
track.Pregap = (ulong)track.Indexes[1] - track.StartSector;
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
if(track.Session > currentSession)
_descriptorStream.WriteLine("REM SESSION {0}", ++currentSession);
2022-03-06 13:29:38 +00:00
if(_separateTracksWriting)
_descriptorStream.WriteLine("FILE \"{0}\" BINARY", Path.GetFileName(track.File));
2022-03-06 13:29:38 +00:00
(byte minute, byte second, byte frame) msf = LbaToMsf(track.StartSector);
_descriptorStream.WriteLine(" TRACK {0:D2} {1}", track.Sequence, GetTrackMode(track));
2022-03-06 13:29:38 +00:00
if(_isCd)
{
if(_trackFlags.TryGetValue((byte)track.Sequence, out byte flagsByte))
if(flagsByte != 0 &&
flagsByte != (byte)CdFlags.DataTrack)
{
var flags = (CdFlags)flagsByte;
2022-03-06 13:29:38 +00:00
_descriptorStream.WriteLine(" FLAGS{0}{1}{2}",
flags.HasFlag(CdFlags.CopyPermitted) ? " DCP" : "",
flags.HasFlag(CdFlags.FourChannel) ? " 4CH" : "",
flags.HasFlag(CdFlags.PreEmphasis) ? " PRE" : "");
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(_trackIsrcs.TryGetValue((byte)track.Sequence, out string isrc) &&
!string.IsNullOrWhiteSpace(isrc))
_descriptorStream.WriteLine(" ISRC {0}", isrc);
}
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
if(track.Pregap > 0 && _isCd)
{
if(track.Sequence > _writingTracks.Where(t => t.Session == track.Session).Min(t => t.Sequence))
_descriptorStream.WriteLine(" INDEX {0:D2} {1:D2}:{2:D2}:{3:D2}", 0, msf.minute, msf.second,
2020-01-09 19:03:46 +00:00
msf.frame);
2022-03-06 13:29:38 +00:00
if(track.Sequence > 1)
msf = LbaToMsf(track.StartSector + track.Pregap);
_descriptorStream.WriteLine(" INDEX {0:D2} {1:D2}:{2:D2}:{3:D2}", 1, msf.minute, msf.second,
msf.frame);
}
else
_descriptorStream.WriteLine(" INDEX {0:D2} {1:D2}:{2:D2}:{3:D2}", 1, msf.minute, msf.second,
msf.frame);
if(_isCd)
foreach(KeyValuePair<ushort, int> index in track.Indexes.Where(i => i.Key > 1))
{
msf = LbaToMsf((ulong)index.Value);
_descriptorStream.WriteLine(" INDEX {0:D2} {1:D2}:{2:D2}:{3:D2}", index.Key, msf.minute,
msf.second, msf.frame);
2020-01-07 17:59:53 +00:00
}
2022-03-06 13:29:38 +00:00
ushort lastSession = _writingTracks.Max(t => t.Session);
2020-07-19 21:39:23 +01:00
2022-03-06 13:29:38 +00:00
if(currentSession >= lastSession)
continue;
2020-07-19 21:39:23 +01:00
2022-11-14 01:04:32 +00:00
Track lastTrackInSession = _writingTracks.Where(t => t.Session == currentSession).MaxBy(t => t.Sequence);
2022-03-06 13:29:38 +00:00
if(track.Sequence != lastTrackInSession.Sequence)
continue;
2022-03-06 13:29:38 +00:00
msf = LbaToMsf(track.EndSector + 1);
_descriptorStream.WriteLine("REM LEAD-OUT {0:D2}:{1:D2}:{2:D2}", msf.minute, msf.second, msf.frame);
}
2022-03-06 13:29:38 +00:00
_descriptorStream.Flush();
_descriptorStream.Close();
2022-03-06 13:29:38 +00:00
IsWriting = false;
ErrorMessage = "";
return true;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
{
ErrorMessage = "Unsupported feature";
return false;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
Track track =
2022-03-07 07:36:44 +00:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
2022-03-06 13:29:38 +00:00
if(track is null)
{
2022-03-06 13:29:38 +00:00
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-01-07 17:59:53 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
switch(tag)
{
2022-03-06 13:29:38 +00:00
case SectorTagType.CdTrackFlags when _isCd:
{
2022-03-06 13:29:38 +00:00
if(data.Length != 1)
{
ErrorMessage = "Incorrect data size for track flags";
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
_trackFlags[(byte)sectorAddress] = data[0];
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
case SectorTagType.CdTrackIsrc when _isCd:
{
2022-03-06 13:29:38 +00:00
if(data != null)
_trackIsrcs[(byte)sectorAddress] = Encoding.UTF8.GetString(data);
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
default:
ErrorMessage = $"Unsupported tag type {tag}";
return false;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag) =>
WriteSectorTag(data, sectorAddress, tag);
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware)
{
DumpHardware = dumpHardware;
2020-01-09 19:03:46 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool SetCicmMetadata(CICMMetadataType metadata) => false;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool SetMetadata(ImageInfo metadata)
{
_discImage.Barcode = metadata.MediaBarcode;
_discImage.Comment = metadata.Comments;
_discImage.Title = metadata.MediaTitle;
_imageInfo.Application = metadata.Application;
_imageInfo.ApplicationVersion = metadata.ApplicationVersion;
2020-01-07 17:59:53 +00:00
2022-03-06 13:29:38 +00:00
return true;
}
}