-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.rs
More file actions
136 lines (119 loc) · 4.14 KB
/
Copy pathcomponent.rs
File metadata and controls
136 lines (119 loc) · 4.14 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use legion::*;
use crate::filter::ExternalLayoutFilter;
use legion::storage::{
ArchetypeSource, ArchetypeWriter, ComponentTypeId, EntityLayout, PackedStorage,
UnknownComponentStorage,
};
use std::any::TypeId;
use std::fmt::Debug;
use std::os::raw::c_void;
pub struct ExternalComponent {
_private: *const c_void,
}
unsafe impl Send for ExternalComponent {}
unsafe impl Sync for ExternalComponent {}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct ComponentData {
pub component_types: *const u32,
pub number_components: u32,
pub components: *const *const c_void,
pub layout: EntityLayout,
}
use log::*;
impl ArchetypeSource for ComponentData {
type Filter = ExternalLayoutFilter;
fn filter(&self) -> Self::Filter {
trace!("filter - start");
let filter = Self::Filter {};
trace!("filter - end");
filter
}
fn layout(&mut self) -> EntityLayout {
trace!("layout - start");
let constructor = || {
let storage = Box::new(PackedStorage::<ExternalComponent>::default())
as Box<dyn UnknownComponentStorage>;
info!(
"REGISTERING storage: {:?}",
&*storage as *const _ as *const c_void
);
storage
};
let mut ids: Vec<ComponentTypeId> = Vec::new();
unsafe {
for component_index in 0..self.number_components {
debug!(
"ext id: {}",
*(self.component_types).offset(component_index as isize) as u32
);
let id = ComponentTypeId {
type_id: TypeId::of::<ExternalComponent>(),
ext_type_id: Some(
*(self.component_types).offset(component_index as isize) as u32
),
name: "external component",
};
ids.push(id);
self.layout.register_component_raw(id, constructor);
}
}
let layout = self.layout.clone();
trace!("layout - end");
layout
}
}
unsafe impl Send for ComponentData {}
unsafe impl Sync for ComponentData {}
impl storage::IntoComponentSource for ComponentData {
type Source = Self;
fn into(self) -> Self {
self
}
}
impl storage::ComponentSource for ComponentData {
fn push_components<'b>(
&mut self,
writer: &mut ArchetypeWriter<'b>,
entities: impl Iterator<Item = Entity>,
) {
trace!("storage - push components _ start");
let mut ids: Vec<ComponentTypeId> = Vec::new();
unsafe {
for component_index in 0..self.number_components {
let id = ComponentTypeId {
type_id: TypeId::of::<ExternalComponent>(),
ext_type_id: Some(*(self.component_types.offset(component_index as isize))),
name: "external component",
};
ids.push(id);
}
}
// Sanity check only. Should be done without consuming the ids vec
// for id in ids {
// info!(
// "Layout has the component id [{:?}]? {}",
// id.ext_type_id,
// self.layout.has_component_by_id(id)
// );
// }
for e in entities {
writer.push(e);
info!("Creating entity - {:?}", e);
break;
}
for component_index in 0..self.number_components {
info!("storing components - #{}", component_index);
unsafe {
let id = ids[component_index as usize];
let mut unkown_component_writer = writer.claim_components_unknown(id);
let comp_ptr = *(self.components).offset(component_index as isize);
debug!("component pointer before storage magic: {:?}", comp_ptr);
let black_magic: *const *const c_void =
&[comp_ptr as *const c_void] as *const *const c_void;
unkown_component_writer.extend_memcopy_raw(black_magic as *mut u8, 1);
}
}
trace!("storage - push components _ end");
}
}