pub struct Ghost { /* private fields */ }Expand description
A fully parsed Mario Kart Wii RKG ghost file.
Holds the file header, decompressed or compressed input data, optional
external footers (CTGP or SP), and CRC-32 checksums. All setter
operations update the parsed fields; call update_raw_data
(or save_to_file, which calls it implicitly) to
flush all changes back into the raw byte buffer before writing.
Implementations§
Source§impl Ghost
impl Ghost
Sourcepub fn new_from_file<T: AsRef<Path>>(path: T) -> Result<Self, GhostError>
pub fn new_from_file<T: AsRef<Path>>(path: T) -> Result<Self, GhostError>
Parses a Ghost from an RKG file at the given path.
§Errors
Returns GhostError::IOError if the file cannot be opened or read,
and other GhostError variants if parsing fails.
Sourcepub fn new(bytes: &[u8]) -> Result<Self, GhostError>
pub fn new(bytes: &[u8]) -> Result<Self, GhostError>
Parses a Ghost from a byte slice.
Detects and parses an optional CTGP or SP footer if present. The base CRC-32 is read from just before the footer when one is found, or from the last 4 bytes of the file otherwise.
§Errors
Returns GhostError::DataLengthTooShort if bytes is shorter than
0x90 bytes OR input data is shorter than the expected input data size.
Returns other GhostError variants if any field fails
to parse.
Sourcepub fn update_raw_data(&mut self) -> Result<(), GhostError>
pub fn update_raw_data(&mut self) -> Result<(), GhostError>
Flushes all parsed field modifications back into the raw byte buffer.
This method recomputes the Mii CRC-16, rebuilds the raw buffer from the current header and input data, resizes the buffer if the input data length has changed, re-inserts any preserved external footer, and finally recomputes both the base CRC-32 and the file-level CRC-32. The CTGP footer SHA-1 field is also updated if a CTGP footer is present.
§Errors
Returns GhostError::MiiError if the Mii data is invalid, or
GhostError::CTGPFooterError if the SHA-1 field cannot be written.
Sourcepub fn save_to_file<T: AsRef<Path>>(
&mut self,
path: T,
) -> Result<(), GhostError>
pub fn save_to_file<T: AsRef<Path>>( &mut self, path: T, ) -> Result<(), GhostError>
Flushes all modifications and writes the ghost to a file at the given path.
§Errors
Returns any error from update_raw_data or
from file creation/writing.
Sourcepub fn compress_input_data(&mut self)
pub fn compress_input_data(&mut self)
Compresses the input data using Yaz1 encoding and sets the compression flag in the header.
Does nothing if the input data is already compressed.
Sourcepub fn decompress_input_data(&mut self)
pub fn decompress_input_data(&mut self)
Decompresses the input data and clears the compression flag in the header.
Does nothing if the input data is not compressed.
Sourcepub fn raw_data(&self) -> &[u8] ⓘ
pub fn raw_data(&self) -> &[u8] ⓘ
Returns the raw file bytes.
May not reflect recent modifications until update_raw_data is called.
Sourcepub fn raw_data_mut(&mut self) -> &mut [u8] ⓘ
pub fn raw_data_mut(&mut self) -> &mut [u8] ⓘ
Returns a mutable reference to the raw file bytes.
Sourcepub fn header_mut(&mut self) -> &mut Header
pub fn header_mut(&mut self) -> &mut Header
Returns a mutable reference to the parsed RKG file header.
Sourcepub fn input_data(&self) -> &InputData
pub fn input_data(&self) -> &InputData
Returns the ghost’s controller input data.
Sourcepub fn input_data_mut(&mut self) -> &mut InputData
pub fn input_data_mut(&mut self) -> &mut InputData
Returns a mutable reference to the ghost’s controller input data.
Returns the footer, if present.
Returns a mutable reference to the footer, if present.
Sourcepub fn base_crc32(&self) -> u32
pub fn base_crc32(&self) -> u32
Returns the CRC-32 of the header and input data, excluding any external footer.
Sourcepub fn verify_base_crc32(&self) -> bool
pub fn verify_base_crc32(&self) -> bool
Returns true if the stored base CRC-32 matches a freshly computed
checksum of the current header and input data bytes.
Sourcepub fn file_crc32(&self) -> u32
pub fn file_crc32(&self) -> u32
Returns the CRC-32 of the entire file excluding its final 4 bytes.
Sourcepub fn verify_file_crc32(&self) -> bool
pub fn verify_file_crc32(&self) -> bool
Returns true if the stored file CRC-32 matches a freshly computed
checksum of the entire file excluding its final 4 bytes.
Returns whether an existing external footer will be preserved when saving.
Sets whether an existing external footer should be preserved when saving.