#pragma once #include "settlement/core/checkpoint_files.hpp" #include "settlement/core/colony_profile.hpp" #include #include #include #include #include namespace settlement::viewer { enum class ViewerEventKind { Replaced, Command, Saved, Error, Fault }; struct ViewerEvent { ViewerEventKind kind; std::uint64_t command = 0; std::optional payload; CommandStatus status = CommandStatus::Unavailable; std::string message; }; struct ViewerSessionOptions { RuntimeOptions runtime; CheckpointFilesOptions files; std::size_t command_capacity = 64; }; // Called only by the UI thread. Polling owns completion delivery; rendering holds // an immutable view independently of owner progress and nested window messages. class ViewerSession { public: explicit ViewerSession(std::size_t workers, ViewerSessionOptions options = {}); ViewerSession(WorldCheckpoint initial, std::size_t workers, ViewerSessionOptions options = {}); ~ViewerSession(); [[nodiscard]] SimulationRuntime& runtime() noexcept { return runtime_; } [[nodiscard]] std::shared_ptr world() const; [[nodiscard]] bool guest() const noexcept { return guest_; } [[nodiscard]] bool changing_world() const noexcept { return changing_world_; } [[nodiscard]] bool can_submit() const; [[nodiscard]] bool can_host_legacy() const; [[nodiscard]] std::uint64_t generation() const noexcept { return generation_; } [[nodiscard]] std::optional submit(SessionPayload payload); [[nodiscard]] bool pending(std::uint64_t id) const noexcept; void set_ticking(bool enabled) noexcept { desired_ticking_ = enabled; } [[nodiscard]] std::vector poll(); void new_colony(ColonyProfile profile = ColonyProfile::Default); void load(const std::filesystem::path& path); void save(const std::filesystem::path& path); void enter_guest(); void receive_guest(std::unique_ptr world); void leave_guest(); private: struct Intent { std::uint64_t id; SessionPayload payload; }; struct Preparing { LoadTicket ticket; std::future completion; }; struct Installing { LoadTicket ticket; WorldCheckpoint candidate; }; struct Saving { std::uint64_t generation; std::uint64_t request; Void::Uuid::Uuid session; std::future completion; }; [[nodiscard]] LoadTicket begin_change(); void replace(WorldCheckpoint candidate); void validate_candidate(const WorldCheckpoint& candidate) const; void fail_change(std::string message); void poll_changes(); void poll_commands(); void poll_saves(); void poll_ticking(); std::size_t workers_; std::size_t command_capacity_; RulesManifest supported_rules_; SimulationRuntime runtime_; CheckpointFiles files_; std::shared_ptr guest_view_; std::unique_ptr guest_world_; std::deque intents_; std::future command_completion_; std::optional preparing_; std::optional installing_; std::future install_completion_; std::vector saving_; std::vector events_; std::future ticking_completion_; bool requested_ticking_ = false; bool applied_ticking_ = false; bool desired_ticking_ = false; bool guest_ = false; bool changing_world_ = false; bool fault_reported_ = false; std::uint64_t generation_ = 1; std::uint64_t next_intent_ = 1; std::uint64_t latest_save_ = 0; }; } // namespace settlement::viewer