Skip to content

Commit

Permalink
Allow start program checks to be overridden.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Jan 26, 2025
1 parent 21e0cd9 commit 52b4510
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 42 deletions.
4 changes: 2 additions & 2 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ file(GLOB MAIN_SOURCES
Source/CommonTools/TrendInference/TimeWindowStatTracker.h
Source/CommonTools/Resources/SpriteDatabase.cpp
Source/CommonTools/Resources/SpriteDatabase.h
Source/CommonTools/StartupChecks/BlackBorderCheck.cpp
Source/CommonTools/StartupChecks/BlackBorderCheck.h
Source/CommonTools/StartupChecks/StartProgramChecks.cpp
Source/CommonTools/StartupChecks/StartProgramChecks.h
Source/CommonTools/StartupChecks/VideoResolutionCheck.cpp
Source/CommonTools/StartupChecks/VideoResolutionCheck.h
Source/CommonTools/VisualDetector.h
Expand Down
4 changes: 2 additions & 2 deletions SerialPrograms/SerialPrograms.pro
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ SOURCES += \
Source/CommonTools/Options/ScreenWatchOption.cpp \
Source/CommonTools/Options/StringSelectOption.cpp \
Source/CommonTools/Resources/SpriteDatabase.cpp \
Source/CommonTools/StartupChecks/BlackBorderCheck.cpp \
Source/CommonTools/StartupChecks/StartProgramChecks.cpp \
Source/CommonTools/StartupChecks/VideoResolutionCheck.cpp \
Source/CommonTools/TrendInference/AnomalyDetector.cpp \
Source/CommonTools/VisualDetectors/BlackBorderDetector.cpp \
Expand Down Expand Up @@ -1408,7 +1408,7 @@ HEADERS += \
Source/CommonTools/Options/StringSelectTableOption.h \
Source/CommonTools/Options/TrainOCRModeOption.h \
Source/CommonTools/Resources/SpriteDatabase.h \
Source/CommonTools/StartupChecks/BlackBorderCheck.h \
Source/CommonTools/StartupChecks/StartProgramChecks.h \
Source/CommonTools/StartupChecks/VideoResolutionCheck.h \
Source/CommonTools/TrendInference/AnomalyDetector.h \
Source/CommonTools/TrendInference/TimeWindowStatTracker.h \
Expand Down
21 changes: 0 additions & 21 deletions SerialPrograms/Source/CommonTools/StartupChecks/BlackBorderCheck.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Black Border Check
/* Start Program Checks
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
Expand All @@ -10,34 +10,37 @@
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonFramework/Tools/VideoStream.h"
#include "CommonTools/VisualDetectors/BlackBorderDetector.h"
#include "BlackBorderCheck.h"
#include "StartProgramChecks.h"

namespace PokemonAutomation{
namespace StartProgramChecks{


void start_program_video_check(VideoStream& stream, FeedbackType feedback){
void check_feedback(VideoStream& stream, FeedbackType feedback){
if (feedback == FeedbackType::NONE){
return;
}

VideoSnapshot screen = stream.video().snapshot();

if (!screen){
if (feedback == FeedbackType::REQUIRED || feedback == FeedbackType::VIDEO_AUDIO){
throw UserSetupError(stream.logger(), "This program requires video feedback. Please make sure the video is working.");
}
return;
}
}

void check_border(VideoStream& stream){
BlackBorderDetector detector;
VideoOverlaySet set(stream.overlay());
detector.make_overlays(set);

VideoSnapshot screen = stream.video().snapshot();
if (detector.detect(screen)){
throw UserSetupError(stream.logger(), "Black border detected! Please set your screen size to 100% in the TV Settings on your Nintendo Switch.");
}

}



}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Start Program Checks
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#ifndef PokemonAutomation_CommonTools_StartProgramChecks_H
#define PokemonAutomation_CommonTools_StartProgramChecks_H

#include "CommonFramework/Globals.h"

namespace PokemonAutomation{
class VideoStream;
namespace StartProgramChecks{



void check_feedback(VideoStream& stream, FeedbackType feedback);
void check_border(VideoStream& stream);



}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/Options/Environment/SleepSuppressOption.h"
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
#include "CommonTools/StartupChecks/BlackBorderCheck.h"
#include "NintendoSwitch/Controllers/NintendoSwitch_Controller.h"
#include "NintendoSwitch_MultiSwitchProgramOption.h"
#include "NintendoSwitch_MultiSwitchProgramSession.h"
Expand Down Expand Up @@ -84,12 +83,22 @@ void MultiSwitchProgramSession::run_program_instance(MultiSwitchProgramEnvironme
}
}

// Startup Checks
size_t consoles = m_system.count();
for (size_t c = 0; c < consoles; c++){
if (!m_system[c].controller_session().ready()){
throw UserSetupError(m_system[c].logger(), "Cannot Start: Serial connection not ready.");
}
start_program_video_check(env.consoles[c], m_option.descriptor().feedback());
m_option.instance().start_program_controller_check(
scope,
m_system[c].controller_session(), c
);
m_option.instance().start_program_feedback_check(
scope,
env.consoles[c], c,
m_option.descriptor().feedback()
);
m_option.instance().start_program_border_check(
scope,
env.consoles[c], c
);
}

m_scope.store(&scope, std::memory_order_release);
Expand Down Expand Up @@ -165,6 +174,7 @@ void MultiSwitchProgramSession::internal_run_program(){
}



CancellableHolder<CancellableScope> scope;
MultiSwitchProgramEnvironment env(
program_info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
#include "CommonFramework/Notifications/ProgramInfo.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonTools/StartupChecks/BlackBorderCheck.h"
#include "NintendoSwitch/Controllers/NintendoSwitch_Controller.h"
#include "NintendoSwitch_SingleSwitchProgramOption.h"
#include "NintendoSwitch_SingleSwitchProgramSession.h"
Expand Down Expand Up @@ -59,11 +58,10 @@ void SingleSwitchProgramSession::run_program_instance(SingleSwitchProgramEnviron
}
}

if (!m_system.controller_session().ready()){
throw UserSetupError(m_system.logger(), "Cannot Start: Serial connection not ready.");
}

start_program_video_check(env.console, m_option.descriptor().feedback());
// Startup Checks
m_option.instance().start_program_controller_check(scope, m_system.controller_session());
m_option.instance().start_program_feedback_check(scope, env.console, m_option.descriptor().feedback());
m_option.instance().start_program_border_check(scope, env.console);

m_scope.store(&scope, std::memory_order_release);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "Common/Cpp/Concurrency/AsyncDispatcher.h"
#include "CommonFramework/VideoPipeline/VideoOverlay.h"
#include "CommonFramework/VideoPipeline/Stats/ThreadUtilizationStats.h"
#include "CommonTools/StartupChecks/StartProgramChecks.h"
#include "Controllers/ControllerSession.h"
#include "NintendoSwitch_MultiSwitchProgram.h"
#include "Framework/NintendoSwitch_MultiSwitchProgramOption.h"

Expand Down Expand Up @@ -121,6 +123,31 @@ MultiSwitchProgramInstance::MultiSwitchProgramInstance(
error_notification_tags
)
{}


void MultiSwitchProgramInstance::start_program_controller_check(
CancellableScope& scope,
ControllerSession& session, size_t console_index
){
if (!session.ready()){
throw UserSetupError(session.logger(), "Cannot Start: Controller is not ready.");
}
}
void MultiSwitchProgramInstance::start_program_feedback_check(
CancellableScope& scope,
VideoStream& stream, size_t console_index,
FeedbackType feedback_type
){
StartProgramChecks::check_feedback(stream, feedback_type);
}
void MultiSwitchProgramInstance::start_program_border_check(
CancellableScope& scope,
VideoStream& stream, size_t console_index
){
StartProgramChecks::check_border(stream);
}


void MultiSwitchProgramInstance::add_option(ConfigOption& option, std::string serialization_string){
m_options.add_option(option, std::move(serialization_string));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"

namespace PokemonAutomation{
class ControllerSession;
namespace NintendoSwitch{


Expand Down Expand Up @@ -129,6 +130,24 @@ class MultiSwitchProgramInstance{
virtual void program(MultiSwitchProgramEnvironment& env, CancellableScope& scope) = 0;


public:
// Startup Checks: Feel free to override to change behavior.

virtual void start_program_controller_check(
CancellableScope& scope,
ControllerSession& session, size_t console_index
);
virtual void start_program_feedback_check(
CancellableScope& scope,
VideoStream& stream, size_t console_index,
FeedbackType feedback_type
);
virtual void start_program_border_check(
CancellableScope& scope,
VideoStream& stream, size_t console_index
);


public:
// Settings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/

#include "Common/Cpp/Json/JsonValue.h"
//#include "CommonFramework/VideoPipeline/VideoFeed.h"
//#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonTools/StartupChecks/StartProgramChecks.h"
#include "Controllers/ControllerSession.h"
#include "NintendoSwitch_SingleSwitchProgram.h"
#include "Framework/NintendoSwitch_SingleSwitchProgramOption.h"

Expand Down Expand Up @@ -64,6 +68,31 @@ SingleSwitchProgramInstance::SingleSwitchProgramInstance(
error_notification_tags
)
{}


void SingleSwitchProgramInstance::start_program_controller_check(
CancellableScope& scope,
ControllerSession& session
){
if (!session.ready()){
throw UserSetupError(session.logger(), "Cannot Start: Controller is not ready.");
}
}
void SingleSwitchProgramInstance::start_program_feedback_check(
CancellableScope& scope,
VideoStream& stream,
FeedbackType feedback_type
){
StartProgramChecks::check_feedback(stream, feedback_type);
}
void SingleSwitchProgramInstance::start_program_border_check(
CancellableScope& scope,
VideoStream& stream
){
StartProgramChecks::check_border(stream);
}


void SingleSwitchProgramInstance::add_option(ConfigOption& option, std::string serialization_string){
m_options.add_option(option, std::move(serialization_string));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"

namespace PokemonAutomation{
class ControllerSession;
namespace NintendoSwitch{


Expand Down Expand Up @@ -112,6 +113,24 @@ class SingleSwitchProgramInstance{
virtual void program(SingleSwitchProgramEnvironment& env, SwitchControllerContext& context) = 0;


public:
// Startup Checks: Feel free to override to change behavior.

virtual void start_program_controller_check(
CancellableScope& scope,
ControllerSession& session
);
virtual void start_program_feedback_check(
CancellableScope& scope,
VideoStream& stream,
FeedbackType feedback_type
);
virtual void start_program_border_check(
CancellableScope& scope,
VideoStream& stream
);


public:
// Settings

Expand Down

0 comments on commit 52b4510

Please sign in to comment.