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
|
use crate::WeakWorld;
use azalea_core::ResourceLocation;
use log::error;
use std::{
collections::HashMap,
sync::{Arc, Weak},
};
/// A container of [`WeakWorld`]s. Worlds are stored as a Weak pointer here, so
/// if no clients are using a world it will be forgotten.
#[derive(Default)]
pub struct WeakWorldContainer {
pub worlds: HashMap<ResourceLocation, Weak<WeakWorld>>,
}
impl WeakWorldContainer {
pub fn new() -> Self {
WeakWorldContainer {
worlds: HashMap::new(),
}
}
/// Get a world from the container.
pub fn get(&self, name: &ResourceLocation) -> Option<Arc<WeakWorld>> {
self.worlds.get(name).and_then(|world| world.upgrade())
}
/// Add an empty world to the container (or not if it already exists) and
/// returns a strong reference to the world.
#[must_use = "the world will be immediately forgotten if unused"]
pub fn insert(&mut self, name: ResourceLocation, height: u32, min_y: i32) -> Arc<WeakWorld> {
if let Some(existing) = self.worlds.get(&name).and_then(|world| world.upgrade()) {
if existing.height() != height {
error!(
"Shared dimension height mismatch: {} != {}",
existing.height(),
height,
);
}
if existing.min_y() != min_y {
error!(
"Shared world min_y mismatch: {} != {}",
existing.min_y(),
min_y,
);
}
existing
} else {
let world = Arc::new(WeakWorld::new(height, min_y));
self.worlds.insert(name, Arc::downgrade(&world));
world
}
}
}
|