Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into blockbreak
Browse files Browse the repository at this point in the history
  • Loading branch information
koskja committed Aug 28, 2022
2 parents becec55 + 2f99d76 commit f9f5b6f
Show file tree
Hide file tree
Showing 35 changed files with 1,148 additions and 718 deletions.
351 changes: 175 additions & 176 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ members = [
"feather/server",

# Other
"tools/proxy",
"proxy",
]

# No longer need to use release for "development":
Expand Down
3 changes: 0 additions & 3 deletions feather/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,3 @@ bytemuck = { version = "1", features = ["derive"] }
rand = "0.8"
rand_pcg = "0.3"
serde_test = "1"

[features]
proxy = []
19 changes: 9 additions & 10 deletions feather/base/src/anvil/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,17 @@ pub struct InventorySlot {

impl InventorySlot {
/// Converts an [`ItemStack`] and network protocol index into an [`InventorySlot`].
pub fn from_network_index(network: usize, stack: &ItemStack) -> Option<Self> {
let slot = if SLOT_HOTBAR_OFFSET <= network && network < SLOT_HOTBAR_OFFSET + HOTBAR_SIZE {
#[allow(clippy::manual_range_contains)]
pub fn from_network_index(index: usize, stack: &ItemStack) -> Option<Self> {
let slot = if SLOT_HOTBAR_OFFSET <= index && index < SLOT_HOTBAR_OFFSET + HOTBAR_SIZE {
// Hotbar
(network - SLOT_HOTBAR_OFFSET) as i8
} else if network == SLOT_OFFHAND {
(index - SLOT_HOTBAR_OFFSET) as i8
} else if index == SLOT_OFFHAND {
-106
} else if SLOT_ARMOR_MIN <= network && network <= SLOT_ARMOR_MAX {
((SLOT_ARMOR_MAX - network) + 100) as i8
} else if SLOT_INVENTORY_OFFSET <= network
&& network < SLOT_INVENTORY_OFFSET + INVENTORY_SIZE
{
network as i8
} else if SLOT_ARMOR_MIN <= index && index <= SLOT_ARMOR_MAX {
((SLOT_ARMOR_MAX - index) + 100) as i8
} else if SLOT_INVENTORY_OFFSET <= index && index < SLOT_INVENTORY_OFFSET + INVENTORY_SIZE {
index as i8
} else {
return None;
};
Expand Down
3 changes: 0 additions & 3 deletions feather/base/src/chunk/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ impl BlockStore {
&self.blocks
}

#[cfg(feature = "proxy")]
pub fn data_mut(&mut self) -> &mut PackedArray {
&mut self.blocks
}
Expand All @@ -60,7 +59,6 @@ impl BlockStore {
self.palette.as_ref()
}

#[cfg(feature = "proxy")]
pub fn palette_mut(&mut self) -> Option<&mut Palette> {
self.palette.as_mut()
}
Expand All @@ -83,7 +81,6 @@ impl BlockStore {
self.air_block_count
}

#[cfg(feature = "proxy")]
pub fn set_air_blocks(&mut self, new_value: u32) {
self.air_block_count = new_value;
}
Expand Down
1 change: 0 additions & 1 deletion feather/base/src/chunk/heightmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ where
self.heights.set(index, height as u64);
}

#[cfg(feature = "proxy")]
pub fn set_height_index(&mut self, index: usize, height: i64) {
self.heights.as_u64_mut_vec()[index] = height as u64;
}
Expand Down
11 changes: 2 additions & 9 deletions feather/base/src/chunk/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ impl Default for LightStore {
}

impl LightStore {
/// Creates a `LightStore` with all light set to 15.
/// Creates a `LightStore` with sky light set to 15.
pub fn new() -> Self {
let mut this = LightStore {
block_light: PackedArray::new(SECTION_VOLUME, 4),
sky_light: PackedArray::new(SECTION_VOLUME, 4),
};
fill_with_default_light(&mut this.block_light);
fill_with_default_light(&mut this.sky_light);
this.sky_light.fill(15);
this
}

Expand Down Expand Up @@ -73,9 +72,3 @@ impl LightStore {
&self.sky_light
}
}

fn fill_with_default_light(arr: &mut PackedArray) {
for i in 0..arr.len() {
arr.set(i, 15);
}
}
2 changes: 0 additions & 2 deletions feather/base/src/chunk/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ impl PackedArray {
self.bits_per_value
}

#[cfg(feature = "proxy")]
pub fn set_bits_per_value(&mut self, new_value: usize) {
self.bits_per_value = new_value;
}
Expand All @@ -176,7 +175,6 @@ impl PackedArray {
&self.bits
}

#[cfg(feature = "proxy")]
pub fn as_u64_mut_vec(&mut self) -> &mut Vec<u64> {
&mut self.bits
}
Expand Down
5 changes: 2 additions & 3 deletions feather/common/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl ChunkMap {
/// Retrieves a handle to the chunk at the given
/// position, or `None` if it is not loaded.
pub fn chunk_at_mut(&self, pos: ChunkPosition) -> Option<RwLockWriteGuard<Chunk>> {
self.0.get(&pos).map(|lock| lock.write()).flatten()
self.0.get(&pos).and_then(|lock| lock.write())
}

/// Returns an `Arc<RwLock<Chunk>>` at the given position.
Expand All @@ -212,8 +212,7 @@ impl ChunkMap {

let (x, y, z) = chunk_relative_pos(pos.into());
self.chunk_at(pos.chunk())
.map(|chunk| chunk.block_at(x, y, z))
.flatten()
.and_then(|chunk| chunk.block_at(x, y, z))
}

pub fn set_block_at(&self, pos: ValidBlockPosition, block: BlockId) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions feather/plugin-host/src/host_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod block;
mod component;
mod entity;
mod entity_builder;
mod event;
mod plugin_message;
mod query;
mod system;
Expand Down Expand Up @@ -49,6 +50,7 @@ use block::*;
use component::*;
use entity::*;
use entity_builder::*;
use event::*;
use plugin_message::*;
use query::*;
use system::*;
Expand All @@ -57,6 +59,8 @@ host_calls! {
"register_system" => register_system,
"entity_get_component" => entity_get_component,
"entity_set_component" => entity_set_component,
"entity_add_event" => entity_add_event,
"add_event" => add_event,
"entity_builder_new_empty" => entity_builder_new_empty,
"entity_builder_new" => entity_builder_new,
"entity_builder_add_component" => entity_builder_add_component,
Expand Down
38 changes: 24 additions & 14 deletions feather/plugin-host/src/host_calls/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,36 @@ pub fn entity_get_component(
Ok(())
}

struct SetComponentVisitor<'a> {
cx: &'a PluginContext,
entity: Entity,
bytes_ptr: PluginPtr<u8>,
bytes_len: u32,
pub(crate) struct InsertComponentVisitor<'a> {
pub cx: &'a PluginContext,
pub bytes_ptr: PluginPtr<u8>,
pub bytes_len: u32,
pub action: SetComponentAction,
}

pub(crate) enum SetComponentAction {
SetComponent(Entity),
AddEntityEvent(Entity),
AddEvent,
}

impl<'a> ComponentVisitor<anyhow::Result<()>> for SetComponentVisitor<'a> {
impl<'a> ComponentVisitor<anyhow::Result<()>> for InsertComponentVisitor<'a> {
fn visit<T: quill_common::Component>(self) -> anyhow::Result<()> {
let component = self
.cx
.read_component::<T>(self.bytes_ptr, self.bytes_len)?;
let mut game = self.cx.game_mut();

let existing_component = game.ecs.get_mut::<T>(self.entity);
if let Ok(mut existing_component) = existing_component {
*existing_component = component;
} else {
drop(existing_component);
let _ = game.ecs.insert(self.entity, component);
match self.action {
SetComponentAction::SetComponent(entity) => {
let _ = game.ecs.insert(entity, component);
}
SetComponentAction::AddEntityEvent(entity) => {
let _ = game.ecs.insert_entity_event(entity, component);
}
SetComponentAction::AddEvent => {
game.ecs.insert_event(component);
}
}

Ok(())
Expand All @@ -79,11 +89,11 @@ pub fn entity_set_component(
) -> anyhow::Result<()> {
let entity = Entity::from_bits(entity);
let component = HostComponent::from_u32(component).context("invalid component")?;
let visitor = SetComponentVisitor {
let visitor = InsertComponentVisitor {
cx,
entity,
bytes_ptr,
bytes_len,
action: SetComponentAction::SetComponent(entity),
};
component.visit(visitor)
}
42 changes: 42 additions & 0 deletions feather/plugin-host/src/host_calls/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::context::{PluginContext, PluginPtr};
use crate::host_calls::component::{InsertComponentVisitor, SetComponentAction};
use anyhow::Context;
use feather_ecs::Entity;
use feather_plugin_host_macros::host_function;
use quill_common::HostComponent;

#[host_function]
pub fn entity_add_event(
cx: &PluginContext,
entity: u64,
event: u32,
bytes_ptr: PluginPtr<u8>,
bytes_len: u32,
) -> anyhow::Result<()> {
let entity = Entity::from_bits(entity);
let event = HostComponent::from_u32(event).context("invalid component")?;
let visitor = InsertComponentVisitor {
cx,
bytes_ptr,
bytes_len,
action: SetComponentAction::AddEntityEvent(entity),
};
event.visit(visitor)
}

#[host_function]
pub fn add_event(
cx: &PluginContext,
event: u32,
bytes_ptr: PluginPtr<u8>,
bytes_len: u32,
) -> anyhow::Result<()> {
let event = HostComponent::from_u32(event).context("invalid component")?;
let visitor = InsertComponentVisitor {
cx,
bytes_ptr,
bytes_len,
action: SetComponentAction::AddEvent,
};
event.visit(visitor)
}
5 changes: 0 additions & 5 deletions feather/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,3 @@ thiserror = "1"
uuid = "0.8"
libcraft-core = { path = "../../libcraft/core" }
libcraft-items = { path = "../../libcraft/items" }



[features]
proxy = ["base/proxy"]
56 changes: 31 additions & 25 deletions feather/protocol/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use libcraft_items::InventorySlot::*;
use num_traits::{FromPrimitive, ToPrimitive};
use quill_common::components::PreviousGamemode;
use serde::{de::DeserializeOwned, Serialize};
use std::io::ErrorKind;
use std::{
borrow::Cow,
collections::BTreeMap,
Expand Down Expand Up @@ -160,31 +161,11 @@ where
pub struct VarInt(pub i32);

impl Readable for VarInt {
fn read(buffer: &mut Cursor<&[u8]>, version: ProtocolVersion) -> anyhow::Result<Self>
fn read(buffer: &mut Cursor<&[u8]>, _version: ProtocolVersion) -> anyhow::Result<Self>
where
Self: Sized,
{
let mut num_read = 0;
let mut result = 0;

loop {
let read = u8::read(buffer, version)?;
let value = i32::from(read & 0b0111_1111);
result |= value.overflowing_shl(7 * num_read).0;

num_read += 1;

if num_read > 5 {
bail!(
"VarInt too long (max length: 5, value read so far: {})",
result
);
}
if read & 0b1000_0000 == 0 {
break;
}
}
Ok(VarInt(result))
Self::read_from(buffer).map_err(Into::into)
}
}

Expand Down Expand Up @@ -214,8 +195,9 @@ impl From<i32> for VarInt {
}

impl VarInt {
pub fn write_to(&self, mut writer: impl Write) -> io::Result<()> {
pub fn write_to(&self, mut writer: impl Write) -> io::Result<usize> {
let mut x = self.0 as u32;
let mut i = 0;
loop {
let mut temp = (x & 0b0111_1111) as u8;
x >>= 7;
Expand All @@ -225,11 +207,35 @@ impl VarInt {

writer.write_all(&[temp])?;

i += 1;
if x == 0 {
break;
}
}
Ok(())
Ok(i)
}
pub fn read_from(mut reader: impl Read) -> io::Result<Self> {
let mut num_read = 0;
let mut result = 0;

loop {
let read = reader.read_u8()?;
let value = i32::from(read & 0b0111_1111);
result |= value.overflowing_shl(7 * num_read).0;

num_read += 1;

if num_read > 5 {
return Err(io::Error::new(
ErrorKind::InvalidData,
"VarInt too long (max length: 5)",
));
}
if read & 0b1000_0000 == 0 {
break;
}
}
Ok(VarInt(result))
}
}

Expand Down Expand Up @@ -559,7 +565,7 @@ impl Readable for Slot {
Ok(Filled(
ItemStackBuilder::with_item(item)
.count(count)
.apply_damage(tags.map(|t| t.damage).flatten())
.apply_damage(tags.and_then(|t| t.damage))
.into(),
))
} else {
Expand Down
Loading

0 comments on commit f9f5b6f

Please sign in to comment.