|
7 | 7 | #include <QEventLoop> |
8 | 8 | #include "Common/Cpp/Time.h" |
9 | 9 | //#include "CommonFramework/Logging/Logger.h" |
| 10 | +#include "CommonFramework/GlobalSettingsPanel.h" |
10 | 11 | #include "CommonFramework/Options/Environment/ThemeSelectorOption.h" |
11 | 12 | #include "SysbotBase_Connection.h" |
12 | 13 |
|
13 | | -//#include <iostream> |
14 | | -//using std::cout; |
15 | | -//using std::endl; |
| 14 | +// REMOVE |
| 15 | +#include <iostream> |
| 16 | +using std::cout; |
| 17 | +using std::endl; |
16 | 18 |
|
17 | 19 | namespace PokemonAutomation{ |
18 | 20 | namespace SysbotBase{ |
@@ -171,38 +173,85 @@ void TcpSysbotBase_Connection::on_connect_finished(const std::string& error_mess |
171 | 173 |
|
172 | 174 | write_data("configure echoCommands 0\r\n"); |
173 | 175 | write_data("getVersion\r\n"); |
174 | | - write_data("configure mainLoopSleepTime 0\r\n"); |
175 | 176 |
|
176 | | - m_thread = std::thread(&TcpSysbotBase_Connection::thread_loop, this); |
| 177 | +// m_thread = std::thread(&TcpSysbotBase_Connection::thread_loop, this); |
177 | 178 |
|
178 | 179 | // set_status_line0(m_version); |
179 | 180 |
|
180 | | - declare_ready(controller_mode_status()); |
| 181 | +// declare_ready(controller_mode_status()); |
181 | 182 | }catch (...){} |
182 | 183 | } |
183 | 184 | 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 | + |
184 | 187 | WallClock now = current_time(); |
185 | 188 | { |
186 | 189 | std::lock_guard<std::mutex> lg(m_lock); |
187 | 190 | m_last_receive = now; |
188 | 191 | } |
| 192 | + |
| 193 | + |
189 | 194 | 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; |
198 | 204 | } |
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(); |
200 | 212 | } |
201 | 213 |
|
| 214 | +// m_listeners.run_method_unique(&Listener::on_receive_data, data, bytes); |
| 215 | + |
202 | 216 | }catch (...){} |
203 | 217 | } |
| 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); |
204 | 230 |
|
| 231 | + std::lock_guard<std::mutex> lg(m_lock); |
| 232 | + if (!m_thread.joinable()){ |
| 233 | + set_mode(str); |
| 234 | + } |
| 235 | + } |
205 | 236 |
|
| 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 | +} |
206 | 255 |
|
207 | 256 |
|
208 | 257 |
|
|
0 commit comments