195 lines
6.5 KiB
C++
195 lines
6.5 KiB
C++
#pragma once
|
|
#include <PluginInterface.hpp>
|
|
#include <PluginTypes.hpp>
|
|
#include <Types.hpp>
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <dylib.hpp>
|
|
#include <future>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <queue>
|
|
#include <thread>
|
|
|
|
// 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; // 192kHz for high quality audio (CODEC_OPUS_MUSIC)
|
|
static constexpr int CHANNELS = 2;
|
|
static constexpr int SAMPLE_SIZE = sizeof(int16_t);
|
|
};
|
|
|
|
// Server groups cache entry
|
|
struct ServerGroupsCacheEntry {
|
|
std::vector<uint64_t> groups;
|
|
std::chrono::steady_clock::time_point timestamp;
|
|
};
|
|
|
|
// 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() override;
|
|
[[nodiscard]] Api::Result<std::optional<Api::ClientInfo>>
|
|
getClient(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
getClientIp(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
getClientAvatarPath(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
getClientAvatarUrl(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
requestClientAvatar(uint64_t clientId,
|
|
const std::string& destDir) 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 short>& 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;
|
|
void clearEventCallbacks();
|
|
|
|
// TeamSpeak event handlers (called by TeamSpeak SDK)
|
|
void onConnectionEstablished(uint64_t serverConnectionHandlerId);
|
|
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 group list callback (called by TeamSpeak SDK)
|
|
void onServerGroupByClientID(uint64_t serverConnectionHandlerId,
|
|
uint64_t clientDatabaseId,
|
|
const char* groupName,
|
|
uint64_t serverGroupId,
|
|
uint64_t clientId);
|
|
|
|
// 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 noexcept
|
|
{
|
|
return audioDeviceRegistered_.load();
|
|
}
|
|
Api::Result<void> closeAudioDevice();
|
|
[[nodiscard]] bool isAudioDeviceOpen() const noexcept;
|
|
|
|
// Set server connection handler
|
|
void setServerConnectionHandlerId(uint64_t handlerId) noexcept;
|
|
[[nodiscard]] uint64_t
|
|
getServerConnectionHandlerId() const noexcept;
|
|
|
|
// Audio data submission (for internal use)
|
|
void submitAudioData(std::span<const short> samples);
|
|
|
|
// Automatic configuration
|
|
Api::Result<void> configureAudioProfile();
|
|
Api::Result<void> checkAndHandleFirstRun();
|
|
|
|
private:
|
|
|
|
void broadcastEvent(const Api::PluginEvent& event);
|
|
[[nodiscard]] std::string getClientName(uint64_t clientId) const;
|
|
[[nodiscard]] std::string getChannelName(uint64_t channelId) const;
|
|
|
|
// Fetch server groups for a client (returns future that completes
|
|
// when groups are received)
|
|
[[nodiscard]] std::future<std::vector<uint64_t>>
|
|
fetchServerGroups(uint64_t clientId);
|
|
|
|
// Get cached server groups (or empty if not cached)
|
|
[[nodiscard]] std::vector<uint64_t>
|
|
getCachedServerGroups(uint64_t clientId);
|
|
|
|
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<short>> audioQueue_;
|
|
std::mutex audioMutex_;
|
|
std::condition_variable audioCv_;
|
|
std::atomic<bool> stopAudioThread_{false};
|
|
std::jthread audioThread_;
|
|
|
|
// Server groups cache
|
|
mutable std::mutex serverGroupsMutex_;
|
|
std::map<uint64_t, ServerGroupsCacheEntry> serverGroupsCache_;
|
|
std::map<uint64_t, std::promise<std::vector<uint64_t>>>
|
|
pendingServerGroupRequests_;
|
|
|
|
// Connection info
|
|
Api::ServerConfig currentServerConfig_;
|
|
};
|
|
|
|
} // namespace Echoion::Plugin
|