#![forbid(unsafe_code)]
#![warn(rust_2018_idioms)]
pub mod assets;
pub mod blocks18;
pub mod dashcam;
pub mod entities;
pub mod feed;
pub mod hub;
pub mod protocol;
pub mod record;
pub mod replay;
mod server;
pub mod skins;
pub mod stream;
pub mod tiles;
mod watch;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
pub use dashcam::{DashCam, DashCamOptions};
pub use feed::{BotView, now_ms};
pub use record::{FeedRecorder, ReplayMeta, list_recordings, prune_recordings};
pub use watch::Watched;
use crate::feed::FeedConfig;
use crate::hub::Hub;
use crate::skins::SkinStore;
#[derive(Debug, Clone)]
pub struct ViewerConfig {
pub account: String,
pub host: IpAddr,
pub port: u16,
pub view_distance: u8,
pub idle: Duration,
pub token: Option<String>,
pub replay_dir: Option<PathBuf>,
pub cache_dir: PathBuf,
pub client_jar: Option<PathBuf>,
pub entities: EntityTracking,
}
impl ViewerConfig {
#[must_use]
pub fn new(account: impl Into<String>) -> Self {
ViewerConfig {
account: account.into(),
host: IpAddr::V4(Ipv4Addr::LOCALHOST),
port: 0,
view_distance: 8,
idle: Duration::from_secs(60),
token: None,
replay_dir: None,
cache_dir: PathBuf::from(".cache/ghast-viewer"),
client_jar: None,
entities: EntityTracking::Always,
}
}
#[must_use]
pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
}
#[must_use]
pub fn host(mut self, host: IpAddr) -> Self {
self.host = host;
self
}
#[must_use]
pub fn view_distance(mut self, d: u8) -> Self {
self.view_distance = d.clamp(1, 12);
self
}
#[must_use]
pub fn token(mut self, token: impl Into<String>) -> Self {
self.token = Some(token.into());
self
}
#[must_use]
pub fn replay_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.replay_dir = Some(dir.into());
self
}
#[must_use]
pub fn cache_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.cache_dir = dir.into();
self
}
#[must_use]
pub fn client_jar(mut self, jar: impl Into<PathBuf>) -> Self {
self.client_jar = Some(jar.into());
self
}
#[must_use]
pub fn entities(mut self, e: EntityTracking) -> Self {
self.entities = e;
self
}
#[must_use]
pub fn idle(mut self, d: Duration) -> Self {
self.idle = d;
self
}
fn feed(&self) -> FeedConfig {
FeedConfig {
account: self.account.clone(),
view_distance: i32::from(self.view_distance.clamp(1, 12)),
unbudgeted: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EntityTracking {
#[default]
Always,
WhileWatched,
Off,
}
#[must_use]
pub fn world_config() -> ghast_world::WorldConfig {
ghast_world::WorldConfig::default()
.with_light(true)
.with_biomes(true)
.with_block_entities(true)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub struct ViewerStatus {
pub running: bool,
pub port: Option<u16>,
pub viewers: usize,
#[serde(rename = "inGame")]
pub in_game: bool,
}
struct Inner {
cfg: ViewerConfig,
hub: Arc<Hub>,
server: server::Control,
}
#[derive(Clone)]
pub struct Viewer {
inner: Arc<Inner>,
}
impl std::fmt::Debug for Viewer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Viewer")
.field("account", &self.inner.cfg.account)
.field("status", &self.status())
.finish()
}
}
impl Viewer {
#[must_use]
pub fn new(cfg: ViewerConfig) -> Self {
Self::with_options(cfg, false)
}
#[doc(hidden)]
#[must_use]
pub fn new_inline(cfg: ViewerConfig) -> Self {
Self::with_options(cfg, true)
}
fn with_options(cfg: ViewerConfig, inline: bool) -> Self {
let mut feed = cfg.feed();
feed.unbudgeted = inline;
let hub = Hub::new(feed, Arc::clone(SkinStore::global()), inline);
Viewer {
inner: Arc::new(Inner {
cfg,
hub,
server: server::Control::default(),
}),
}
}
#[must_use]
pub fn config(&self) -> &ViewerConfig {
&self.inner.cfg
}
#[must_use]
pub fn hub(&self) -> &Arc<Hub> {
&self.inner.hub
}
pub fn watch<B>(&self, behavior: B) -> Watched<B> {
Watched::new(behavior, self.clone())
}
pub async fn start(&self) -> std::io::Result<SocketAddr> {
server::start(self).await
}
pub async fn stop(&self) {
self.inner.server.stop().await;
}
pub fn set_status(&self, status: &str, message: &str) {
self.inner.hub.set_status(status, message);
}
#[must_use]
pub fn status(&self) -> ViewerStatus {
ViewerStatus {
running: self.inner.server.port().is_some(),
port: self.inner.server.port(),
viewers: self
.inner
.hub
.viewers
.load(std::sync::atomic::Ordering::Relaxed)
+ self.inner.server.replays(),
in_game: self.inner.hub.in_game(),
}
}
pub(crate) fn control(&self) -> &server::Control {
&self.inner.server
}
}