Compare commits
2 commits
64aa5f2353
...
312b91fedb
| Author | SHA1 | Date | |
|---|---|---|---|
| 312b91fedb | |||
| 6fe8c2bf61 |
9 changed files with 64 additions and 17 deletions
|
|
@ -36,7 +36,7 @@ impl ConfigArgs {
|
||||||
}).await.clone()
|
}).await.clone()
|
||||||
}
|
}
|
||||||
pub async fn to_partial_config_with_default(&self, app_name: &'static str) -> PartialConfig {
|
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.merge(self.to_partial_config_without_default(app_name).await);
|
||||||
default
|
default
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,9 @@ jni = "0.21.1"
|
||||||
ndk = "0.9.0"
|
ndk = "0.9.0"
|
||||||
|
|
||||||
[target.'cfg(target_os="ios")'.dependencies]
|
[target.'cfg(target_os="ios")'.dependencies]
|
||||||
objc = "0.2.7"
|
objc2 = "0.6.2"
|
||||||
objc-foundation = "0.1.1"
|
objc2-foundation = "0.3.1"
|
||||||
objc_id = "0.1.1"
|
objc2-app-kit = "0.3.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.20.0"
|
tempfile = "3.20.0"
|
||||||
|
|
|
||||||
|
|
@ -116,8 +116,7 @@ impl PartialConfig {
|
||||||
file.write_all(toml::to_string(self)?.as_bytes()).await?;
|
file.write_all(toml::to_string(self)?.as_bytes()).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
#[cfg(not(any(target_os="android", target_os="ios")))]
|
pub fn default(app_name: &'static str) -> Self {
|
||||||
pub fn default_desktop(app_name: &'static str) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
p2p: Some(PartialP2pConfig::default()),
|
p2p: Some(PartialP2pConfig::default()),
|
||||||
rpc: Some(PartialRpcConfig::default(app_name)),
|
rpc: Some(PartialRpcConfig::default(app_name)),
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,19 @@ pub struct PartialRpcConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialRpcConfig {
|
impl PartialRpcConfig {
|
||||||
|
#[cfg(not(target_os="ios"))]
|
||||||
pub fn default(app_name: &'static str) -> Self {
|
pub fn default(app_name: &'static str) -> Self {
|
||||||
let username = whoami::username();
|
let username = whoami::username();
|
||||||
Self{
|
Self{
|
||||||
socket_path: Some(std::env::temp_dir().join(username).join(String::from(app_name) + ".sock")),
|
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 {
|
impl Emptiable for PartialRpcConfig {
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ impl PartialStorageConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(target_os="android")]
|
#[cfg(target_os="android")]
|
||||||
fn default_android() -> Self{
|
pub fn default_android() -> Self{
|
||||||
let ctx = ndk_context::android_context();
|
let ctx = ndk_context::android_context();
|
||||||
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }?;
|
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }?;
|
||||||
let mut env = vm.attach_current_thread()?;
|
let mut env = vm.attach_current_thread()?;
|
||||||
|
|
@ -66,15 +66,22 @@ impl PartialStorageConfig {
|
||||||
Ok(cache_dir.to_string())
|
Ok(cache_dir.to_string())
|
||||||
|
|
||||||
}
|
}
|
||||||
#[cfg(false)]
|
#[cfg(target_os="ios")]
|
||||||
fn default_ios(){
|
pub fn default(_: &'static str) -> Self{
|
||||||
unsafe {
|
|
||||||
let file_manager: *mut Object = msg_send![Class::get("NSFileManager").unwrap(), defaultManager];
|
use objc2::rc::Retained;
|
||||||
let paths: Id<Object> = msg_send![file_manager, URLsForDirectory:1 inDomains:1];
|
use objc2::msg_send;
|
||||||
let first_path: *mut Object = msg_send![paths, firstObject];
|
use objc2_foundation::*;
|
||||||
let path: Id<NSString> = Id::from_ptr(msg_send![first_path, path]);
|
|
||||||
Some(path.as_str().to_string())
|
let home_dir: Retained<NSString> = unsafe {NSHomeDirectory()};
|
||||||
|
|
||||||
|
let path = PathBuf::from(home_dir.to_string());
|
||||||
|
Self {
|
||||||
|
data_directory: Some(path.join("Library")),
|
||||||
|
cache_directory: Some(path.join("Library").join("Cache")),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
pub mod global;
|
pub mod global;
|
||||||
pub mod gui;
|
pub mod gui;
|
||||||
pub mod rpc;
|
pub mod rpc;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod cli;
|
||||||
use caretta_sync::utils::Runnable;
|
use caretta_sync::utils::Runnable;
|
||||||
use caretta_sync_example_core::global::APP_NAME;
|
use caretta_sync_example_core::global::APP_NAME;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,4 @@ crate-type = ["lib", "cdylib"]
|
||||||
bevy.workspace = true
|
bevy.workspace = true
|
||||||
caretta-sync-example-core.path = "../core"
|
caretta-sync-example-core.path = "../core"
|
||||||
caretta-sync.path = "../.."
|
caretta-sync.path = "../.."
|
||||||
|
tokio.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,41 @@ use bevy::{
|
||||||
window::{AppLifecycle, ScreenEdge, WindowMode},
|
window::{AppLifecycle, ScreenEdge, WindowMode},
|
||||||
winit::WinitSettings,
|
winit::WinitSettings,
|
||||||
};
|
};
|
||||||
|
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]
|
#[bevy_main]
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
|
||||||
|
//init_config();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
app.add_plugins(
|
app.add_plugins(
|
||||||
DefaultPlugins.set(LogPlugin {
|
DefaultPlugins.set(LogPlugin {
|
||||||
|
|
@ -59,7 +91,7 @@ fn setup_scene(
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
.with_child((
|
.with_child((
|
||||||
Text::new(format!( "{:?}", std::fs::read_dir(std::env::current_dir().unwrap()).unwrap().map(|x| x.unwrap().path()).collect::<Vec<std::path::PathBuf>>())),
|
Text::new(format!("{:?}", PartialConfig::default(APP_NAME))),
|
||||||
TextFont {
|
TextFont {
|
||||||
font_size: 16.0,
|
font_size: 16.0,
|
||||||
..default()
|
..default()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue