diff --git a/3rdPartyBinaries/Sleepy.dll b/3rdPartyBinaries/Sleepy.dll deleted file mode 100644 index 3d7b601fed..0000000000 Binary files a/3rdPartyBinaries/Sleepy.dll and /dev/null differ diff --git a/3rdPartyBinaries/Sleepy.lib b/3rdPartyBinaries/Sleepy.lib deleted file mode 100644 index 8943da6e92..0000000000 Binary files a/3rdPartyBinaries/Sleepy.lib and /dev/null differ diff --git a/README.md b/README.md index 086ba6e3b3..3eb2aec8ef 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ This is the source code for all the main Arduino programs. | [QDarkStyleSheet](https://github.com/ColinDuquesnoy/QDarkStyleSheet) | MIT | | [Qt Wav Reader](https://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/spectrum/app/wavfile.cpp?h=5.15) | BSD | | [nlohmann json](https://github.com/nlohmann/json) | MIT | -| [Sleepy Discord](https://github.com/yourWaifu/sleepy-discord) | MIT | | [D++](https://github.com/brainboxdotcc/DPP) | Apache 2.0 | | [LUFA](https://github.com/abcminiuser/lufa) | MIT | | [Tesseract](https://github.com/tesseract-ocr/tesseract) | Apache 2.0 | diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 023f7141f6..2d0cd8fe51 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -711,8 +711,6 @@ file(GLOB MAIN_SOURCES Source/Integrations/IntegrationsAPI.h Source/Integrations/ProgramTracker.cpp Source/Integrations/ProgramTracker.h - Source/Integrations/SleepyDiscordRunner.cpp - Source/Integrations/SleepyDiscordRunner.h Source/Kernels/AbsFFT/Kernels_AbsFFT.cpp Source/Kernels/AbsFFT/Kernels_AbsFFT.h Source/Kernels/AbsFFT/Kernels_AbsFFT_Arch.h @@ -2462,7 +2460,6 @@ if (MSVC) OpenCV_lib ONNX_lib ONNX_Providers_lib - Sleepy.lib dpp_lib ) target_compile_definitions( @@ -2470,7 +2467,6 @@ if (MSVC) _CRT_SECURE_NO_WARNINGS _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR PA_TESSERACT - PA_SLEEPY PA_DPP ) diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 6765e5177b..f43b58048f 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -15,7 +15,6 @@ #include "Logging/Logger.h" #include "Logging/OutputRedirector.h" //#include "Tools/StatsDatabase.h" -#include "Integrations/SleepyDiscordRunner.h" #include "Globals.h" #include "GlobalSettingsPanel.h" //#include "Windows/DpiScaler.h" @@ -111,15 +110,8 @@ int main(int argc, char *argv[]){ Integration::DiscordIntegrationSettingsOption& discord_settings = GlobalSettings::instance().DISCORD->integration; if (discord_settings.run_on_start){ -#ifdef PA_SLEEPY - if (discord_settings.library0 == Integration::DiscordIntegrationSettingsOption::Library::SleepyDiscord){ - Integration::SleepyDiscordRunner::sleepy_connect(); - } -#endif #ifdef PA_DPP - if (discord_settings.library0 == Integration::DiscordIntegrationSettingsOption::Library::DPP){ - Integration::DppClient::Client::instance().connect(); - } + Integration::DppClient::Client::instance().connect(); #endif discord_settings.on_config_value_changed(nullptr); } @@ -144,9 +136,6 @@ int main(int argc, char *argv[]){ #ifdef PA_DPP Integration::DppClient::Client::instance().disconnect(); #endif -#ifdef PA_SLEEPY - Integration::SleepyDiscordRunner::sleepy_terminate(); -#endif return ret; } diff --git a/SerialPrograms/Source/CommonFramework/Notifications/ProgramNotifications.cpp b/SerialPrograms/Source/CommonFramework/Notifications/ProgramNotifications.cpp index 76aecde236..dc73cf51d2 100644 --- a/SerialPrograms/Source/CommonFramework/Notifications/ProgramNotifications.cpp +++ b/SerialPrograms/Source/CommonFramework/Notifications/ProgramNotifications.cpp @@ -15,7 +15,6 @@ #include "CommonFramework/Tools/ProgramEnvironment.h" #include "CommonFramework/ProgramStats/StatsTracking.h" #include "Integrations/DiscordWebhook.h" -#include "Integrations/SleepyDiscordRunner.h" #include "ProgramNotifications.h" //#include @@ -120,22 +119,11 @@ void send_raw_notification( hasFile ? file : nullptr ); -#ifdef PA_SLEEPY - if (GlobalSettings::instance().DISCORD->integration.library0 == Integration::DiscordIntegrationSettingsOption::Library::SleepyDiscord){ - Integration::SleepyDiscordRunner::send_embed_sleepy( - should_ping, tags, std::move(embed), - hasFile ? file : nullptr - ); - } -#endif - #ifdef PA_DPP - if (GlobalSettings::instance().DISCORD->integration.library0 == Integration::DiscordIntegrationSettingsOption::Library::DPP){ - Integration::DppClient::Client::instance().send_embed_dpp( - should_ping, color, tags, std::move(embed), - hasFile ? file : nullptr - ); - } + Integration::DppClient::Client::instance().send_embed_dpp( + should_ping, color, tags, std::move(embed), + hasFile ? file : nullptr + ); #endif } void send_raw_notification( @@ -173,22 +161,11 @@ void send_raw_notification( hasFile ? file : nullptr ); -#ifdef PA_SLEEPY - if (GlobalSettings::instance().DISCORD->integration.library0 == Integration::DiscordIntegrationSettingsOption::Library::SleepyDiscord){ - Integration::SleepyDiscordRunner::send_embed_sleepy( - should_ping, tags, std::move(embed), - hasFile ? file : nullptr - ); - } -#endif - #ifdef PA_DPP - if (GlobalSettings::instance().DISCORD->integration.library0 == Integration::DiscordIntegrationSettingsOption::Library::DPP){ - Integration::DppClient::Client::instance().send_embed_dpp( - should_ping, color, tags, std::move(embed), - hasFile ? file : nullptr - ); - } + Integration::DppClient::Client::instance().send_embed_dpp( + should_ping, color, tags, std::move(embed), + hasFile ? file : nullptr + ); #endif } diff --git a/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.cpp b/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.cpp index 4ed3c7223c..b2b538a435 100644 --- a/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.cpp +++ b/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.cpp @@ -11,7 +11,6 @@ //#include "CommonFramework/Globals.h" //#include "CommonFramework/GlobalSettingsPanel.h" #include "DppIntegration/DppClient.h" -#include "SleepyDiscordRunner.h" #include "DiscordIntegrationSettings.h" //#include @@ -23,7 +22,6 @@ namespace Integration{ DiscordIntegrationSettingsOption::~DiscordIntegrationSettingsOption(){ - library0.remove_listener(*this); this->remove_listener(*this); } DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption() @@ -38,15 +36,6 @@ DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption() LockMode::LOCK_WHILE_RUNNING, false ) - , library0( - "Discord Integration Library:
Restart the program for this to take effect.", - { - {Library::SleepyDiscord, "sleepy", "Sleepy Discord (normal commands, deprecated)"}, - {Library::DPP, "dpp", "D++ (slash commands and normal commands)"}, - }, - LockMode::LOCK_WHILE_RUNNING, - Library::DPP - ) , command_type( "Discord Integration Command Type:
Restart the program for this to take effect.", { @@ -68,11 +57,6 @@ DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption() LockMode::LOCK_WHILE_RUNNING, "^", "^" ) - , use_suffix( - "Use Suffix (Sleepy):
Use a suffix instead of a prefix for commands.", - LockMode::LOCK_WHILE_RUNNING, - false - ) , game_status( false, "Discord Game Status:
Enter a status message your bot would display.", @@ -85,18 +69,6 @@ DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption() LockMode::LOCK_WHILE_RUNNING, "", "Automation at your service!" ) - , sudo( - false, - "Discord Sudo (Sleepy):
Enter user ID(s) to grant sudo access to.", - LockMode::LOCK_WHILE_RUNNING, - "", "123456789012345678" - ) - , owner( - false, - "Discord Owner (Sleepy):
Enter the bot owner's ID (your own ID).", - LockMode::LOCK_WHILE_RUNNING, - "", "123456789012345678" - ) , allow_buttons_from_users( "Allow Buttons from Users:
Allow other users to issue button presses.", LockMode::LOCK_WHILE_RUNNING, @@ -104,68 +76,32 @@ DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption() ) { PA_ADD_OPTION(run_on_start); - PA_ADD_OPTION(library0); PA_ADD_OPTION(command_type); PA_ADD_OPTION(token); PA_ADD_OPTION(command_prefix); - PA_ADD_OPTION(use_suffix); PA_ADD_OPTION(game_status); PA_ADD_OPTION(hello_message); - PA_ADD_OPTION(sudo); - PA_ADD_OPTION(owner); PA_ADD_OPTION(allow_buttons_from_users); PA_ADD_OPTION(channels); DiscordIntegrationSettingsOption::on_config_value_changed(this); this->add_listener(*this); - library0.add_listener(*this); } void DiscordIntegrationSettingsOption::on_config_value_changed([[maybe_unused]] void* object){ // cout << this->enabled() << endl; -#if (defined PA_SLEEPY || defined PA_DPP) - bool options_enabled = this->enabled(); - switch (library0){ -#ifdef PA_SLEEPY - case Library::SleepyDiscord:{ - options_enabled &= !SleepyDiscordRunner::is_running(); - ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED; - - library0.set_visibility(state); - command_type.set_visibility(ConfigOptionState::HIDDEN); - token.set_visibility(state); - game_status.set_visibility(state); - hello_message.set_visibility(state); - - command_prefix.set_visibility(state); - use_suffix.set_visibility(state); - sudo.set_visibility(state); - owner.set_visibility(state); - allow_buttons_from_users.set_visibility(ConfigOptionState::HIDDEN); - break; - } -#endif #ifdef PA_DPP - case Library::DPP:{ - options_enabled &= !DppClient::Client::instance().is_initialized(); - ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED; - - library0.set_visibility(state); - command_type.set_visibility(state); - token.set_visibility(state); - game_status.set_visibility(state); - hello_message.set_visibility(state); - allow_buttons_from_users.set_visibility(state); - - command_prefix.set_visibility(state); - use_suffix.set_visibility(ConfigOptionState::HIDDEN); - sudo.set_visibility(ConfigOptionState::HIDDEN); - owner.set_visibility(ConfigOptionState::HIDDEN); - break; - } -#endif - default:; - } + bool options_enabled = this->enabled(); + options_enabled &= !DppClient::Client::instance().is_initialized(); + ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED; + + command_type.set_visibility(state); + token.set_visibility(state); + game_status.set_visibility(state); + hello_message.set_visibility(state); + allow_buttons_from_users.set_visibility(state); + + command_prefix.set_visibility(state); #endif } @@ -181,7 +117,7 @@ ConfigWidget* DiscordIntegrationSettingsOption::make_QtWidget(QWidget& parent){ DiscordIntegrationSettingsWidget::DiscordIntegrationSettingsWidget(QWidget& parent, DiscordIntegrationSettingsOption& value) : GroupWidget(parent, value) { -#if (defined PA_SLEEPY || defined PA_DPP) +#ifdef PA_DPP QWidget* control_buttons = new QWidget(this); m_options_layout->insertWidget(0, control_buttons); @@ -207,38 +143,14 @@ DiscordIntegrationSettingsWidget::DiscordIntegrationSettingsWidget(QWidget& pare connect( button_start, &QPushButton::clicked, this, [this, &value](bool){ - switch (value.library0){ -#ifdef PA_SLEEPY - case DiscordIntegrationSettingsOption::Library::SleepyDiscord: - SleepyDiscordRunner::sleepy_connect(); - break; -#endif -#ifdef PA_DPP - case DiscordIntegrationSettingsOption::Library::DPP: - DppClient::Client::instance().connect(); - break; -#endif - default:; - } + DppClient::Client::instance().connect(); value.on_config_value_changed(this); } ); connect( button_stop, &QPushButton::clicked, this, [this, &value](bool){ - switch (value.library0){ -#ifdef PA_SLEEPY - case DiscordIntegrationSettingsOption::Library::SleepyDiscord: - SleepyDiscordRunner::sleepy_terminate(); - break; -#endif -#ifdef PA_DPP - case DiscordIntegrationSettingsOption::Library::DPP: - DppClient::Client::instance().disconnect(); - break; -#endif - default:; - } + DppClient::Client::instance().disconnect(); value.on_config_value_changed(this); } ); diff --git a/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.h b/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.h index d488952e1b..4020189462 100644 --- a/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.h +++ b/SerialPrograms/Source/Integrations/DiscordIntegrationSettings.h @@ -29,12 +29,6 @@ class DiscordIntegrationSettingsOption : public GroupOption, private ConfigOptio BooleanCheckBoxOption run_on_start; - enum class Library{ - SleepyDiscord, - DPP, - }; - EnumDropdownOption library0; - enum class CommandType{ SlashCommands, MessageCommands, @@ -43,11 +37,8 @@ class DiscordIntegrationSettingsOption : public GroupOption, private ConfigOptio StringOption token; StringOption command_prefix; - BooleanCheckBoxOption use_suffix; StringOption game_status; StringOption hello_message; - StringOption sudo; - StringOption owner; BooleanCheckBoxOption allow_buttons_from_users; DiscordIntegrationTable channels; }; diff --git a/SerialPrograms/Source/Integrations/DiscordSettingsOption.cpp b/SerialPrograms/Source/Integrations/DiscordSettingsOption.cpp index 5106f542c5..2785a93fcd 100644 --- a/SerialPrograms/Source/Integrations/DiscordSettingsOption.cpp +++ b/SerialPrograms/Source/Integrations/DiscordSettingsOption.cpp @@ -70,7 +70,7 @@ DiscordSettingsOption::DiscordSettingsOption() { PA_ADD_OPTION(message); PA_ADD_OPTION(webhooks); -#if defined PA_SLEEPY || defined PA_DPP +#ifdef PA_DPP PA_ADD_OPTION(integration); #endif } diff --git a/SerialPrograms/Source/Integrations/SleepyDiscordRunner.cpp b/SerialPrograms/Source/Integrations/SleepyDiscordRunner.cpp deleted file mode 100644 index 2e3203f0ef..0000000000 --- a/SerialPrograms/Source/Integrations/SleepyDiscordRunner.cpp +++ /dev/null @@ -1,634 +0,0 @@ - -#ifdef PA_SLEEPY - -#include -#include -#include -#include -#include -#include "Common/Cpp/PrettyPrint.h" -#include "Common/Cpp/StringTools.h" -#include "Common/Cpp/PanicDump.h" -#include "Common/Cpp/Concurrency/ScheduledTaskRunner.h" -#include "Common/Qt/StringToolsQt.h" -#include "CommonFramework/Globals.h" -#include "CommonFramework/GlobalSettingsPanel.h" -#include "CommonFramework/Logging/Logger.h" -#include "CommonFramework/Notifications/EventNotificationOption.h" -#include "IntegrationsAPI.h" -#include "SleepyDiscordRunner.h" - -#include -using std::cout; -using std::endl; - -namespace PokemonAutomation{ -namespace Integration{ -namespace SleepyDiscordRunner{ - -using namespace SleepyDiscord; -const char* enum_str_callback[] = { - "Fault", - "API", - "Disconnected", - "Connected", - "Settings Initialized", - "Settings Updated", - "Commands Initialized", - "Callbacks Set", - "Invalid Command", - "Terminating", - "Remove File", -}; - -const char* enum_str_command[] = { - "Click", - "Click DPad", - - "Left Stick", - "Right Stick", - - "Screenshot JPG", - "Screenshot PNG", - "Start", - "Stop", - "Shutdown", - "Get Connected Bots", - "Reload Settings", - "Reset Camera", - "Reset Serial", - "Terminate", - - "Hi", - "Ping", - "About", - "Help", -}; - -const char* enum_str_state[] = { - "STOPPED", - "RUNNING", - "FINISHED", - "STOPPING", -}; - -std::unordered_map str_buttons = { - { 1, "Y" }, - { 2, "B" }, - { 4, "A" }, - { 8, "X" }, - { 16, "L" }, - { 32, "R" }, - { 64, "ZL" }, - { 128, "ZR" }, - { 256, "MINUS" }, - { 512, "PLUS" }, - { 1024, "LStick" }, - { 2048, "RStick" }, - { 4096, "HOME" }, - { 8192, "Capture" }, -}; - -std::unordered_map str_dpad = { - { 0, "DUp" }, - { 2, "DRight" }, - { 4, "DDown" }, - { 6, "DLeft" }, -}; - - -Logger& sleepy_logger(){ - static TaggedLogger logger(global_logger_raw(), "SleepyDiscord"); - return logger; -} - - -struct SleepyDiscordRequest{ - SleepyDiscordRequest() = default; - SleepyDiscordRequest(std::string embed, std::string channels, std::string messages, std::shared_ptr file) : - embed(std::move(embed)), - channels(std::move(channels)), - messages(std::move(messages)), - file(std::move(file)) {} - - std::string embed; - std::string channels; - std::string messages; - std::shared_ptr file; -}; - -class SleepyDiscordSender{ -private: - SleepyDiscordSender() -// : m_stopping(false) -// , m_thread(run_with_catch, "SleepyDiscordSender::thread_loop()", [this]{ thread_loop(); }) - : m_dispatcher(nullptr, 1) - , m_queue(m_dispatcher) - {} - ~SleepyDiscordSender(){ -// { -// std::lock_guard lg(m_lock); -// m_stopping = true; -// m_cv.notify_all(); -// } -// m_thread.join(); - } - -public: - static SleepyDiscordSender& instance(){ - static SleepyDiscordSender sender; - return sender; - } - - void send( - std::string embed, - std::string channels, - std::chrono::milliseconds delay, - std::string messages, - std::shared_ptr file - ){ -// std::lock_guard lg(m_lock); - m_queue.add_event( - delay > std::chrono::milliseconds(10000) ? std::chrono::milliseconds(0) : delay, - [embed = std::move(embed), channels = std::move(channels), messages = std::move(messages), file = std::move(file)]() mutable { - if (file == nullptr || file->filepath().empty()){ - sendMessage(&channels[0], &messages[0], &embed[0], nullptr); - }else{ - std::string filepath = file->filepath(); - sendMessage(&channels[0], &messages[0], &embed[0], &filepath[0]); - } - } - ); - sleepy_logger().log("Sending notification... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE); -// m_queue.emplace_back(embed, channels, messages, std::move(file)); -// m_cv.notify_all(); - - } - -private: -#if 0 - void thread_loop(){ - while (true){ - SleepyDiscordRequest item; - { - std::unique_lock lg(m_lock); - if (m_stopping){ - break; - } - if (m_queue.empty()){ - m_cv.wait(lg); - continue; - } - - item = std::move(m_queue.front()); - m_queue.pop_front(); - } - - if (item.file != nullptr && !item.file->filepath().empty()){ - std::string filepath = item.file->filepath(); - sendMessage(&item.channels[0], &item.messages[0], &item.embed[0], &filepath[0]); - }else sendMessage(&item.channels[0], &item.messages[0], &item.embed[0], nullptr); - } - } -#endif - -private: -// bool m_stopping; -// std::mutex m_lock; -// std::condition_variable m_cv; -// std::deque m_queue; -// std::thread m_thread; - AsyncDispatcher m_dispatcher; - ScheduledTaskRunner m_queue; -}; - -class SleepyDiscordClient; -std::unique_ptr m_sleepy_client; - -std::mutex m_connect_lock; -std::mutex m_client_lock; - - - - - -struct CommandArgs{ - uint64_t id; - uint16_t button; - uint16_t hold_ticks; - uint8_t x; - uint8_t y; -}; - - -class SleepyDiscordClient{ -public: - bool m_connected = false; - -public: - void send( - std::string embed, - std::string channels, - std::chrono::milliseconds delay, - std::string messages, - std::shared_ptr file - ){ - if (m_sleepy_client != nullptr){ - if (file){ -// cout << "Sending: " << file->filepath().toStdString() << endl; - m_active_list.emplace(file->filepath(), file); - } - SleepyDiscordSender::instance().send(embed, channels, delay, messages, std::move(file)); - }else{ - sleepy_logger().log("SleepyDiscordClient::send(): Not connected.", COLOR_RED); - } - } - - void callback(int response, const char* message){ - if (m_sleepy_client == nullptr){ - return; - } - - std::string msg = (std::string)message + " (Callback: " + (std::string)enum_str_callback[response] + ")"; - Color color = response == SleepyResponse::Disconnected || response == SleepyResponse::Fault ? COLOR_RED : COLOR_PURPLE; - - switch (response){ - case SleepyResponse::Connected: m_connected = true; break; - case SleepyResponse::Disconnected: m_connected = false; break; - case SleepyResponse::RemoveFile:{ -#if 1 - auto iter = m_active_list.find(message); -// cout << "Removing: " << message << endl; -// cout << "m_active_list = " << m_active_list.size() << endl; - if (iter != m_active_list.end()){ - msg = "Marking sent file as done. (Callback: " + (std::string)enum_str_callback[response] + ")"; - m_active_list.erase(iter); - }else{ - msg = "Unknown sent file. (Callback: " + (std::string)enum_str_callback[response] + ")"; -// color = COLOR_RED; - } - m_active_list.erase(message); -#else - bool success = QFile(message).remove(); - if (success){ - msg = "Removed sent file. (Callback: " + (std::string)enum_str_callback[response] + ")"; - }else{ - msg = "Failed to remove sent file. (Callback: " + (std::string)enum_str_callback[response] + ")"; - color = COLOR_RED; - } -#endif - break; - } - } - - sleepy_logger().log(msg, color); - } - - void cmd_callback(SleepyRequest request, char* channel, uint64_t id, uint16_t button, uint16_t hold_ticks, uint8_t x, uint8_t y){ - if (m_sleepy_client == nullptr){ - return; - } - - cout << "cmd_callback(): " << request << endl; - - std::string cmd = "Received command: " + (std::string)enum_str_command[request] + "."; - sleepy_logger().log(cmd, COLOR_PURPLE); - - switch (request){ - case SleepyRequest::Click: - run_Click(channel, id, hold_ticks, button); - break; - case SleepyRequest::DPad: - run_DPad(channel, id, hold_ticks, button); - break; - case SleepyRequest::SetLStick: - run_SetLStick(channel, id, hold_ticks, x, y); - return; - case SleepyRequest::SetRStick: - run_SetRStick(channel, id, hold_ticks, x, y); - return; - case SleepyRequest::ScreenshotJpg: - run_ScreenshotJpg(channel, id); - return; - case SleepyRequest::ScreenshotPng: - run_ScreenshotPng(channel, id); - return; - case SleepyRequest::Start: - run_start(channel, id); - break; - case SleepyRequest::Stop: - run_stop(channel, id); - break; - case SleepyRequest::Shutdown: - run_Shutdown(); - return; - case SleepyRequest::GetConnectedBots: - run_GetConnectedBots(channel); - return; - case SleepyRequest::ReloadSettings: - run_ReloadSettings(channel); - return; - case SleepyRequest::ResetCamera: - run_ResetCamera(channel, id); - return; - case SleepyRequest::ResetSerial: - run_ResetSerial(channel, id); - return; - } - } - - - -private: - void send_response(SleepyRequest request, char* channel, std::string message, std::shared_ptr file = nullptr){ - std::string filename = file == nullptr ? "" : file->filepath(); - if ((int)request <= 11){ - program_response(request, channel, &message[0], &filename[0]); - }else{ - send("", channel, std::chrono::milliseconds(0), &message[0], std::move(file)); - } - } - - void run_Click(char* channel, uint64_t id, uint16_t hold_ticks, uint16_t button){ - std::string message; - auto it = str_buttons.find(button); - if (it == str_buttons.end()){ - message = "Unrecognized button input"; - }else{ - message = Integration::press_button(id, button, hold_ticks); - } - if (message.empty()){ - message = "Console ID " + std::to_string(id) + " clicked " + it->second + "."; - } - send_response(SleepyRequest::Click, channel, message); - } - void run_DPad(char* channel, uint64_t id, uint16_t hold_ticks, uint16_t button){ - std::string message; - auto it = str_dpad.find(button); - if (it == str_dpad.end()){ - message = "Unrecognized button input"; - }else{ - message = Integration::press_dpad(id, button, hold_ticks); - } - if (message.empty()){ - message = "Console ID " + std::to_string(id) + " clicked " + it->second + "."; - } - send_response(SleepyRequest::DPad, channel, message); - } - void run_SetLStick(char* channel, uint64_t id, uint16_t hold_ticks, uint8_t x, uint8_t y){ - std::string message = Integration::press_left_joystick(id, x, y, hold_ticks); - if (message.empty()){ - message = "Console ID " + std::to_string(id) + " moved the left joystick."; - } - send_response(SleepyRequest::SetLStick, channel, message); - } - void run_SetRStick(char* channel, uint64_t id, uint16_t hold_ticks, uint8_t x, uint8_t y){ - std::string message = Integration::press_right_joystick(id, x, y, hold_ticks); - if (message.empty()){ - message = "Console ID " + std::to_string(id) + " moved the right joystick."; - } - send_response(SleepyRequest::SetRStick, channel, message); - } - void run_ScreenshotPng(char* channel, uint64_t id){ - std::string filepath = "capture.png"; - std::string message = Integration::screenshot(id, filepath.c_str()); - if (!message.empty()){ - send_response(SleepyRequest::ScreenshotPng, channel, message); - return; - }else{ - message = "Captured image from console ID " + std::to_string(id) + "."; - } - - std::shared_ptr file(new PendingFileSend(filepath, true)); - send_response(SleepyRequest::ScreenshotPng, channel, message, std::move(file)); - } - void run_ScreenshotJpg(char* channel, uint64_t id){ - std::string filepath = "capture.jpg"; - std::string message = Integration::screenshot(id, filepath.c_str()); - if (!message.empty()){ - send_response(SleepyRequest::ScreenshotJpg, channel, message); - return; - }else{ - message = "Captured image from console ID " + std::to_string(id) + "."; - } - - std::shared_ptr file(new PendingFileSend(filepath, true)); - send_response(SleepyRequest::ScreenshotJpg, channel, message, std::move(file)); - } - void run_start(char* channel, uint64_t id){ - std::string message = Integration::start_program(id); - if (!message.empty()){ - send_response(SleepyRequest::Start, channel, message); - return; - }else{ - message = "Console ID " + std::to_string(id) + " started the program."; - } - send_response(SleepyRequest::Start, channel, message); - } - void run_stop(char* channel, uint64_t id){ - std::string message = Integration::stop_program(id); - if (!message.empty()){ - send_response(SleepyRequest::Stop, channel, message); - return; - }else{ - message = "Console ID " + std::to_string(id) + " stopped the program."; - } - send_response(SleepyRequest::Stop, channel, message); - } - void run_Shutdown(){ - program_response(SleepyRequest::Terminate); - m_sleepy_client.reset(); - - // TODO: Can't do this since it skips all the shutdown sequence from all the other threads. - exit(0); - } - void run_GetConnectedBots(char* channel){ - std::string message = Integration::status(); - send_response(SleepyRequest::GetConnectedBots, channel, message); - } - void run_ReloadSettings(char* channel){ - std::string message = initialize_sleepy_settings() - ? "Successfully reloaded Discord settings." - : "Failed to reload Discord settings."; - send_response(SleepyRequest::ReloadSettings, channel, message); - } - void run_ResetCamera(char* channel, uint64_t id){ - std::string message = Integration::reset_camera(id); - send_response(SleepyRequest::ResetCamera, channel, message.empty() ? "Camera was reset." : message); - } - void run_ResetSerial(char* channel, uint64_t id){ - std::string message = Integration::reset_serial(id); - send_response(SleepyRequest::ResetSerial, channel, message); - } - - std::map> m_active_list; -}; - - - - - - - -bool is_running(){ - std::lock_guard lg(m_connect_lock); - return m_sleepy_client != nullptr; -} - -bool is_connected(){ - std::lock_guard lg(m_connect_lock); - return m_sleepy_client != nullptr && m_sleepy_client->m_connected; -} - -void sleepy_connect(){ - std::lock_guard lg(m_connect_lock); - { -// std::lock_guard lg(m_client_lock); -// if (!PreloadSettings::instance().DEVELOPER_MODE){ -// return; -// } - if (m_sleepy_client != nullptr){ - sleepy_logger().log("sleepy_connect(): Already initialized!", COLOR_PURPLE); - return; - } - if (!initialize_sleepy_settings()){ - sleepy_logger().log("sleepy_connect(): initialize_sleepy_settings() failed.", COLOR_RED); - return; - } - sleepy_logger().log("Connecting...", COLOR_PURPLE); - } - client_connect(); - sleepy_logger().log("Finished Connecting...", COLOR_PURPLE); -} - -void sleepy_terminate(){ - std::lock_guard lg(m_client_lock); - program_response(SleepyRequest::Terminate); - if (m_sleepy_client != nullptr){ - m_sleepy_client.reset(); - } -} - -void sleepy_response(int response, char* message){ - //std::lock_guard lg(m_client_lock); - if (m_sleepy_client != nullptr){ - return m_sleepy_client->callback(response, message); - } -} - -void sleepy_cmd_response(int request, char* channel, uint64_t console_id, uint16_t button, uint16_t hold_ticks, uint8_t x, uint8_t y){ - std::lock_guard lg(m_client_lock); - if (m_sleepy_client != nullptr){ - return m_sleepy_client->cmd_callback((SleepyRequest)request, channel, console_id, button, hold_ticks, x, y); - } -} - -void send_embed_sleepy( - bool should_ping, - const std::vector& tags, - const JsonObject& embed, - std::shared_ptr file -){ - DiscordSettingsOption& settings = GlobalSettings::instance().DISCORD; - if (!settings.integration.enabled()){ - return; - } - - std::lock_guard lg(m_client_lock); - if (m_sleepy_client == nullptr){ - return; - } - - MessageBuilder builder(tags); - const DiscordIntegrationTable& channels = settings.integration.channels; - - std::vector> table = channels.copy_snapshot(); - for (size_t i = 0; i < table.size(); i++){ - const Integration::DiscordIntegrationChannel& channel = *table[i]; - if (((std::string)channel.tags_text).empty() || !channel.enabled){ - continue; - } - if (!builder.should_send(EventNotificationOption::parse_tags(channel.tags_text))){ - continue; - } - - sleepy_logger().log("send_message_sleepy(): Sending...", COLOR_PURPLE); - std::chrono::seconds delay(channel.delay); - m_sleepy_client->send( - embed.dump(), - channel.channel_id, - std::chrono::seconds(delay), - builder.build_message( - std::chrono::seconds(delay), - should_ping && channel.ping, - settings.message.user_id, - settings.message.message - ), - file == nullptr ? nullptr : file - ); - } -} - - -bool initialize_sleepy_settings(){ - // Must be called inside lock. -// std::lock_guard lg(m_client_lock); - DiscordSettingsOption& settings = GlobalSettings::instance().DISCORD; - if (!check_if_empty(settings)){ - return false; - } - - bool suffix = settings.integration.use_suffix; - std::string param_string; - param_string += StringTools::replace(settings.integration.token, " ", "") + "|"; - param_string += (std::string)settings.integration.command_prefix + "|"; - param_string += StringTools::replace(settings.integration.owner, " ", "") + "|"; - param_string += (std::string)settings.integration.game_status + "|"; - param_string += StringTools::replace(settings.integration.hello_message, "@", "") + "|"; - param_string += PROGRAM_VERSION + "|"; - param_string += PROJECT_SOURCE_URL; - - std::string sudo = StringTools::replace(settings.integration.sudo, " ", ""); - -// std::string w_channels = StringTools::replace(settings.integration.channels_whitelist.get(), " ", ""); - std::string w_channels; - for (const std::string& channel_id : settings.integration.channels.command_channels()){ - if (!w_channels.empty()){ - w_channels += ","; - } - w_channels += channel_id; - } - - m_sleepy_client = std::unique_ptr(new SleepyDiscordClient()); - apply_settings(sleepy_response, sleepy_cmd_response, &w_channels[0], &sudo[0], ¶m_string[0], suffix); - return true; -} - -bool check_if_empty(const DiscordSettingsOption& settings){ - - if (!settings.integration.enabled()){ - return false; - } - if (((std::string)settings.integration.token).empty()){ - return false; - }else if (((std::string)settings.integration.token).find(",") != std::string::npos){ - sleepy_logger().log("\"Token\" must only contain one token. Stopping...", COLOR_RED); - return false; - }else if (((std::string)settings.integration.owner).find(",") != std::string::npos){ - sleepy_logger().log("\"Owner\" must only contain one Discord ID (yours). Stopping...", COLOR_RED); - return false; - }else if (((std::string)settings.integration.command_prefix).empty()){ - sleepy_logger().log("Please enter a Discord command prefix. Stopping...", COLOR_RED); - return false; - } - return true; -} - - - - - -} -} -} -#endif diff --git a/SerialPrograms/Source/Integrations/SleepyDiscordRunner.h b/SerialPrograms/Source/Integrations/SleepyDiscordRunner.h deleted file mode 100644 index 4c85fd79e0..0000000000 --- a/SerialPrograms/Source/Integrations/SleepyDiscordRunner.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef PokemonAutomation_SleepyPA_H -#define PokemonAutomation_SleepyPA_H - -#include -#include "CommonFramework/PersistentSettings.h" -#include "CommonFramework/Options/ScreenshotFormatOption.h" -#include "CommonFramework/Notifications/MessageAttachment.h" -#include "Integrations/DiscordSettingsOption.h" - -#ifdef SLEEPY_STATIC - #define SLEEPY_EXPORT -#else - -#ifdef _WIN32 -#ifdef _WINDLL - #define SLEEPY_EXPORT __declspec(dllexport) -#else - #define SLEEPY_EXPORT __declspec(dllimport) -#endif - -#else - #define SLEEPY_EXPORT __attribute__((visibility("default"))) -#endif -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -namespace SleepyDiscord{ - typedef void (*SleepyCallback)(int response, char* message); - typedef void (*SleepyCommandCallback)(int request, char* channel, uint64_t console_id, uint16_t button, uint16_t hold_ticks, uint8_t x, uint8_t y); - SLEEPY_EXPORT void client_connect(); - SLEEPY_EXPORT void client_disconnect(); - SLEEPY_EXPORT void apply_settings(SleepyCallback callback, SleepyCommandCallback cmd_callback, char* w_channels, char* sudo, char* params, bool suffix); - SLEEPY_EXPORT void program_response(int response, char* channel = nullptr, char* message = nullptr, char* filepath = nullptr); - SLEEPY_EXPORT void sendMessage(char* channels, char* messages = nullptr, char* json = nullptr, char* filePath = nullptr); - void client_run(); - -enum SleepyResponse{ - Fault = 0, - API = 1, - Disconnected = 2, - Connected = 3, - SettingsInitialized = 4, - SettingsUpdated = 5, - CommandsInitialized = 6, - CallbacksSet = 7, - InvalidCommand = 8, - Terminating = 9, - RemoveFile = 10, -}; - -enum SleepyRequest{ - Click = 0, - DPad = 1, - - SetLStick = 2, - SetRStick = 3, - - ScreenshotJpg = 4, - ScreenshotPng = 5, - Start = 6, - Stop = 7, - Shutdown = 8, - GetConnectedBots = 9, - ReloadSettings = 10, - ResetCamera = 11, - ResetSerial = 12, - Terminate = 13, - - Hi = 14, - Ping = 15, - About = 16, - Help = 17, -}; -} - -#ifdef __cplusplus -} - -namespace PokemonAutomation{ -namespace Integration{ -namespace SleepyDiscordRunner{ - - bool is_running(); - bool is_connected(); - void send_embed_sleepy( - bool should_ping, - const std::vector& tags, - const JsonObject& embed, - std::shared_ptr file - ); - void sleepy_response(int response, char* message); - void sleepy_cmd_response(int request, char* channel, uint64_t console_id, uint16_t button, uint16_t hold_ticks, uint8_t x, uint8_t y); - void sleepy_connect(); - void sleepy_terminate(); - bool initialize_sleepy_settings(); - bool check_if_empty(const DiscordSettingsOption& settings); -} -} -} - -#endif -#endif