From 458758faf9a96beced06180fac6deffbd16b67a8 Mon Sep 17 00:00:00 2001 From: jugeeya Date: Mon, 10 Oct 2022 17:53:08 -0700 Subject: [PATCH] Spawn web session normally; use web_session feature gate for experimental version --- Cargo.toml | 1 + src/common/button_config.rs | 2 -- src/common/menu.rs | 72 +++++++++++++++++++++---------------- src/lib.rs | 1 + src/training/save_states.rs | 2 +- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2c46617..eb7b424 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,3 +52,4 @@ plugin-dependencies = [ [features] outside_training_mode = [] +web_session = [] \ No newline at end of file diff --git a/src/common/button_config.rs b/src/common/button_config.rs index bc8b694..9b699a0 100644 --- a/src/common/button_config.rs +++ b/src/common/button_config.rs @@ -1,7 +1,5 @@ use lazy_static::lazy_static; -use parking_lot::Mutex; use smash::app::lua_bind::ControlModule; -use smash::lib::lua_const::*; use serde::Deserialize; use std::collections::HashMap; use toml; diff --git a/src/common/menu.rs b/src/common/menu.rs index 295a409..d63d997 100644 --- a/src/common/menu.rs +++ b/src/common/menu.rs @@ -8,7 +8,6 @@ use skyline::info::get_program_id; use skyline::nn::hid::NpadGcState; use skyline::nn::web::WebSessionBootMode; use skyline_web::{Background, WebSession, Webpage}; -use smash::lib::lua_const::*; use std::fs; use std::path::Path; use training_mod_consts::{MenuJsonStruct, TrainingModpackMenu}; @@ -121,7 +120,13 @@ pub fn spawn_menu() { frame_counter::start_counting(QUICK_MENU_FRAME_COUNTER_INDEX); if MENU.quick_menu == OnOff::Off { - WEB_MENU_ACTIVE = true; + #[cfg(feature = "web_session")] { + WEB_MENU_ACTIVE = true; + } + + #[cfg(not(feature = "web_session"))] { + spawn_web_session(new_web_session(false)); + } } else { QUICK_MENU_ACTIVE = true; } @@ -398,6 +403,38 @@ pub unsafe fn quick_menu_loop() { static mut WEB_MENU_ACTIVE: bool = false; +unsafe fn spawn_web_session(session: WebSession) { + println!("[Training Modpack] Opening menu session..."); + let message_send = MenuJsonStruct { + menu: MENU, + defaults_menu: DEFAULTS_MENU, + }; + session.send_json(&message_send); + println!( + "[Training Modpack] Sending message:\n{}", + serde_json::to_string_pretty(&message_send).unwrap() + ); + session.show(); + let message_recv = session.recv(); + println!( + "[Training Modpack] Received menu from web:\n{}", + &message_recv + ); + println!("[Training Modpack] Tearing down Training Modpack menu session"); + session.exit(); + session.wait_for_exit(); + set_menu_from_json(&message_recv); +} + +unsafe fn new_web_session(hidden: bool) -> WebSession { + Webpage::new() + .background(Background::BlurredScreenshot) + .htdocs_dir("training_modpack") + .start_page("training_menu.html") + .open_session(if hidden { WebSessionBootMode::InitiallyHidden } else { WebSessionBootMode::Default }) + .unwrap() +} + pub unsafe fn web_session_loop() { // Don't query the FighterManager too early otherwise it will crash... std::thread::sleep(std::time::Duration::new(30, 0)); // sleep for 30 secs on bootup @@ -407,28 +444,8 @@ pub unsafe fn web_session_loop() { if (is_ready_go() || entry_count() > 0) && is_training_mode() { if web_session.is_some() { if WEB_MENU_ACTIVE { - println!("[Training Modpack] Opening menu session..."); - let session = web_session.unwrap(); - let message_send = MenuJsonStruct { - menu: MENU, - defaults_menu: DEFAULTS_MENU, - }; - session.send_json(&message_send); - println!( - "[Training Modpack] Sending message:\n{}", - serde_json::to_string_pretty(&message_send).unwrap() - ); - session.show(); - let message_recv = session.recv(); - println!( - "[Training Modpack] Received menu from web:\n{}", - &message_recv - ); - println!("[Training Modpack] Tearing down Training Modpack menu session"); - session.exit(); - session.wait_for_exit(); + spawn_web_session(web_session.unwrap()); web_session = None; - set_menu_from_json(&message_recv); WEB_MENU_ACTIVE = false; } } else { @@ -437,14 +454,7 @@ pub unsafe fn web_session_loop() { // Investigate whether we can minimize this lag by // waiting until the player is idle or using CPU boost mode println!("[Training Modpack] Starting new menu session..."); - web_session = Some( - Webpage::new() - .background(Background::BlurredScreenshot) - .htdocs_dir("training_modpack") - .start_page("training_menu.html") - .open_session(WebSessionBootMode::InitiallyHidden) - .unwrap(), - ); + web_session = Some(new_web_session(true)); } } else { // No longer in training mode, tear down the session. diff --git a/src/lib.rs b/src/lib.rs index 92322ba..4310773 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,6 +145,7 @@ pub fn main() { std::thread::spawn(|| unsafe { quick_menu_loop() }); + #[cfg(feature = "web_session")] if !is_emulator() { std::thread::spawn(|| unsafe { web_session_loop() }); } diff --git a/src/training/save_states.rs b/src/training/save_states.rs index 5d2aa2e..cff1bab 100644 --- a/src/training/save_states.rs +++ b/src/training/save_states.rs @@ -78,7 +78,7 @@ macro_rules! default_save_state { }; } -use crate::{get_module_accessor, is_ptrainer, ITEM_MANAGER_ADDR}; +use crate::{is_ptrainer, ITEM_MANAGER_ADDR}; use SaveState::*; static mut SAVE_STATE_PLAYER: SavedState = default_save_state!();