From 3f5e8398d74ec4cf76bab25a09b3ce70be767cb7 Mon Sep 17 00:00:00 2001 From: jugeeya Date: Tue, 4 Oct 2022 01:23:55 -0700 Subject: [PATCH] Fix Quick Menu selection (failed to r/w hitbox vis + quick menu previously) --- src/common/menu.rs | 37 +++++++++++++++++----------------- src/lib.rs | 3 ++- training_mod_consts/src/lib.rs | 5 ++++- training_mod_tui/src/main.rs | 21 ++++++++++++------- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/common/menu.rs b/src/common/menu.rs index cd862bd..a6a6de6 100644 --- a/src/common/menu.rs +++ b/src/common/menu.rs @@ -76,16 +76,6 @@ pub unsafe fn write_menu() { const MENU_CONF_PATH: &str = "sd:/TrainingModpack/training_modpack_menu.conf"; pub unsafe fn set_menu_from_json(message: &str) { - if MENU.quick_menu == OnOff::Off { - if is_emulator() { - skyline::error::show_error( - 0x69, - "Cannot use web menu on emulator.\n\0", - "Only the quick menu is runnable via emulator currently.\n\0", - ); - MENU.quick_menu = OnOff::On; - } - } if let Ok(message_json) = serde_json::from_str::(message) { // Includes both MENU and DEFAULTS_MENU // From Web Applet @@ -114,6 +104,16 @@ pub unsafe fn set_menu_from_json(message: &str) { message ); }; + if MENU.quick_menu == OnOff::Off { + if is_emulator() { + skyline::error::show_error( + 0x69, + "Cannot use web menu on emulator.\n\0", + "Only the quick menu is runnable via emulator currently.\n\0", + ); + MENU.quick_menu = OnOff::On; + } + } EVENT_QUEUE.push(Event::menu_open(message.to_string())); } @@ -223,7 +223,7 @@ pub static mut BUTTON_PRESSES: ButtonPresses = ButtonPresses { pub fn handle_get_npad_state(state: *mut NpadGcState, _controller_id: *const u32) { unsafe { - if menu::QUICK_MENU_ACTIVE { + if QUICK_MENU_ACTIVE { // TODO: This should make more sense, look into. // BUTTON_PRESSES.a.is_pressed = (*state).Buttons & (1 << 0) > 0; // BUTTON_PRESSES.b.is_pressed = (*state).Buttons & (1 << 1) > 0; @@ -293,7 +293,7 @@ pub fn render_text_to_screen(s: &str) { pub unsafe fn quick_menu_loop() { loop { std::thread::sleep(std::time::Duration::from_secs(10)); - let menu = consts::get_menu(); + let menu = get_menu(); let mut app = training_mod_tui::App::new(menu); @@ -303,7 +303,7 @@ pub unsafe fn quick_menu_loop() { let mut has_slept_millis = 0; let render_frames = 5; let mut json_response = String::new(); - let button_presses = &mut menu::BUTTON_PRESSES; + let button_presses = &mut BUTTON_PRESSES; let mut received_input = true; loop { button_presses.a.read_press().then(|| { @@ -315,11 +315,11 @@ pub unsafe fn quick_menu_loop() { received_input = true; if !app.outer_list { app.on_b() - } else if frame_counter::get_frame_count(menu::QUICK_MENU_FRAME_COUNTER_INDEX) == 0 + } else if frame_counter::get_frame_count(QUICK_MENU_FRAME_COUNTER_INDEX) == 0 { // Leave menu. - menu::QUICK_MENU_ACTIVE = false; - menu::set_menu_from_json(&json_response); + QUICK_MENU_ACTIVE = false; + set_menu_from_json(&json_response); } }); button_presses.zl.read_press().then(|| { @@ -352,9 +352,10 @@ pub unsafe fn quick_menu_loop() { if has_slept_millis < 16 * render_frames { continue; } + has_slept_millis = 16; - if !menu::QUICK_MENU_ACTIVE { - app = training_mod_tui::App::new(consts::get_menu()); + if !QUICK_MENU_ACTIVE { + app = training_mod_tui::App::new(get_menu()); set_should_display_text_to_screen(false); continue; } diff --git a/src/lib.rs b/src/lib.rs index 72b763d..2be5209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,6 +106,7 @@ pub fn main() { unsafe { DEFAULTS_MENU.quick_menu = OnOff::On; MENU.quick_menu = OnOff::On; + BASE_MENU.quick_menu = OnOff::On; } } @@ -127,7 +128,7 @@ pub fn main() { std::thread::spawn(|| unsafe { quick_menu_loop() }); - if is_emulator() { + if !is_emulator() { std::thread::spawn(|| unsafe { web_session_loop() }); } } diff --git a/training_mod_consts/src/lib.rs b/training_mod_consts/src/lib.rs index d5e9f3f..5e46348 100644 --- a/training_mod_consts/src/lib.rs +++ b/training_mod_consts/src/lib.rs @@ -1497,7 +1497,10 @@ pub unsafe fn get_menu() -> UiMenu<'static> { ); overall_menu.tabs.push(misc_tab); - let non_ui_menu = serde_json::to_string(&MENU).unwrap().replace("\"", ""); + let non_ui_menu = serde_json::to_string(&MENU).unwrap() + .replace("\"", "") + .replace("{", "") + .replace("}", ""); let toggle_values_all = non_ui_menu.split(',').collect::>(); let mut sub_menu_id_to_vals: HashMap<&str, u32> = HashMap::new(); for toggle_values in toggle_values_all { diff --git a/training_mod_tui/src/main.rs b/training_mod_tui/src/main.rs index 04cd63e..febec79 100644 --- a/training_mod_tui/src/main.rs +++ b/training_mod_tui/src/main.rs @@ -30,21 +30,24 @@ fn test_backend_setup(ui_menu: UiMenu) -> Result< } #[test] -fn ensure_menu_retains_multi_selections() -> Result<(), Box> { +fn ensure_menu_retains_selections() -> Result<(), Box> { let menu; + let prev_menu; unsafe { + prev_menu = MENU; menu = get_menu(); - println!("MENU.miss_tech_state: {}", MENU.miss_tech_state); } let (mut terminal, mut app) = test_backend_setup(menu)?; let mut json_response = String::new(); let _frame_res = terminal.draw(|f| json_response = training_mod_tui::ui(f, &mut app))?; - set_menu_from_json(json_response); unsafe { - // At this point, we didn't change the menu at all; we should still see all missed tech flags. - assert_eq!(MENU.miss_tech_state, - MissTechFlags::all()); + MENU = serde_json::from_str::(&json_response).unwrap(); + // At this point, we didn't change the menu at all; we should still see all the same options. + assert_eq!( + serde_json::to_string(&prev_menu).unwrap(), + serde_json::to_string(&MENU).unwrap() + ); } Ok(()) @@ -97,7 +100,11 @@ fn main() -> Result<(), Box> { if let Err(err) = res { println!("{:?}", err) } else { - println!("URL: {}", res.as_ref().unwrap()); + println!("JSON: {}", res.as_ref().unwrap()); + unsafe { + MENU = serde_json::from_str::(&res.as_ref().unwrap()).unwrap(); + println!("MENU: {:#?}", MENU); + } } }