ICoreInterface is the vtable ABI contract between the already-loaded Plugin and a freshly hot-reloaded Core. reloadConfig() was added mid-interface (after getConfigJson), which shifted every later vtable slot — so the still-running Plugin's calls to saveState/restoreState/getStateFormatVersion/onPluginEvent dispatched to the wrong methods on the new Core. Symptom: state restore read a garbage version (2303262719), threw basic_string::_M_create, and left the Core uninitialized (onPluginEvent ignored, initialized=false). Move reloadConfig() to the very end of the interface (append-only) so all pre-existing slots keep their indices. Add a comment documenting the rule. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
164 lines
5.7 KiB
C++
164 lines
5.7 KiB
C++
#pragma once
|
|
|
|
#include "OpaqueHandle.hpp"
|
|
#include "PluginTypes.hpp"
|
|
#include "StateBlob.hpp"
|
|
#include "Types.hpp"
|
|
#include <PluginInterface.hpp>
|
|
#include <expected>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <future>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace Echoion::Api
|
|
{
|
|
|
|
// Forward declarations - Core provides implementations
|
|
struct TrackInfo;
|
|
struct QueueEntry;
|
|
struct PlaylistInfo;
|
|
|
|
// Minimal PlaybackState for Plugin
|
|
struct PlaybackSummary {
|
|
std::optional<TrackSummary> currentTrack;
|
|
bool isPlaying = false;
|
|
bool isPaused = false;
|
|
std::chrono::milliseconds position{0};
|
|
float volume = 0.5f;
|
|
bool repeat = false;
|
|
bool shuffle = false;
|
|
};
|
|
|
|
// Callback types for async operations
|
|
using TrackCallback = std::function<void(const TrackSummary&)>;
|
|
using StateCallback = std::function<void(const PlaybackSummary&)>;
|
|
|
|
// Forward declarations
|
|
class ICoreInterface;
|
|
using CoreInterfacePtr = std::shared_ptr<ICoreInterface>;
|
|
using CoreCreateFunc = std::function<CoreInterfacePtr()>;
|
|
using CoreDestroyFunc = std::function<void(ICoreInterface*)>;
|
|
|
|
// Core interface that the Plugin will use to communicate with the Core
|
|
// MINIMAL VERSION - breaks dependencies on Core-specific types
|
|
class ICoreInterface
|
|
{
|
|
public:
|
|
|
|
virtual ~ICoreInterface() = default;
|
|
|
|
// Lifecycle
|
|
// Plugin passes config file path, Core loads config internally
|
|
virtual Result<void>
|
|
initialize(const std::filesystem::path& configPath) = 0;
|
|
virtual Result<void> shutdown() = 0;
|
|
virtual bool isInitialized() const noexcept = 0;
|
|
|
|
// Playback control - using string identifiers instead of TrackInfo
|
|
virtual Result<void> play(const std::string& trackId) = 0;
|
|
virtual Result<void> pause() = 0;
|
|
virtual Result<void> resume() = 0;
|
|
virtual Result<void> stop() = 0;
|
|
virtual Result<void> skip(uint32_t count = 1) = 0;
|
|
virtual Result<void> seek(std::chrono::milliseconds position) = 0;
|
|
virtual Result<void> setVolume(float volume) = 0;
|
|
|
|
// Queue management - using string identifiers
|
|
virtual Result<uint64_t> queueTrack(const std::string& trackId,
|
|
uint64_t clientId) = 0;
|
|
virtual Result<void> removeFromQueue(uint64_t queueId) = 0;
|
|
virtual Result<void> clearQueue() = 0;
|
|
|
|
virtual Result<void> moveInQueue(uint64_t queueId,
|
|
uint32_t newPosition) = 0;
|
|
|
|
// Track info - return summaries instead of full TrackInfo
|
|
[[nodiscard]] virtual Result<std::optional<TrackSummary>>
|
|
getCurrentTrackSummary() const = 0;
|
|
[[nodiscard]] virtual Result<PlaybackSummary>
|
|
getPlaybackSummary() const = 0;
|
|
|
|
// Playlist - using string identifiers
|
|
virtual Result<void> loadPlaylist(const std::string& playlistId,
|
|
uint64_t clientId) = 0;
|
|
[[nodiscard]] virtual Result<std::vector<std::string>>
|
|
getPlaylistIds() const = 0;
|
|
|
|
// Configuration - using serialization instead of AppConfig
|
|
// Plugin sends JSON string, Core parses internally
|
|
virtual Result<void>
|
|
updateConfigJson(const std::string& configJson) = 0;
|
|
[[nodiscard]] virtual Result<std::string> getConfigJson() const = 0;
|
|
|
|
// Hot reload - using serialization instead of HotReloadState
|
|
// Returns opaque handle that Plugin just passes back
|
|
[[nodiscard]] virtual Result<std::string> prepareForHotReload() = 0;
|
|
virtual Result<void>
|
|
restoreAfterHotReload(const std::string& state) = 0;
|
|
|
|
// Decoupled state management - Plugin never knows state internals
|
|
// Core handles all serialization/deserialization internally
|
|
// State format can change without Plugin rebuild
|
|
[[nodiscard]] virtual StateBlob saveState() = 0;
|
|
virtual Result<void> restoreState(const StateBlob& state) = 0;
|
|
|
|
// Version info for state compatibility checking
|
|
[[nodiscard]] virtual uint32_t getStateFormatVersion() const = 0;
|
|
[[nodiscard]] virtual bool
|
|
canRestoreStateVersion(uint32_t version) const = 0;
|
|
|
|
// Event handling
|
|
virtual void
|
|
registerEventCallback(const EventCallback& callback) = 0;
|
|
virtual void
|
|
unregisterEventCallback(const EventCallback& callback) = 0;
|
|
|
|
// Plugin event processing
|
|
virtual Result<void> onPluginEvent(const PluginEvent& event) = 0;
|
|
|
|
// Status
|
|
[[nodiscard]] virtual std::string getVersion() const = 0;
|
|
[[nodiscard]] virtual std::chrono::system_clock::time_point
|
|
getUptime() const = 0;
|
|
|
|
// Plugin interface
|
|
virtual void
|
|
setPluginInterface(PluginInterfacePtr pluginInterface) = 0;
|
|
|
|
// Trigger auto-connect (call after setPluginInterface)
|
|
virtual void triggerAutoConnect() = 0;
|
|
|
|
// === Advanced methods for Core-to-Core communication (not for
|
|
// Plugin) === These are only called by Core implementations, not
|
|
// Plugin They use OpaqueHandle for type safety without exposing
|
|
// types
|
|
|
|
// Full TrackInfo access (for internal use)
|
|
[[nodiscard]] virtual Result<OpaqueHandle<TrackInfo>>
|
|
getCurrentTrackHandle() const = 0;
|
|
|
|
// Full QueueEntry access (for internal use)
|
|
[[nodiscard]] virtual Result<std::vector<OpaqueHandle<QueueEntry>>>
|
|
getQueueHandles() const = 0;
|
|
|
|
// Raw config access (for internal use)
|
|
[[nodiscard]] virtual Result<AppConfigHandle>
|
|
getConfigHandle() const = 0;
|
|
virtual Result<void>
|
|
updateConfigFromHandle(AppConfigHandle config) = 0;
|
|
|
|
// Reload configuration from the config file on disk.
|
|
// NOTE: append-only. ICoreInterface is the hot-reload ABI contract
|
|
// between the (already-loaded) Plugin and a freshly reloaded Core.
|
|
// Inserting/removing a virtual method anywhere above shifts every
|
|
// later vtable slot, so the old Plugin would call the wrong methods
|
|
// on the new Core (state-transfer corruption on hot reload). Only
|
|
// ever add new virtuals at the very end.
|
|
virtual Result<void> reloadConfig() = 0;
|
|
};
|
|
|
|
} // namespace Echoion::Api
|