From 312b91fedb87e1bf6e636e10bdbca4d7ca639dda Mon Sep 17 00:00:00 2001 From: fluo10 Date: Fri, 29 Aug 2025 06:37:29 +0900 Subject: [PATCH] Fix desktop example --- cli/src/cli/args/config.rs | 2 +- core/src/config/mod.rs | 3 +-- core/src/config/rpc.rs | 7 +++++++ core/src/config/storage.rs | 6 +++--- examples/desktop/src/main.rs | 1 + examples/mobile/Cargo.toml | 1 + examples/mobile/src/lib.rs | 35 +++++++++++++++++++++++++++++++++-- 7 files changed, 47 insertions(+), 8 deletions(-) diff --git a/cli/src/cli/args/config.rs b/cli/src/cli/args/config.rs index 515ebfa..6c813a3 100644 --- a/cli/src/cli/args/config.rs +++ b/cli/src/cli/args/config.rs @@ -36,7 +36,7 @@ impl ConfigArgs { }).await.clone() } pub async fn to_partial_config_with_default(&self, app_name: &'static str) -> PartialConfig { - let mut default = PartialConfig::default_desktop(app_name); + let mut default = PartialConfig::default(app_name); default.merge(self.to_partial_config_without_default(app_name).await); default } diff --git a/core/src/config/mod.rs b/core/src/config/mod.rs index a1718b8..9a3d280 100644 --- a/core/src/config/mod.rs +++ b/core/src/config/mod.rs @@ -116,8 +116,7 @@ impl PartialConfig { file.write_all(toml::to_string(self)?.as_bytes()).await?; Ok(()) } - #[cfg(not(any(target_os="android", target_os="ios")))] - pub fn default_desktop(app_name: &'static str) -> Self { + pub fn default(app_name: &'static str) -> Self { Self { p2p: Some(PartialP2pConfig::default()), rpc: Some(PartialRpcConfig::default(app_name)), diff --git a/core/src/config/rpc.rs b/core/src/config/rpc.rs index 14a985b..65eeac6 100644 --- a/core/src/config/rpc.rs +++ b/core/src/config/rpc.rs @@ -31,12 +31,19 @@ pub struct PartialRpcConfig { } impl PartialRpcConfig { + #[cfg(not(target_os="ios"))] pub fn default(app_name: &'static str) -> Self { let username = whoami::username(); Self{ socket_path: Some(std::env::temp_dir().join(username).join(String::from(app_name) + ".sock")), } } + #[cfg(target_os="ios")] + pub fn default(app_name: &'static str) -> Self { + Self{ + socket_path: Some(std::env::temp_dir().join(String::from(app_name) + ".sock")), + } + } } impl Emptiable for PartialRpcConfig { diff --git a/core/src/config/storage.rs b/core/src/config/storage.rs index 4792ede..642bf0a 100644 --- a/core/src/config/storage.rs +++ b/core/src/config/storage.rs @@ -67,7 +67,7 @@ impl PartialStorageConfig { } #[cfg(target_os="ios")] - pub fn default() -> Self{ + pub fn default(_: &'static str) -> Self{ use objc2::rc::Retained; use objc2::msg_send; @@ -77,8 +77,8 @@ impl PartialStorageConfig { let path = PathBuf::from(home_dir.to_string()); Self { - data_directory: Some(path.clone()), - cache_directory: Some(path.clone()), + data_directory: Some(path.join("Library")), + cache_directory: Some(path.join("Library").join("Cache")), } diff --git a/examples/desktop/src/main.rs b/examples/desktop/src/main.rs index bf2420b..d1cc501 100644 --- a/examples/desktop/src/main.rs +++ b/examples/desktop/src/main.rs @@ -1,3 +1,4 @@ +mod cli; use caretta_sync::utils::Runnable; use caretta_sync_example_core::global::APP_NAME; use clap::Parser; diff --git a/examples/mobile/Cargo.toml b/examples/mobile/Cargo.toml index 939a63b..2868671 100644 --- a/examples/mobile/Cargo.toml +++ b/examples/mobile/Cargo.toml @@ -18,3 +18,4 @@ crate-type = ["lib", "cdylib"] bevy.workspace = true caretta-sync-example-core.path = "../core" caretta-sync.path = "../.." +tokio.workspace = true diff --git a/examples/mobile/src/lib.rs b/examples/mobile/src/lib.rs index 655dd53..68291d8 100644 --- a/examples/mobile/src/lib.rs +++ b/examples/mobile/src/lib.rs @@ -6,10 +6,41 @@ use bevy::{ window::{AppLifecycle, ScreenEdge, WindowMode}, winit::WinitSettings, }; -use caretta_sync::config::PartialStorageConfig; +use caretta_sync::{config::{Config, PartialConfig, PartialP2pConfig, PartialStorageConfig, StorageConfig}, server::ServerTrait, utils::{Emptiable, Mergeable}}; +use caretta_sync_example_core::{global::APP_NAME, server::Server}; + +#[tokio::main] +pub async fn init_config() { + let storage_config: StorageConfig = PartialStorageConfig::default(APP_NAME).try_into().unwrap(); + let config_path = storage_config.data_directory.join("config.toml"); + let mut config = PartialConfig::read_from(&config_path).await.unwrap(); + if let Some(x) = if let Some(y) = config.p2p.as_mut() { + if y.private_key.is_none() { + Some(y.clone().with_new_private_key()) + } else { + None + } + + } else { + Some(PartialP2pConfig::empty().with_new_private_key()) + } { + config.p2p = Some(x); + config.write_to(&config_path).await.unwrap() + } + let mut default = PartialConfig::default(APP_NAME); + default.merge(config); + let config2 : Config = default.try_into().unwrap(); + Server::serve_all(&config2).await; + +} #[bevy_main] pub fn main() { + + //init_config(); + + + let mut app = App::new(); app.add_plugins( DefaultPlugins.set(LogPlugin { @@ -60,7 +91,7 @@ fn setup_scene( } )) .with_child(( - Text::new(format!("{:?}", PartialStorageConfig::default())), + Text::new(format!("{:?}", PartialConfig::default(APP_NAME))), TextFont { font_size: 16.0, ..default()