From 06379fbd5c48aa7e25bf4569a29fb05dfc4c8e97 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 4 Jul 2026 01:49:55 +0300 Subject: [PATCH] Release 1.0.0 Signed-off-by: faumaray --- CMakeLists.txt | 1 + include/echoion/plugin/CoreLoader.hpp | 2 + src/CoreLoader.cpp | 30 +++-- src/FileWatcher.cpp | 2 +- src/Plugin.cpp | 179 ++++++++++++++++++++------ src/PluginInterface.cpp | 24 ++-- 6 files changed, 180 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c972fb..965d63c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ target_link_libraries(EchoionPlugin PRIVATE Echoion::ApiPluginInterface Echoion::ApiLogger Echoion::ApiCoreInterface + Void::Data Void::Stacktrace ) diff --git a/include/echoion/plugin/CoreLoader.hpp b/include/echoion/plugin/CoreLoader.hpp index 1804146..f77166a 100644 --- a/include/echoion/plugin/CoreLoader.hpp +++ b/include/echoion/plugin/CoreLoader.hpp @@ -74,6 +74,8 @@ namespace Echoion::Plugin // Deferred reload - waits for current track to finish std::thread deferredReloadThread_; std::atomic hasPendingReload_{false}; + std::atomic stopDeferredReload_{false}; + std::condition_variable deferredReloadCv_; std::filesystem::path pendingReloadPath_; std::string pendingTrackId_; // Track ID that was playing when // reload scheduled diff --git a/src/CoreLoader.cpp b/src/CoreLoader.cpp index d5e3a69..c09bef2 100644 --- a/src/CoreLoader.cpp +++ b/src/CoreLoader.cpp @@ -134,9 +134,22 @@ namespace Echoion::Plugin // Poll every 500ms to check if the specific track finished // (playback may continue with next track, but we only care about THIS - // track) + // track). The wait is interruptible so the destructor can join this + // thread instead of detaching it (detaching risks use-after-free when + // the plugin unloads mid-wait). while (isSameTrackPlaying(trackId)) { - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::unique_lock lock(pendingReloadMutex_); + if (deferredReloadCv_.wait_for( + lock, std::chrono::milliseconds(500), + [this] { return stopDeferredReload_.load(); })) { + Void::Log::info("CoreLoader", "{}", + "Deferred hot-reload cancelled (loader shutting down)"); + return; + } + } + + if (stopDeferredReload_.load()) { + return; } Void::Log::info("CoreLoader", "{}", "Track finished, starting hot reload"); @@ -164,11 +177,12 @@ namespace Echoion::Plugin CoreLoader::~CoreLoader() { - // Stop any deferred reload thread + // Signal the deferred reload thread to stop and wait for it. It must + // never outlive this object: it dereferences core_ and this. + stopDeferredReload_ = true; + deferredReloadCv_.notify_all(); if (deferredReloadThread_.joinable()) { - // We can't really stop it gracefully, but we can detach it - // to avoid hanging on shutdown - deferredReloadThread_.detach(); + deferredReloadThread_.join(); } unload(); cleanupTempFiles(); @@ -372,7 +386,7 @@ namespace Echoion::Plugin std::cout << std::format("[Echoion] Loading core library: {}", path.string()) - << std::endl; + << '\n'; // Use no decorations - the path already has the correct extension // (.ts) library_ = std::make_unique( @@ -444,7 +458,7 @@ namespace Echoion::Plugin library_.reset(); loaded_ = false; - std::cout << "[Echoion] Core library unloaded" << std::endl; + std::cout << "[Echoion] Core library unloaded" << '\n'; return {}; } diff --git a/src/FileWatcher.cpp b/src/FileWatcher.cpp index 9f358b1..39184fa 100644 --- a/src/FileWatcher.cpp +++ b/src/FileWatcher.cpp @@ -85,7 +85,7 @@ namespace Echoion::Plugin std::cerr << std::format("FileWatcher callback error: {}", e.what()) - << std::endl; + << '\n'; } } } diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 7fca35f..e141fc8 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -3,8 +3,10 @@ #include "echoion/plugin/PluginInterface.hpp" #include "teamspeak/public_errors.h" #include +#include #include #include +#include #include #include #include @@ -94,10 +96,10 @@ namespace Echoion::Plugin if (g_ts3Functions && g_ts3Functions->getConfigPath && g_ts3Functions->getPluginPath) { g_ts3FunctionsReady.store(true); - std::cout << "[Echoion] TS3 function pointers set" << std::endl; + std::cout << "[Echoion] TS3 function pointers set" << '\n'; } else { std::cerr << "[Echoion] TS3 function pointers incomplete" - << std::endl; + << '\n'; } } @@ -113,7 +115,7 @@ namespace Echoion::Plugin pluginID = std::make_unique(sz); _strcpy(pluginID.get(), sz, id); std::cout << std::format("[Echoion] Plugin ID: {}", pluginID.get()) - << std::endl; + << '\n'; } // Forward declarations for helper functions @@ -134,7 +136,7 @@ namespace Echoion::Plugin g_coreLoader->setHotReloadCallback([]() { std::cout << "[Echoion] Auto-reload callback triggered, " "re-registering interface..." - << std::endl; + << '\n'; reregisterPluginInterface(); }); } @@ -143,16 +145,16 @@ namespace Echoion::Plugin g_coreLoader->setConfigPath(getConfigPath()); std::cout << std::format("[Echoion] Config path: {}", getConfigPath().string()) - << std::endl; + << '\n'; std::cout << std::format("[Echoion] Loading Core from: {}", getCorePath().string()) - << std::endl; + << '\n'; auto loadResult = g_coreLoader->load(getCorePath()); if (!loadResult) { std::cerr << std::format("[Echoion] Failed to load core: {}", loadResult.error().message) - << std::endl; + << '\n'; return false; } @@ -175,7 +177,7 @@ namespace Echoion::Plugin } catch (const std::exception& e) { std::cerr << std::format("[Echoion] Exception loading core: {}", e.what()) - << std::endl; + << '\n'; return false; } } @@ -196,13 +198,13 @@ namespace Echoion::Plugin if (!g_ts3FunctionsReady.load()) { std::cerr << "[Echoion] TS3 functions not ready, aborting init" - << std::endl; + << '\n'; g_deferredInitRunning = false; return; } std::cout << "[Echoion] Starting deferred initialization..." - << std::endl; + << '\n'; try { // Create plugin interface @@ -214,7 +216,7 @@ namespace Echoion::Plugin std::cerr << std::format("[Echoion] Failed to initialize " "plugin interface: {}", result.error().message) - << std::endl; + << '\n'; g_deferredInitRunning = false; return; } @@ -224,29 +226,29 @@ namespace Echoion::Plugin if (!deviceResult) { std::cout << std::format("[Echoion] Audio device skipped: {}", deviceResult.error().message) - << std::endl; + << '\n'; } else { - std::cout << "[Echoion] Audio device registered" << std::endl; + std::cout << "[Echoion] Audio device registered" << '\n'; } // Load Core library (Core will handle config loading and // auto-connect) if (ensureCoreLoaded()) { - std::cout << "[Echoion] Core loaded successfully" << std::endl; + std::cout << "[Echoion] Core loaded successfully" << '\n'; if (g_coreLoader) { g_coreLoader->enableAutoReload(); } } else { - std::cout << "[Echoion] Core failed to load" << std::endl; + std::cout << "[Echoion] Core failed to load" << '\n'; } - std::cout << "[Echoion] Initialization complete" << std::endl; + std::cout << "[Echoion] Initialization complete" << '\n'; g_deferredInitComplete = true; } catch (const std::exception& e) { std::cerr << std::format("[Echoion] Deferred init failed: {}", e.what()) - << std::endl; + << '\n'; } g_deferredInitRunning = false; @@ -254,7 +256,7 @@ namespace Echoion::Plugin TS3_PLUGIN_API int ts3plugin_init() { - std::cout << "[Echoion] Plugin init started" << std::endl; + std::cout << "[Echoion] Plugin init started" << '\n'; // Install a terminate handler that dumps a symbolized stack trace // (with source snippets) before the process dies — invaluable for @@ -265,7 +267,7 @@ namespace Echoion::Plugin std::cerr << "[Echoion] FATAL: std::terminate — stack trace:\n"; const auto frames = Void::Stacktrace::capture_stacktrace(); std::cerr << Void::Stacktrace::format_stacktrace(frames) - << std::endl; + << '\n'; std::abort(); }); }); @@ -276,7 +278,7 @@ namespace Echoion::Plugin TS3_PLUGIN_API void ts3plugin_shutdown() { - std::cout << "[Echoion] Shutting down..." << std::endl; + std::cout << "[Echoion] Shutting down..." << '\n'; g_deferredInitRunning = false; @@ -315,7 +317,7 @@ namespace Echoion::Plugin g_ts3Functions = nullptr; g_ts3FunctionsReady = false; - std::cout << "[Echoion] Shutdown complete" << std::endl; + std::cout << "[Echoion] Shutdown complete" << '\n'; } // Handle incoming text messages @@ -380,52 +382,52 @@ namespace Echoion::Plugin if (!g_coreLoader) { std::cerr << "[Echoion] Cannot re-register: CoreLoader not available" - << std::endl; + << '\n'; return false; } std::cout << "[Echoion] Re-registering plugin interface after hot reload..." - << std::endl; + << '\n'; // Get the new core instance auto core = g_coreLoader->getCore(); if (!core) { - std::cerr << "[Echoion] ERROR: Core is null!" << std::endl; + std::cerr << "[Echoion] ERROR: Core is null!" << '\n'; return false; } if (!g_pluginInterface) { std::cerr << "[Echoion] ERROR: PluginInterface is null!" - << std::endl; + << '\n'; return false; } std::cout << std::format("[Echoion] New Core pointer: {}", reinterpret_cast(core.get())) - << std::endl; + << '\n'; // Set the plugin interface first (before registering callbacks) core->setPluginInterface(g_pluginInterface); - std::cout << "[Echoion] Plugin interface set on new Core" << std::endl; + std::cout << "[Echoion] Plugin interface set on new Core" << '\n'; // Clear old callbacks (they point to destroyed Core) and register new // one - std::cout << "[Echoion] About to clear callbacks..." << std::endl; + std::cout << "[Echoion] About to clear callbacks..." << '\n'; g_pluginInterface->clearEventCallbacks(); - std::cout << "[Echoion] Old event callbacks cleared" << std::endl; + std::cout << "[Echoion] Old event callbacks cleared" << '\n'; - std::cout << "[Echoion] About to register new callback..." << std::endl; + std::cout << "[Echoion] About to register new callback..." << '\n'; g_pluginInterface->registerEventCallback( [core](const Api::PluginEvent& event) { core->onPluginEvent(event); }); - std::cout << "[Echoion] New event callback registered" << std::endl; + std::cout << "[Echoion] New event callback registered" << '\n'; core->skip(0); core->resume(); std::cout << "[Echoion] Re-registration complete - bot should respond " "to commands" - << std::endl; + << '\n'; return true; } @@ -435,17 +437,17 @@ namespace Echoion::Plugin { if (!g_coreLoader) { std::cerr << "[Echoion] Cannot reload: CoreLoader not available" - << std::endl; + << '\n'; return false; } - std::cout << "[Echoion] Starting hot reload (Plugin)..." << std::endl; + std::cout << "[Echoion] Starting hot reload (Plugin)..." << '\n'; auto result = g_coreLoader->hotReload(getCorePath()); if (!result) { std::cerr << std::format("[Echoion] Hot reload failed: {}", result.error().message) - << std::endl; + << '\n'; return false; } @@ -467,6 +469,43 @@ namespace Echoion::Plugin } } + // Global plugin menu. Item ids match the existing + // ts3plugin_onMenuItemEvent handler below (1..5). + TS3_PLUGIN_API void ts3plugin_initMenus(struct PluginMenuItem*** menuItems, + char** menuIcon) + { + struct Entry { + int id; + const char* text; + }; + static constexpr Entry entries[] = { + {1, "Play / Pause"}, {2, "Skip Track"}, + {3, "Stop Playback"}, {4, "Hot Reload Core"}, + {5, "Connect / Disconnect"}, + }; + constexpr size_t count = std::size(entries); + + // The client frees each item, the array and the icon string via + // ts3plugin_freeMemory, so everything must come from malloc. + auto** items = static_cast( + std::malloc((count + 1) * sizeof(PluginMenuItem*))); + for (size_t i = 0; i < count; ++i) { + auto* item = static_cast( + std::malloc(sizeof(PluginMenuItem))); + item->type = PLUGIN_MENU_TYPE_GLOBAL; + item->id = entries[i].id; + std::snprintf(item->text, PLUGIN_MENU_BUFSZ, "%s", + entries[i].text); + item->icon[0] = '\0'; + items[i] = item; + } + items[count] = nullptr; + *menuItems = items; + *menuIcon = nullptr; + } + + TS3_PLUGIN_API void ts3plugin_freeMemory(void* data) { std::free(data); } + // Menu item callbacks TS3_PLUGIN_API void ts3plugin_onMenuItemEvent(uint64 serverConnectionHandlerId, @@ -516,7 +555,7 @@ namespace Echoion::Plugin std::cerr << std::format("[Echoion] Reconnect failed: {}", result.error().message) - << std::endl; + << '\n'; } } } @@ -524,6 +563,65 @@ namespace Echoion::Plugin } } + // Move the bot into teamspeak.defaultChannelId (config) once fully + // connected. No-op when the option is absent/non-numeric or the bot is + // already there. + static void joinDefaultChannel(uint64 serverConnectionHandlerId) + { + if (!g_ts3Functions || !ensureCoreLoaded()) { + return; + } + auto core = g_coreLoader->getCore(); + if (!core) { + return; + } + auto configJson = core->getConfigJson(); + if (!configJson) { + return; + } + auto parsed = Void::Json::parse(*configJson); + if (!parsed) { + return; + } + const auto* teamspeak = parsed->find("teamspeak"); + const auto* channel = + teamspeak ? teamspeak->find("defaultChannelId") : nullptr; + if (!channel || !channel->is_string()) { + return; + } + uint64 targetChannel = 0; + try { + targetChannel = std::stoull(channel->as_string()); + } catch (const std::exception&) { + std::cerr << "[Echoion] defaultChannelId is not numeric, " + "skipping auto-join" + << '\n'; + return; + } + if (targetChannel == 0) { + return; + } + + anyID myId = 0; + if (g_ts3Functions->getClientID(serverConnectionHandlerId, &myId) != + ERROR_ok) { + return; + } + uint64 currentChannel = 0; + if (g_ts3Functions->getChannelOfClient(serverConnectionHandlerId, + myId, ¤tChannel) == + ERROR_ok && + currentChannel == targetChannel) { + return; // already there + } + + std::cout << std::format("[Echoion] Auto-joining channel {}", + targetChannel) + << '\n'; + g_ts3Functions->requestClientMove(serverConnectionHandlerId, myId, + targetChannel, "", nullptr); + } + // Connection status changes TS3_PLUGIN_API void ts3plugin_onConnectStatusChangeEvent(uint64 serverConnectionHandlerId, @@ -538,20 +636,21 @@ namespace Echoion::Plugin switch (newStatus) { case STATUS_CONNECTING: - std::cout << "[Echoion] Connecting..." << std::endl; + std::cout << "[Echoion] Connecting..." << '\n'; break; case STATUS_CONNECTED: - std::cout << "[Echoion] Connected" << std::endl; + std::cout << "[Echoion] Connected" << '\n'; if (g_pluginInterface) { g_pluginInterface->closeAudioDevice(); g_pluginInterface->openAudioDevice(); } break; case STATUS_CONNECTION_ESTABLISHED: - std::cout << "[Echoion] Connection established" << std::endl; + std::cout << "[Echoion] Connection established" << '\n'; + joinDefaultChannel(serverConnectionHandlerId); break; case STATUS_DISCONNECTED: - std::cout << "[Echoion] Disconnected" << std::endl; + std::cout << "[Echoion] Disconnected" << '\n'; if (g_pluginInterface) { g_pluginInterface->closeAudioDevice(); } diff --git a/src/PluginInterface.cpp b/src/PluginInterface.cpp index 2ab1a51..317af24 100644 --- a/src/PluginInterface.cpp +++ b/src/PluginInterface.cpp @@ -14,8 +14,8 @@ namespace Echoion::Plugin // TeamSpeak rejects over-long text messages (long replies such as // search results or the queue silently never arrive). Split into // UTF-8-safe chunks on line boundaries so every line is delivered. - // A
…
block is kept whole in its own message — - // splitting it across messages would break the collapsible. + // A ":::details … :::" collapsible block is kept whole in its own + // message — splitting it across messages would break the fold. constexpr std::size_t kMaxMessageBytes = 900; std::vector chunkMessage(const std::string& message, @@ -44,10 +44,16 @@ namespace Echoion::Plugin : message.substr(pos, nl - pos + 1); pos = (nl == std::string::npos) ? message.size() : nl + 1; - const bool opens = - line.find("
") != std::string::npos; - const bool closes = - line.find("
") != std::string::npos; + // ":::details …" opens a collapsible block; a line that is + // just ":::" closes it. + const bool opens = line.starts_with(":::details"); + std::string_view trimmed = line; + while (!trimmed.empty() && + (trimmed.back() == '\n' || trimmed.back() == '\r' || + trimmed.back() == ' ' || trimmed.back() == '\t')) { + trimmed.remove_suffix(1); + } + const bool closes = (trimmed == ":::"); if (detailsDepth == 0 && !current.empty()) { // Start each top-level
block in a fresh message, @@ -876,7 +882,7 @@ namespace Echoion::Plugin Void::Log::warn("PluginInterface", "{}", ""); std::cerr << std::format( "[WARNING] unregisterCustomDevice not available") - << std::endl; + << '\n'; audioDeviceRegistered_ = false; return {}; } @@ -887,12 +893,12 @@ namespace Echoion::Plugin std::cerr << std::format( "[WARNING] Failed to unregister audio device: {}", error) - << std::endl; + << '\n'; } audioDeviceRegistered_ = false; std::cout << std::format("[INFO] Audio device unregistered") - << std::endl; + << '\n'; return {}; }