Skip to main content

nms_cache/
data.rs

1//! Cache data types -- the subset of GalaxyModel that gets serialized.
2
3use rkyv::{Archive, Deserialize, Serialize};
4
5use nms_core::address::GalacticAddress;
6use nms_core::player::{PlayerBase, PlayerState};
7use nms_core::system::Planet;
8
9/// Flattened galaxy data for cache serialization.
10///
11/// Contains all the raw data needed to reconstruct a `GalaxyModel`.
12/// Indices (graph, R-tree, HashMaps) are rebuilt on load.
13#[derive(Archive, Serialize, Deserialize, Debug)]
14pub struct CacheData {
15    /// All discovered systems.
16    pub systems: Vec<CachedSystem>,
17
18    /// All player bases.
19    pub bases: Vec<PlayerBase>,
20
21    /// Player state at time of caching.
22    pub player_state: Option<PlayerState>,
23
24    /// Save file version that produced this cache.
25    pub save_version: u32,
26
27    /// Timestamp when the cache was created (Unix seconds).
28    pub cached_at: u64,
29}
30
31/// A system with flattened fields for cache storage.
32///
33/// We flatten `System` rather than embedding it because `System` contains
34/// `Option<DateTime<Utc>>` which doesn't support rkyv. Timestamps are
35/// stored as Unix seconds and converted on load.
36#[derive(Archive, Serialize, Deserialize, Debug)]
37pub struct CachedSystem {
38    pub address: GalacticAddress,
39    pub name: Option<String>,
40    pub discoverer: Option<String>,
41    /// Timestamp as Unix seconds (converted from `DateTime<Utc>`).
42    pub timestamp_secs: Option<i64>,
43    pub planets: Vec<Planet>,
44}