pub fn melt(&self, data: &[u8]) -> Result<(Vec<u8>, HashSet<u16>), ImperatorError> {
        let mut result = Vec::with_capacity(data.len());
        let mut unknown_tokens = HashSet::new();

        // if there is a save id line in the data, we should preserve it
        let has_save_id = data.get(0..3).map_or(false, |x| x == b"SAV");
        let data = if has_save_id {
            let split_ind = data.iter().position(|&x| x == b'\n').unwrap_or(0);
            let at = std::cmp::max(split_ind, 0);
            let (header, rest) = data.split_at(at + 1);
            result.extend_from_slice(header);
            rest
        } else {
            data
        };

        let mut reader = Cursor::new(data);
        match detect_encoding(&mut reader)? {
            BodyEncoding::Plain => self.convert(data, &mut result, &mut unknown_tokens)?,
            BodyEncoding::Zip(mut zip) => {
                let size = zip
                    .by_name("gamestate")
                    .map_err(|e| ImperatorErrorKind::ZipMissingEntry("gamestate", e))
                    .map(|x| x.size())?;
                result.reserve(size as usize);

                let mut zip_file = zip
                    .by_name("gamestate")
                    .map_err(|e| ImperatorErrorKind::ZipMissingEntry("gamestate", e))?;

                match self.extraction {
                    Extraction::InMemory => {
                        let mut inflated_data: Vec<u8> = Vec::with_capacity(size as usize);
                        zip_file
                            .read_to_end(&mut inflated_data)
                            .map_err(|e| ImperatorErrorKind::ZipExtraction("gamestate", e))?;
                        self.convert(&inflated_data, &mut result, &mut unknown_tokens)?
                    }

                    #[cfg(feature = "mmap")]
                    Extraction::MmapTemporaries => {
                        let mut mmap = memmap::MmapMut::map_anon(zip_file.size() as usize)?;
                        std::io::copy(&mut zip_file, &mut mmap.as_mut())
                            .map_err(|e| ImperatorErrorKind::ZipExtraction("gamestate", e))?;
                        self.convert(&mmap[..], &mut result, &unknown_tokens)?
                    }
                }
            }
        }

        Ok((result, unknown_tokens))
    }
/// Given one of the accepted inputs, this will return the save id line (if present in the input)
/// with the gamestate data decoded from binary to plain text.
Given one of the accepted inputs, this will return the save id line (if present in the input)
with the gamestate data decoded from binary to plain text.
Given one of the accepted inputs this will return the save id line ( if present in the input ) with the gamestate data decoded from binary to plain text .
pub fn melt(&self, data: &[u8]) -> Result<(Vec<u8>, HashSet<u16>), ImperatorError> {
        let mut result = Vec::with_capacity(data.len());
        let mut unknown_tokens = HashSet::new();
        // if there is a save id line in the data, we should preserve it
        let has_save_id = data.get(0..3).map_or(false, |x| x == b"SAV");
        let data = if has_save_id {
            let split_ind = data.iter().position(|&x| x == b'\n').unwrap_or(0);
            let at = std::cmp::max(split_ind, 0);
            let (header, rest) = data.split_at(at + 1);
            result.extend_from_slice(header);
            rest
        } else {
            data
        };
        let mut reader = Cursor::new(data);
        match detect_encoding(&mut reader)? {
            BodyEncoding::Plain => self.convert(data, &mut result, &mut unknown_tokens)?,
            BodyEncoding::Zip(mut zip) => {
                let size = zip
                    .by_name("gamestate")
                    .map_err(|e| ImperatorErrorKind::ZipMissingEntry("gamestate", e))
                    .map(|x| x.size())?;
                result.reserve(size as usize);
                let mut zip_file = zip
                    .by_name("gamestate")
                    .map_err(|e| ImperatorErrorKind::ZipMissingEntry("gamestate", e))?;
                match self.extraction {
                    Extraction::InMemory => {
                        let mut inflated_data: Vec<u8> = Vec::with_capacity(size as usize);
                        zip_file
                            .read_to_end(&mut inflated_data)
                            .map_err(|e| ImperatorErrorKind::ZipExtraction("gamestate", e))?;
                        self.convert(&inflated_data, &mut result, &mut unknown_tokens)?
                    }
                    #[cfg(feature = "mmap")]
                    Extraction::MmapTemporaries => {
                        let mut mmap = memmap::MmapMut::map_anon(zip_file.size() as usize)?;
                        std::io::copy(&mut zip_file, &mut mmap.as_mut())
                            .map_err(|e| ImperatorErrorKind::ZipExtraction("gamestate", e))?;
                        self.convert(&mmap[..], &mut result, &unknown_tokens)?
                    }
                }
            }
        }
        Ok((result, unknown_tokens))
    }
pub fn melt ( & self , data : & [ u8 ] ) -> Result < ( Vec < u8 > , HashSet < u16 > ) , ImperatorError > { let mut result = Vec :: with_capacity ( data . len ( ) ) ; let mut unknown_tokens = HashSet :: new ( ) ; // if there is a save id line in the data, we should preserve it let has_save_id = data . get ( 0 .. 3 ) . map_or ( false , | x | x == b" " ) ; let data = if has_save_id { let split_ind = data . iter ( ) . position ( | & x | x == b'\n' ) . unwrap_or ( 0 ) ; let at = std :: cmp :: max ( split_ind , 0 ) ; let ( header , rest ) = data . split_at ( at + 1 ) ; result . extend_from_slice ( header ) ; rest } else { data } ; let mut reader = Cursor :: new ( data ) ; match detect_encoding ( & mut reader ) ? { BodyEncoding :: Plain => self . convert ( data , & mut result , & mut unknown_tokens ) ? , BodyEncoding :: Zip ( mut zip ) => { let size = zip . by_name ( " " ) . map_err ( | e | ImperatorErrorKind :: ZipMissingEntry ( " " , e ) ) . map ( | x | x . size ( ) ) ? ; result . reserve ( size as usize ) ; let mut zip_file = zip . by_name ( " " ) . map_err ( | e | ImperatorErrorKind :: ZipMissingEntry ( " " , e ) ) ? ; match self . extraction { Extraction :: InMemory => { let mut inflated_data : Vec < u8 > = Vec :: with_capacity ( size as usize ) ; zip_file . read_to_end ( & mut inflated_data ) . map_err ( | e | ImperatorErrorKind :: ZipExtraction ( " " , e ) ) ? ; self . convert ( & inflated_data , & mut result , & mut unknown_tokens ) ? } # [ cfg ( feature " " ) ] Extraction :: MmapTemporaries => { let mut mmap = memmap :: MmapMut :: map_anon ( zip_file . size ( ) as usize ) ? ; std :: io :: copy ( & mut zip_file , & mut mmap . as_mut ( ) ) . map_err ( | e | ImperatorErrorKind :: ZipExtraction ( " " , e ) ) ? ; self . convert ( & mmap [ .. ] , & mut result , & unknown_tokens ) ? } } } } Ok ( ( result , unknown_tokens ) ) }
Given one of the accepted inputs, this will return the save id line (if present in the input)
with the gamestate data decoded from binary to plain text.
Given one of the accepted inputs this will return the save id line ( if present in the input ) with the gamestate data decoded from binary to plain text .
&self
data
&[u8]
&self
data
&[u8]