1 parent
446416c723
commit
06379fbd5c
6 files changed
+180
-58
No files matched your search
@@ -38,6 +38,7 @@ target_link_libraries(EchoionPlugin PRIVATE
|
||||
Echoion::ApiPluginInterface
|
||||
Echoion::ApiLogger
|
||||
Echoion::ApiCoreInterface
|
||||
Void::Data
|
||||
Void::Stacktrace
|
||||
)
|
||||
|
||||
|
||||
@@ -74,6 +74,8 @@ namespace Echoion::Plugin
|
||||
// Deferred reload - waits for current track to finish
|
||||
std::thread deferredReloadThread_;
|
||||
std::atomic<bool> hasPendingReload_{false};
|
||||
std::atomic<bool> stopDeferredReload_{false};
|
||||
std::condition_variable deferredReloadCv_;
|
||||
std::filesystem::path pendingReloadPath_;
|
||||
std::string pendingTrackId_; // Track ID that was playing when
|
||||
// reload scheduled
|
||||
|
||||
+22
-8
@@ -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<std::mutex> 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<dylib::library>(
|
||||
@@ -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 {};
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -85,7 +85,7 @@ namespace Echoion::Plugin
|
||||
std::cerr
|
||||
<< std::format("FileWatcher callback error: {}",
|
||||
e.what())
|
||||
<< std::endl;
|
||||
<< '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+139
-40
@@ -3,8 +3,10 @@
|
||||
#include "echoion/plugin/PluginInterface.hpp"
|
||||
#include "teamspeak/public_errors.h"
|
||||
#include <CoreInterface.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#include <PluginTypes.hpp>
|
||||
#include <cstdio>
|
||||
#include <Types.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -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<char[]>(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<uintptr_t>(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<PluginMenuItem**>(
|
||||
std::malloc((count + 1) * sizeof(PluginMenuItem*)));
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
auto* item = static_cast<PluginMenuItem*>(
|
||||
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();
|
||||
}
|
||||
|
||||
+15
-9
@@ -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 <details>…</details> 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<std::string> 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("<details>") != std::string::npos;
|
||||
const bool closes =
|
||||
line.find("</details>") != 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 <details> 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 {};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user