Fix desktop example

This commit is contained in:
fluo10 2025-08-29 06:37:29 +09:00
parent 6fe8c2bf61
commit 312b91fedb
7 changed files with 47 additions and 8 deletions

View file

@ -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
}

View file

@ -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)),

View file

@ -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 {

View file

@ -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")),
}

View file

@ -1,3 +1,4 @@
mod cli;
use caretta_sync::utils::Runnable;
use caretta_sync_example_core::global::APP_NAME;
use clap::Parser;

View file

@ -18,3 +18,4 @@ crate-type = ["lib", "cdylib"]
bevy.workspace = true
caretta-sync-example-core.path = "../core"
caretta-sync.path = "../.."
tokio.workspace = true

View file

@ -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()