Skip to content

Commit dbc5e30

Browse files
committed
Fix crash when editing item printer table. Fix date-spam missing date.
1 parent d1fb3ce commit dbc5e30

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Navigation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void home_to_date_time(ProControllerContext& context, bool to_date_change, bool
9595
return;
9696
}
9797

98-
ssf_press_button(context, BUTTON_A);
98+
ssf_press_button(context, BUTTON_A, 1000ms, timing_variation, timing_variation);
9999
ssf_issue_scroll(context, SSF_SCROLL_DOWN);
100100
ssf_issue_scroll(context, SSF_SCROLL_DOWN);
101101
}

SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNG.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,16 @@ ItemPrinterPrizeResult ItemPrinterRNG::run_print_at_date(
317317
ItemPrinterPrizeResult prize_result;
318318
bool printed = false;
319319
bool overworld_seen = false;
320+
size_t failures = 0;
321+
std::chrono::seconds next_wait_time = std::chrono::seconds(120);
320322
while (true){
323+
if (failures >= 5){
324+
OperationFailedException::fire(
325+
ErrorReport::SEND_ERROR_REPORT,
326+
"Unable to print after 5 attempts.",
327+
env.console
328+
);
329+
}
321330
context.wait_for_all_requests();
322331

323332
OverworldWatcher overworld(env.console, COLOR_BLUE);
@@ -326,7 +335,7 @@ ItemPrinterPrizeResult ItemPrinterRNG::run_print_at_date(
326335
DateChangeWatcher date_reader;
327336
WhiteButtonWatcher material(COLOR_GREEN, WhiteButton::ButtonX, {0.63, 0.93, 0.17, 0.06});
328337
int ret = wait_until(
329-
env.console, context, std::chrono::seconds(120),
338+
env.console, context, next_wait_time,
330339
{
331340
overworld,
332341
dialog,
@@ -335,6 +344,7 @@ ItemPrinterPrizeResult ItemPrinterRNG::run_print_at_date(
335344
material,
336345
}
337346
);
347+
next_wait_time = std::chrono::seconds(120);
338348
switch (ret){
339349
case 0:
340350
overworld_seen = true;
@@ -357,6 +367,7 @@ ItemPrinterPrizeResult ItemPrinterRNG::run_print_at_date(
357367
home_to_date_time(context, true, false);
358368
pbf_press_button(context, BUTTON_A, 10, 30);
359369
context.wait_for_all_requests();
370+
next_wait_time = std::chrono::seconds(5);
360371
continue;
361372
}
362373
case 3:{
@@ -431,13 +442,17 @@ ItemPrinterPrizeResult ItemPrinterRNG::run_print_at_date(
431442
continue;
432443
}
433444
default:
445+
failures++;
434446
stats.errors++;
435447
env.update_stats();
448+
env.console.log("No state detected after 2 minutes.", COLOR_RED);
449+
#if 0
436450
OperationFailedException::fire(
437451
ErrorReport::SEND_ERROR_REPORT,
438452
"No state detected after 2 minutes.",
439453
env.console
440454
);
455+
#endif
441456
}
442457
}
443458
}

SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNGTable.cpp

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "Common/Qt/TimeQt.h"
88
#include "PokemonSV_ItemPrinterRNGTable.h"
99

10+
//#include <iostream>
11+
//using std::cout;
12+
//using std::endl;
13+
1014
namespace PokemonAutomation{
1115
namespace NintendoSwitch{
1216
namespace PokemonSV{
@@ -79,29 +83,52 @@ std::unique_ptr<EditableTableRow> ItemPrinterRngRow::clone() const{
7983
// - also, hide the date if chain enabled.
8084
// - trigger the listener for the parent table.
8185
void ItemPrinterRngRow::value_changed(void* object){
86+
{
87+
WriteSpinLock lg1(m_pending_lock);
88+
m_pending.emplace_back(object);
89+
}
8290

83-
ItemPrinterRngTable& table = static_cast<ItemPrinterRngTable&>(parent());
91+
bool keep_going;
92+
do{
93+
std::unique_lock<std::mutex> lg(m_update_lock, std::try_to_lock_t());
94+
if (!lg.owns_lock()){
95+
return;
96+
}
8497

85-
if (object == &desired_item){
86-
ItemPrinter::PrebuiltOptions option = desired_item;
87-
if (option != ItemPrinter::PrebuiltOptions::NONE){
88-
const ItemPrinter::ItemPrinterEnumOption& option_data = option_lookup_by_enum(option);
89-
chain = false;
90-
date.set(from_seconds_since_epoch(option_data.seed));
91-
jobs.set(option_data.jobs);
98+
{
99+
WriteSpinLock lg1(m_pending_lock);
100+
object = m_pending.front();
101+
m_pending.pop_front();
92102
}
93-
}else if (object == &date || object == &jobs || object == &chain){
94-
date.set_visibility(chain ? ConfigOptionState::HIDDEN : ConfigOptionState::ENABLED);
95-
const ItemPrinter::ItemPrinterEnumOption* option_data = ItemPrinter::option_lookup_by_seed(to_seconds_since_epoch(date));
96-
// seed found in the PrebuiltOptions table, and jobs number matches and chain disabled
97-
if (option_data != nullptr && option_data->jobs == jobs && !chain){
98-
desired_item.set(option_data->enum_value);
99-
}else{
100-
desired_item.set(ItemPrinter::PrebuiltOptions::NONE);
103+
104+
ItemPrinterRngTable& table = static_cast<ItemPrinterRngTable&>(parent());
105+
106+
if (object == &desired_item){
107+
ItemPrinter::PrebuiltOptions option = desired_item;
108+
if (option != ItemPrinter::PrebuiltOptions::NONE){
109+
const ItemPrinter::ItemPrinterEnumOption& option_data = option_lookup_by_enum(option);
110+
chain = false;
111+
jobs.set(option_data.jobs);
112+
date.set(from_seconds_since_epoch(option_data.seed));
113+
}
114+
}else if (object == &date || object == &jobs || object == &chain){
115+
date.set_visibility(chain ? ConfigOptionState::HIDDEN : ConfigOptionState::ENABLED);
116+
const ItemPrinter::ItemPrinterEnumOption* option_data = ItemPrinter::option_lookup_by_seed(to_seconds_since_epoch(date));
117+
// cout << "option_data = " << option_data << endl;
118+
// seed found in the PrebuiltOptions table, and jobs number matches and chain disabled
119+
if (option_data != nullptr && option_data->jobs == jobs && !chain){
120+
desired_item.set(option_data->enum_value);
121+
}else{
122+
// cout << "option_data->jobs = " << (size_t)option_data->jobs << ", jobs = " << jobs.current_value() << ", chain = " << chain << endl;
123+
desired_item.set(ItemPrinter::PrebuiltOptions::NONE);
124+
}
101125
}
102-
}
103126

104-
table.report_value_changed(object);
127+
table.report_value_changed(object);
128+
129+
WriteSpinLock lg1(m_pending_lock);
130+
keep_going = !m_pending.empty();
131+
}while (keep_going);
105132
}
106133

107134
ItemPrinterRngTable::ItemPrinterRngTable(std::string label)

SerialPrograms/Source/PokemonSV/Programs/ItemPrinter/PokemonSV_ItemPrinterRNGTable.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef PokemonAutomation_PokemonSV_ItemPrinterRNGTable_H
88
#define PokemonAutomation_PokemonSV_ItemPrinterRNGTable_H
99

10+
#include <deque>
11+
#include <mutex>
1012
#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
1113
#include "Common/Cpp/Options/EnumDropdownOption.h"
1214
#include "Common/Cpp/Options/DateOption.h"
@@ -49,6 +51,11 @@ class ItemPrinterRngRow : public EditableTableRow, public ConfigOption::Listener
4951
DateTimeCell date;
5052
EnumDropdownCell<ItemPrinterJobs> jobs;
5153
EnumDropdownCell<ItemPrinter::PrebuiltOptions> desired_item;
54+
55+
private:
56+
SpinLock m_pending_lock;
57+
std::deque<void*> m_pending;
58+
std::mutex m_update_lock;
5259
};
5360

5461

0 commit comments

Comments
 (0)