use std::{any::TypeId, collections::HashMap};
use naia_serde::{BitReader, BitWrite, Serde, SerdeErr};
use crate::{
ComponentUpdate, LocalEntityAndGlobalEntityConverter,
Replicate, ReplicateBuilder,
};
use crate::world::component::replicate::SplitUpdateResult;
type NetId = u16;
fn bit_width_for_kind_count(count: NetId) -> u8 {
if count < 2 {
0
} else {
(count as u32).next_power_of_two().trailing_zeros() as u8
}
}
#[derive(Eq, Hash, Copy, Clone, PartialEq, Debug)]
pub struct ComponentKind {
type_id: TypeId,
}
impl From<TypeId> for ComponentKind {
fn from(type_id: TypeId) -> Self {
Self { type_id }
}
}
impl From<ComponentKind> for TypeId {
fn from(val: ComponentKind) -> Self {
val.type_id
}
}
impl ComponentKind {
pub fn of<C: Replicate>() -> Self {
Self {
type_id: TypeId::of::<C>(),
}
}
pub fn ser(&self, component_kinds: &ComponentKinds, writer: &mut dyn BitWrite) {
let net_id = component_kinds.kind_to_net_id(self);
let bits = component_kinds.kind_bit_width;
for i in 0..bits {
writer.write_bit((net_id >> i) & 1 != 0);
}
}
pub fn de(component_kinds: &ComponentKinds, reader: &mut BitReader) -> Result<Self, SerdeErr> {
let bits = component_kinds.kind_bit_width;
let mut net_id: NetId = 0;
for i in 0..bits {
if bool::de(reader)? {
net_id |= 1 << i;
}
}
Ok(component_kinds.net_id_to_kind(&net_id))
}
}
pub struct ComponentKinds {
current_net_id: NetId,
kind_bit_width: u8,
kind_map: HashMap<ComponentKind, (NetId, Box<dyn ReplicateBuilder>, String)>,
net_id_map: HashMap<NetId, ComponentKind>,
}
impl Clone for ComponentKinds {
fn clone(&self) -> Self {
let current_net_id = self.current_net_id;
let kind_bit_width = self.kind_bit_width;
let net_id_map = self.net_id_map.clone();
let mut kind_map = HashMap::new();
for (key, value) in self.kind_map.iter() {
kind_map.insert(*key, (value.0, value.1.box_clone(), value.2.clone()));
}
Self {
current_net_id,
kind_bit_width,
kind_map,
net_id_map,
}
}
}
impl Default for ComponentKinds {
fn default() -> Self {
Self::new()
}
}
impl ComponentKinds {
pub fn new() -> Self {
Self {
current_net_id: 0,
kind_bit_width: 0,
kind_map: HashMap::new(),
net_id_map: HashMap::new(),
}
}
pub fn add_component<C: Replicate>(&mut self) {
let component_kind = ComponentKind::of::<C>();
let net_id = self.current_net_id;
self.kind_map.insert(
component_kind,
(net_id, C::create_builder(), C::protocol_name().to_string()),
);
self.net_id_map.insert(net_id, component_kind);
self.current_net_id += 1;
self.kind_bit_width = bit_width_for_kind_count(self.current_net_id);
}
pub fn kind_count(&self) -> u16 {
self.current_net_id
}
pub fn read(
&self,
reader: &mut BitReader,
converter: &dyn LocalEntityAndGlobalEntityConverter,
) -> Result<Box<dyn Replicate>, SerdeErr> {
let component_kind: ComponentKind = ComponentKind::de(self, reader)?;
self
.kind_to_builder(&component_kind)
.read(reader, converter)
}
pub fn read_create_update(&self, reader: &mut BitReader) -> Result<ComponentUpdate, SerdeErr> {
let component_kind: ComponentKind = ComponentKind::de(self, reader)?;
self
.kind_to_builder(&component_kind)
.read_create_update(reader)
}
pub fn split_update(
&self,
converter: &dyn LocalEntityAndGlobalEntityConverter,
component_kind: &ComponentKind,
update: ComponentUpdate,
) -> SplitUpdateResult {
self
.kind_to_builder(component_kind)
.split_update(converter, update)
}
pub fn kind_to_name(&self, component_kind: &ComponentKind) -> String {
self
.kind_map
.get(component_kind)
.expect(
"Must properly initialize Component with Protocol via `add_component()` function!",
)
.2
.clone()
}
fn net_id_to_kind(&self, net_id: &NetId) -> ComponentKind {
*self.net_id_map.get(net_id).expect(
"Must properly initialize Component with Protocol via `add_component()` function!",
)
}
fn kind_to_net_id(&self, component_kind: &ComponentKind) -> NetId {
self
.kind_map
.get(component_kind)
.expect(
"Must properly initialize Component with Protocol via `add_component()` function!",
)
.0
}
pub fn net_id_of(&self, component_kind: &ComponentKind) -> Option<u16> {
self.kind_map.get(component_kind).map(|(net_id, _, _)| *net_id)
}
fn kind_to_builder(&self, component_kind: &ComponentKind) -> &dyn ReplicateBuilder {
self
.kind_map
.get(component_kind)
.expect(
"Must properly initialize Component with Protocol via `add_component()` function!",
)
.1
.as_ref()
}
pub fn kind_is_immutable(&self, component_kind: &ComponentKind) -> bool {
self.kind_map
.get(component_kind)
.map(|(_, builder, _)| builder.is_immutable())
.unwrap_or(false)
}
pub fn all_names(&self) -> Vec<String> {
let mut output = Vec::new();
for (_, _, name) in self.kind_map.values() {
output.push(name.clone());
}
output.sort();
output
}
}