Add support for exploding graphics files

This commit is contained in:
stephenmk 2023-05-02 08:45:13 -05:00
parent 09915cabc5
commit 7d503e8aba
No known key found for this signature in database
GPG key ID: B6DA730DB06235F1
4 changed files with 39 additions and 23 deletions

View file

@ -33,6 +33,7 @@ fn explode() -> Result<(), Error> {
let pages_dir = out_dir(&dict) + "pages/"; let pages_dir = out_dir(&dict) + "pages/";
let audio_dir = out_dir(&dict) + "audio/"; let audio_dir = out_dir(&dict) + "audio/";
let graphics_dir = out_dir(&dict) + "graphics/";
create_dir_all(&pages_dir)?; create_dir_all(&pages_dir)?;
let mut path = String::from(&pages_dir); let mut path = String::from(&pages_dir);
@ -56,6 +57,18 @@ fn explode() -> Result<(), Error> {
} }
} }
if let Some(graphics) = &mut dict.graphics {
create_dir_all(&graphics_dir)?;
let mut path = String::from(&graphics_dir);
for idx in graphics.idx_iter()? {
let (id, graphics) = graphics.get_by_idx(idx)?;
write!(&mut path, "{id}")?;
let mut file = File::create(&path)?;
path.truncate(graphics_dir.len());
file.write_all(graphics)?;
}
}
write_index(&dict, &dict.keys.index_len, "index_len.tsv")?; write_index(&dict, &dict.keys.index_len, "index_len.tsv")?;
write_index(&dict, &dict.keys.index_prefix, "index_prefix.tsv")?; write_index(&dict, &dict.keys.index_prefix, "index_prefix.tsv")?;
write_index(&dict, &dict.keys.index_suffix, "index_suffix.tsv")?; write_index(&dict, &dict.keys.index_suffix, "index_suffix.tsv")?;

View file

@ -5,12 +5,13 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use crate::{audio::Audio, key::Keys, pages::Pages, Error}; use crate::{media::Media, key::Keys, pages::Pages, Error};
pub struct MonokakidoDict { pub struct MonokakidoDict {
paths: Paths, paths: Paths,
pub pages: Pages, pub pages: Pages,
pub audio: Option<Audio>, pub audio: Option<Media>,
pub graphics: Option<Media>,
pub keys: Keys, pub keys: Keys,
} }
@ -135,13 +136,15 @@ impl MonokakidoDict {
contents_dir: contents.dir, contents_dir: contents.dir,
}; };
let pages = Pages::new(&paths)?; let pages = Pages::new(&paths)?;
let audio = Audio::new(&paths)?; let audio = Media::new(&paths)?;
let graphics = Media::new(&paths)?;
let keys = Keys::new(&paths)?; let keys = Keys::new(&paths)?;
Ok(MonokakidoDict { Ok(MonokakidoDict {
paths, paths,
pages, pages,
audio, audio,
graphics,
keys, keys,
}) })
} }

View file

@ -1,5 +1,5 @@
mod abi_utils; mod abi_utils;
mod audio; mod media;
mod dict; mod dict;
mod error; mod error;
mod key; mod key;
@ -7,7 +7,7 @@ mod pages;
mod resource; mod resource;
mod headline; mod headline;
pub use audio::Audio; pub use media::Media;
pub use dict::MonokakidoDict; pub use dict::MonokakidoDict;
pub use error::Error; pub use error::Error;
pub use key::{KeyIndex, Keys, PageItemId}; pub use key::{KeyIndex, Keys, PageItemId};

View file

@ -8,22 +8,22 @@ use crate::{
const RSC_NAME: &str = "audio"; const RSC_NAME: &str = "audio";
pub struct Audio { pub struct Media {
path: PathBuf, path: PathBuf,
res: Option<AudioResource>, res: Option<MediaResource>,
} }
enum AudioResource { enum MediaResource {
Rsc(Rsc), Rsc(Rsc),
Nrsc(Nrsc), Nrsc(Nrsc),
} }
impl Audio { impl Media {
pub fn new(paths: &Paths) -> Result<Option<Self>, Error> { pub fn new(paths: &Paths) -> Result<Option<Self>, Error> {
let mut path = paths.contents_path(); let mut path = paths.contents_path();
path.push(RSC_NAME); path.push(RSC_NAME);
Ok(if path.exists() { Ok(if path.exists() {
Some(Audio { path, res: None }) Some(Media { path, res: None })
} else { } else {
None None
}) })
@ -35,9 +35,9 @@ impl Audio {
let nrsc_index_exists = self.path.exists(); let nrsc_index_exists = self.path.exists();
self.path.pop(); self.path.pop();
self.res = Some(if nrsc_index_exists { self.res = Some(if nrsc_index_exists {
AudioResource::Nrsc(Nrsc::new(&self.path)?) MediaResource::Nrsc(Nrsc::new(&self.path)?)
} else { } else {
AudioResource::Rsc(Rsc::new(&self.path, RSC_NAME)?) MediaResource::Rsc(Rsc::new(&self.path, RSC_NAME)?)
}); });
} }
Ok(()) Ok(())
@ -47,22 +47,22 @@ impl Audio {
self.init()?; self.init()?;
let Some(res) = self.res.as_mut() else { unreachable!() }; let Some(res) = self.res.as_mut() else { unreachable!() };
match res { match res {
AudioResource::Rsc(rsc) => rsc.get(id.parse::<u32>().map_err(|_| Error::InvalidIndex)?), MediaResource::Rsc(rsc) => rsc.get(id.parse::<u32>().map_err(|_| Error::InvalidIndex)?),
AudioResource::Nrsc(nrsc) => nrsc.get(id), MediaResource::Nrsc(nrsc) => nrsc.get(id),
} }
} }
pub fn get_by_idx(&mut self, idx: usize) -> Result<(AudioId, &[u8]), Error> { pub fn get_by_idx(&mut self, idx: usize) -> Result<(MediaId, &[u8]), Error> {
self.init()?; self.init()?;
let Some(res) = self.res.as_mut() else { unreachable!() }; let Some(res) = self.res.as_mut() else { unreachable!() };
Ok(match res { Ok(match res {
AudioResource::Rsc(rsc) => { MediaResource::Rsc(rsc) => {
let (id, page) = rsc.get_by_idx(idx)?; let (id, page) = rsc.get_by_idx(idx)?;
(AudioId::Num(id), page) (MediaId::Num(id), page)
} }
AudioResource::Nrsc(nrsc) => { MediaResource::Nrsc(nrsc) => {
let (id, page) = nrsc.get_by_idx(idx)?; let (id, page) = nrsc.get_by_idx(idx)?;
(AudioId::Str(id), page) (MediaId::Str(id), page)
} }
}) })
} }
@ -71,19 +71,19 @@ impl Audio {
self.init()?; self.init()?;
let Some(res) = self.res.as_ref() else { unreachable!() }; let Some(res) = self.res.as_ref() else { unreachable!() };
Ok(0..match res { Ok(0..match res {
AudioResource::Rsc(rsc) => rsc.len(), MediaResource::Rsc(rsc) => rsc.len(),
AudioResource::Nrsc(nrsc) => nrsc.len(), MediaResource::Nrsc(nrsc) => nrsc.len(),
}) })
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum AudioId<'a> { pub enum MediaId<'a> {
Str(&'a str), Str(&'a str),
Num(u32), Num(u32),
} }
impl Display for AudioId<'_> { impl Display for MediaId<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::Str(str) => f.write_str(str), Self::Str(str) => f.write_str(str),