Bump CMake minimum to 4.0 for VoidLibrary toolchain
This commit is contained in:
1 parent
0fa9db4691
commit
05c9f8bc6c
11 files changed
+2524
-2125
No files matched your search
+501
-413
@@ -1,460 +1,548 @@
|
||||
// TeamSpeak Plugin Entry Point
|
||||
#include <ts3_functions.h>
|
||||
#include <plugin_definitions.h>
|
||||
#include "echoion/plugin/PluginInterface.hpp"
|
||||
#include "echoion/plugin/CoreLoader.hpp"
|
||||
#include "echoion/plugin/PluginInterface.hpp"
|
||||
#include "teamspeak/public_errors.h"
|
||||
#include <echoion/api.hpp>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
#include <CoreInterface.hpp>
|
||||
#include <PluginTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <plugin_definitions.h>
|
||||
#include <thread>
|
||||
#include <ts3_functions.h>
|
||||
|
||||
namespace Echoion::Plugin {
|
||||
|
||||
// Global plugin state
|
||||
static std::shared_ptr<PluginInterface> g_pluginInterface;
|
||||
static std::unique_ptr<CoreLoader> g_coreLoader;
|
||||
|
||||
// TeamSpeak plugin info
|
||||
static constexpr char PLUGIN_NAME[] = "Echoion Music Bot";
|
||||
static constexpr char PLUGIN_VERSION[] = "1.0.0";
|
||||
static constexpr char PLUGIN_AUTHOR[] = "Echoion Team";
|
||||
static constexpr char PLUGIN_DESCRIPTION[] = "Advanced music bot for TeamSpeak";
|
||||
static char *pluginID = NULL;
|
||||
static std::jthread g_deferredInitThread;
|
||||
static std::atomic<bool> g_deferredInitRunning{false};
|
||||
static std::atomic<bool> g_deferredInitComplete{false};
|
||||
static std::atomic<bool> g_ts3FunctionsReady{false};
|
||||
static std::shared_ptr<TS3Functions> g_ts3Functions = nullptr;
|
||||
|
||||
// Helper to get config directory
|
||||
std::filesystem::path getConfigDir() {
|
||||
const char* configHome = std::getenv("XDG_CONFIG_HOME");
|
||||
if (configHome) {
|
||||
return std::filesystem::path(configHome) / "echoion";
|
||||
}
|
||||
const char* home = std::getenv("HOME");
|
||||
if (home) {
|
||||
return std::filesystem::path(home) / ".config" / "echoion";
|
||||
}
|
||||
return std::filesystem::path(".") / "echoion";
|
||||
}
|
||||
|
||||
std::filesystem::path getConfigPath() {
|
||||
return getConfigDir() / "config.json";
|
||||
}
|
||||
|
||||
// Initialize the plugin
|
||||
extern "C" {
|
||||
|
||||
static std::filesystem::path getCorePath()
|
||||
namespace Echoion::Plugin
|
||||
{
|
||||
if (g_ts3FunctionsReady.load())
|
||||
{
|
||||
char path[512];
|
||||
g_ts3Functions->getPluginPath(path, 512, pluginID);
|
||||
return std::string(path) + "Echoion.ts";
|
||||
}
|
||||
return "./plugins/Echoion.ts";
|
||||
}
|
||||
|
||||
// Global plugin state
|
||||
static std::shared_ptr<PluginInterface> g_pluginInterface;
|
||||
static std::unique_ptr<CoreLoader> g_coreLoader;
|
||||
|
||||
// TeamSpeak plugin info
|
||||
static constexpr char PLUGIN_NAME[] = "Echoion Music Bot";
|
||||
static constexpr char PLUGIN_VERSION[] = "1.0.0";
|
||||
static constexpr char PLUGIN_AUTHOR[] = "Echoion Team";
|
||||
static constexpr char PLUGIN_DESCRIPTION[] =
|
||||
"Advanced music bot for TeamSpeak";
|
||||
static std::unique_ptr<char[]> pluginID;
|
||||
static std::jthread g_deferredInitThread;
|
||||
static std::atomic<bool> g_deferredInitRunning{false};
|
||||
static std::atomic<bool> g_deferredInitComplete{false};
|
||||
static std::atomic<bool> g_ts3FunctionsReady{false};
|
||||
static std::shared_ptr<TS3Functions> g_ts3Functions = nullptr;
|
||||
|
||||
// Helper to get config directory
|
||||
std::filesystem::path getConfigDir()
|
||||
{
|
||||
const char* configHome = std::getenv("XDG_CONFIG_HOME");
|
||||
if (configHome) {
|
||||
return std::filesystem::path(configHome) / "echoion";
|
||||
}
|
||||
const char* home = std::getenv("HOME");
|
||||
if (home) {
|
||||
return std::filesystem::path(home) / ".config" / "echoion";
|
||||
}
|
||||
return std::filesystem::path(".") / "echoion";
|
||||
}
|
||||
|
||||
std::filesystem::path getConfigPath()
|
||||
{
|
||||
return getConfigDir() / "config.json";
|
||||
}
|
||||
|
||||
// Initialize the plugin
|
||||
extern "C" {
|
||||
|
||||
static std::filesystem::path getCorePath()
|
||||
{
|
||||
if (g_ts3FunctionsReady.load()) {
|
||||
char path[512];
|
||||
g_ts3Functions->getPluginPath(path, 512, pluginID.get());
|
||||
return std::string(path) + "Echoion.ts";
|
||||
}
|
||||
return "./plugins/Echoion.ts";
|
||||
}
|
||||
|
||||
// Required TeamSpeak plugin functions
|
||||
#ifdef _WIN32
|
||||
#define TS3_PLUGIN_API __declspec(dllexport)
|
||||
#define TS3_PLUGIN_API __declspec(dllexport)
|
||||
#else
|
||||
#define TS3_PLUGIN_API __attribute__((visibility("default")))
|
||||
#define TS3_PLUGIN_API __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
TS3_PLUGIN_API const char* ts3plugin_name() { return PLUGIN_NAME; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_version() { return PLUGIN_VERSION; }
|
||||
TS3_PLUGIN_API int ts3plugin_apiVersion() { return 26; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_author() { return PLUGIN_AUTHOR; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_description() { return PLUGIN_DESCRIPTION; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_name() { return PLUGIN_NAME; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_version() { return PLUGIN_VERSION; }
|
||||
TS3_PLUGIN_API int ts3plugin_apiVersion() { return 26; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_author() { return PLUGIN_AUTHOR; }
|
||||
TS3_PLUGIN_API const char* ts3plugin_description()
|
||||
{
|
||||
return PLUGIN_DESCRIPTION;
|
||||
}
|
||||
|
||||
TS3_PLUGIN_API void ts3plugin_setFunctionPointers(const TS3Functions funcs) {
|
||||
g_ts3Functions = std::make_shared<TS3Functions>(funcs);
|
||||
|
||||
if (g_ts3Functions && g_ts3Functions->getConfigPath && g_ts3Functions->getPluginPath) {
|
||||
g_ts3FunctionsReady.store(true);
|
||||
std::cout << "[Echoion] TS3 function pointers set" << std::endl;
|
||||
} else {
|
||||
std::cerr << "[Echoion] TS3 function pointers incomplete" << std::endl;
|
||||
}
|
||||
}
|
||||
TS3_PLUGIN_API void ts3plugin_setFunctionPointers(const TS3Functions funcs)
|
||||
{
|
||||
g_ts3Functions = std::make_shared<TS3Functions>(funcs);
|
||||
|
||||
if (g_ts3Functions && g_ts3Functions->getConfigPath &&
|
||||
g_ts3Functions->getPluginPath) {
|
||||
g_ts3FunctionsReady.store(true);
|
||||
std::cout << "[Echoion] TS3 function pointers set" << std::endl;
|
||||
} else {
|
||||
std::cerr << "[Echoion] TS3 function pointers incomplete"
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#define _strcpy(dest, destSize, src) \
|
||||
{ \
|
||||
strncpy(dest, src, destSize - 1); \
|
||||
(dest)[destSize - 1] = '\0'; \
|
||||
}
|
||||
|
||||
TS3_PLUGIN_API void ts3plugin_registerPluginID(const char *id)
|
||||
{
|
||||
const size_t sz = strlen(id) + 1;
|
||||
pluginID = (char *)malloc(sz * sizeof(char));
|
||||
_strcpy(pluginID, sz, id);
|
||||
std::cout << std::format("[Echoion] Plugin ID: {}", pluginID) << std::endl;
|
||||
}
|
||||
|
||||
// Forward declarations for helper functions
|
||||
static bool reregisterPluginInterface();
|
||||
static bool reloadCoreWithInterface();
|
||||
|
||||
static bool ensureCoreLoaded() {
|
||||
if (g_coreLoader && g_coreLoader->isLoaded()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!g_coreLoader) {
|
||||
g_coreLoader = std::make_unique<CoreLoader>();
|
||||
// Set up callback for auto-reload to re-register plugin interface
|
||||
g_coreLoader->setHotReloadCallback([]() {
|
||||
std::cout << "[Echoion] Auto-reload callback triggered, re-registering interface..." << std::endl;
|
||||
reregisterPluginInterface();
|
||||
});
|
||||
}
|
||||
|
||||
// Pass config path to CoreLoader (Core will load config internally)
|
||||
g_coreLoader->setConfigPath(getConfigPath());
|
||||
std::cout << std::format("[Echoion] Config path: {}", getConfigPath().string()) << std::endl;
|
||||
|
||||
std::cout << std::format("[Echoion] Loading Core from: {}", getCorePath().string()) << std::endl;
|
||||
auto loadResult = g_coreLoader->load(getCorePath());
|
||||
if (!loadResult) {
|
||||
std::cerr << std::format("[Echoion] Failed to load core: {}", loadResult.error().message) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get core and set plugin interface
|
||||
auto core = g_coreLoader->getCore();
|
||||
if (core && g_pluginInterface) {
|
||||
core->setPluginInterface(g_pluginInterface);
|
||||
|
||||
// Register for plugin events from Core
|
||||
g_pluginInterface->registerEventCallback([core](const Api::PluginEvent& event) {
|
||||
core->onPluginEvent(event);
|
||||
});
|
||||
|
||||
// Trigger auto-connect now that plugin interface is set
|
||||
core->triggerAutoConnect();
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << std::format("[Echoion] Exception loading core: {}", e.what()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void deferredInitialization() {
|
||||
g_deferredInitRunning = true;
|
||||
|
||||
// Wait 3 seconds for TeamSpeak GUI to fully initialize
|
||||
std::this_thread::sleep_for(std::chrono::seconds(30));
|
||||
|
||||
// Wait for TS functions to be set (up to 2 more seconds)
|
||||
int waitCount = 0;
|
||||
while (!g_ts3FunctionsReady.load() && waitCount < 20) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
waitCount++;
|
||||
{ \
|
||||
strncpy(dest, src, destSize - 1); \
|
||||
(dest)[destSize - 1] = '\0'; \
|
||||
}
|
||||
|
||||
if (!g_ts3FunctionsReady.load()) {
|
||||
std::cerr << "[Echoion] TS3 functions not ready, aborting init"
|
||||
TS3_PLUGIN_API void ts3plugin_registerPluginID(const char* id)
|
||||
{
|
||||
const size_t sz = strlen(id) + 1;
|
||||
pluginID = std::make_unique<char[]>(sz);
|
||||
_strcpy(pluginID.get(), sz, id);
|
||||
std::cout << std::format("[Echoion] Plugin ID: {}", pluginID.get())
|
||||
<< std::endl;
|
||||
g_deferredInitRunning = false;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "[Echoion] Starting deferred initialization..." << std::endl;
|
||||
// Forward declarations for helper functions
|
||||
static bool reregisterPluginInterface();
|
||||
static bool reloadCoreWithInterface();
|
||||
|
||||
try {
|
||||
// Create plugin interface
|
||||
g_pluginInterface = std::make_shared<PluginInterface>();
|
||||
|
||||
// Initialize plugin interface with TS functions
|
||||
auto result = g_pluginInterface->initialize(g_ts3Functions.get());
|
||||
if (!result) {
|
||||
std::cerr << std::format("[Echoion] Failed to initialize plugin interface: {}",
|
||||
result.error().message) << std::endl;
|
||||
g_deferredInitRunning = false;
|
||||
return;
|
||||
}
|
||||
static bool ensureCoreLoaded()
|
||||
{
|
||||
if (g_coreLoader && g_coreLoader->isLoaded()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Register audio device
|
||||
auto deviceResult = g_pluginInterface->registerAudioDevice();
|
||||
if (!deviceResult) {
|
||||
std::cout << std::format("[Echoion] Audio device skipped: {}",
|
||||
deviceResult.error().message) << std::endl;
|
||||
} else {
|
||||
std::cout << "[Echoion] Audio device registered" << std::endl;
|
||||
}
|
||||
|
||||
// Load Core library (Core will handle config loading and auto-connect)
|
||||
if (ensureCoreLoaded()) {
|
||||
std::cout << "[Echoion] Core loaded successfully" << std::endl;
|
||||
if (g_coreLoader) {
|
||||
g_coreLoader->enableAutoReload();
|
||||
}
|
||||
} else {
|
||||
std::cout << "[Echoion] Core failed to load" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "[Echoion] Initialization complete" << std::endl;
|
||||
g_deferredInitComplete = true;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << std::format("[Echoion] Deferred init failed: {}", e.what()) << std::endl;
|
||||
}
|
||||
|
||||
g_deferredInitRunning = false;
|
||||
}
|
||||
try {
|
||||
if (!g_coreLoader) {
|
||||
g_coreLoader = std::make_unique<CoreLoader>();
|
||||
// Set up callback for auto-reload to re-register plugin
|
||||
// interface
|
||||
g_coreLoader->setHotReloadCallback([]() {
|
||||
std::cout << "[Echoion] Auto-reload callback triggered, "
|
||||
"re-registering interface..."
|
||||
<< std::endl;
|
||||
reregisterPluginInterface();
|
||||
});
|
||||
}
|
||||
|
||||
TS3_PLUGIN_API int ts3plugin_init() {
|
||||
std::cout << "[Echoion] Plugin init started" << std::endl;
|
||||
g_deferredInitThread = std::jthread(deferredInitialization);
|
||||
return 0;
|
||||
}
|
||||
// Pass config path to CoreLoader (Core will load config internally)
|
||||
g_coreLoader->setConfigPath(getConfigPath());
|
||||
std::cout << std::format("[Echoion] Config path: {}",
|
||||
getConfigPath().string())
|
||||
<< std::endl;
|
||||
|
||||
TS3_PLUGIN_API void ts3plugin_shutdown() {
|
||||
std::cout << "[Echoion] Shutting down..." << std::endl;
|
||||
std::cout << std::format("[Echoion] Loading Core from: {}",
|
||||
getCorePath().string())
|
||||
<< std::endl;
|
||||
auto loadResult = g_coreLoader->load(getCorePath());
|
||||
if (!loadResult) {
|
||||
std::cerr << std::format("[Echoion] Failed to load core: {}",
|
||||
loadResult.error().message)
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
g_deferredInitRunning = false;
|
||||
|
||||
if (g_deferredInitThread.joinable()) {
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
while (g_deferredInitRunning &&
|
||||
std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
if (g_deferredInitThread.joinable()) {
|
||||
if (g_deferredInitRunning) {
|
||||
g_deferredInitThread.detach();
|
||||
} else {
|
||||
g_deferredInitThread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Get core and set plugin interface
|
||||
auto core = g_coreLoader->getCore();
|
||||
if (core && g_pluginInterface) {
|
||||
core->setPluginInterface(g_pluginInterface);
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->disconnectFromServer();
|
||||
}
|
||||
// Register for plugin events from Core
|
||||
g_pluginInterface->registerEventCallback(
|
||||
[core](const Api::PluginEvent& event) {
|
||||
core->onPluginEvent(event);
|
||||
});
|
||||
|
||||
if (g_coreLoader) {
|
||||
g_coreLoader->disableAutoReload();
|
||||
g_coreLoader->unload();
|
||||
g_coreLoader.reset();
|
||||
}
|
||||
// Trigger auto-connect now that plugin interface is set
|
||||
core->triggerAutoConnect();
|
||||
}
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->shutdown();
|
||||
g_pluginInterface = nullptr;
|
||||
}
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << std::format("[Echoion] Exception loading core: {}",
|
||||
e.what())
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
g_ts3Functions = nullptr;
|
||||
g_ts3FunctionsReady = false;
|
||||
void deferredInitialization()
|
||||
{
|
||||
g_deferredInitRunning = true;
|
||||
|
||||
std::cout << "[Echoion] Shutdown complete" << std::endl;
|
||||
}
|
||||
// Wait 3 seconds for TeamSpeak GUI to fully initialize
|
||||
std::this_thread::sleep_for(std::chrono::seconds(30));
|
||||
|
||||
// Handle incoming text messages
|
||||
TS3_PLUGIN_API void ts3plugin_onTextMessageEvent(uint64 serverConnectionHandlerId,
|
||||
anyID targetMode, anyID toID, anyID fromID, const char* fromName,
|
||||
const char* fromUniqueIdentifier, const char* message, int ffIgnored) {
|
||||
|
||||
anyID myID = 0;
|
||||
if (g_ts3Functions && g_ts3Functions->getClientID(serverConnectionHandlerId, &myID) == ERROR_ok) {
|
||||
if (fromID == myID) return;
|
||||
}
|
||||
// Wait for TS functions to be set (up to 2 more seconds)
|
||||
int waitCount = 0;
|
||||
while (!g_ts3FunctionsReady.load() && waitCount < 20) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
waitCount++;
|
||||
}
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->onTextMessageReceived(serverConnectionHandlerId, targetMode,
|
||||
fromID, message);
|
||||
}
|
||||
}
|
||||
if (!g_ts3FunctionsReady.load()) {
|
||||
std::cerr << "[Echoion] TS3 functions not ready, aborting init"
|
||||
<< std::endl;
|
||||
g_deferredInitRunning = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle client connect/disconnect
|
||||
TS3_PLUGIN_API void ts3plugin_onClientMoveEvent(uint64 serverConnectionHandlerId,
|
||||
anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility,
|
||||
const char* moveMessage) {
|
||||
|
||||
if (!g_pluginInterface) return;
|
||||
|
||||
if (oldChannelID == 0) {
|
||||
g_pluginInterface->onClientConnected(serverConnectionHandlerId, clientID);
|
||||
} else if (newChannelID == 0) {
|
||||
g_pluginInterface->onClientDisconnected(serverConnectionHandlerId, clientID);
|
||||
} else {
|
||||
g_pluginInterface->onClientMoved(serverConnectionHandlerId, clientID, newChannelID);
|
||||
}
|
||||
}
|
||||
std::cout << "[Echoion] Starting deferred initialization..."
|
||||
<< std::endl;
|
||||
|
||||
TS3_PLUGIN_API void ts3plugin_onClientMoveTimeoutEvent(uint64 serverConnectionHandlerId,
|
||||
anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility,
|
||||
const char* timeoutMessage) {
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->onClientDisconnected(serverConnectionHandlerId, clientID);
|
||||
}
|
||||
}
|
||||
try {
|
||||
// Create plugin interface
|
||||
g_pluginInterface = std::make_shared<PluginInterface>();
|
||||
|
||||
// Helper to re-register plugin interface after hot reload (Core already reloaded)
|
||||
static bool reregisterPluginInterface() {
|
||||
if (!g_coreLoader) {
|
||||
std::cerr << "[Echoion] Cannot re-register: CoreLoader not available" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "[Echoion] Re-registering plugin interface after hot reload..." << std::endl;
|
||||
|
||||
// Get the new core instance
|
||||
auto core = g_coreLoader->getCore();
|
||||
if (!core) {
|
||||
std::cerr << "[Echoion] ERROR: Core is null!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!g_pluginInterface) {
|
||||
std::cerr << "[Echoion] ERROR: PluginInterface is null!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << std::format("[Echoion] New Core pointer: {}", reinterpret_cast<uintptr_t>(core.get())) << std::endl;
|
||||
|
||||
// Set the plugin interface first (before registering callbacks)
|
||||
core->setPluginInterface(g_pluginInterface);
|
||||
std::cout << "[Echoion] Plugin interface set on new Core" << std::endl;
|
||||
|
||||
// Clear old callbacks (they point to destroyed Core) and register new one
|
||||
std::cout << "[Echoion] About to clear callbacks..." << std::endl;
|
||||
g_pluginInterface->clearEventCallbacks();
|
||||
std::cout << "[Echoion] Old event callbacks cleared" << std::endl;
|
||||
|
||||
std::cout << "[Echoion] About to register new callback..." << std::endl;
|
||||
g_pluginInterface->registerEventCallback([core](const Api::PluginEvent& event) {
|
||||
core->onPluginEvent(event);
|
||||
});
|
||||
std::cout << "[Echoion] New event callback registered" << std::endl;
|
||||
|
||||
std::cout << "[Echoion] Re-registration complete - bot should respond to commands" << std::endl;
|
||||
return true;
|
||||
}
|
||||
// Initialize plugin interface with TS functions
|
||||
auto result = g_pluginInterface->initialize(g_ts3Functions.get());
|
||||
if (!result) {
|
||||
std::cerr << std::format("[Echoion] Failed to initialize "
|
||||
"plugin interface: {}",
|
||||
result.error().message)
|
||||
<< std::endl;
|
||||
g_deferredInitRunning = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Helper to reload core and re-register plugin interface (for manual reload)
|
||||
static bool reloadCoreWithInterface() {
|
||||
if (!g_coreLoader) {
|
||||
std::cerr << "[Echoion] Cannot reload: CoreLoader not available" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "[Echoion] Starting hot reload (Plugin)..." << std::endl;
|
||||
|
||||
auto result = g_coreLoader->hotReload(getCorePath());
|
||||
if (!result) {
|
||||
std::cerr << std::format("[Echoion] Hot reload failed: {}", result.error().message) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now re-register the plugin interface
|
||||
return reregisterPluginInterface();
|
||||
}
|
||||
// Register audio device
|
||||
auto deviceResult = g_pluginInterface->registerAudioDevice();
|
||||
if (!deviceResult) {
|
||||
std::cout << std::format("[Echoion] Audio device skipped: {}",
|
||||
deviceResult.error().message)
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "[Echoion] Audio device registered" << std::endl;
|
||||
}
|
||||
|
||||
// Handle plugin commands from TS3
|
||||
TS3_PLUGIN_API void ts3plugin_onPluginCommandEvent(uint64 serverConnectionHandlerId,
|
||||
const char* pluginName, const char* command) {
|
||||
|
||||
if (std::strcmp(command, "reload") == 0) {
|
||||
if (ensureCoreLoaded()) {
|
||||
reloadCoreWithInterface();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Load Core library (Core will handle config loading and
|
||||
// auto-connect)
|
||||
if (ensureCoreLoaded()) {
|
||||
std::cout << "[Echoion] Core loaded successfully" << std::endl;
|
||||
if (g_coreLoader) {
|
||||
g_coreLoader->enableAutoReload();
|
||||
}
|
||||
} else {
|
||||
std::cout << "[Echoion] Core failed to load" << std::endl;
|
||||
}
|
||||
|
||||
// Menu item callbacks
|
||||
TS3_PLUGIN_API void ts3plugin_onMenuItemEvent(uint64 serverConnectionHandlerId,
|
||||
enum PluginMenuType type, int menuItemID, uint64 selectedItemID) {
|
||||
|
||||
if (!ensureCoreLoaded()) return;
|
||||
|
||||
auto core = g_coreLoader->getCore();
|
||||
if (!core) return;
|
||||
|
||||
switch (menuItemID) {
|
||||
case 1: { // Play/Pause
|
||||
auto state = core->getPlaybackState();
|
||||
if (state) {
|
||||
if (state->isPlaying && !state->isPaused) {
|
||||
core->pause();
|
||||
} else if (state->isPaused) {
|
||||
core->resume();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: // Skip
|
||||
core->skip(1);
|
||||
break;
|
||||
case 3: // Stop
|
||||
core->stop();
|
||||
break;
|
||||
case 4: // Hot Reload Core
|
||||
reloadCoreWithInterface();
|
||||
break;
|
||||
case 5: // Connect/Disconnect
|
||||
if (g_pluginInterface) {
|
||||
if (g_pluginInterface->isConnected()) {
|
||||
g_pluginInterface->disconnectFromServer();
|
||||
} else {
|
||||
// Core handles connection, just trigger reconnect
|
||||
if (auto result = g_pluginInterface->reconnect(); !result) {
|
||||
std::cerr << std::format("[Echoion] Reconnect failed: {}", result.error().message) << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << "[Echoion] Initialization complete" << std::endl;
|
||||
g_deferredInitComplete = true;
|
||||
|
||||
// Connection status changes
|
||||
TS3_PLUGIN_API void ts3plugin_onConnectStatusChangeEvent(uint64 serverConnectionHandlerId,
|
||||
int newStatus, unsigned int errorNumber) {
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->setServerConnectionHandlerId(serverConnectionHandlerId);
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << std::format("[Echoion] Deferred init failed: {}",
|
||||
e.what())
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
switch (newStatus) {
|
||||
case STATUS_CONNECTING:
|
||||
std::cout << "[Echoion] Connecting..." << std::endl;
|
||||
break;
|
||||
case STATUS_CONNECTED:
|
||||
std::cout << "[Echoion] Connected" << std::endl;
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->closeAudioDevice();
|
||||
g_pluginInterface->openAudioDevice();
|
||||
}
|
||||
break;
|
||||
case STATUS_CONNECTION_ESTABLISHED:
|
||||
std::cout << "[Echoion] Connection established" << std::endl;
|
||||
break;
|
||||
case STATUS_DISCONNECTED:
|
||||
std::cout << "[Echoion] Disconnected" << std::endl;
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->closeAudioDevice();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_deferredInitRunning = false;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
TS3_PLUGIN_API int ts3plugin_init()
|
||||
{
|
||||
std::cout << "[Echoion] Plugin init started" << std::endl;
|
||||
g_deferredInitThread = std::jthread(deferredInitialization);
|
||||
return 0;
|
||||
}
|
||||
|
||||
TS3_PLUGIN_API void ts3plugin_shutdown()
|
||||
{
|
||||
std::cout << "[Echoion] Shutting down..." << std::endl;
|
||||
|
||||
g_deferredInitRunning = false;
|
||||
|
||||
if (g_deferredInitThread.joinable()) {
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
while (g_deferredInitRunning &&
|
||||
std::chrono::steady_clock::now() - start <
|
||||
std::chrono::seconds(5)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
if (g_deferredInitThread.joinable()) {
|
||||
if (g_deferredInitRunning) {
|
||||
g_deferredInitThread.detach();
|
||||
} else {
|
||||
g_deferredInitThread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->disconnectFromServer();
|
||||
}
|
||||
|
||||
if (g_coreLoader) {
|
||||
g_coreLoader->disableAutoReload();
|
||||
g_coreLoader->unload();
|
||||
g_coreLoader.reset();
|
||||
}
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->shutdown();
|
||||
g_pluginInterface = nullptr;
|
||||
}
|
||||
|
||||
g_ts3Functions = nullptr;
|
||||
g_ts3FunctionsReady = false;
|
||||
|
||||
std::cout << "[Echoion] Shutdown complete" << std::endl;
|
||||
}
|
||||
|
||||
// Handle incoming text messages
|
||||
TS3_PLUGIN_API void ts3plugin_onTextMessageEvent(
|
||||
uint64 serverConnectionHandlerId, anyID targetMode, anyID toID,
|
||||
anyID fromID, const char* fromName, const char* fromUniqueIdentifier,
|
||||
const char* message, int ffIgnored)
|
||||
{
|
||||
|
||||
anyID myID = 0;
|
||||
if (g_ts3Functions &&
|
||||
g_ts3Functions->getClientID(serverConnectionHandlerId, &myID) ==
|
||||
ERROR_ok) {
|
||||
if (fromID == myID) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->onTextMessageReceived(
|
||||
serverConnectionHandlerId, targetMode, fromID, message);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle client connect/disconnect
|
||||
TS3_PLUGIN_API void ts3plugin_onClientMoveEvent(
|
||||
uint64 serverConnectionHandlerId, anyID clientID, uint64 oldChannelID,
|
||||
uint64 newChannelID, int visibility, const char* moveMessage)
|
||||
{
|
||||
|
||||
if (!g_pluginInterface) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (oldChannelID == 0) {
|
||||
g_pluginInterface->onClientConnected(serverConnectionHandlerId,
|
||||
clientID);
|
||||
} else if (newChannelID == 0) {
|
||||
g_pluginInterface->onClientDisconnected(serverConnectionHandlerId,
|
||||
clientID);
|
||||
} else {
|
||||
g_pluginInterface->onClientMoved(serverConnectionHandlerId,
|
||||
clientID, newChannelID);
|
||||
}
|
||||
}
|
||||
|
||||
TS3_PLUGIN_API void ts3plugin_onClientMoveTimeoutEvent(
|
||||
uint64 serverConnectionHandlerId, anyID clientID, uint64 oldChannelID,
|
||||
uint64 newChannelID, int visibility, const char* timeoutMessage)
|
||||
{
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->onClientDisconnected(serverConnectionHandlerId,
|
||||
clientID);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to re-register plugin interface after hot reload (Core already
|
||||
// reloaded)
|
||||
static bool reregisterPluginInterface()
|
||||
{
|
||||
if (!g_coreLoader) {
|
||||
std::cerr
|
||||
<< "[Echoion] Cannot re-register: CoreLoader not available"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout
|
||||
<< "[Echoion] Re-registering plugin interface after hot reload..."
|
||||
<< std::endl;
|
||||
|
||||
// Get the new core instance
|
||||
auto core = g_coreLoader->getCore();
|
||||
if (!core) {
|
||||
std::cerr << "[Echoion] ERROR: Core is null!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!g_pluginInterface) {
|
||||
std::cerr << "[Echoion] ERROR: PluginInterface is null!"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << std::format("[Echoion] New Core pointer: {}",
|
||||
reinterpret_cast<uintptr_t>(core.get()))
|
||||
<< std::endl;
|
||||
|
||||
// Set the plugin interface first (before registering callbacks)
|
||||
core->setPluginInterface(g_pluginInterface);
|
||||
std::cout << "[Echoion] Plugin interface set on new Core" << std::endl;
|
||||
|
||||
// Clear old callbacks (they point to destroyed Core) and register new
|
||||
// one
|
||||
std::cout << "[Echoion] About to clear callbacks..." << std::endl;
|
||||
g_pluginInterface->clearEventCallbacks();
|
||||
std::cout << "[Echoion] Old event callbacks cleared" << std::endl;
|
||||
|
||||
std::cout << "[Echoion] About to register new callback..." << std::endl;
|
||||
g_pluginInterface->registerEventCallback(
|
||||
[core](const Api::PluginEvent& event) {
|
||||
core->onPluginEvent(event);
|
||||
});
|
||||
std::cout << "[Echoion] New event callback registered" << std::endl;
|
||||
core->skip(0);
|
||||
core->resume();
|
||||
std::cout << "[Echoion] Re-registration complete - bot should respond "
|
||||
"to commands"
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper to reload core and re-register plugin interface (for manual
|
||||
// reload)
|
||||
static bool reloadCoreWithInterface()
|
||||
{
|
||||
if (!g_coreLoader) {
|
||||
std::cerr << "[Echoion] Cannot reload: CoreLoader not available"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "[Echoion] Starting hot reload (Plugin)..." << std::endl;
|
||||
|
||||
auto result = g_coreLoader->hotReload(getCorePath());
|
||||
if (!result) {
|
||||
std::cerr << std::format("[Echoion] Hot reload failed: {}",
|
||||
result.error().message)
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now re-register the plugin interface
|
||||
return reregisterPluginInterface();
|
||||
}
|
||||
|
||||
// Handle plugin commands from TS3
|
||||
TS3_PLUGIN_API void
|
||||
ts3plugin_onPluginCommandEvent(uint64 serverConnectionHandlerId,
|
||||
const char* pluginName,
|
||||
const char* command)
|
||||
{
|
||||
|
||||
if (std::strcmp(command, "reload") == 0) {
|
||||
if (ensureCoreLoaded()) {
|
||||
reloadCoreWithInterface();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Menu item callbacks
|
||||
TS3_PLUGIN_API void
|
||||
ts3plugin_onMenuItemEvent(uint64 serverConnectionHandlerId,
|
||||
enum PluginMenuType type, int menuItemID,
|
||||
uint64 selectedItemID)
|
||||
{
|
||||
|
||||
if (!ensureCoreLoaded()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto core = g_coreLoader->getCore();
|
||||
if (!core) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (menuItemID) {
|
||||
case 1:
|
||||
{ // Play/Pause
|
||||
auto state = core->getPlaybackSummary();
|
||||
if (state) {
|
||||
if (state->isPlaying && !state->isPaused) {
|
||||
core->pause();
|
||||
} else if (state->isPaused) {
|
||||
core->resume();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: // Skip
|
||||
core->skip(1);
|
||||
break;
|
||||
case 3: // Stop
|
||||
core->stop();
|
||||
break;
|
||||
case 4: // Hot Reload Core
|
||||
reloadCoreWithInterface();
|
||||
break;
|
||||
case 5: // Connect/Disconnect
|
||||
if (g_pluginInterface) {
|
||||
if (g_pluginInterface->isConnected()) {
|
||||
g_pluginInterface->disconnectFromServer();
|
||||
} else {
|
||||
// Core handles connection, just trigger reconnect
|
||||
if (auto result = g_pluginInterface->reconnect();
|
||||
!result) {
|
||||
std::cerr
|
||||
<< std::format("[Echoion] Reconnect failed: {}",
|
||||
result.error().message)
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Connection status changes
|
||||
TS3_PLUGIN_API void
|
||||
ts3plugin_onConnectStatusChangeEvent(uint64 serverConnectionHandlerId,
|
||||
int newStatus,
|
||||
unsigned int errorNumber)
|
||||
{
|
||||
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->setServerConnectionHandlerId(
|
||||
serverConnectionHandlerId);
|
||||
}
|
||||
|
||||
switch (newStatus) {
|
||||
case STATUS_CONNECTING:
|
||||
std::cout << "[Echoion] Connecting..." << std::endl;
|
||||
break;
|
||||
case STATUS_CONNECTED:
|
||||
std::cout << "[Echoion] Connected" << std::endl;
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->closeAudioDevice();
|
||||
g_pluginInterface->openAudioDevice();
|
||||
}
|
||||
break;
|
||||
case STATUS_CONNECTION_ESTABLISHED:
|
||||
std::cout << "[Echoion] Connection established" << std::endl;
|
||||
break;
|
||||
case STATUS_DISCONNECTED:
|
||||
std::cout << "[Echoion] Disconnected" << std::endl;
|
||||
if (g_pluginInterface) {
|
||||
g_pluginInterface->closeAudioDevice();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
} // namespace Echoion::Plugin
|
||||
Reference in new issue
Block a user