|
| 1 | +use byteorder::{ReadBytesExt as _, LE}; |
| 2 | +use io_ext::ReaderExt as _; |
| 3 | +use std::{ |
| 4 | + collections::{hash_map::Entry, HashMap}, |
| 5 | + io::{BufReader, Read}, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::{error::ModpkgError, Modpkg, ModpkgAuthor, ModpkgChunk, ModpkgLicense}; |
| 9 | + |
| 10 | +impl Modpkg { |
| 11 | + pub const MAGIC: u64 = u64::from_le_bytes(*b"_modpkg_"); |
| 12 | + |
| 13 | + pub fn read(reader: &mut BufReader<impl Read>) -> Result<Self, ModpkgError> { |
| 14 | + let magic = reader.read_u64::<LE>()?; |
| 15 | + if magic != Self::MAGIC { |
| 16 | + return Err(ModpkgError::InvalidMagic(magic)); |
| 17 | + } |
| 18 | + |
| 19 | + let version = reader.read_u32::<LE>()?; |
| 20 | + if version != 1 { |
| 21 | + return Err(ModpkgError::InvalidVersion(version)); |
| 22 | + } |
| 23 | + |
| 24 | + let name = reader.read_len_prefixed_string::<LE>()?; |
| 25 | + let display_name = reader.read_len_prefixed_string::<LE>()?; |
| 26 | + let description = reader.read_len_prefixed_string::<LE>()?; |
| 27 | + let version = reader.read_len_prefixed_string::<LE>()?; |
| 28 | + let distributor = reader.read_len_prefixed_string::<LE>()?; |
| 29 | + |
| 30 | + let authors = Self::read_authors(reader)?; |
| 31 | + let license = ModpkgLicense::read(reader)?; |
| 32 | + let chunks = Self::read_chunks(reader)?; |
| 33 | + Ok(Self { |
| 34 | + name, |
| 35 | + display_name, |
| 36 | + description: match description.len() { |
| 37 | + 0 => None, |
| 38 | + _ => Some(description), |
| 39 | + }, |
| 40 | + version, |
| 41 | + distributor: match distributor.len() { |
| 42 | + 0 => None, |
| 43 | + _ => Some(distributor), |
| 44 | + }, |
| 45 | + authors, |
| 46 | + license, |
| 47 | + chunks, |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + fn read_authors(reader: &mut BufReader<impl Read>) -> Result<Vec<ModpkgAuthor>, ModpkgError> { |
| 52 | + let count = reader.read_u32::<LE>()?; |
| 53 | + let mut authors = Vec::with_capacity(count as usize); |
| 54 | + for _ in 0..count { |
| 55 | + let name = reader.read_len_prefixed_string::<LE>()?; |
| 56 | + let role = reader.read_len_prefixed_string::<LE>()?; |
| 57 | + |
| 58 | + authors.push(ModpkgAuthor { |
| 59 | + name, |
| 60 | + role: match role.len() { |
| 61 | + 0 => None, |
| 62 | + _ => Some(role), |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + Ok(authors) |
| 68 | + } |
| 69 | + |
| 70 | + fn read_chunks( |
| 71 | + reader: &mut BufReader<impl Read>, |
| 72 | + ) -> Result<HashMap<u64, ModpkgChunk>, ModpkgError> { |
| 73 | + let chunk_count = reader.read_u32::<LE>()?; |
| 74 | + let mut chunks = HashMap::with_capacity(chunk_count as usize); |
| 75 | + for _ in 0..chunk_count { |
| 76 | + let chunk = ModpkgChunk::read(reader)?; |
| 77 | + match chunks.entry(chunk.path_hash()) { |
| 78 | + Entry::Occupied(_) => { |
| 79 | + return Err(ModpkgError::DuplicateChunk(chunk.path_hash())); |
| 80 | + } |
| 81 | + Entry::Vacant(entry) => { |
| 82 | + entry.insert(chunk); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Ok(chunks) |
| 88 | + } |
| 89 | +} |
0 commit comments