Hot reload fix

This commit is contained in:
faumaray committed 2026-02-03 04:48:29 +03:00
1 parent 8b8a7f4f9a
commit 0fa9db4691
6 files changed
+677 -178

No files matched your search

+90 -24
View File
@@ -103,6 +103,10 @@ TS3_PLUGIN_API void ts3plugin_registerPluginID(const char *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;
@@ -111,6 +115,11 @@ static bool ensureCoreLoaded() {
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)
@@ -149,24 +158,25 @@ void deferredInitialization() {
g_deferredInitRunning = true;
// Wait 3 seconds for TeamSpeak GUI to fully initialize
std::this_thread::sleep_for(std::chrono::seconds(3));
// 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_ts3FunctionsReady.load()) {
std::cerr << "[Echoion] TS3 functions not ready, aborting init" << std::endl;
g_deferredInitRunning = false;
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++;
}
if (!g_ts3FunctionsReady.load()) {
std::cerr << "[Echoion] TS3 functions not ready, aborting init"
<< std::endl;
g_deferredInitRunning = false;
return;
}
std::cout << "[Echoion] Starting deferred initialization..." << std::endl;
try {
}
std::cout << "[Echoion] Starting deferred initialization..." << std::endl;
try {
// Create plugin interface
g_pluginInterface = std::make_shared<PluginInterface>();
@@ -297,16 +307,74 @@ TS3_PLUGIN_API void ts3plugin_onClientMoveTimeoutEvent(uint64 serverConnectionHa
}
}
// 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;
}
// 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()) {
auto result = g_coreLoader->hotReload(getCorePath());
if (!result) {
std::cerr << std::format("[Echoion] Hot reload failed: {}", result.error().message) << std::endl;
}
reloadCoreWithInterface();
}
}
}
@@ -339,9 +407,7 @@ TS3_PLUGIN_API void ts3plugin_onMenuItemEvent(uint64 serverConnectionHandlerId,
core->stop();
break;
case 4: // Hot Reload Core
if (auto result = g_coreLoader->hotReload(getCorePath()); !result) {
std::cerr << std::format("[Echoion] Hot reload failed: {}", result.error().message) << std::endl;
}
reloadCoreWithInterface();
break;
case 5: // Connect/Disconnect
if (g_pluginInterface) {