Skip to content

Commit cbb7424

Browse files
committed
missing file
1 parent 9fd132e commit cbb7424

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#include <QEventLoop>
88
#include "Common/Cpp/Time.h"
99
//#include "CommonFramework/Logging/Logger.h"
10+
#include "CommonFramework/GlobalSettingsPanel.h"
1011
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
1112
#include "SysbotBase_Connection.h"
1213

13-
//#include <iostream>
14-
//using std::cout;
15-
//using std::endl;
14+
// REMOVE
15+
#include <iostream>
16+
using std::cout;
17+
using std::endl;
1618

1719
namespace PokemonAutomation{
1820
namespace SysbotBase{
@@ -171,38 +173,85 @@ void TcpSysbotBase_Connection::on_connect_finished(const std::string& error_mess
171173

172174
write_data("configure echoCommands 0\r\n");
173175
write_data("getVersion\r\n");
174-
write_data("configure mainLoopSleepTime 0\r\n");
175176

176-
m_thread = std::thread(&TcpSysbotBase_Connection::thread_loop, this);
177+
// m_thread = std::thread(&TcpSysbotBase_Connection::thread_loop, this);
177178

178179
// set_status_line0(m_version);
179180

180-
declare_ready(controller_mode_status());
181+
// declare_ready(controller_mode_status());
181182
}catch (...){}
182183
}
183184
void TcpSysbotBase_Connection::on_receive_data(const void* data, size_t bytes){
185+
// cout << "on_receive_data(): " << std::string((const char*)data, bytes - 2) << endl;
186+
184187
WallClock now = current_time();
185188
{
186189
std::lock_guard<std::mutex> lg(m_lock);
187190
m_last_receive = now;
188191
}
192+
193+
189194
try{
190-
// cout << "sys-botbase Response: " << std::string((const char*)data, bytes) << endl;
191-
m_listeners.run_method_unique(&Listener::on_receive_data, data, bytes);
192-
193-
// Version #
194-
std::string str((const char*)data, bytes);
195-
if (str.find('.') != std::string::npos){
196-
while (!str.empty() && str.back() <= 32){
197-
str.pop_back();
195+
const char* ptr = (const char*)data;
196+
for (size_t c = 0; c < bytes; c++){
197+
char ch = ptr[c];
198+
if (ch == '\r'){
199+
continue;
200+
}
201+
if (ch != '\n'){
202+
m_receive_buffer.emplace_back(ch);
203+
continue;
198204
}
199-
set_status_line0("sys-botbase: Version " + str, COLOR_BLUE);
205+
process_message(
206+
std::string(
207+
m_receive_buffer.begin(),
208+
m_receive_buffer.end()
209+
)
210+
);
211+
m_receive_buffer.clear();
200212
}
201213

214+
// m_listeners.run_method_unique(&Listener::on_receive_data, data, bytes);
215+
202216
}catch (...){}
203217
}
218+
void TcpSysbotBase_Connection::process_message(const std::string& message){
219+
// cout << "sys-botbase Response: " << message << endl;
220+
221+
m_listeners.run_method_unique(&Listener::on_message, message);
222+
223+
// Version #
224+
std::string str = message;
225+
if (str.find('.') != std::string::npos){
226+
while (!str.empty() && str.back() <= 32){
227+
str.pop_back();
228+
}
229+
set_status_line0("sys-botbase: Version " + str, COLOR_BLUE);
204230

231+
std::lock_guard<std::mutex> lg(m_lock);
232+
if (!m_thread.joinable()){
233+
set_mode(str);
234+
}
235+
}
205236

237+
}
238+
void TcpSysbotBase_Connection::set_mode(const std::string& sbb_version){
239+
if (sbb_version.rfind("2.", 0) == 0){
240+
m_logger.log("Detected sbb2. Using old (slow) command set.", COLOR_ORANGE);
241+
write_data("configure mainLoopSleepTime 0\r\n");
242+
m_supports_command_queue = false;
243+
}else if (PreloadSettings::instance().DEVELOPER_MODE && sbb_version.rfind("3.", 0) == 0){
244+
m_logger.log("Detected sbb3. Using CC command queue.", COLOR_BLUE);
245+
write_data("configure enablePA 1\r\n");
246+
m_supports_command_queue = true;
247+
}else{
248+
m_logger.log("Unrecognized sbb version: " + sbb_version, COLOR_RED);
249+
return;
250+
}
251+
252+
m_thread = std::thread(&TcpSysbotBase_Connection::thread_loop, this);
253+
declare_ready(controller_mode_status());
254+
}
206255

207256

208257

0 commit comments

Comments
 (0)