1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Handling of WTE files.

use crate::ffi;
use core::ffi::CStr;
use core::mem::MaybeUninit;

/// A Rust-owned WTE file.
pub struct OwnedWte(ffi::wte_handle);

impl OwnedWte {
    /// Take ownership of an WTE handle.
    ///
    /// # Safety
    /// The handle must be valid.
    pub unsafe fn from_handle(handle: ffi::wte_handle) -> Self {
        Self(handle)
    }

    /// Loads a SIR0-wrapped WTE file from ROM.
    ///
    /// # Safety
    /// The path must point to a valid WTE file.
    pub unsafe fn load_from_rom<C: AsRef<CStr>>(path: C, malloc_flags: u32) -> Self {
        let mut handle = MaybeUninit::uninit();
        ffi::LoadWteFromRom(handle.as_mut_ptr(), path.as_ref().as_ptr(), malloc_flags);
        Self(handle.assume_init())
    }

    /// Loads a SIR0-wrapped WTE file from a file directory.
    ///
    /// # Safety
    /// The path must point to a valid WTE file.
    pub unsafe fn load_from_dir(pack_file_id: u16, file_index: u16, malloc_flags: u32) -> Self {
        let mut handle = MaybeUninit::uninit();
        ffi::LoadWteFromFileDirectory(handle.as_mut_ptr(), pack_file_id, file_index, malloc_flags);
        Self(handle.assume_init())
    }
}

/// Unloads the WTE file.
impl Drop for OwnedWte {
    fn drop(&mut self) {
        unsafe {
            ffi::UnloadWte(&mut self.0);
        }
    }
}