Skip to content

Commit b5c2e2d

Browse files
authored
Merge branch 'PokemonAutomation:main' into gba-rg35xxsp-test
2 parents 347a94f + a585b21 commit b5c2e2d

File tree

12 files changed

+240
-139
lines changed

12 files changed

+240
-139
lines changed

Common/Cpp/Concurrency/ScheduledTaskRunner.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,43 @@ ScheduledTaskRunner::~ScheduledTaskRunner(){
2020
// ScheduledTaskRunner::cancel(nullptr);
2121
{
2222
std::lock_guard<std::mutex> lg(m_lock);
23+
// cout << "ScheduledTaskRunner: (Destructor - start): " << this << endl;
2324
m_stopped = true;
2425
m_cv.notify_all();
2526
}
2627
m_runner.reset();
28+
// cout << "ScheduledTaskRunner: (Destructor - end): " << this << endl;
2729
}
2830
ScheduledTaskRunner::ScheduledTaskRunner(AsyncDispatcher& dispatcher)
2931
: m_stopped(false)
3032
, m_runner(dispatcher.dispatch([this]{ thread_loop(); }))
31-
{}
33+
{
34+
// cout << "ScheduledTaskRunner: (Constructor): " << this << endl;
35+
}
3236
size_t ScheduledTaskRunner::size() const{
3337
std::lock_guard<std::mutex> lg(m_lock);
3438
return m_schedule.size();
3539
}
3640
WallClock ScheduledTaskRunner::next_event() const{
3741
std::lock_guard<std::mutex> lg(m_lock);
38-
if (m_schedule.empty()){
42+
if (m_stopped || m_schedule.empty()){
3943
return WallClock::max();
4044
}
4145
return m_schedule.begin()->first;
4246
}
4347
void ScheduledTaskRunner::add_event(WallClock time, std::function<void()> callback){
4448
std::lock_guard<std::mutex> lg(m_lock);
49+
if (m_stopped){
50+
return;
51+
}
4552
m_schedule.emplace(time, std::move(callback));
4653
m_cv.notify_all();
4754
}
4855
void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, std::function<void()> callback){
4956
std::lock_guard<std::mutex> lg(m_lock);
57+
if (m_stopped){
58+
return;
59+
}
5060
m_schedule.emplace(current_time() + time_from_now, std::move(callback));
5161
m_cv.notify_all();
5262
}
@@ -62,6 +72,7 @@ bool ScheduledTaskRunner::cancel(std::exception_ptr exception) noexcept{
6272
#endif
6373
void ScheduledTaskRunner::thread_loop(){
6474
std::unique_lock<std::mutex> lg(m_lock);
75+
// cout << "ScheduledTaskRunner: (Starting thread loop): " << this << endl;
6576
// WallClock last_check_timestamp = current_time();
6677
while (!m_stopped){
6778
#if 0
@@ -71,7 +82,9 @@ void ScheduledTaskRunner::thread_loop(){
7182
#endif
7283

7384
if (m_schedule.empty()){
85+
// cout << "ScheduledTaskRunner: (Sleeping): " << this << endl;
7486
m_cv.wait(lg);
87+
// cout << "ScheduledTaskRunner: (Waking): " << this << endl;
7588
continue;
7689
}
7790

@@ -81,11 +94,15 @@ void ScheduledTaskRunner::thread_loop(){
8194
m_cv.wait_until(lg, item->first);
8295
continue;
8396
}
97+
// cout << "ScheduledTaskRunner: (task - start): " << this << endl;
8498
lg.unlock();
8599
item->second();
86100
lg.lock();
101+
// cout << "ScheduledTaskRunner: (task - end): " << this << endl;
87102
m_schedule.erase(item);
88103
}
104+
// cout << "ScheduledTaskRunner: (Clearing schedule): " << this << endl;
105+
m_schedule.clear();
89106
}
90107

91108

SerialPrograms/Source/CommonFramework/ErrorReports/ErrorReports.cpp

Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#include "Common/Cpp/PrettyPrint.h"
1111
#include "Common/Cpp/Json/JsonArray.h"
1212
#include "Common/Cpp/Json/JsonObject.h"
13+
#include "Common/Cpp/Concurrency/AsyncDispatcher.h"
1314
#include "CommonFramework/Globals.h"
1415
#include "CommonFramework/GlobalSettingsPanel.h"
16+
#include "CommonFramework/GlobalServices.h"
1517
#include "CommonFramework/Logging/Logger.h"
1618
#include "CommonFramework/Notifications/ProgramNotifications.h"
1719
#include "CommonFramework/Environment/Environment.h"
@@ -165,56 +167,63 @@ SendableErrorReport::SendableErrorReport(std::string directory)
165167
JsonValue json = load_json_file(m_directory + "Report.json");
166168
const JsonObject& obj = json.to_object_throw();
167169
m_timestamp = obj.get_string_throw("Timestamp");
168-
m_processor = obj.get_string_throw("Processor");
169-
m_program = obj.get_string_throw("Program");
170-
m_program_id = obj.get_string_throw("ProgramID");
171-
m_program_runtime_millis = obj.get_integer_throw("ElapsedTimeMillis");
172-
m_title = obj.get_string_throw("Title");
173-
{
174-
const JsonArray& messages = obj.get_array_throw("Messages");
175-
for (const JsonValue& message : messages){
176-
const JsonArray& item = message.to_array_throw();
177-
if (item.size() != 2){
178-
throw ParseException("Expected 2 values for message.");
170+
171+
// If we error from this point on, we'll just move it to the sent folder.
172+
try{
173+
m_processor = obj.get_string_throw("Processor");
174+
m_program = obj.get_string_throw("Program");
175+
m_program_id = obj.get_string_throw("ProgramID");
176+
m_program_runtime_millis = obj.get_integer_throw("ElapsedTimeMillis");
177+
m_title = obj.get_string_throw("Title");
178+
{
179+
const JsonArray& messages = obj.get_array_throw("Messages");
180+
for (const JsonValue& message : messages){
181+
const JsonArray& item = message.to_array_throw();
182+
if (item.size() != 2){
183+
throw ParseException("Expected 2 values for message.");
184+
}
185+
m_messages.emplace_back(
186+
item[0].to_string_throw(),
187+
item[1].to_string_throw()
188+
);
179189
}
180-
m_messages.emplace_back(
181-
item[0].to_string_throw(),
182-
item[1].to_string_throw()
183-
);
184190
}
185-
}
186-
{
187-
const std::string* image_name = obj.get_string("Screenshot");
188-
if (image_name){
189-
try{
190-
m_image_owner = ImageRGB32(m_directory + *image_name);
191-
m_image = m_image_owner;
192-
}catch (FileException&){}
191+
{
192+
const std::string* image_name = obj.get_string("Screenshot");
193+
if (image_name){
194+
try{
195+
m_image_owner = ImageRGB32(m_directory + *image_name);
196+
m_image = m_image_owner;
197+
}catch (FileException&){}
198+
}
193199
}
194-
}
195-
{
196-
const std::string* video_name = obj.get_string("Video");
197-
if (video_name){
198-
m_video_name = *video_name;
200+
{
201+
const std::string* video_name = obj.get_string("Video");
202+
if (video_name){
203+
m_video_name = *video_name;
204+
}
199205
}
200-
}
201-
{
202-
const std::string* dump_name = obj.get_string("Dump");
203-
if (dump_name){
204-
m_dump_name = *dump_name;
206+
{
207+
const std::string* dump_name = obj.get_string("Dump");
208+
if (dump_name){
209+
m_dump_name = *dump_name;
210+
}
205211
}
206-
}
207-
{
208-
const std::string* logs_name = obj.get_string("Logs");
209-
if (logs_name){
210-
m_logs_name = *logs_name;
212+
{
213+
const std::string* logs_name = obj.get_string("Logs");
214+
if (logs_name){
215+
m_logs_name = *logs_name;
216+
}
211217
}
212-
}
213-
{
214-
const JsonArray& files = obj.get_array_throw("Files");
215-
for (const JsonValue& file : files){
216-
m_files.emplace_back(file.to_string_throw());
218+
{
219+
const JsonArray& files = obj.get_array_throw("Files");
220+
for (const JsonValue& file : files){
221+
m_files.emplace_back(file.to_string_throw());
222+
}
217223
}
224+
}catch (...){
225+
move_to_sent();
226+
throw;
218227
}
219228
}
220229
void SendableErrorReport::add_file(std::string filename){
@@ -270,23 +279,22 @@ void SendableErrorReport::save(Logger* logger) const{
270279
report.dump(m_directory + "Report.json");
271280
}
272281

273-
#ifndef PA_OFFICIAL
274-
bool SendableErrorReport::send(Logger& logger){
275-
return false;
276-
}
277-
#endif
278-
279282
void SendableErrorReport::move_to_sent(){
280283
// cout << "move_to_sent()" << endl;
281284
QDir().mkdir(QString::fromStdString(ERROR_PATH_SENT));
282285

283286
std::string new_directory = ERROR_PATH_SENT + "/" + m_timestamp + "/";
284287
// cout << "old: " << m_directory << endl;
285288
// cout << "new: " << new_directory << endl;
286-
QDir().rename(
289+
bool success = QDir().rename(
287290
QString::fromStdString(m_directory),
288291
QString::fromStdString(new_directory)
289292
);
293+
if (success){
294+
global_logger_tagged().log("Moved error report " + m_timestamp + ".");
295+
}else{
296+
global_logger_tagged().log("Unable to move error report " + m_timestamp + ".", COLOR_RED);
297+
}
290298
m_directory = std::move(new_directory);
291299
}
292300

@@ -306,37 +314,46 @@ std::vector<std::string> SendableErrorReport::get_pending_reports(){
306314
return ret;
307315
}
308316

317+
#ifndef PA_OFFICIAL
318+
void SendableErrorReport::send(Logger& logger, std::shared_ptr<SendableErrorReport> report){}
319+
#endif
320+
309321

310322

323+
// Send all the reports. This function will return early and all the reports
324+
// will be sent asynchronously in the background.
311325
void send_reports(Logger& logger, const std::vector<std::string>& reports){
312326
for (const std::string& path : reports){
313327
try{
314-
SendableErrorReport report(path);
315-
report.send(logger);
328+
// static int c = 0;
329+
// cout << "Sending... " << c++ << endl;
330+
// std:::shared_ptr because it needs to take ownership for
331+
// destruction at a later time.
332+
SendableErrorReport::send(logger, std::make_shared<SendableErrorReport>(path));
316333
}catch (Exception& e){
317334
logger.log("Unable to send report: " + path + ", Message: " + e.to_str(), COLOR_RED);
318335
}catch (...){
319336
logger.log("Unable to send report: " + path, COLOR_RED);
320337
}
321338
}
322339
}
323-
void send_all_unsent_reports(Logger& logger, bool allow_prompt){
340+
std::unique_ptr<AsyncTask> send_all_unsent_reports(Logger& logger, bool allow_prompt){
324341
#ifdef PA_OFFICIAL
325342
ErrorReportSendMode mode = GlobalSettings::instance().ERROR_REPORTS->SEND_MODE;
326343
if (mode == ErrorReportSendMode::NEVER_SEND_ANYTHING){
327-
return;
344+
return nullptr;
328345
}
329346

330347
std::vector<std::string> reports = SendableErrorReport::get_pending_reports();
331348
global_logger_tagged().log("Found " + std::to_string(reports.size()) + " unsent error reports.", COLOR_PURPLE);
332349

333350
if (reports.empty()){
334-
return;
351+
return nullptr;
335352
}
336353

337354
if (mode == ErrorReportSendMode::PROMPT_WHEN_CONVENIENT){
338355
if (!allow_prompt){
339-
return;
356+
return nullptr;
340357
}
341358
QMessageBox box;
342359
QMessageBox::StandardButton button = box.information(
@@ -356,13 +373,17 @@ void send_all_unsent_reports(Logger& logger, bool allow_prompt){
356373
QMessageBox::StandardButton::Yes
357374
);
358375
if (button != QMessageBox::StandardButton::Yes){
359-
return;
376+
return nullptr;
360377
}
361378
}
362379

363380
global_logger_tagged().log("Attempting to send " + std::to_string(reports.size()) + " error reports.", COLOR_PURPLE);
364-
send_reports(global_logger_tagged(), reports);
365381

382+
return global_async_dispatcher().dispatch([reports = std::move(reports)]{
383+
send_reports(global_logger_tagged(), reports);
384+
});
385+
#else
386+
return nullptr;
366387
#endif
367388
}
368389

SerialPrograms/Source/CommonFramework/ErrorReports/ErrorReports.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef PokemonAutomation_ErrorReports_H
88
#define PokemonAutomation_ErrorReports_H
99

10+
#include <memory>
1011
#include "Common/Cpp/AbstractLogger.h"
1112
#include "Common/Cpp/Options/GroupOption.h"
1213
#include "Common/Cpp/Options/StaticTextOption.h"
@@ -19,6 +20,7 @@
1920
namespace PokemonAutomation{
2021

2122

23+
class AsyncTask;
2224
class ConsoleHandle;
2325

2426

@@ -76,10 +78,10 @@ class SendableErrorReport{
7678
void add_file(std::string filename);
7779

7880
void save(Logger* logger) const;
79-
bool send(Logger& logger);
8081
void move_to_sent();
8182

8283
static std::vector<std::string> get_pending_reports();
84+
static void send(Logger& logger, std::shared_ptr<SendableErrorReport> report);
8385

8486
private:
8587
std::string m_timestamp;
@@ -100,7 +102,7 @@ class SendableErrorReport{
100102

101103

102104
void send_reports(Logger& logger, const std::vector<std::string>& reports);
103-
void send_all_unsent_reports(Logger& logger, bool allow_prompt);
105+
std::unique_ptr<AsyncTask> send_all_unsent_reports(Logger& logger, bool allow_prompt);
104106

105107

106108
void report_error(

SerialPrograms/Source/CommonFramework/GlobalServices.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
*
55
*/
66

7-
//#include "Common/Cpp/Concurrency/ScheduledTaskRunner.h"
7+
#include "Common/Cpp/Concurrency/ScheduledTaskRunner.h"
88
#include "Common/Cpp/Concurrency/Watchdog.h"
99
#include "GlobalServices.h"
1010

1111
namespace PokemonAutomation{
1212

1313

14-
#if 0
1514
AsyncDispatcher& global_async_dispatcher(){
1615
static AsyncDispatcher dispatcher(nullptr, 1);
1716
return dispatcher;
1817
}
18+
#if 0
1919
ScheduledTaskRunner& global_scheduled_task_runner(){
2020
static ScheduledTaskRunner runner(global_async_dispatcher());
2121
return runner;

SerialPrograms/Source/CommonFramework/GlobalServices.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ScheduledTaskRunner;
1414
class Watchdog;
1515

1616

17-
//AsyncDispatcher& global_async_dispatcher();
17+
AsyncDispatcher& global_async_dispatcher();
1818
//ScheduledTaskRunner& global_scheduled_task_runner();
1919
Watchdog& global_watchdog();
2020

SerialPrograms/Source/CommonFramework/Inference/BlackScreenDetector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class BlackScreenWatcher : public DetectorToFinder<BlackScreenDetector>{
6666
double max_rgb_sum = 100,
6767
double max_stddev_sum = 10,
6868
FinderType finder_type = FinderType::PRESENT,
69-
std::chrono::milliseconds duration = std::chrono::milliseconds(250)
69+
std::chrono::milliseconds duration = std::chrono::milliseconds(100)
7070
)
7171
: DetectorToFinder("BlackScreenWatcher", finder_type, duration, color, box, max_rgb_sum, max_stddev_sum)
7272
{}
@@ -80,8 +80,8 @@ class BlackScreenOverWatcher : public VisualInferenceCallback{
8080
const ImageFloatBox& box = {0.1, 0.1, 0.8, 0.8},
8181
double max_rgb_sum = 100,
8282
double max_stddev_sum = 10,
83-
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250),
84-
std::chrono::milliseconds release_duration = std::chrono::milliseconds(250)
83+
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(100),
84+
std::chrono::milliseconds release_duration = std::chrono::milliseconds(100)
8585
);
8686

8787
bool black_is_over(const ImageViewRGB32& frame);

0 commit comments

Comments
 (0)