use std::io::{self, Read, Write};
use std::path::Path;
use super::IOStream;
use crate::disc::{DiscTitle, Disc};
use crate::drive::DriveSession;
use crate::error::Error;
pub struct DiscOptions {
pub device: Option<String>,
pub keydb_path: Option<String>,
pub title_index: Option<usize>,
}
impl Default for DiscOptions {
fn default() -> Self {
Self { device: None, keydb_path: None, title_index: None }
}
}
pub struct DiscStream {
disc_title: DiscTitle,
disc: Disc,
session: DriveSession,
title_index: usize,
batch_buf: Vec<u8>,
batch_pos: usize,
started: bool,
eof: bool,
}
impl DiscStream {
pub fn open(opts: DiscOptions) -> Result<Self, Error> {
let device = match opts.device {
Some(ref d) => crate::drive::resolve_device(d)?.0,
None => crate::drive::find_drive()
.ok_or_else(|| Error::DeviceNotFound { path: String::new() })?,
};
let mut session = DriveSession::open(Path::new(&device))?;
session.wait_ready()?;
let _ = session.init();
let _ = session.probe_disc();
let scan_opts = match opts.keydb_path {
Some(ref kp) => crate::disc::ScanOptions::with_keydb(kp),
None => crate::disc::ScanOptions::default(),
};
let disc = Disc::scan(&mut session, &scan_opts)?;
let title_index = opts.title_index.unwrap_or(0);
if title_index >= disc.titles.len() {
return Err(Error::DiscTitleRange { index: title_index, count: disc.titles.len() });
}
let disc_title = disc.titles[title_index].clone();
Ok(Self {
disc_title, disc, session, title_index,
batch_buf: Vec::new(), batch_pos: 0,
started: false, eof: false,
})
}
pub fn disc(&self) -> &Disc { &self.disc }
}
impl IOStream for DiscStream {
fn info(&self) -> &DiscTitle { &self.disc_title }
fn finish(&mut self) -> io::Result<()> { Ok(()) }
}
impl Read for DiscStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.batch_pos < self.batch_buf.len() {
let n = (self.batch_buf.len() - self.batch_pos).min(buf.len());
buf[..n].copy_from_slice(&self.batch_buf[self.batch_pos..self.batch_pos + n]);
self.batch_pos += n;
return Ok(n);
}
if self.eof { return Ok(0); }
if !self.started {
self.started = true;
}
let mut reader = self.disc.open_title(&mut self.session, self.title_index)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
match reader.read_batch() {
Ok(Some(batch)) => {
let n = batch.len().min(buf.len());
buf[..n].copy_from_slice(&batch[..n]);
if batch.len() > n {
self.batch_buf = batch.to_vec();
self.batch_pos = n;
} else {
self.batch_buf.clear();
self.batch_pos = 0;
}
Ok(n)
}
Ok(None) => { self.eof = true; Ok(0) }
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
}
}
}
impl Write for DiscStream {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Unsupported, "disc is read-only"))
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}