1#![forbid(unsafe_code)]
36#![warn(rust_2018_idioms)]
37
38pub mod assets;
39pub mod blocks18;
40pub mod dashcam;
41pub mod entities;
42pub mod feed;
43pub mod hub;
44#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
45mod legacy_table;
46pub mod protocol;
47pub mod record;
48pub mod replay;
49mod server;
50pub mod skins;
51pub mod stream;
52pub mod tiles;
53#[cfg(feature = "v26_1")]
54pub mod v26_1;
55#[cfg(feature = "v26_2")]
56pub mod v26_2;
57#[cfg(feature = "v26_3")]
58pub mod v26_3;
59mod watch;
60mod window;
61
62#[derive(Clone, Copy)]
65pub(crate) struct ModernMap {
66 pub(crate) block: fn(u32) -> u16,
67 pub(crate) item: fn(i32, i16) -> (u16, u16),
68 pub(crate) entity_text: fn(&ghast_entity::Entity) -> Option<serde_json::Value>,
69 pub(crate) stack_text:
70 for<'a> fn(&ghast_inventory::Stack, &dyn Fn(i32) -> Option<&'a str>) -> serde_json::Value,
71 pub(crate) entity_name: fn(&str) -> String,
72 pub(crate) info: fn(ghast_entity::EntityType) -> Option<&'static ghast_data::EntityInfo>,
73 pub(crate) item_stack:
74 fn(&ghast_entity::Entity) -> Option<&ghast_protocol::types::slot::v775::ItemStack>,
75 pub(crate) flags: fn(&ghast_entity::Entity) -> u8,
76 pub(crate) inventory: ghast_inventory::Version,
77 pub(crate) fx_packets: &'static [i32],
79 pub(crate) fx: fn(i32, &bytes::Bytes) -> Option<(i32, u8)>,
81 pub(crate) game: &'static str,
83}
84
85impl std::fmt::Debug for ModernMap {
86 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87 f.debug_struct("ModernMap")
88 .field("inventory", &self.inventory)
89 .finish_non_exhaustive()
90 }
91}
92
93pub(crate) fn modern_map(v: ghast_client::GameVersion) -> Option<&'static ModernMap> {
96 match v {
97 #[cfg(feature = "v26_1")]
98 ghast_client::GameVersion::V26_1 => Some(&v26_1::MAP),
99 #[cfg(feature = "v26_2")]
100 ghast_client::GameVersion::V26_2 => Some(&v26_2::MAP),
101 #[cfg(feature = "v26_3")]
102 ghast_client::GameVersion::V26_3 => Some(&v26_3::MAP),
103 _ => None,
104 }
105}
106
107use std::net::{IpAddr, Ipv4Addr, SocketAddr};
108use std::path::PathBuf;
109use std::sync::Arc;
110use std::time::Duration;
111
112pub use dashcam::{DashCam, DashCamOptions};
113pub use feed::{BotView, now_ms};
114pub use record::{FeedRecorder, ReplayMeta, list_recordings, prune_recordings};
115pub use watch::Watched;
116
117use crate::feed::FeedConfig;
118use crate::hub::Hub;
119use crate::skins::SkinStore;
120
121#[derive(Debug, Clone)]
123pub struct ViewerConfig {
124 pub account: String,
126 pub host: IpAddr,
128 pub port: u16,
130 pub view_distance: u8,
132 pub idle: Duration,
134 pub token: Option<String>,
137 pub replay_dir: Option<PathBuf>,
139 pub cache_dir: PathBuf,
142 pub client_jar: Option<PathBuf>,
144 pub entities: EntityTracking,
147}
148
149impl ViewerConfig {
150 #[must_use]
152 pub fn new(account: impl Into<String>) -> Self {
153 ViewerConfig {
154 account: account.into(),
155 host: IpAddr::V4(Ipv4Addr::LOCALHOST),
156 port: 0,
157 view_distance: 8,
158 idle: Duration::from_secs(60),
159 token: None,
160 replay_dir: None,
161 cache_dir: PathBuf::from(".cache/ghast-viewer"),
162 client_jar: None,
163 entities: EntityTracking::Always,
164 }
165 }
166
167 #[must_use]
169 pub fn port(mut self, port: u16) -> Self {
170 self.port = port;
171 self
172 }
173
174 #[must_use]
176 pub fn host(mut self, host: IpAddr) -> Self {
177 self.host = host;
178 self
179 }
180
181 #[must_use]
183 pub fn view_distance(mut self, d: u8) -> Self {
184 self.view_distance = d.clamp(1, 12);
185 self
186 }
187
188 #[must_use]
190 pub fn token(mut self, token: impl Into<String>) -> Self {
191 self.token = Some(token.into());
192 self
193 }
194
195 #[must_use]
197 pub fn replay_dir(mut self, dir: impl Into<PathBuf>) -> Self {
198 self.replay_dir = Some(dir.into());
199 self
200 }
201
202 #[must_use]
204 pub fn cache_dir(mut self, dir: impl Into<PathBuf>) -> Self {
205 self.cache_dir = dir.into();
206 self
207 }
208
209 #[must_use]
211 pub fn client_jar(mut self, jar: impl Into<PathBuf>) -> Self {
212 self.client_jar = Some(jar.into());
213 self
214 }
215
216 #[must_use]
218 pub fn entities(mut self, e: EntityTracking) -> Self {
219 self.entities = e;
220 self
221 }
222
223 #[must_use]
225 pub fn idle(mut self, d: Duration) -> Self {
226 self.idle = d;
227 self
228 }
229
230 fn feed(&self) -> FeedConfig {
231 FeedConfig {
232 account: self.account.clone(),
233 view_distance: i32::from(self.view_distance.clamp(1, 12)),
234 unbudgeted: false,
235 }
236 }
237}
238
239#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
242pub enum EntityTracking {
243 #[default]
245 Always,
246 WhileWatched,
250 Off,
252}
253
254#[must_use]
258pub fn world_config() -> ghast_world::WorldConfig {
259 ghast_world::WorldConfig::default()
260 .with_light(true)
261 .with_biomes(true)
262 .with_block_entities(true)
263}
264
265#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
267pub struct ViewerStatus {
268 pub running: bool,
269 pub port: Option<u16>,
270 pub viewers: usize,
271 #[serde(rename = "inGame")]
272 pub in_game: bool,
273}
274
275struct Inner {
276 cfg: ViewerConfig,
277 hub: Arc<Hub>,
278 server: server::Control,
279}
280
281#[derive(Clone)]
283pub struct Viewer {
284 inner: Arc<Inner>,
285}
286
287impl std::fmt::Debug for Viewer {
288 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289 f.debug_struct("Viewer")
290 .field("account", &self.inner.cfg.account)
291 .field("status", &self.status())
292 .finish()
293 }
294}
295
296impl Viewer {
297 #[must_use]
299 pub fn new(cfg: ViewerConfig) -> Self {
300 Self::with_options(cfg, false)
301 }
302
303 #[doc(hidden)]
305 #[must_use]
306 pub fn new_inline(cfg: ViewerConfig) -> Self {
307 Self::with_options(cfg, true)
308 }
309
310 fn with_options(cfg: ViewerConfig, inline: bool) -> Self {
311 let mut feed = cfg.feed();
312 feed.unbudgeted = inline;
313 let hub = Hub::new(feed, Arc::clone(SkinStore::global()), inline);
314 Viewer {
315 inner: Arc::new(Inner {
316 cfg,
317 hub,
318 server: server::Control::default(),
319 }),
320 }
321 }
322
323 #[must_use]
325 pub fn config(&self) -> &ViewerConfig {
326 &self.inner.cfg
327 }
328
329 #[must_use]
331 pub fn hub(&self) -> &Arc<Hub> {
332 &self.inner.hub
333 }
334
335 pub fn watch<B>(&self, behavior: B) -> Watched<B> {
337 Watched::new(behavior, self.clone())
338 }
339
340 pub async fn start(&self) -> std::io::Result<SocketAddr> {
342 server::start(self).await
343 }
344
345 pub async fn stop(&self) {
347 self.inner.server.stop().await;
348 }
349
350 pub fn set_status(&self, status: &str, message: &str) {
353 self.inner.hub.set_status(status, message);
354 }
355
356 #[must_use]
358 pub fn status(&self) -> ViewerStatus {
359 ViewerStatus {
360 running: self.inner.server.port().is_some(),
361 port: self.inner.server.port(),
362 viewers: self
363 .inner
364 .hub
365 .viewers
366 .load(std::sync::atomic::Ordering::Relaxed)
367 + self.inner.server.replays(),
368 in_game: self.inner.hub.in_game(),
369 }
370 }
371
372 pub(crate) fn control(&self) -> &server::Control {
373 &self.inner.server
374 }
375}