Skip to content

Commit ffa901d

Browse files
committed
Reduce usage of ConsoleHandle from infra.
1 parent e77a266 commit ffa901d

34 files changed

+270
-236
lines changed

SerialPrograms/Source/CommonFramework/Exceptions/OperationFailedException.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef PokemonAutomation_OperationFailedException_H
88
#define PokemonAutomation_OperationFailedException_H
99

10-
#include "CommonFramework/VideoPipeline/VideoStream.h"
10+
#include "CommonFramework/Tools/VideoStream.h"
1111
#include "ScreenshotException.h"
1212

1313
namespace PokemonAutomation{
@@ -26,15 +26,15 @@ class OperationFailedException : public ScreenshotException{
2626
std::string message,
2727
VideoStream& stream
2828
){
29-
throw_and_log<OperationFailedException>(stream.logger, error_report, std::move(message), stream);
29+
throw_and_log<OperationFailedException>(stream.logger(), error_report, std::move(message), stream);
3030
}
3131
[[noreturn]] static void fire(
3232
ErrorReport error_report,
3333
std::string message,
3434
VideoStream& stream,
3535
std::shared_ptr<const ImageRGB32> screenshot
3636
){
37-
throw_and_log<OperationFailedException>(stream.logger, error_report, std::move(message), &stream, std::move(screenshot));
37+
throw_and_log<OperationFailedException>(stream.logger(), error_report, std::move(message), &stream, std::move(screenshot));
3838
}
3939

4040
virtual const char* name() const override{ return "OperationFailedException"; }

SerialPrograms/Source/CommonFramework/Exceptions/ScreenshotException.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "CommonFramework/ErrorReports/ErrorReports.h"
99
#include "CommonFramework/Notifications/ProgramNotifications.h"
1010
#include "CommonFramework/VideoPipeline/VideoFeed.h"
11-
#include "CommonFramework/VideoPipeline/VideoStream.h"
11+
#include "CommonFramework/Tools/VideoStream.h"
1212
#include "CommonFramework/Tools/ProgramEnvironment.h"
1313
#include "ScreenshotException.h"
1414

@@ -31,10 +31,10 @@ ScreenshotException::ScreenshotException(
3131
: m_send_error_report(error_report)
3232
, m_message(std::move(message))
3333
, m_stream(&stream)
34-
, m_screenshot(stream.video.snapshot().frame)
34+
, m_screenshot(stream.video().snapshot().frame)
3535
{
3636
if (m_screenshot == nullptr || !*m_screenshot){
37-
stream.logger.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
37+
stream.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
3838
}
3939
}
4040
ScreenshotException::ScreenshotException(
@@ -66,9 +66,9 @@ void ScreenshotException::add_stream_if_needed(VideoStream& stream){
6666
m_stream = &stream;
6767
}
6868
if (!m_screenshot){
69-
m_screenshot = stream.video.snapshot();
69+
m_screenshot = stream.video().snapshot();
7070
if (m_screenshot == nullptr || !*m_screenshot){
71-
stream.logger.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
71+
stream.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
7272
}
7373
}
7474
}
@@ -95,7 +95,7 @@ void ScreenshotException::send_notification(ProgramEnvironment& env, EventNotifi
9595
name(),
9696
embeds,
9797
screenshot(),
98-
m_stream ? &m_stream->video_history : nullptr
98+
m_stream ? &m_stream->history() : nullptr
9999
);
100100
}
101101

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceRoutines.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
#include "Common/Cpp/Exceptions.h"
8-
#include "Common/Cpp/Concurrency/AsyncDispatcher.h"
8+
//#include "Common/Cpp/Concurrency/AsyncDispatcher.h"
99
#include "ClientSource/Connection/BotBase.h"
10-
#include "CommonFramework/Tools/ProgramEnvironment.h"
10+
//#include "CommonFramework/Tools/ProgramEnvironment.h"
1111
#include "InferenceSession.h"
1212
#include "InferenceRoutines.h"
1313

@@ -20,15 +20,15 @@ namespace PokemonAutomation{
2020

2121

2222
int wait_until(
23-
ConsoleHandle& console, CancellableScope& scope,
23+
VideoStream& stream, CancellableScope& scope,
2424
WallClock deadline,
2525
const std::vector<PeriodicInferenceCallback>& callbacks,
2626
std::chrono::milliseconds default_video_period,
2727
std::chrono::milliseconds default_audio_period
2828
){
2929
CancellableHolder<CancellableScope> subscope(scope);
3030
InferenceSession session(
31-
subscope, console,
31+
subscope, stream,
3232
callbacks,
3333
default_video_period, default_audio_period
3434
);
@@ -47,15 +47,15 @@ int wait_until(
4747

4848

4949
int run_until(
50-
ConsoleHandle& console, BotBaseContext& context,
50+
VideoStream& stream, BotBaseContext& context,
5151
std::function<void(BotBaseContext& context)>&& command,
5252
const std::vector<PeriodicInferenceCallback>& callbacks,
5353
std::chrono::milliseconds default_video_period,
5454
std::chrono::milliseconds default_audio_period
5555
){
56-
BotBaseContext subcontext(context, console.botbase());
56+
BotBaseContext subcontext(context, context.botbase());
5757
InferenceSession session(
58-
subcontext, console,
58+
subcontext, stream,
5959
callbacks,
6060
default_video_period, default_audio_period
6161
);
@@ -75,17 +75,18 @@ int run_until(
7575

7676

7777

78+
#if 0
7879
int run_until_with_time_limit(
79-
ProgramEnvironment& env, ConsoleHandle& console, BotBaseContext& context,
80+
ProgramEnvironment& env, VideoStream& stream, BotBaseContext& context,
8081
WallClock deadline,
8182
std::function<void(BotBaseContext& context)>&& command,
8283
const std::vector<PeriodicInferenceCallback>& callbacks,
8384
std::chrono::milliseconds default_video_period,
8485
std::chrono::milliseconds default_audio_period
8586
){
86-
BotBaseContext subcontext(context, console.botbase());
87+
BotBaseContext subcontext(context, context.botbase());
8788
InferenceSession session(
88-
subcontext, console,
89+
subcontext, stream,
8990
callbacks,
9091
default_video_period, default_audio_period
9192
);
@@ -110,7 +111,7 @@ int run_until_with_time_limit(
110111

111112
return timed_out ? -2 : session.triggered_index();
112113
}
113-
114+
#endif
114115

115116

116117

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceRoutines.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <chrono>
1212
#include <vector>
1313
#include "Common/Cpp/Time.h"
14-
#include "CommonFramework/Tools/ConsoleHandle.h"
14+
#include "CommonFramework/Tools/VideoStream.h"
1515
#include "InferenceCallback.h"
1616

1717
namespace PokemonAutomation{
@@ -29,21 +29,21 @@ class ProgramEnvironment;
2929
// Exceptions thrown in either the commands or the callbacks will stop
3030
// everything and will be propagated out of this function.
3131
int wait_until(
32-
ConsoleHandle& console, CancellableScope& scope,
32+
VideoStream& stream, CancellableScope& scope,
3333
WallClock deadline,
3434
const std::vector<PeriodicInferenceCallback>& callbacks,
3535
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
3636
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
3737
);
3838
inline int wait_until(
39-
ConsoleHandle& console, CancellableScope& scope,
39+
VideoStream& stream, CancellableScope& scope,
4040
std::chrono::milliseconds timeout,
4141
const std::vector<PeriodicInferenceCallback>& callbacks,
4242
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
4343
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
4444
){
4545
return wait_until(
46-
console, scope,
46+
stream, scope,
4747
current_time() + timeout,
4848
callbacks,
4949
default_video_period, default_audio_period
@@ -61,14 +61,15 @@ inline int wait_until(
6161
// Exceptions thrown in either the commands or the callbacks will stop
6262
// everything and will be propagated out of this function.
6363
int run_until(
64-
ConsoleHandle& console, BotBaseContext& context,
64+
VideoStream& stream, BotBaseContext& context,
6565
std::function<void(BotBaseContext& context)>&& command,
6666
const std::vector<PeriodicInferenceCallback>& callbacks,
6767
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
6868
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
6969
);
7070

7171

72+
#if 0
7273
// Same as "run_until()", but will cancel the commands and return if a timeout
7374
// is reached.
7475
//
@@ -78,14 +79,14 @@ int run_until(
7879
// - -2 if timed out. Nothing triggered, and command did not finish.
7980
//
8081
int run_until_with_time_limit(
81-
ProgramEnvironment& env, ConsoleHandle& console, BotBaseContext& context,
82+
ProgramEnvironment& env, VideoStream& stream, BotBaseContext& context,
8283
WallClock deadline,
8384
std::function<void(BotBaseContext& context)>&& command,
8485
const std::vector<PeriodicInferenceCallback>& callbacks,
8586
std::chrono::milliseconds default_video_period,
8687
std::chrono::milliseconds default_audio_period
8788
);
88-
89+
#endif
8990

9091

9192

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceSession.cpp

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#include "Common/Cpp/Exceptions.h"
8-
#include "CommonFramework/Tools/ConsoleHandle.h"
8+
#include "CommonFramework/Tools/VideoStream.h"
99
#include "InferenceCallback.h"
1010
#include "VisualInferenceCallback.h"
1111
#include "AudioInferenceCallback.h"
@@ -16,62 +16,14 @@
1616
namespace PokemonAutomation{
1717

1818

19-
20-
21-
#if 0
22-
InferenceSession::InferenceSession(
23-
Cancellable& scope, ConsoleHandle& console,
24-
const std::vector<InferenceCallback*>& callbacks,
25-
std::chrono::milliseconds default_video_period,
26-
std::chrono::milliseconds default_audio_period
27-
)
28-
: m_console(console)
29-
, m_overlays(console.overlay())
30-
, m_triggered(nullptr)
31-
{
32-
try{
33-
for (size_t c = 0; c < callbacks.size(); c++){
34-
InferenceCallback* callback = callbacks[c];
35-
if (callback == nullptr){
36-
continue;
37-
}
38-
if (!m_map.emplace(callback, c).second){
39-
throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Attempted to add the same callback twice.");
40-
}
41-
switch (callback->type()){
42-
case InferenceType::VISUAL:{
43-
VisualInferenceCallback& visual_callback = static_cast<VisualInferenceCallback&>(*callback);
44-
console.video_inference_pivot().add_callback(
45-
scope, &m_triggered,
46-
visual_callback,
47-
default_video_period
48-
);
49-
visual_callback.make_overlays(m_overlays);
50-
break;
51-
}
52-
case InferenceType::AUDIO:
53-
console.audio_inference_pivot().add_callback(
54-
scope, &m_triggered,
55-
static_cast<AudioInferenceCallback&>(*callback),
56-
default_audio_period
57-
);
58-
break;
59-
}
60-
}
61-
}catch (...){
62-
clear();
63-
throw;
64-
}
65-
}
66-
#endif
6719
InferenceSession::InferenceSession(
68-
Cancellable& scope, ConsoleHandle& console,
20+
Cancellable& scope, VideoStream& stream,
6921
const std::vector<PeriodicInferenceCallback>& callbacks,
7022
std::chrono::milliseconds default_video_period,
7123
std::chrono::milliseconds default_audio_period
7224
)
73-
: m_console(console)
74-
, m_overlays(console.overlay())
25+
: m_stream(stream)
26+
, m_overlays(stream.overlay())
7527
, m_triggered(nullptr)
7628
{
7729
try{
@@ -81,12 +33,12 @@ InferenceSession::InferenceSession(
8133
continue;
8234
}
8335
if (!m_map.emplace(callback.callback, c).second){
84-
throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Attempted to add the same callback twice.");
36+
throw InternalProgramError(&stream.logger(), PA_CURRENT_FUNCTION, "Attempted to add the same callback twice.");
8537
}
8638
switch (callback.callback->type()){
8739
case InferenceType::VISUAL:{
8840
VisualInferenceCallback& visual_callback = static_cast<VisualInferenceCallback&>(*callback.callback);
89-
console.video_inference_pivot().add_callback(
41+
stream.video_inference_pivot().add_callback(
9042
scope, &m_triggered,
9143
visual_callback,
9244
callback.period > std::chrono::milliseconds(0) ? callback.period : default_video_period
@@ -95,7 +47,7 @@ InferenceSession::InferenceSession(
9547
break;
9648
}
9749
case InferenceType::AUDIO:
98-
console.audio_inference_pivot().add_callback(
50+
stream.audio_inference_pivot().add_callback(
9951
scope, &m_triggered,
10052
static_cast<AudioInferenceCallback&>(*callback.callback),
10153
callback.period > std::chrono::milliseconds(0) ? callback.period : default_audio_period
@@ -131,16 +83,16 @@ void InferenceSession::clear() noexcept{
13183
for (auto& item : m_map){
13284
switch (item.first->type()){
13385
case InferenceType::VISUAL:{
134-
StatAccumulatorI32 stats = m_console.video_inference_pivot().remove_callback(static_cast<VisualInferenceCallback&>(*item.first));
86+
StatAccumulatorI32 stats = m_stream.video_inference_pivot().remove_callback(static_cast<VisualInferenceCallback&>(*item.first));
13587
try{
136-
stats.log(m_console, item.first->label(), UNITS, DIVIDER);
88+
stats.log(m_stream.logger(), item.first->label(), UNITS, DIVIDER);
13789
}catch (...){}
13890
break;
13991
}
14092
case InferenceType::AUDIO:{
141-
StatAccumulatorI32 stats = m_console.audio_inference_pivot().remove_callback(static_cast<AudioInferenceCallback&>(*item.first));
93+
StatAccumulatorI32 stats = m_stream.audio_inference_pivot().remove_callback(static_cast<AudioInferenceCallback&>(*item.first));
14294
try{
143-
stats.log(m_console, item.first->label(), UNITS, DIVIDER);
95+
stats.log(m_stream.logger(), item.first->label(), UNITS, DIVIDER);
14496
}catch (...){}
14597
break;
14698
}

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceSession.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace PokemonAutomation{
1818

1919
class Cancellable;
20-
class ConsoleHandle;
20+
class VideoStream;
2121

2222

2323

@@ -40,16 +40,8 @@ class ConsoleHandle;
4040
//
4141
class InferenceSession{
4242
public:
43-
#if 0
4443
InferenceSession(
45-
Cancellable& scope, ConsoleHandle& console,
46-
const std::vector<InferenceCallback*>& callbacks,
47-
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
48-
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
49-
);
50-
#endif
51-
InferenceSession(
52-
Cancellable& scope, ConsoleHandle& console,
44+
Cancellable& scope, VideoStream& stream,
5345
const std::vector<PeriodicInferenceCallback>& callbacks,
5446
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
5547
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
@@ -65,7 +57,7 @@ class InferenceSession{
6557

6658

6759
private:
68-
ConsoleHandle& m_console;
60+
VideoStream& m_stream;
6961
VideoOverlaySet m_overlays;
7062
std::map<InferenceCallback*, size_t> m_map;
7163
std::atomic<InferenceCallback*> m_triggered;

0 commit comments

Comments
 (0)