feat(state): save state to ~/.local/state/stardust/<uid> on graceful disconnect
This commit is contained in:
@@ -191,7 +191,7 @@ impl Client {
|
|||||||
}
|
}
|
||||||
pub async fn save_state(&self) -> Option<ClientState> {
|
pub async fn save_state(&self) -> Option<ClientState> {
|
||||||
let internal = self.root.get()?.save_state().await.ok()?;
|
let internal = self.root.get()?.save_state().await.ok()?;
|
||||||
Some(dbg!(ClientState::from_deserialized(self, internal)))
|
Some(ClientState::from_deserialized(self, internal))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
@@ -1,19 +1,34 @@
|
|||||||
use super::client::Client;
|
use super::client::{get_env, Client};
|
||||||
use crate::nodes::{spatial::Spatial, Node};
|
use crate::nodes::{spatial::Spatial, Node};
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{io::Write, path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref CLIENT_STATES: Mutex<FxHashMap<String, Arc<ClientState>>> = Default::default();
|
pub static ref CLIENT_STATES: Mutex<FxHashMap<String, Arc<ClientState>>> = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct LaunchInfo {
|
||||||
|
pub cmdline: Vec<String>,
|
||||||
|
pub cwd: PathBuf,
|
||||||
|
pub env: FxHashMap<String, String>,
|
||||||
|
}
|
||||||
|
impl LaunchInfo {
|
||||||
|
fn from_client(client: &Client) -> Option<Self> {
|
||||||
|
Some(LaunchInfo {
|
||||||
|
cmdline: client.get_cmdline()?,
|
||||||
|
cwd: client.get_cwd()?,
|
||||||
|
env: get_env(client.pid?).ok()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct ClientState {
|
pub struct ClientState {
|
||||||
pub cmdline: Option<Vec<String>>,
|
pub launch_info: Option<LaunchInfo>,
|
||||||
pub cwd: Option<PathBuf>,
|
|
||||||
pub data: Option<Vec<u8>>,
|
pub data: Option<Vec<u8>>,
|
||||||
pub root: Mat4,
|
pub root: Mat4,
|
||||||
pub spatial_anchors: FxHashMap<String, Mat4>,
|
pub spatial_anchors: FxHashMap<String, Mat4>,
|
||||||
@@ -21,8 +36,7 @@ pub struct ClientState {
|
|||||||
impl ClientState {
|
impl ClientState {
|
||||||
pub fn from_deserialized(client: &Client, state: ClientStateInternal) -> Self {
|
pub fn from_deserialized(client: &Client, state: ClientStateInternal) -> Self {
|
||||||
ClientState {
|
ClientState {
|
||||||
cmdline: client.get_cmdline(),
|
launch_info: LaunchInfo::from_client(client),
|
||||||
cwd: client.get_cwd(),
|
|
||||||
data: state.data,
|
data: state.data,
|
||||||
root: Self::spatial_transform(client, &state.root.unwrap_or_default())
|
root: Self::spatial_transform(client, &state.root.unwrap_or_default())
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
@@ -45,11 +59,12 @@ impl ClientState {
|
|||||||
token
|
token
|
||||||
}
|
}
|
||||||
pub fn to_file(self) {
|
pub fn to_file(self) {
|
||||||
let state_dir = directories::ProjectDirs::from("", "", "stardust")
|
let project_dirs = directories::ProjectDirs::from("", "", "stardust").unwrap();
|
||||||
.unwrap()
|
let state_dir = project_dirs.state_dir().unwrap();
|
||||||
.state_dir()
|
std::fs::create_dir_all(state_dir).unwrap();
|
||||||
|
let mut file = std::fs::File::create(state_dir.join(nanoid::nanoid!())).unwrap();
|
||||||
|
file.write_all(&stardust_xr::schemas::flex::flexbuffers::to_vec(self).unwrap())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// std::fs::File::create(state_dir.join(""))
|
|
||||||
}
|
}
|
||||||
pub fn apply_to(&self, client: &Arc<Client>) -> ClientStateInternal {
|
pub fn apply_to(&self, client: &Arc<Client>) -> ClientStateInternal {
|
||||||
if let Some(root) = client.root.get() {
|
if let Some(root) = client.root.get() {
|
||||||
@@ -77,8 +92,7 @@ impl ClientState {
|
|||||||
impl Default for ClientState {
|
impl Default for ClientState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
cmdline: None,
|
launch_info: None,
|
||||||
cwd: None,
|
|
||||||
data: None,
|
data: None,
|
||||||
root: Mat4::IDENTITY,
|
root: Mat4::IDENTITY,
|
||||||
spatial_anchors: Default::default(),
|
spatial_anchors: Default::default(),
|
||||||
|
|||||||
Reference in New Issue
Block a user