Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-11 15:06:50 +03:00
commit cf48f2f350
6 files changed
+992

No files matched your search

+166
View File
@@ -0,0 +1,166 @@
// Standalone Echoion host.
//
// Replaces the TeamSpeak-client + plugin sandwich: loads the same hot-reloadable
// Core shared library via the (TeamSpeak-independent) CoreLoader, but backs
// IPluginInterface with a native TeamSpeak UDP protocol client instead of the
// TS3 Client Plugin SDK. The bot therefore runs as a plain daemon — no
// TeamSpeak client, no Weston/RDP, no plugin directory.
#include "echoion/plugin/CoreLoader.hpp"
#include "echoion/standalone/StandaloneClientInterface.hpp"
#include <CoreInterface.hpp>
#include <Void/Log/Log.hpp>
#include <Void/Stacktrace/Stacktrace.hpp>
#include <atomic>
#include <condition_variable>
#include <csignal>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
namespace
{
std::atomic<bool> g_stop{false};
std::condition_variable g_cv;
std::mutex g_mu;
void onSignal(int)
{
g_stop = true;
g_cv.notify_all();
}
std::filesystem::path configDir()
{
if (const char* x = std::getenv("XDG_CONFIG_HOME")) {
return std::filesystem::path(x) / "echoion";
}
if (const char* h = std::getenv("HOME")) {
return std::filesystem::path(h) / ".config" / "echoion";
}
return std::filesystem::path(".") / "echoion";
}
std::string envOr(const char* key, const std::string& fallback)
{
const char* v = std::getenv(key);
return v ? std::string(v) : fallback;
}
int envInt(const char* key, int fallback)
{
const char* v = std::getenv(key);
if (!v) {
return fallback;
}
try {
return std::stoi(v);
} catch (...) {
return fallback;
}
}
// Locate the Core shared library ("Echoion.ts").
std::filesystem::path corePath()
{
if (const char* p = std::getenv("ECHOION_CORE_PATH")) {
return p;
}
for (const char* c :
{"./Echoion.ts", "./bin/Echoion.ts", "./plugins/Echoion.ts"}) {
if (std::filesystem::exists(c)) {
return c;
}
}
return "./Echoion.ts";
}
} // namespace
int main(int argc, char** argv)
{
std::set_terminate([] {
const auto frames = Void::Stacktrace::capture_stacktrace();
std::cerr << Void::Stacktrace::format_stacktrace(frames) << '\n';
std::abort();
});
std::filesystem::path cfgDir = configDir();
std::filesystem::path cfgPath = cfgDir / "config.json";
// Optional CLI: --config <path>
for (int i = 1; i + 1 < argc; ++i) {
if (std::string(argv[i]) == "--config") {
cfgPath = argv[i + 1];
}
}
Echoion::Standalone::StandaloneSettings settings;
settings.identityPath =
envOr("ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string());
settings.securityLevel = envInt("ECHOION_SECURITY_LEVEL", 8);
settings.opusBitrate = envInt("ECHOION_OPUS_BITRATE", 96000);
auto interface = std::make_shared<Echoion::Standalone::StandaloneClientInterface>(
settings);
if (auto r = interface->initialize(); !r) {
Void::Log::error("main", "interface init failed: {}",
r.error().message);
return 1;
}
Echoion::Plugin::CoreLoader loader;
loader.setConfigPath(cfgPath);
auto corePathResolved = corePath();
Void::Log::info("main", "Loading Core from {}", corePathResolved.string());
if (auto r = loader.load(corePathResolved); !r) {
Void::Log::error("main", "failed to load Core: {}", r.error().message);
return 1;
}
auto core = loader.getCore();
if (!core) {
Void::Log::error("main", "Core not available after load");
return 1;
}
core->setPluginInterface(interface);
interface->registerEventCallback([core](const Echoion::Api::PluginEvent& ev) {
std::ignore = core->onPluginEvent(ev);
});
// Re-register the Core callback after each hot reload (new Core instance).
loader.setHotReloadCallback([&loader, interface] {
if (auto c = loader.getCore()) {
c->setPluginInterface(interface);
interface->registerEventCallback(
[c](const Echoion::Api::PluginEvent& ev) {
std::ignore = c->onPluginEvent(ev);
});
c->triggerAutoConnect();
}
});
core->triggerAutoConnect();
loader.enableAutoReload();
std::signal(SIGINT, onSignal);
std::signal(SIGTERM, onSignal);
Void::Log::info("main", "Echoion standalone running. Ctrl-C to stop.");
{
std::unique_lock<std::mutex> lock(g_mu);
g_cv.wait(lock, [] { return g_stop.load(); });
}
Void::Log::info("main", "Shutting down...");
loader.disableAutoReload();
(void)interface->disconnectFromServer();
(void)loader.unload();
(void)interface->shutdown();
return 0;
}