Improve world::Index

This commit is contained in:
Benoît du Garreau 2020-12-17 00:42:47 +01:00 committed by Marcel Märtens
parent 1c4e334cd4
commit 989683d2d3
8 changed files with 28 additions and 46 deletions

4
Cargo.lock generated
View File

@ -243,9 +243,9 @@ checksum = "bbf56136a5198c7b01a49e3afcbef6cf84597273d298f54432926024107b0109"
[[package]]
name = "assets_manager"
version = "0.4.1"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8048fbc7e6314fa56e2c7faeb0e1c7be01d23c87bad301a7877afaa493ec3dbb"
checksum = "3593b8ddb9708251c7a57b07c917c77ecc685e804b697366d26fb4af64c9b960"
dependencies = [
"ahash 0.6.2",
"bincode",

View File

@ -30,7 +30,7 @@ vek = { version = "0.12.0", features = ["serde"] }
uuid = { version = "0.8.1", default-features = false, features = ["serde", "v4"] }
# Assets
assets_manager = {version = "0.4.1", features = ["bincode", "ron", "json", "hot-reloading"]}
assets_manager = {version = "0.4.2", features = ["bincode", "ron", "json", "hot-reloading"]}
directories-next = "2.0"
dot_vox = "4.0"
image = { version = "0.23.12", default-features = false, features = ["png"] }

View File

@ -3,7 +3,6 @@
use dot_vox::DotVoxData;
use image::DynamicImage;
use lazy_static::lazy_static;
use serde::Deserialize;
use std::{
borrow::Cow,
fs, io,
@ -12,6 +11,7 @@ use std::{
};
pub use assets_manager::{
asset::Ron,
loader::{
self, BincodeLoader, BytesLoader, JsonLoader, LoadFrom, Loader, RonLoader, StringLoader,
},
@ -119,20 +119,6 @@ impl Loader<DotVoxAsset> for DotVoxLoader {
}
}
/// Load from an arbitrary RON file.
#[derive(Deserialize)]
#[serde(transparent)]
pub struct Ron<T>(pub T);
impl<T> Asset for Ron<T>
where
T: Send + Sync + for<'de> Deserialize<'de> + 'static,
{
type Loader = RonLoader;
const EXTENSION: &'static str = "ron";
}
impl Asset for DotVoxAsset {
type Loader = DotVoxLoader;

View File

@ -7,7 +7,7 @@ use crate::{
};
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use std::{time::Duration};
use std::time::Duration;
use tracing::error;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]

View File

@ -1,10 +1,8 @@
use common::assets::{self, AssetExt};
use deunicode::deunicode;
use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
};
use hashbrown::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use tracing::warn;
/// The reference language, aka the more up-to-date localization data.

View File

@ -8,7 +8,7 @@ const H: usize = 480;
#[allow(clippy::or_fun_call)] // TODO: Pending review in #587
fn main() {
let seed = 1337;
let (ref index, colors) = Index::new(seed);
let index = &Index::new(seed);
let mut win =
minifb::Window::new("Settlement Viewer", W, H, minifb::WindowOptions::default()).unwrap();
@ -17,7 +17,7 @@ fn main() {
let mut focus = Vec2::<f32>::zero();
let mut zoom = 1.0;
let colors = &**colors.read();
let colors = &**index.colors().read();
let index = IndexRef { colors, index };
while win.is_open() {

View File

@ -14,6 +14,7 @@ pub struct Index {
pub time: f32,
pub noise: Noise,
pub sites: Store<Site>,
colors: AssetHandle<Arc<Colors>>,
}
/// An owned reference to indexed data.
@ -25,10 +26,6 @@ pub struct Index {
pub struct IndexOwned {
colors: Arc<Colors>,
index: Arc<Index>,
/// Stored separatly so `colors` is only updated when
/// `reload_colors_if_changed` is called
colors_handle: AssetHandle<Arc<Colors>>,
}
impl Deref for IndexOwned {
@ -54,27 +51,28 @@ impl<'a> Deref for IndexRef<'a> {
impl Index {
/// NOTE: Panics if the color manifest cannot be loaded.
pub fn new(seed: u32) -> (Self, AssetHandle<Arc<Colors>>) {
pub fn new(seed: u32) -> Self {
let colors = Arc::<Colors>::load_expect(WORLD_COLORS_MANIFEST);
(
Self {
seed,
time: 0.0,
noise: Noise::new(seed),
sites: Store::default(),
},
Self {
seed,
time: 0.0,
noise: Noise::new(seed),
sites: Store::default(),
colors,
)
}
}
pub fn colors(&self) -> AssetHandle<Arc<Colors>> { self.colors }
}
impl IndexOwned {
pub fn new(index: Index, colors: AssetHandle<Arc<Colors>>) -> Self {
pub fn new(index: Index) -> Self {
let colors = index.colors.cloned();
Self {
index: Arc::new(index),
colors: colors.cloned(),
colors_handle: colors,
colors,
}
}
@ -89,9 +87,9 @@ impl IndexOwned {
&mut self,
reload: impl FnOnce(&mut Self) -> R,
) -> Option<R> {
self.colors_handle.reloaded().then(move || {
// Reload the color from the asse handle, which is updated automatically
self.colors = self.colors_handle.cloned();
self.index.colors.reloaded_global().then(move || {
// Reload the color from the asset handle, which is updated automatically
self.colors = self.index.colors.cloned();
reload(self)
})
}

View File

@ -81,13 +81,13 @@ impl World {
pub fn generate(seed: u32, opts: sim::WorldOpts) -> (Self, IndexOwned) {
// NOTE: Generating index first in order to quickly fail if the color manifest
// is broken.
let (mut index, colors) = Index::new(seed);
let mut index = Index::new(seed);
let mut sim = sim::WorldSim::generate(seed, opts);
let civs = civ::Civs::generate(seed, &mut sim, &mut index);
sim2::simulate(&mut index, &mut sim);
(Self { sim, civs }, IndexOwned::new(index, colors))
(Self { sim, civs }, IndexOwned::new(index))
}
pub fn sim(&self) -> &sim::WorldSim { &self.sim }