Files
stoneheart_at_home/app/viewer_session.hpp

112 lines
3.7 KiB
C++

#pragma once
#include "settlement/core/checkpoint_files.hpp"
#include "settlement/core/colony_profile.hpp"
#include <deque>
#include <filesystem>
#include <optional>
#include <string>
#include <vector>
namespace settlement::viewer {
enum class ViewerEventKind { Replaced, Command, Saved, Error, Fault };
struct ViewerEvent {
ViewerEventKind kind;
std::uint64_t command = 0;
std::optional<SessionPayload> 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<const World> 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<std::uint64_t> submit(SessionPayload payload);
[[nodiscard]] bool pending(std::uint64_t id) const noexcept;
void set_ticking(bool enabled) noexcept { desired_ticking_ = enabled; }
[[nodiscard]] std::vector<ViewerEvent> 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> world);
void leave_guest();
private:
struct Intent {
std::uint64_t id;
SessionPayload payload;
};
struct Preparing {
LoadTicket ticket;
std::future<CheckpointLoadResult> completion;
};
struct Installing {
LoadTicket ticket;
WorldCheckpoint candidate;
};
struct Saving {
std::uint64_t generation;
std::uint64_t request;
Void::Uuid::Uuid session;
std::future<SaveResult> 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<const World> guest_view_;
std::unique_ptr<World> guest_world_;
std::deque<Intent> intents_;
std::future<RuntimeResult> command_completion_;
std::optional<Preparing> preparing_;
std::optional<Installing> installing_;
std::future<RuntimeResult> install_completion_;
std::vector<Saving> saving_;
std::vector<ViewerEvent> events_;
std::future<RuntimeResult> 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