Skip to content

Commit 55d1c47

Browse files
committed
Fix SwSh eggs again. Throttle logging so program doesn't freeze is serial spams messages.
1 parent fd743a8 commit 55d1c47

File tree

7 files changed

+67
-6
lines changed

7 files changed

+67
-6
lines changed

ClientSource/Connection/MessageLogger.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include "Common/Cpp/PrettyPrint.h"
78
#include "Common/SerialPABotBase/SerialPABotBase_Protocol.h"
89
#include "ClientSource/Libraries/MessageConverter.h"
910
#include "BotBaseMessage.h"
@@ -83,17 +84,45 @@ void MessageLogger::on_recv(const BotBaseMessage& message){
8384
SerialLogger::SerialLogger(Logger& logger, bool log_everything)
8485
: MessageLogger(log_everything)
8586
, m_logger(logger)
87+
, m_history(1000)
8688
{}
8789
void SerialLogger::log(const char* msg, Color color){
88-
m_logger.log(msg, color);
90+
if (ok_to_log()){
91+
m_logger.log(msg, color);
92+
}
8993
}
9094
void SerialLogger::log(const std::string& msg, Color color){
91-
m_logger.log(msg, color);
95+
if (ok_to_log()){
96+
m_logger.log(msg, color);
97+
}
9298
}
9399
void SerialLogger::log(std::string msg){
94-
m_logger.log(msg, COLOR_DARKGREEN);
100+
if (ok_to_log()){
101+
m_logger.log(msg, COLOR_DARKGREEN);
102+
}
95103
}
96104

105+
bool SerialLogger::ok_to_log(){
106+
WallClock now = current_time();
107+
WriteSpinLock lg(m_lock);
108+
while (!m_history.empty()){
109+
if (now - m_history.front() < std::chrono::seconds(1)){
110+
break;
111+
}
112+
m_history.pop_front();
113+
}
114+
if (!m_history.try_push_back(now)){
115+
m_messages_dropped++;
116+
return false;
117+
}
118+
119+
if (m_messages_dropped != 0){
120+
m_logger.log("Dropped " + tostr_u_commas(m_messages_dropped) + " message(s) due to logging rate limit.", COLOR_RED);
121+
m_messages_dropped = 0;
122+
}
123+
124+
return true;
125+
}
97126

98127

99128

ClientSource/Connection/MessageLogger.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <atomic>
1111
#include "Common/Cpp/AbstractLogger.h"
12+
#include "Common/Cpp/Time.h"
13+
#include "Common/Cpp/Containers/CircularBuffer.h"
14+
#include "Common/Cpp/Concurrency/SpinLock.h"
1215
#include "MessageSniffer.h"
1316

1417
namespace PokemonAutomation{
@@ -45,8 +48,14 @@ class SerialLogger : public Logger, public MessageLogger{
4548
virtual void log(const std::string& msg, Color color = Color()) override;
4649
virtual void log(std::string msg) override;
4750

51+
private:
52+
bool ok_to_log();
53+
4854
private:
4955
Logger& m_logger;
56+
SpinLock m_lock;
57+
size_t m_messages_dropped = 0;
58+
CircularBuffer<WallClock> m_history;
5059
};
5160

5261

Common/Cpp/Concurrency/AsyncDispatcher.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Common/Cpp/PanicDump.h"
88
#include "AsyncDispatcher.h"
99

10+
//#include <Windows.h>
1011
//#include <iostream>
1112
//using std::cout;
1213
//using std::endl;
@@ -62,6 +63,7 @@ AsyncDispatcher::~AsyncDispatcher(){
6263
m_cv.notify_all();
6364
}
6465
for (std::thread& thread : m_threads){
66+
// cout << "AsyncDispatcher::~AsyncDispatcher() joining = " << thread.get_id() << endl;
6567
thread.join();
6668
}
6769
for (AsyncTask* task : m_queue){
@@ -144,6 +146,7 @@ void AsyncDispatcher::run_in_parallel(
144146

145147

146148
void AsyncDispatcher::thread_loop(){
149+
// cout << "AsyncDispatcher::thread_loop() Start = " << GetCurrentThreadId() << ", threads = " << m_threads.size() << endl;
147150
if (m_new_thread_callback){
148151
m_new_thread_callback();
149152
}
@@ -158,6 +161,9 @@ void AsyncDispatcher::thread_loop(){
158161
}
159162

160163
if (m_stopping){
164+
// cout << "AsyncDispatcher::thread_loop() End (inside-start) = " << GetCurrentThreadId() << endl;
165+
// Sleep(10000);
166+
// cout << "AsyncDispatcher::thread_loop() End (inside-done) = " << GetCurrentThreadId() << endl;
161167
return;
162168
}
163169
if (m_queue.empty()){
@@ -185,6 +191,7 @@ void AsyncDispatcher::thread_loop(){
185191
}
186192
task->signal();
187193
}
194+
// cout << "AsyncDispatcher::thread_loop() End (outside) = " << GetCurrentThreadId() << endl;
188195
}
189196

190197

Common/Cpp/Containers/CircularBuffer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class CircularBuffer{
4242

4343
template <class... Args>
4444
Object& push_back(Args&&... args);
45+
template <class... Args>
46+
Object* try_push_back(Args&&... args);
4547
void pop_front();
4648

4749
private:
@@ -174,6 +176,20 @@ Object& CircularBuffer<Object>::push_back(Args&&... args){
174176
m_back++;
175177
return m_ptr[back];
176178
}
179+
template <typename Object>
180+
template <class... Args>
181+
Object* CircularBuffer<Object>::try_push_back(Args&&... args){
182+
if (m_back - m_front >= m_capacity){
183+
return nullptr;
184+
}
185+
size_t back = m_back;
186+
if (back >= m_capacity){
187+
back -= m_capacity;
188+
}
189+
new (m_ptr + back) Object(std::forward<Args>(args)...);
190+
m_back++;
191+
return &m_ptr[back];
192+
}
177193

178194
template <typename Object>
179195
void CircularBuffer<Object>::pop_front(){

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace PokemonAutomation{
2626
const bool IS_BETA_VERSION = true;
2727
const int PROGRAM_VERSION_MAJOR = 0;
2828
const int PROGRAM_VERSION_MINOR = 52;
29-
const int PROGRAM_VERSION_PATCH = 11;
29+
const int PROGRAM_VERSION_PATCH = 13;
3030

3131
const std::string PROGRAM_VERSION_BASE =
3232
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +

SerialPrograms/Source/PokemonSwSh/PokemonSwSh_Settings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ GameSettings::GameSettings()
213213
0.3, 0
214214
)
215215
, LINE_SPARKLE_ALPHA(
216-
"<b>Star Sparkle Alpha:</b>",
216+
"<b>Line Sparkle Alpha:</b>",
217217
LockMode::LOCK_WHILE_RUNNING,
218218
0.3, 0
219219
)

SerialPrograms/Source/PokemonSwSh/Programs/EggPrograms/PokemonSwSh_EggAutonomous.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ void EggAutonomous::mash_B_until_y_comm_icon(
869869
int ret = run_until<ProControllerContext>(
870870
env.console, context,
871871
[](ProControllerContext& context){
872-
pbf_mash_button(context, BUTTON_B, 1000ms);
872+
pbf_mash_button(context, BUTTON_B, 10s);
873873
},
874874
{y_comm_detector}
875875
);

0 commit comments

Comments
 (0)