-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathmod.rs
More file actions
140 lines (127 loc) · 4.11 KB
/
mod.rs
File metadata and controls
140 lines (127 loc) · 4.11 KB
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
use crate::shared::{self, message::MessageExt};
use ambient_ecs::{generated::messages, query, EntityId, FnSystem, SystemGroup, World};
use ambient_native_std::asset_cache::AssetCache;
use ambient_network::server::{ForkingEvent, ShutdownEvent};
use std::{path::PathBuf, sync::Arc};
mod implementation;
mod network;
pub fn initialize(
world: &mut World,
assets: &AssetCache,
hosted: bool,
data_path: PathBuf,
messenger: Arc<dyn Fn(&World, EntityId, shared::MessageType, &str) + Send + Sync>,
) -> anyhow::Result<()> {
shared::initialize(
world,
assets,
messenger,
Arc::new(move |id| Bindings {
base: Default::default(),
world_ref: Default::default(),
id,
reqwest_client: reqwest::Client::new(),
last_http_request_id: 0,
hosted,
}),
if hosted {
None
} else {
Some(data_path.as_ref())
},
)?;
network::initialize(world);
Ok(())
}
pub fn systems() -> SystemGroup {
SystemGroup::new(
"core/wasm/server",
vec![
Box::new(FnSystem::new(move |world, _| {
profiling::scope!("WASM module collision event");
// trigger collision event
let collisions = match world.resource_opt(ambient_physics::collisions()) {
Some(collisions) => collisions.lock().clone(),
None => return,
};
for (a, b, positions, normals) in collisions.into_iter() {
messages::Collision::new(vec![a, b], positions, normals)
.run(world, None)
.unwrap();
}
})),
Box::new(FnSystem::new(move |world, _| {
profiling::scope!("WASM module collider loads");
// trigger collider loads
let collider_loads = match world.resource_opt(ambient_physics::collider_loads()) {
Some(collider_loads) => collider_loads.clone(),
None => return,
};
if collider_loads.is_empty() {
return;
}
messages::ColliderLoads::new(collider_loads)
.run(world, None)
.unwrap();
})),
Box::new(shared::systems()),
],
)
}
pub fn on_forking_systems() -> SystemGroup<ForkingEvent> {
SystemGroup::new(
"core/wasm/server/on_forking_systems",
vec![Box::new(FnSystem::new(move |world, _| {
// Reset the states of all the modules when we fork.
shared::reload_all(world);
}))],
)
}
pub fn on_shutdown_systems() -> SystemGroup<ShutdownEvent> {
SystemGroup::new(
"core/wasm/server/on_shutdown_systems",
vec![Box::new(FnSystem::new(move |world, _| {
let modules = query(()).incl(shared::is_module()).collect_ids(world, None);
for module_id in modules {
shared::unload(world, module_id, "shutting down");
}
}))],
)
}
#[derive(Clone)]
struct Bindings {
base: shared::bindings::BindingsBase,
world_ref: shared::bindings::WorldRef,
id: EntityId,
reqwest_client: reqwest::Client,
last_http_request_id: u64,
/// Whether or not this server is running in a hosted environment,
/// and should thus have some of its functionality disabled
hosted: bool,
}
impl Bindings {
pub fn world(&self) -> &World {
unsafe { self.world_ref.world() }
}
pub fn world_mut(&mut self) -> &mut World {
unsafe { self.world_ref.world_mut() }
}
}
impl shared::bindings::BindingsBound for Bindings {
fn base(&self) -> &shared::bindings::BindingsBase {
&self.base
}
fn base_mut(&mut self) -> &mut shared::bindings::BindingsBase {
&mut self.base
}
fn set_world(&mut self, world: &mut World) {
unsafe {
self.world_ref.set_world(world);
}
}
fn clear_world(&mut self) {
unsafe {
self.world_ref.clear_world();
}
}
}