diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchProgramSession.cpp b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchProgramSession.cpp index cc9c5c9010..6433e18738 100644 --- a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchProgramSession.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchProgramSession.cpp @@ -59,7 +59,7 @@ bool MultiSwitchProgramSession::try_shutdown(){ MultiSwitchProgramSession::~MultiSwitchProgramSession(){ blocking_shutdown( logger(), - "SingleSwitchProgramSession", + "MultiSwitchProgramSession", [this]{ return try_shutdown(); } ); } diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.cpp b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.cpp deleted file mode 100644 index 489fa8ddb6..0000000000 --- a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* Single Switch Program Option - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "Common/Cpp/Json/JsonValue.h" -#include "Common/Cpp/Json/JsonObject.h" -#include "NintendoSwitch_SingleSwitchProgramOption.h" -#include "UI/NintendoSwitch_SingleSwitchProgramWidget.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ - - -SingleSwitchProgramOption::~SingleSwitchProgramOption() = default; -SingleSwitchProgramOption::SingleSwitchProgramOption(const SingleSwitchProgramDescriptor& descriptor) - : PanelSession(descriptor) - , m_descriptor(descriptor) - , m_system(descriptor.allow_commands_while_running()) - , m_instance(descriptor.make_instance()) -{} - -void SingleSwitchProgramOption::from_json(const JsonValue& json){ - const JsonObject* obj = json.to_object(); - if (obj == nullptr){ - return; - } - const JsonValue* value = obj->get_value("SwitchSetup"); - if (value){ - m_system.load_json(*value); - } - m_instance->from_json(json); -} -JsonValue SingleSwitchProgramOption::to_json() const{ - JsonObject obj = std::move(*m_instance->to_json().to_object()); - obj["SwitchSetup"] = m_system.to_json(); - return obj; -} - -ConfigOption& SingleSwitchProgramOption::options(){ - return m_instance->m_options; -} - -std::string SingleSwitchProgramOption::check_validity() const{ - return m_instance->check_validity(); -} -void SingleSwitchProgramOption::restore_defaults(){ - m_instance->restore_defaults(); -} - - -QWidget* SingleSwitchProgramOption::make_widget(QWidget& parent){ - return new SingleSwitchProgramWidget2(parent, *this); -} - - - - - -} -} diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.h b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.h deleted file mode 100644 index 4cf29690ef..0000000000 --- a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.h +++ /dev/null @@ -1,63 +0,0 @@ -/* Single Switch Program Option - * - * From: https://github.com/PokemonAutomation/ - * - * This class represents the serializable state of a Switch program. - * This class maintains no UI and is not thread-safe. - * - * Note that this class does own the "SingleSwitchProgramInstance", object - * which is controlled by the individual program itself. There the running - * program can do whatever it wants - including keeping run-time state. - * - */ - -#ifndef PokemonAutomation_NintendoSwitch_SingleSwitchProgramOption_H -#define PokemonAutomation_NintendoSwitch_SingleSwitchProgramOption_H - -#include "CommonFramework/Panels/PanelSession.h" -#include "NintendoSwitch_SwitchSystemOption.h" - -namespace PokemonAutomation{ - class ConfigOption; -namespace NintendoSwitch{ - -class SingleSwitchProgramDescriptor; -class SingleSwitchProgramInstance; - - -class SingleSwitchProgramOption final : public PanelSession{ -public: - ~SingleSwitchProgramOption(); - SingleSwitchProgramOption(const SingleSwitchProgramDescriptor& descriptor); - - virtual void from_json(const JsonValue& json) override; - virtual JsonValue to_json() const override; - -public: - const SingleSwitchProgramDescriptor& descriptor() const{ return m_descriptor; } - SwitchSystemOption& system(){ return m_system; } - SingleSwitchProgramInstance& instance(){ return *m_instance; } - ConfigOption& options(); - - std::string check_validity() const; - void restore_defaults(); - -private: - virtual QWidget* make_widget(QWidget& parent) override; - -private: - const SingleSwitchProgramDescriptor& m_descriptor; - SwitchSystemOption m_system; - std::unique_ptr m_instance; -}; - - - - - - - - -} -} -#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.cpp b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.cpp index 580e1918f9..e74ad3280b 100644 --- a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.cpp @@ -6,6 +6,8 @@ #include "Common/Cpp/ScopeExit.h" #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/Json/JsonObject.h" #include "Common/Cpp/EarlyShutdown.h" #include "Common/Cpp/Concurrency/SpinPause.h" #include "Common/Cpp/Containers/FixedLimitVector.tpp" @@ -17,12 +19,13 @@ #include "CommonFramework/Notifications/ProgramInfo.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "NintendoSwitch/NintendoSwitch_Settings.h" -#include "NintendoSwitch_SingleSwitchProgramOption.h" #include "NintendoSwitch_SingleSwitchProgramSession.h" +#include "UI/NintendoSwitch_SingleSwitchProgramWidget.h" -//#include -//using std::cout; -//using std::endl; +// REMOVE +#include +using std::cout; +using std::endl; namespace PokemonAutomation{ @@ -30,10 +33,13 @@ namespace NintendoSwitch{ -SingleSwitchProgramSession::SingleSwitchProgramSession(SingleSwitchProgramOption& option, size_t console_number) - : ProgramSession(option.descriptor()) - , m_option(option) - , m_system(option.system(), console_number, instance_id()) +SingleSwitchProgramSession::SingleSwitchProgramSession(const SingleSwitchProgramDescriptor& descriptor) + : PanelSession(descriptor) + , ProgramSession(descriptor) + , m_descriptor(descriptor) + , m_system_option(descriptor.allow_commands_while_running()) + , m_system(m_system_option, 0, instance_id()) + , m_instance(descriptor.make_instance()) , m_scope(nullptr) {} @@ -59,10 +65,13 @@ void SingleSwitchProgramSession::restore_defaults(){ return; } logger().log("Restoring settings to defaults..."); - m_option.restore_defaults(); + m_instance->restore_defaults(); +} +ConfigOption& SingleSwitchProgramSession::options(){ + return m_instance->m_options; } std::string SingleSwitchProgramSession::check_validity() const{ - return m_option.check_validity(); + return m_instance->check_validity(); } @@ -77,16 +86,16 @@ void SingleSwitchProgramSession::run_program_instance(SingleSwitchProgramEnviron } // Startup Checks - m_option.instance().start_program_controller_check( + m_instance->start_program_controller_check( m_system.controller() ); - m_option.instance().start_program_feedback_check( + m_instance->start_program_feedback_check( env.console, - m_option.descriptor().feedback() + m_descriptor.feedback() ); - m_option.instance().start_program_border_check( + m_instance->start_program_border_check( env.console, - m_option.descriptor().feedback() + m_descriptor.feedback() ); // Attach all the controllers to the scope so they can be cancelled from the top. @@ -110,7 +119,7 @@ void SingleSwitchProgramSession::run_program_instance(SingleSwitchProgramEnviron m_scope.store(nullptr, std::memory_order_release); }); - m_option.instance().program(env, scope); + m_instance->program(env, scope); env.console.wait_for_all_controllers(); } void SingleSwitchProgramSession::internal_stop_program(){ @@ -148,14 +157,14 @@ void SingleSwitchProgramSession::internal_run_program(){ } } - m_option.options().reset_state(); + m_instance->m_options.reset_state(); SleepSuppressScope sleep_scope(GlobalSettings::instance().SLEEP_SUPPRESS->PROGRAM_RUNNING); ProgramInfo program_info( identifier(), - m_option.descriptor().category(), - m_option.descriptor().display_name(), + m_descriptor.category(), + m_descriptor.display_name(), timestamp() ); CancellableHolder scope; @@ -188,7 +197,7 @@ void SingleSwitchProgramSession::internal_run_program(){ }catch (ProgramFinishedException& e){ logger().log("Program finished early!", COLOR_BLUE); env.console.overlay().add_log("- Program Finished -"); - send_program_finished_notification(env, m_option.instance().NOTIFICATION_PROGRAM_FINISH, e.message(), *e.screenshot()); + send_program_finished_notification(env, m_instance->NOTIFICATION_PROGRAM_FINISH, e.message(), *e.screenshot()); }catch (InvalidConnectionStateException& e){ logger().log("Program stopped due to connection issue.", COLOR_RED); env.console.overlay().add_log("- Invalid Connection -", COLOR_RED); @@ -206,7 +215,7 @@ void SingleSwitchProgramSession::internal_run_program(){ message = e.name(); } report_error(message); - e.send_fatal_error_notif_and_telemetry_report(env, m_option.instance().NOTIFICATION_ERROR_FATAL); + e.send_fatal_error_notif_and_telemetry_report(env, m_instance->NOTIFICATION_ERROR_FATAL); }catch (OperationFailedException& e){ // no screenshot logger().log("Program stopped with an exception!", COLOR_RED); env.console.overlay().add_log("- Program Error -", COLOR_RED); @@ -216,7 +225,7 @@ void SingleSwitchProgramSession::internal_run_program(){ message = e.name(); } report_error(message); - e.send_fatal_error_notif_and_telemetry_report(env, m_option.instance().NOTIFICATION_ERROR_FATAL); + e.send_fatal_error_notif_and_telemetry_report(env, m_instance->NOTIFICATION_ERROR_FATAL); }catch (FatalProgramException& e){ logger().log("Program stopped with an exception!", COLOR_RED); env.console.overlay().add_log("- Program Error -", COLOR_RED); @@ -226,7 +235,7 @@ void SingleSwitchProgramSession::internal_run_program(){ message = e.name(); } report_error(message); - e.send_fatal_error_notif_and_telemetry_report(env, m_option.instance().NOTIFICATION_ERROR_FATAL); + e.send_fatal_error_notif_and_telemetry_report(env, m_instance->NOTIFICATION_ERROR_FATAL); }catch (Exception& e){ logger().log("Program stopped with an exception!", COLOR_RED); env.console.overlay().add_log("- Program Error -", COLOR_RED); @@ -236,7 +245,7 @@ void SingleSwitchProgramSession::internal_run_program(){ } report_error(message); send_program_fatal_error_notification( - env, m_option.instance().NOTIFICATION_ERROR_FATAL, + env, m_instance->NOTIFICATION_ERROR_FATAL, message ); } @@ -249,7 +258,7 @@ void SingleSwitchProgramSession::internal_run_program(){ } report_error(message); send_program_fatal_error_notification( - env, m_option.instance().NOTIFICATION_ERROR_FATAL, + env, m_instance->NOTIFICATION_ERROR_FATAL, message ); }catch (...){ @@ -257,13 +266,44 @@ void SingleSwitchProgramSession::internal_run_program(){ env.console.overlay().add_log("- Unknown Error -", COLOR_RED); report_error("Unknown error."); send_program_fatal_error_notification( - env, m_option.instance().NOTIFICATION_ERROR_FATAL, + env, m_instance->NOTIFICATION_ERROR_FATAL, "Unknown error." ); } } +void SingleSwitchProgramSession::from_json(const JsonValue& json){ + cout << "SingleSwitchProgramSession::from_json()" << endl; + const JsonObject* obj = json.to_object(); + if (obj == nullptr){ + return; + } + const JsonValue* value = obj->get_value("SwitchSetup"); + if (value){ + SwitchSystemOption option(m_system_option.m_allow_commands_while_locked, *value); + m_system.load(option); + } + m_instance->from_json(json); +} +JsonValue SingleSwitchProgramSession::to_json() const{ + cout << "SingleSwitchProgramSession::to_json()" << endl; + JsonObject obj = std::move(*m_instance->to_json().to_object()); + obj["SwitchSetup"] = m_system_option.to_json(); + return obj; +} +QWidget* SingleSwitchProgramSession::make_widget(QWidget& parent){ + return new SingleSwitchProgramWidget2(parent, *this); +} + + + + + + + + + } } diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.h b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.h index 7cbb91405a..df45abef3a 100644 --- a/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.h +++ b/SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.h @@ -15,6 +15,7 @@ #ifndef PokemonAutomation_NintendoSwitch_SingleSwitchProgramSession_H #define PokemonAutomation_NintendoSwitch_SingleSwitchProgramSession_H +#include "CommonFramework/Panels/PanelSession.h" #include "CommonFramework/ProgramSession.h" #include "NintendoSwitch_SwitchSystemSession.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" @@ -25,16 +26,20 @@ namespace NintendoSwitch{ class SingleSwitchProgramOption; -class SingleSwitchProgramSession final : public ProgramSession{ +class SingleSwitchProgramSession final : public PanelSession, public ProgramSession{ public: bool try_shutdown(); ~SingleSwitchProgramSession(); - SingleSwitchProgramSession(SingleSwitchProgramOption& option, size_t console_number); + SingleSwitchProgramSession(const SingleSwitchProgramDescriptor& descriptor); void restore_defaults(); + public: + const SingleSwitchProgramDescriptor& descriptor() const{ return m_descriptor; } SwitchSystemSession& system(){ return m_system; } + ConfigOption& options(); + private: virtual std::string check_validity() const override; @@ -43,13 +48,24 @@ class SingleSwitchProgramSession final : public ProgramSession{ virtual void internal_stop_program() override; +private: + virtual void from_json(const JsonValue& json) override; + virtual JsonValue to_json() const override; + virtual QWidget* make_widget(QWidget& parent) override; + + private: void run_program_instance(SingleSwitchProgramEnvironment& env, CancellableScope& scope); + private: - SingleSwitchProgramOption& m_option; + const SingleSwitchProgramDescriptor& m_descriptor; + + SwitchSystemOption m_system_option; SwitchSystemSession m_system; + std::unique_ptr m_instance; + std::atomic m_scope; }; diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.cpp b/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.cpp index 7f1099a61e..1144ca5777 100644 --- a/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.cpp @@ -17,7 +17,7 @@ #include "CommonFramework/Panels/UI/PanelElements.h" #include "CommonFramework/ProgramStats/StatsTracking.h" #include "CommonFramework/ResourceDownload/ProgramResourceDownloadWidget.h" -#include "NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.h" +#include "NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.h" #include "NintendoSwitch_SingleSwitchProgramWidget.h" //#include @@ -48,15 +48,15 @@ SingleSwitchProgramWidget2::~SingleSwitchProgramWidget2(){ } SingleSwitchProgramWidget2::SingleSwitchProgramWidget2( QWidget& parent, - SingleSwitchProgramOption& option + SingleSwitchProgramSession& session ) : QWidget(&parent) - , m_session(option, 0) + , m_session(session) { m_layout = new QVBoxLayout(this); m_layout->setContentsMargins(0, 0, 0, 0); - const SingleSwitchProgramDescriptor& descriptor = option.descriptor(); + const SingleSwitchProgramDescriptor& descriptor = session.descriptor(); CollapsibleGroupBox* header = make_panel_header( *this, @@ -94,7 +94,7 @@ SingleSwitchProgramWidget2::SingleSwitchProgramWidget2( m_system = dynamic_cast(wrapper.release()); scroll_layout->addWidget(m_system); - m_options = ConfigWidget::make_from_option(option.options(), this); + m_options = ConfigWidget::make_from_option(session.options(), this); scroll_layout->addWidget(&m_options->widget()); scroll_layout->addStretch(1); @@ -130,7 +130,7 @@ SingleSwitchProgramWidget2::SingleSwitchProgramWidget2( m_actions_bar, &RunnablePanelActionBar::defaults_clicked, this, [&]{ std::lock_guard lg(m_session.program_lock()); - option.restore_defaults(); + session.restore_defaults(); m_options->update_all(false); } ); diff --git a/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.h b/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.h index 3971510fa6..17fb749ea8 100644 --- a/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.h +++ b/SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.h @@ -35,7 +35,7 @@ class SingleSwitchProgramWidget2 : public QWidget, private ProgramSession::Liste ~SingleSwitchProgramWidget2(); SingleSwitchProgramWidget2( QWidget& parent, - SingleSwitchProgramOption& option + SingleSwitchProgramSession& session ); private: @@ -49,7 +49,8 @@ class SingleSwitchProgramWidget2 : public QWidget, private ProgramSession::Liste ProgramResourceDownloadTableWidget* ensure_downloads_table(); private: - SingleSwitchProgramSession m_session; + std::optional m_owner; + SingleSwitchProgramSession& m_session; QVBoxLayout* m_layout; QWidget* m_system; ConfigWidget* m_options; diff --git a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp index 4e8752f67e..343a09d8fc 100644 --- a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp +++ b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp @@ -13,7 +13,7 @@ #include "CommonTools/StartupChecks/StartProgramChecks.h" #include "Controllers/ControllerSession.h" #include "Commands/NintendoSwitch_Commands_PushButtons.h" -#include "Framework/NintendoSwitch_SingleSwitchProgramOption.h" +#include "Framework/NintendoSwitch_SingleSwitchProgramSession.h" #include "NintendoSwitch_SingleSwitchProgram.h" namespace PokemonAutomation{ @@ -50,7 +50,7 @@ SingleSwitchProgramDescriptor::SingleSwitchProgramDescriptor( , m_deprecated(deprecated) {} std::unique_ptr SingleSwitchProgramDescriptor::make_panel() const{ - return std::make_unique(*this); + return std::make_unique(*this); } diff --git a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h index 4dc4210081..e9059bf0dc 100644 --- a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h +++ b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h @@ -152,6 +152,7 @@ class SingleSwitchProgramInstance{ protected: friend class SingleSwitchProgramOption; + friend class SingleSwitchProgramSession; BatchOption m_options; void add_option(ConfigOption& option, std::string serialization_string); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 26448e0e94..93b38f78f8 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1187,8 +1187,6 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchSystemOption.h Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchSystemSession.cpp Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchSystemSession.h - Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.cpp - Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramOption.h Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.cpp Source/NintendoSwitch/Framework/NintendoSwitch_SingleSwitchProgramSession.h Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemOption.cpp