-
Notifications
You must be signed in to change notification settings - Fork 124
/
main.rs
197 lines (170 loc) · 6.84 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use ambient_std::{
asset_cache::{AssetCache, SyncAssetKeyExt},
download_asset::AssetsCacheOnDisk,
};
use clap::Parser;
mod cli;
mod client;
mod server;
mod shared;
use ambient_physics::physx::PhysicsKey;
use anyhow::Context;
use cli::Cli;
use log::LevelFilter;
use server::QUIC_INTERFACE_PORT;
fn setup_logging() -> anyhow::Result<()> {
const MODULES: &[(LevelFilter, &[&str])] = &[
(
LevelFilter::Error,
&[
// Warns about extra syntactic elements; we are not concerned with these.
"fbxcel",
],
),
(
LevelFilter::Warn,
&[
"ambient_build",
"ambient_gpu",
"ambient_model",
"ambient_physics",
"ambient_std",
"cranelift_codegen",
"naga",
"tracing",
"wgpu_core",
"wgpu_hal",
],
),
];
// Initialize the logger and lower the log level for modules we don't need to hear from by default.
#[cfg(not(feature = "tracing"))]
{
let mut builder = env_logger::builder();
builder.filter_level(LevelFilter::Info);
for (level, modules) in MODULES {
for module in *modules {
builder.filter_module(module, *level);
}
}
builder.parse_default_env().try_init()?;
Ok(())
}
#[cfg(feature = "tracing")]
{
use tracing::metadata::Level;
use tracing_log::AsTrace;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{registry, EnvFilter};
let mut filter = tracing_subscriber::filter::Targets::new().with_default(tracing::metadata::LevelFilter::DEBUG);
for (level, modules) in MODULES {
for &module in *modules {
filter = filter.with_target(module, level.as_trace());
}
}
// BLOCKING: pending https://github.com/tokio-rs/tracing/issues/2507
// let modules: Vec<_> = MODULES.iter().flat_map(|&(level, modules)| modules.iter().map(move |&v| format!("{v}={level}"))).collect();
// eprintln!("{modules:#?}");
// let mut filter = tracing_subscriber::filter::EnvFilter::builder().with_default_directive(Level::INFO.into()).from_env_lossy();
// for module in modules {
// filter = filter.add_directive(module.parse().unwrap());
// }
// let mut filter = std::env::var("RUST_LOG").unwrap_or_default().parse::<tracing_subscriber::filter::Targets>().unwrap_or_default();
// filter.extend(MODULES.iter().flat_map(|&(level, modules)| modules.iter().map(move |&v| (v, level.as_trace()))));
let env_filter = EnvFilter::builder().with_default_directive(Level::INFO.into()).from_env_lossy();
registry()
.with(filter)
.with(env_filter)
//
.with(
tracing_tree::HierarchicalLayer::new(4)
.with_indent_lines(true)
.with_verbose_entry(true)
.with_verbose_exit(true)
.with_timer(tracing_tree::time::OffsetDateTime),
// .with_timer(tracing_tree::time::Uptime::from(std::time::Instant::now())),
)
// .with(tracing_subscriber::fmt::Layer::new().pretty())
.try_init()?;
Ok(())
}
}
fn main() -> anyhow::Result<()> {
setup_logging()?;
shared::components::init()?;
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
let assets = AssetCache::new(runtime.handle().clone());
PhysicsKey.get(&assets); // Load physics
AssetsCacheOnDisk.insert(&assets, false); // Disable disk caching for now; see https://github.com/AmbientRun/Ambient/issues/81
let cli = Cli::parse();
let current_dir = std::env::current_dir()?;
let project_path = cli.project().and_then(|p| p.path.clone()).unwrap_or_else(|| current_dir.clone());
let project_path =
if project_path.is_absolute() { project_path } else { ambient_std::path::normalize(¤t_dir.join(project_path)) };
if project_path.exists() && !project_path.is_dir() {
anyhow::bail!("Project path {project_path:?} exists and is not a directory.");
}
// If new: create project, immediately exit
if let Cli::New { name, .. } = &cli {
if let Err(err) = cli::new_project::new_project(&project_path, name.as_deref()) {
eprintln!("Failed to create project: {err:?}");
}
return Ok(());
}
// If a project was specified, assume that assets need to be built
let manifest = cli
.project()
.map(|_| {
anyhow::Ok(ambient_project::Manifest::parse(
&std::fs::read_to_string(project_path.join("ambient.toml")).context("No project manifest was found. Please create one.")?,
)?)
})
.transpose()?;
if let Some(manifest) = manifest.as_ref() {
if !cli.project().unwrap().no_build {
let project_name = manifest.project.name.as_deref().unwrap_or("project");
log::info!("Building {}", project_name);
runtime.block_on(ambient_build::build(
PhysicsKey.get(&assets),
&assets,
project_path.clone(),
manifest,
cli.project().map(|p| p.release).unwrap_or(false),
));
log::info!("Done building {}", project_name);
}
}
// If this is just a build, exit now
if matches!(&cli, Cli::Build { .. }) {
return Ok(());
}
// Otherwise, either connect to a server or host one
let server_addr = if let Cli::Join { host, .. } = &cli {
if let Some(mut host) = host.clone() {
if !host.contains(':') {
host = format!("{host}:{QUIC_INTERFACE_PORT}");
}
runtime.block_on(tokio::net::lookup_host(&host))?.next().ok_or_else(|| anyhow::anyhow!("No address found for host {host}"))?
} else {
format!("127.0.0.1:{QUIC_INTERFACE_PORT}").parse()?
}
} else {
let port = server::start(&runtime, assets.clone(), cli.clone(), project_path, manifest.as_ref().expect("no manifest"));
format!("127.0.0.1:{port}").parse()?
};
// Time to join!
let handle = runtime.handle().clone();
if let Some(run) = cli.run() {
// If we have run parameters, start a client and join a server
runtime.block_on(client::run(assets, server_addr, run, cli.project().and_then(|p| p.path.clone())));
} else {
// Otherwise, wait for the Ctrl+C signal
handle.block_on(async move {
match tokio::signal::ctrl_c().await {
Ok(()) => {}
Err(err) => log::error!("Unable to listen for shutdown signal: {}", err),
}
});
}
Ok(())
}