use crate::{BlockRegistry, Chunk, ChunkPos, VoxelError};
use std::sync::Arc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GeneratorInfo {
pub name: &'static str,
pub version: u32,
pub seed: u64,
}
pub struct GenerationContext<'a> {
registry: Arc<BlockRegistry>,
cancelled: &'a (dyn Fn() -> bool + Sync),
}
impl<'a> GenerationContext<'a> {
pub fn new(registry: Arc<BlockRegistry>, cancelled: &'a (dyn Fn() -> bool + Sync)) -> Self {
Self {
registry,
cancelled,
}
}
pub fn registry(&self) -> &Arc<BlockRegistry> {
&self.registry
}
pub fn check_cancelled(&self) -> Result<(), VoxelError> {
if (self.cancelled)() {
Err(VoxelError::Cancelled)
} else {
Ok(())
}
}
}
impl GenerationContext<'static> {
pub fn uncancelled(registry: Arc<BlockRegistry>) -> Self {
Self::new(registry, &|| false)
}
}
pub trait ChunkGenerator: Send + Sync {
fn info(&self) -> GeneratorInfo;
fn generate(
&self,
position: ChunkPos,
context: &GenerationContext<'_>,
) -> Result<Chunk, VoxelError>;
}
pub fn generate_chunk(
generator: &dyn ChunkGenerator,
position: ChunkPos,
context: &GenerationContext<'_>,
) -> Result<Chunk, VoxelError> {
position.origin()?;
context.check_cancelled()?;
let chunk = generator.generate(position, context)?;
context.check_cancelled()?;
if !std::ptr::eq(chunk.registry(), context.registry().as_ref()) {
return Err(VoxelError::RegistryMismatch);
}
Ok(chunk)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::BlockId;
use std::sync::atomic::{AtomicBool, Ordering};
struct Empty;
impl ChunkGenerator for Empty {
fn info(&self) -> GeneratorInfo {
GeneratorInfo {
name: "test:empty",
version: 1,
seed: 0,
}
}
fn generate(&self, _: ChunkPos, ctx: &GenerationContext<'_>) -> Result<Chunk, VoxelError> {
Chunk::filled(ctx.registry().clone(), BlockId::AIR)
}
}
#[test]
fn validates_position_and_preserves_game_save_policy() {
let registry = Arc::new(BlockRegistry::new());
let context = GenerationContext::uncancelled(registry);
assert!(
generate_chunk(&Empty, ChunkPos::default(), &context)
.unwrap()
.is_dirty()
);
assert!(matches!(
generate_chunk(&Empty, ChunkPos::new(i32::MAX, 0, 0), &context),
Err(VoxelError::InvalidChunkPosition)
));
assert!(matches!(
generate_chunk(
&Empty,
ChunkPos::default(),
&GenerationContext::new(context.registry().clone(), &|| true)
),
Err(VoxelError::Cancelled)
));
}
#[test]
fn rejects_cancellation_after_work_and_a_foreign_registry() {
struct Cancel<'a>(&'a AtomicBool);
impl ChunkGenerator for Cancel<'_> {
fn info(&self) -> GeneratorInfo {
Empty.info()
}
fn generate(
&self,
_: ChunkPos,
ctx: &GenerationContext<'_>,
) -> Result<Chunk, VoxelError> {
self.0.store(true, Ordering::Release);
Empty.generate(ChunkPos::default(), ctx)
}
}
let flag = AtomicBool::new(false);
let cancelled = || flag.load(Ordering::Acquire);
let context = GenerationContext::new(Arc::new(BlockRegistry::new()), &cancelled);
assert!(matches!(
generate_chunk(&Cancel(&flag), ChunkPos::default(), &context),
Err(VoxelError::Cancelled)
));
struct Foreign;
impl ChunkGenerator for Foreign {
fn info(&self) -> GeneratorInfo {
Empty.info()
}
fn generate(
&self,
_: ChunkPos,
_: &GenerationContext<'_>,
) -> Result<Chunk, VoxelError> {
Chunk::filled(Arc::new(BlockRegistry::new()), BlockId::AIR)
}
}
flag.store(false, Ordering::Release);
assert!(matches!(
generate_chunk(&Foreign, ChunkPos::default(), &context),
Err(VoxelError::RegistryMismatch)
));
}
}