use crate::ext::io::*; use crate::types::*; use anyhow::Result; use std::io::{Read, Seek, Write}; pub trait ReadSeek: Read + Seek + std::fmt::Debug {} pub trait WriteSeek: Write + Seek {} pub trait AnyDebug: std::fmt::Debug + std::any::Any {} impl ReadSeek for T {} impl WriteSeek for T {} impl AnyDebug for T {} pub trait ScriptBuilder: std::fmt::Debug { fn default_encoding(&self) -> Encoding; fn default_archive_encoding(&self) -> Option { None } fn default_patched_encoding(&self) -> Encoding { self.default_encoding() } fn build_script( &self, buf: Vec, filename: &str, encoding: Encoding, archive_encoding: Encoding, config: &ExtraConfig, archive: Option<&Box>, ) -> Result>; fn build_script_from_file( &self, filename: &str, encoding: Encoding, archive_encoding: Encoding, config: &ExtraConfig, archive: Option<&Box>, ) -> Result> { let data = crate::utils::files::read_file(filename)?; self.build_script(data, filename, encoding, archive_encoding, config, archive) } fn build_script_from_reader( &self, mut reader: Box, filename: &str, encoding: Encoding, archive_encoding: Encoding, config: &ExtraConfig, archive: Option<&Box>, ) -> Result> { let mut data = Vec::new(); reader .read_to_end(&mut data) .map_err(|e| anyhow::anyhow!("Failed to read from reader: {}", e))?; self.build_script(data, filename, encoding, archive_encoding, config, archive) } fn extensions(&self) -> &'static [&'static str]; fn is_this_format(&self, _filename: &str, _buf: &[u8], _buf_len: usize) -> Option { None } fn script_type(&self) -> &'static ScriptType; fn is_archive(&self) -> bool { false } fn create_archive( &self, _filename: &str, _files: &[&str], _encoding: Encoding, _config: &ExtraConfig, ) -> Result> { Err(anyhow::anyhow!( "This script type does not support creating an archive." )) } fn can_create_file(&self) -> bool { false } fn create_file<'a>( &'a self, _filename: &'a str, _writer: Box, _encoding: Encoding, _file_encoding: Encoding, _config: &ExtraConfig, ) -> Result<()> { Err(anyhow::anyhow!( "This script type does not support creating directly." )) } fn create_file_filename( &self, filename: &str, output_filename: &str, encoding: Encoding, file_encoding: Encoding, config: &ExtraConfig, ) -> Result<()> { let f = std::fs::File::create(output_filename)?; let f = std::io::BufWriter::new(f); self.create_file(filename, Box::new(f), encoding, file_encoding, config) } #[cfg(feature = "image")] fn is_image(&self) -> bool { false } #[cfg(feature = "image")] fn can_create_image_file(&self) -> bool { false } #[cfg(feature = "image")] fn create_image_file<'a>( &'a self, _data: ImageData, _writer: Box, _options: &ExtraConfig, ) -> Result<()> { Err(anyhow::anyhow!( "This script type does not support creating an image file." )) } #[cfg(feature = "image")] fn create_image_file_filename( &self, data: ImageData, filename: &str, options: &ExtraConfig, ) -> Result<()> { let f = std::fs::File::create(filename)?; let f = std::io::BufWriter::new(f); self.create_image_file(data, Box::new(f), options) } } pub trait ArchiveContent: Read { fn name(&self) -> &str; fn is_script(&self) -> bool { self.script_type().is_some() } fn script_type(&self) -> Option<&ScriptType> { None } fn data(&mut self) -> Result> { let mut data = Vec::new(); self.read_to_end(&mut data)?; Ok(data) } fn to_data<'a>(&'a mut self) -> Result> { Ok(Box::new(MemReader::new(self.data()?))) } } pub trait Script: std::fmt::Debug + std::any::Any { fn default_output_script_type(&self) -> OutputScriptType; fn is_output_supported(&self, output: OutputScriptType) -> bool { !matches!(output, OutputScriptType::Custom) } fn custom_output_extension<'a>(&'a self) -> &'a str { "" } fn default_format_type(&self) -> FormatOptions; fn extract_messages(&self) -> Result> { if !self.is_archive() { return Err(anyhow::anyhow!( "This script type does not support extracting messages." )); } Ok(vec![]) } fn import_messages<'a>( &'a self, _messages: Vec, _file: Box, _encoding: Encoding, _replacement: Option<&'a ReplacementTable>, ) -> Result<()> { if !self.is_archive() { return Err(anyhow::anyhow!( "This script type does not support importing messages." )); } Ok(()) } fn import_messages_filename( &self, _messages: Vec, _filename: &str, _encoding: Encoding, _replacement: Option<&ReplacementTable>, ) -> Result<()> { let f = std::fs::File::create(_filename)?; let f = std::io::BufWriter::new(f); self.import_messages(_messages, Box::new(f), _encoding, _replacement) } fn custom_export(&self, _filename: &std::path::Path, _encoding: Encoding) -> Result<()> { Err(anyhow::anyhow!( "This script type does not support custom export." )) } fn custom_import<'a>( &'a self, _custom_filename: &'a str, _file: Box, _encoding: Encoding, _output_encoding: Encoding, ) -> Result<()> { Err(anyhow::anyhow!( "This script type does not support custom import." )) } fn custom_import_filename( &self, custom_filename: &str, filename: &str, encoding: Encoding, output_encoding: Encoding, ) -> Result<()> { let f = std::fs::File::create(filename)?; let f = std::io::BufWriter::new(f); self.custom_import(custom_filename, Box::new(f), encoding, output_encoding) } fn is_archive(&self) -> bool { false } fn iter_archive_filename<'a>( &'a self, ) -> Result> + 'a>> { Err(anyhow::anyhow!( "This script type does not support iterating over archive filenames." )) } fn iter_archive_offset<'a>(&'a self) -> Result> + 'a>> { Err(anyhow::anyhow!( "This script type does not support iterating over archive offsets." )) } fn open_file<'a>(&'a self, _index: usize) -> Result> { Err(anyhow::anyhow!( "This script type does not support opening files." )) } fn open_file_by_name<'a>( &'a self, name: &str, ignore_case: bool, ) -> Result> { for (i, fname) in self.iter_archive_filename()?.enumerate() { if let Ok(fname) = fname { if fname == name || (ignore_case && fname.eq_ignore_ascii_case(name)) { return self.open_file(i); } } } Err(anyhow::anyhow!( "File with name '{}' not found in archive.", name )) } fn open_file_by_offset<'a>(&'a self, offset: u64) -> Result> { for (i, off) in self.iter_archive_offset()?.enumerate() { if let Ok(off) = off { if off == offset { return self.open_file(i); } } } Err(anyhow::anyhow!( "File with offset '{}' not found in archive.", offset )) } /// Returns output extension for archive output folder. fn archive_output_ext<'a>(&'a self) -> Option<&'a str> { None } #[cfg(feature = "image")] fn is_image(&self) -> bool { false } #[cfg(feature = "image")] fn export_image(&self) -> Result { Err(anyhow::anyhow!( "This script type does not support to export image." )) } #[cfg(feature = "image")] fn import_image<'a>(&'a self, _data: ImageData, _file: Box) -> Result<()> { Err(anyhow::anyhow!( "This script type does not support to import image." )) } #[cfg(feature = "image")] fn import_image_filename(&self, data: ImageData, filename: &str) -> Result<()> { let f = std::fs::File::create(filename)?; let f = std::io::BufWriter::new(f); self.import_image(data, Box::new(f)) } #[cfg(feature = "image")] fn is_multi_image(&self) -> bool { false } #[cfg(feature = "image")] fn export_multi_image<'a>( &'a self, ) -> Result> + 'a>> { Err(anyhow::anyhow!( "This script type does not support to export multi image." )) } #[cfg(feature = "image")] fn import_multi_image<'a>( &'a self, _data: Vec, _file: Box, ) -> Result<()> { Err(anyhow::anyhow!( "This script type does not support to import multi image." )) } #[cfg(feature = "image")] fn import_multi_image_filename( &self, data: Vec, filename: &str, ) -> Result<()> { let f = std::fs::File::create(filename)?; let f = std::io::BufWriter::new(f); self.import_multi_image(data, Box::new(f)) } fn extra_info<'a>(&'a self) -> Option> { None } } pub trait Archive { fn new_file<'a>(&'a mut self, name: &str) -> Result>; fn write_header(&mut self) -> Result<()>; }