Initial commit

This commit is contained in:
faumaray committed 2026-02-01 23:07:06 +03:00
commit 8b8a7f4f9a
17 files changed
+4930

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
#pragma once
#include <echoion/api.hpp>
#include <dylib.hpp>
#include "FileWatcher.hpp"
#include "echoion/api/ConfigTypes.hpp"
#include <memory>
#include <mutex>
#include <atomic>
#include <filesystem>
#include <thread>
#include <condition_variable>
#include <queue>
namespace Echoion::Plugin {
class CoreLoader {
public:
CoreLoader();
~CoreLoader();
Api::Result<void> load(const std::filesystem::path& libraryPath);
Api::Result<void> unload();
Api::Result<void> hotReload(const std::filesystem::path& newLibraryPath);
[[nodiscard]] bool isLoaded() const noexcept;
[[nodiscard]] Api::CoreInterfacePtr getCore() const;
void setConfigPath(const std::filesystem::path& configPath);
[[nodiscard]] std::filesystem::path getConfigPath() const;
[[nodiscard]] bool isHotReloading() const noexcept;
// Automatic hot reload
void enableAutoReload();
void disableAutoReload();
[[nodiscard]] bool isAutoReloadEnabled() const noexcept;
private:
Api::Result<void> loadLibraryInternal(const std::filesystem::path& path);
Api::Result<void> unloadLibraryInternal();
[[nodiscard]] Api::HotReloadState saveStateInternal();
[[nodiscard]] Api::Result<void> restoreStateInternal(const Api::HotReloadState& state);
[[nodiscard]] std::filesystem::path getTempLibraryPath(const std::filesystem::path& original) const;
void cleanupTempFiles();
std::unique_ptr<dylib::library> library_;
Api::CoreInterfacePtr core_;
std::filesystem::path currentLibraryPath_;
std::filesystem::path tempLibraryPath_;
std::filesystem::path configPath_;
mutable std::mutex mutex_;
std::atomic<bool> loaded_{false};
std::atomic<bool> hotReloading_{false};
// Auto reload
FileWatcher fileWatcher_;
std::atomic<bool> autoReloadEnabled_{false};
void onCoreFileChanged();
};
} // namespace Echoion::Plugin
+62
View File
@@ -0,0 +1,62 @@
#pragma once
#include <functional>
#include <filesystem>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>
namespace Echoion::Plugin {
/**
* @brief Simple file watcher that monitors a file for changes
* and triggers a callback when the file is modified.
*/
class FileWatcher {
public:
using ChangeCallback = std::function<void()>;
FileWatcher();
~FileWatcher();
// Disable copy
FileWatcher(const FileWatcher&) = delete;
FileWatcher& operator=(const FileWatcher&) = delete;
/**
* @brief Start watching a file for changes
* @param filePath Path to the file to watch
* @param callback Function to call when file changes
* @param intervalMs Polling interval in milliseconds
*/
void startWatching(
const std::filesystem::path& filePath,
ChangeCallback callback,
std::chrono::milliseconds intervalMs = std::chrono::milliseconds(1000)
);
/**
* @brief Stop watching
*/
void stopWatching();
[[nodiscard]] bool isWatching() const noexcept;
private:
void watchLoop();
std::filesystem::path watchedPath_;
ChangeCallback callback_;
std::chrono::milliseconds intervalMs_{1000};
std::atomic<bool> watching_{false};
std::atomic<bool> stopRequested_{false};
std::thread watchThread_;
std::mutex mutex_;
// Store last modification time
std::filesystem::file_time_type lastWriteTime_;
std::uintmax_t lastFileSize_ = 0;
};
} // namespace Echoion::Plugin
+117
View File
@@ -0,0 +1,117 @@
#pragma once
#include <echoion/api.hpp>
#include <dylib.hpp>
#include <memory>
#include <mutex>
#include <atomic>
#include <thread>
#include <queue>
#include <condition_variable>
// TeamSpeak SDK headers
#include "ts3_functions.h"
namespace Echoion::Plugin {
// Audio device configuration
struct AudioDeviceConfig {
static constexpr const char* DEVICE_ID = "echoion_audio_device";
static constexpr const char* DEVICE_NAME = "Echoion Music Bot Audio";
static constexpr int SAMPLE_RATE = 192000; // Match reference implementation
static constexpr int CHANNELS = 2;
static constexpr int SAMPLE_SIZE = sizeof(int16_t);
};
// Plugin implementation of IPluginInterface for the Core to use
class PluginInterface : public Api::IPluginInterface {
public:
PluginInterface();
~PluginInterface() override;
// Initialize with TeamSpeak functions
Api::Result<void> initialize(TS3Functions* ts3Functions);
// IPluginInterface implementation
Api::Result<void> initialize() override;
Api::Result<void> shutdown() override;
// Client operations
[[nodiscard]] Api::Result<std::vector<Api::ClientInfo>> getClients() const override;
[[nodiscard]] Api::Result<std::optional<Api::ClientInfo>> getClient(uint64_t clientId) const override;
Api::Result<void> moveClient(uint64_t clientId, uint64_t channelId) override;
Api::Result<void> kickClient(uint64_t clientId, const std::string& reason) override;
// Channel operations
[[nodiscard]] Api::Result<std::vector<Api::ChannelInfo>> getChannels() const override;
[[nodiscard]] Api::Result<std::optional<Api::ChannelInfo>> getChannel(uint64_t channelId) const override;
// Messaging
Api::Result<void> sendTextMessage(uint64_t targetId, const std::string& message) override;
Api::Result<void> sendChannelMessage(uint64_t channelId, const std::string& message) override;
Api::Result<void> sendServerMessage(const std::string& message) override;
// Audio
Api::Result<void> sendAudioData(const std::span<const uint8_t>& data) override;
Api::Result<void> setWhisperList(const std::vector<uint64_t>& clientIds) override;
// Event subscription
void registerEventCallback(const Api::EventCallback& callback) override;
void unregisterEventCallback(const Api::EventCallback& callback) override;
// TeamSpeak event handlers (called by TeamSpeak SDK)
void onClientConnected(uint64_t serverConnectionHandlerId, uint64_t clientId);
void onClientDisconnected(uint64_t serverConnectionHandlerId, uint64_t clientId);
void onClientMoved(uint64_t serverConnectionHandlerId, uint64_t clientId, uint64_t newChannelId);
void onTextMessageReceived(uint64_t serverConnectionHandlerId, uint64_t targetMode,
uint64_t fromId, const std::string& message);
void onVoiceDataReceived(uint64_t serverConnectionHandlerId, uint64_t clientId,
const std::span<const uint8_t>& data);
// Server connection management (IPluginInterface implementation)
Api::Result<void> connectToServer(const Api::ServerConfig& config) override;
Api::Result<void> disconnectFromServer() override;
[[nodiscard]] bool isConnected() const override;
Api::Result<void> reconnect() override;
// Audio device management
Api::Result<void> registerAudioDevice();
Api::Result<void> unregisterAudioDevice();
Api::Result<void> openAudioDevice();
[[nodiscard]] bool isAudioDeviceRegistered() const { return audioDeviceRegistered_; }
Api::Result<void> closeAudioDevice();
[[nodiscard]] bool isAudioDeviceOpen() const;
// Set server connection handler
void setServerConnectionHandlerId(uint64_t handlerId);
[[nodiscard]] uint64_t getServerConnectionHandlerId() const;
// Audio data submission (for internal use)
void submitAudioData(const std::span<const int16_t> samples);
private:
void broadcastEvent(const Api::PluginEvent& event);
std::string getClientName(uint64_t clientId) const;
std::string getChannelName(uint64_t channelId) const;
TS3Functions* ts3Functions_ = nullptr;
uint64_t serverConnectionHandlerId_ = 0;
std::atomic<bool> initialized_{false};
std::atomic<bool> audioDeviceRegistered_{false};
std::atomic<bool> audioDeviceOpen_{false};
std::vector<Api::EventCallback> eventCallbacks_;
mutable std::mutex eventMutex_;
// Audio data queue
std::queue<std::vector<int16_t>> audioQueue_;
std::mutex audioMutex_;
std::condition_variable audioCv_;
std::atomic<bool> stopAudioThread_{false};
std::jthread audioThread_;
// Connection info
Api::ServerConfig currentServerConfig_;
};
} // namespace Echoion::Plugin