From 60f3e1fe5a9c7f9d8baf1ad1a933b09361e57655 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 14 Feb 2023 18:38:26 +0100 Subject: [PATCH] Refreshed PS module due to ctru::init --- ctru-rs/Cargo.toml | 4 ++-- ctru-rs/src/applets/swkbd.rs | 6 +---- ctru-rs/src/services/ps.rs | 46 ++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index 4774d0e..84e837b 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -15,8 +15,8 @@ name = "ctru" cfg-if = "1.0" ctru-sys = { path = "../ctru-sys", version = "0.4" } const-zero = "0.1.0" -linker-fix-3ds = { path = "../../rust-linker-fix-3ds" } -pthread-3ds = { path = "../../pthread-3ds" } +linker-fix-3ds = { git = "https://github.com/rust3ds/rust-linker-fix-3ds.git" } +pthread-3ds = {git = "https://github.com/rust3ds/pthread-3ds.git" } libc = "0.2.121" bitflags = "1.0.0" widestring = "0.2.2" diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 9bc461d..976bdec 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -126,11 +126,7 @@ impl Swkbd { /// the output will be truncated but should still be well-formed UTF-8 pub fn get_bytes(&mut self, buf: &mut [u8]) -> Result { unsafe { - match swkbdInputText( - self.state.as_mut(), - buf.as_mut_ptr(), - buf.len(), - ) { + match swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), buf.len()) { ctru_sys::SWKBD_BUTTON_NONE => Err(self.parse_swkbd_error()), ctru_sys::SWKBD_BUTTON_LEFT => Ok(Button::Left), ctru_sys::SWKBD_BUTTON_MIDDLE => Ok(Button::Middle), diff --git a/ctru-rs/src/services/ps.rs b/ctru-rs/src/services/ps.rs index 10ae07c..cd5cfe4 100644 --- a/ctru-rs/src/services/ps.rs +++ b/ctru-rs/src/services/ps.rs @@ -5,6 +5,7 @@ //! See also use crate::error::ResultCode; +use crate::Result; #[repr(u32)] pub enum AESAlgorithm { @@ -30,23 +31,44 @@ pub enum AESKeyType { Keyslot39Nfc, } -pub fn local_friend_code_seed() -> crate::Result { - let mut seed: u64 = 0; +pub struct Ps(()); - ResultCode(unsafe { ctru_sys::PS_GetLocalFriendCodeSeed(&mut seed) })?; - Ok(seed) -} +impl Ps { + pub fn new() -> Result { + unsafe { + ResultCode(ctru_sys::psInit())?; + Ok(Ps(())) + } + } + + pub fn local_friend_code_seed(&self) -> crate::Result { + let mut seed: u64 = 0; + + ResultCode(unsafe { ctru_sys::PS_GetLocalFriendCodeSeed(&mut seed) })?; + Ok(seed) + } -pub fn device_id() -> crate::Result { - let mut id: u32 = 0; + pub fn device_id(&self) -> crate::Result { + let mut id: u32 = 0; - ResultCode(unsafe { ctru_sys::PS_GetDeviceId(&mut id) })?; - Ok(id) + ResultCode(unsafe { ctru_sys::PS_GetDeviceId(&mut id) })?; + Ok(id) + } + + pub fn generate_random_bytes(&self, out: &mut [u8]) -> crate::Result<()> { + ResultCode(unsafe { + ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len()) + })?; + Ok(()) + } } -pub fn generate_random_bytes(out: &mut [u8]) -> crate::Result<()> { - ResultCode(unsafe { ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len()) })?; - Ok(()) +impl Drop for Ps { + fn drop(&mut self) { + unsafe { + ctru_sys::psExit(); + } + } } #[cfg(test)]