Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomly generated IDs #931

Merged
merged 20 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 41 additions & 36 deletions app/src/cli/assets.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use ambient_native_std::asset_cache::AssetCache;
use anyhow::Context;
use clap::{Args, Subcommand};

use super::PackagePath;

#[derive(Subcommand, Clone, Debug)]
pub enum AssetCommand {
pub enum Assets {
/// Migrate json pipelines to toml
#[command(name = "migrate-pipelines-toml")]
MigratePipelinesToml(MigrateOptions),
Expand All @@ -17,14 +18,14 @@ pub enum AssetCommand {

#[derive(Args, Clone, Debug)]
pub struct MigrateOptions {
#[arg(index = 1, default_value = "./assets")]
#[arg(default_value = "./assets")]
/// The path to the assets folder
pub path: PathBuf,
}

#[derive(Args, Clone, Debug)]
pub struct ImportOptions {
#[arg(index = 1)]
#[arg()]
/// The path to the assets you want to import
pub path: PathBuf,
#[arg(long)]
Expand All @@ -35,41 +36,45 @@ pub struct ImportOptions {
pub collider_from_model: bool,
}

pub async fn handle(command: &AssetCommand, assets: &crate::AssetCache) -> anyhow::Result<()> {
pub async fn handle(command: &Assets, assets: &AssetCache) -> anyhow::Result<()> {
match command {
AssetCommand::MigratePipelinesToml(opt) => {
let path = PackagePath::new_local(opt.path.clone())?;
ambient_build::migrate::toml::process(path.fs_path.unwrap())
.await
.context("Failed to migrate pipelines")?;
Assets::MigratePipelinesToml(opt) => {
migrate_pipelines_toml(opt).await?;
}
AssetCommand::Import(opt) => match opt.path.extension() {
Some(ext) => {
if ext == "wav" || ext == "mp3" || ext == "ogg" {
let convert = opt.convert_audio;
ambient_build::pipelines::import_audio(opt.path.clone(), convert)
.context("Failed to import audio")?;
} else if ext == "fbx" || ext == "glb" || ext == "gltf" || ext == "obj" {
let collider_from_model = opt.collider_from_model;
ambient_build::pipelines::import_model(opt.path.clone(), collider_from_model)
.context("Failed to import models")?;
} else if ext == "jpg" || ext == "png" || ext == "gif" || ext == "webp" {
// TODO: import textures API may change, so this is just a placeholder
todo!();
} else {
anyhow::bail!("Unsupported file type");
}
ambient_build::build_assets(
assets,
&PathBuf::from("assets"),
&PathBuf::from("build"),
true,
)
.await?;
}
None => anyhow::bail!("Unknown file type"),
},
Assets::Import(opt) => import(opt, assets).await?,
}

Ok(())
}

async fn migrate_pipelines_toml(opt: &MigrateOptions) -> Result<(), anyhow::Error> {
let path = PackagePath::new_local(opt.path.clone())?;
ambient_build::migrate::toml::process(path.fs_path.unwrap())
.await
.context("Failed to migrate pipelines")?;
Ok(())
}

async fn import(opt: &ImportOptions, assets: &AssetCache) -> anyhow::Result<()> {
let Some(ext) = opt.path.extension() else {
anyhow::bail!("Unknown file type");
};

if ext == "wav" || ext == "mp3" || ext == "ogg" {
let convert = opt.convert_audio;
ambient_build::pipelines::import_audio(opt.path.clone(), convert)
.context("Failed to import audio")?;
} else if ext == "fbx" || ext == "glb" || ext == "gltf" || ext == "obj" {
let collider_from_model = opt.collider_from_model;
ambient_build::pipelines::import_model(opt.path.clone(), collider_from_model)
.context("Failed to import models")?;
} else if ext == "jpg" || ext == "png" || ext == "gif" || ext == "webp" {
// TODO: import textures API may change, so this is just a placeholder
todo!();
} else {
anyhow::bail!("Unsupported file type");
}
ambient_build::build_assets(assets, Path::new("assets"), Path::new("build"), true).await?;

Ok(())
}
45 changes: 0 additions & 45 deletions app/src/cli/client.rs

This file was deleted.

44 changes: 44 additions & 0 deletions app/src/cli/join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use ambient_native_std::{
asset_cache::{AssetCache, SyncAssetKeyExt},
download_asset::ReqwestClientKey,
};
use ambient_network::native::client::ResolvedAddr;
use clap::Parser;

use crate::{client, server::QUIC_INTERFACE_PORT};

use super::ClientCli;

#[derive(Parser, Clone, Debug)]
/// Join a multiplayer session
pub struct Join {
#[command(flatten)]
pub client: ClientCli,
/// The server to connect to; defaults to localhost
pub host: Option<String>,
}

pub fn handle(args: &Join, rt: &tokio::runtime::Runtime, assets: AssetCache) -> anyhow::Result<()> {
let assets_ref = &assets;
let server_addr = rt.block_on(async move {
let Some(mut host) = args.host.as_ref().cloned() else {
return Ok(ResolvedAddr::localhost_with_port(QUIC_INTERFACE_PORT));
};

if host.starts_with("http://") || host.starts_with("https://") {
tracing::info!("NOTE: Joining server by http url is still experimental and can be removed without warning.");

let reqwest = &ReqwestClientKey.get(assets_ref);
host = reqwest.get(host).send().await?.text().await?;

if host.is_empty() {
anyhow::bail!("Failed to resolve host");
}
}
if !host.contains(':') {
host = format!("{host}:{QUIC_INTERFACE_PORT}");
}
ResolvedAddr::lookup_host(&host).await
})?;
client::run(rt, assets, server_addr, &args.client, None)
}
Loading