Skip to content

Commit

Permalink
Merge pull request #122 from DeltaF1/mii-selector-return
Browse files Browse the repository at this point in the history
Handle empty Mii selector return
  • Loading branch information
Meziu committed May 9, 2023
2 parents ca8f13d + b50bacf commit f6d16d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
26 changes: 15 additions & 11 deletions ctru-rs/examples/mii-selector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ctru::applets::mii_selector::MiiSelector;
use ctru::applets::mii_selector::{LaunchError, MiiSelector, Options};
use ctru::prelude::*;

fn main() {
Expand All @@ -10,20 +10,24 @@ fn main() {
let _console = Console::new(gfx.top_screen.borrow_mut());

let mut mii_selector = MiiSelector::new();
mii_selector.set_options(Options::MII_SELECTOR_CANCEL);
mii_selector.set_initial_index(3);
mii_selector.blacklist_user_mii(0.into());
mii_selector.set_title("Great Mii Selector!");

let result = mii_selector.launch().unwrap();

println!("Is Mii selected?: {:?}", result.is_mii_selected);
println!("Mii type: {:?}", result.mii_type);
println!("Name: {:?}", result.mii_data.name);
println!("Author: {:?}", result.mii_data.author_name);
println!(
"Does the Mii have moles?: {:?}",
result.mii_data.mole_details.is_enabled
);
match mii_selector.launch() {
Ok(result) => {
println!("Mii type: {:?}", result.mii_type);
println!("Name: {:?}", result.mii_data.name);
println!("Author: {:?}", result.mii_data.author_name);
println!(
"Does the Mii have moles?: {:?}",
result.mii_data.mole_details.is_enabled
);
}
Err(LaunchError::InvalidChecksum) => println!("Corrupt Mii selected"),
Err(LaunchError::NoMiiSelected) => println!("No Mii selected"),
}

// Main loop
while apt.main_loop() {
Expand Down
9 changes: 7 additions & 2 deletions ctru-rs/src/applets/mii_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ pub struct MiiSelector {
#[derive(Clone, Debug)]
pub struct SelectionResult {
pub mii_data: MiiData,
pub is_mii_selected: bool,
pub mii_type: MiiType,
}

/// Error type for the Mii selector
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum LaunchError {
/// The selected Mii's data is corrupt in some way
InvalidChecksum,
/// Either the user cancelled the selection (see [Options::MII_SELECTOR_CANCEL]), or no valid Miis were available to select
NoMiiSelected,
}

impl MiiSelector {
Expand Down Expand Up @@ -147,6 +149,10 @@ impl MiiSelector {
let mut return_val = Box::<ctru_sys::MiiSelectorReturn>::default();
unsafe { ctru_sys::miiSelectorLaunch(self.config.as_mut(), return_val.as_mut()) }

if return_val.no_mii_selected != 0 {
return Err(LaunchError::NoMiiSelected);
}

if unsafe { ctru_sys::miiSelectorChecksumIsValid(return_val.as_mut()) } {
Ok((*return_val).into())
} else {
Expand All @@ -168,7 +174,6 @@ impl From<ctru_sys::MiiSelectorReturn> for SelectionResult {

SelectionResult {
mii_data: raw_mii_data.into(),
is_mii_selected: ret.no_mii_selected == 0,
mii_type: if ret.guest_mii_index != 0xFFFFFFFF {
MiiType::Guest {
index: ret.guest_mii_index,
Expand Down

0 comments on commit f6d16d1

Please sign in to comment.