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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// mod some_unit_replica {
// use naia_shared::Replicate;
//
// #[derive(Replicate)]
// pub struct UnitHolder;
//
// impl UnitHolder {
// pub fn new() -> Self {
// return UnitHolder::new_complete();
// }
// }
// }
//
// mod some_named_replica {
// use naia_shared::{Property, Replicate};
//
// #[derive(Replicate)]
// pub struct NamedStringHolder {
// pub string_1: Property<String>,
// pub string_2: Property<String>,
// }
//
// impl NamedStringHolder {
// pub fn new(string_1: &str, string_2: &str) -> Self {
// return NamedStringHolder::new_complete(string_1.to_string(), string_2.to_string());
// }
// }
// }
//
// mod some_tuple_replica {
// use naia_shared::{Property, Replicate};
//
// #[derive(Replicate)]
// pub struct TupleStringHolder(pub Property<String>, pub Property<String>);
//
// impl TupleStringHolder {
// pub fn new(string_1: &str, string_2: &str) -> Self {
// return TupleStringHolder::new_complete(string_1.to_string(), string_2.to_string());
// }
// }
// }
//
// mod some_entity_replica {
// use naia_shared::{EntityProperty, Replicate};
// #[derive(Replicate)]
// pub struct EntityPropertyHolder {
// pub entity_1: EntityProperty,
// }
// impl EntityPropertyHolder {
// pub fn new() -> Self {
// return EntityPropertyHolder::new_complete();
// }
// }
// }
//
// mod some_nonreplicated_replica {
// use naia_shared::{Property, Replicate};
//
// #[derive(Replicate)]
// pub struct MixedReplicationHolder {
// pub string_1: Property<String>,
// pub string_2: String,
// }
//
// impl MixedReplicationHolder {
// pub fn new(string_1: &str, string_2: &str) -> Self {
// return MixedReplicationHolder::new_complete(
// string_1.to_string(),
// string_2.to_string(),
// );
// }
// }
// }
//
// use naia_shared::{
// BigMapKey, BitReader, BitWriter, EntityAndGlobalEntityConverter, EntityDoesNotExistError,
// FakeEntityConverter, GlobalEntity, LocalEntityAndGlobalEntityConverter, Protocol, Replicate,
// };
//
// use some_entity_replica::EntityPropertyHolder;
// use some_named_replica::NamedStringHolder;
// use some_nonreplicated_replica::MixedReplicationHolder;
// use some_tuple_replica::TupleStringHolder;
// use some_unit_replica::UnitHolder;
//
// #[test]
// fn read_write_unit_replica() {
// // Protocol
// let protocol = Protocol::builder().add_component::<UnitHolder>().build();
// let component_kinds = protocol.component_kinds;
//
// // Write
// let mut writer = BitWriter::new();
//
// let in_1 = UnitHolder::new();
//
// in_1.write(&component_kinds, &mut writer, &FakeEntityConverter);
//
// let bytes = writer.to_bytes();
//
// // Read
//
// let mut reader = BitReader::new(&bytes);
//
// let out_1 = component_kinds
// .read(&mut reader, &FakeEntityConverter)
// .expect("should deserialize correctly");
//
// let _typed_out_1 = out_1.to_boxed_any().downcast_ref::<UnitHolder>().unwrap();
// }
//
// #[test]
// fn read_write_named_replica() {
// // Protocol
// let protocol = Protocol::builder()
// .add_component::<NamedStringHolder>()
// .build();
// let component_kinds = protocol.component_kinds;
//
// // Write
// let mut writer = BitWriter::new();
//
// let in_1 = NamedStringHolder::new("hello world", "goodbye world");
//
// in_1.write(&component_kinds, &mut writer, &FakeEntityConverter);
//
// let bytes = writer.to_bytes();
//
// // Read
//
// let mut reader = BitReader::new(&bytes);
//
// let out_1 = component_kinds
// .read(&mut reader, &FakeEntityConverter)
// .expect("should deserialize correctly")
// .to_boxed_any();
//
// let typed_out_1 = out_1.downcast_ref::<NamedStringHolder>().unwrap();
// assert!(in_1.string_1.equals(&typed_out_1.string_1));
// assert!(in_1.string_2.equals(&typed_out_1.string_2));
// assert_eq!(*in_1.string_1, "hello world".to_string());
// assert_eq!(*in_1.string_2, "goodbye world".to_string());
// assert_eq!(*typed_out_1.string_1, "hello world".to_string());
// assert_eq!(*typed_out_1.string_2, "goodbye world".to_string());
// }
//
// #[test]
// fn read_write_tuple_replica() {
// // Protocol
// let protocol = Protocol::builder()
// .add_component::<TupleStringHolder>()
// .build();
// let component_kinds = protocol.component_kinds;
//
// // Write
// let mut writer = BitWriter::new();
//
// let in_1 = TupleStringHolder::new("hello world", "goodbye world");
//
// in_1.write(&component_kinds, &mut writer, &FakeEntityConverter);
//
// let bytes = writer.to_bytes();
//
// // Read
//
// let mut reader = BitReader::new(&bytes);
//
// let out_1 = component_kinds
// .read(&mut reader, &FakeEntityConverter)
// .expect("should deserialize correctly")
// .to_boxed_any();
//
// let typed_out_1 = out_1.downcast_ref::<TupleStringHolder>().unwrap();
// assert!(in_1.0.equals(&typed_out_1.0));
// assert!(in_1.1.equals(&typed_out_1.1));
// assert_eq!(*in_1.0, "hello world".to_string());
// assert_eq!(*in_1.1, "goodbye world".to_string());
// assert_eq!(*typed_out_1.0, "hello world".to_string());
// assert_eq!(*typed_out_1.1, "goodbye world".to_string());
// }
//
// #[test]
// fn read_write_entity_replica() {
// pub struct TestEntityConverter;
//
// impl EntityAndGlobalEntityConverter<u64> for TestEntityConverter {
// fn global_entity_to_entity(
// &self,
// global_entity: &GlobalEntity,
// ) -> Result<u64, EntityDoesNotExistError> {
// Ok(global_entity.to_u64())
// }
// fn entity_to_global_entity(
// &self,
// entity: &u64,
// ) -> Result<GlobalEntity, EntityDoesNotExistError> {
// Ok(GlobalEntity::from_u64(*entity))
// }
// }
// impl LocalEntityAndGlobalEntityConverter for TestEntityConverter {
// fn global_entity_to_host_entity(
// &self,
// global_entity: &GlobalEntity,
// ) -> Result<HostEntity, EntityDoesNotExistError> {
// Ok(LocalEntity::new_host(global_entity.to_u64() as u16))
// }
// fn host_entity_to_global_entity(
// &self,
// host_entity: &HostEntity,
// ) -> Result<GlobalEntity, EntityDoesNotExistError> {
// let local_entity_value = (*host_entity).value();
// Ok(GlobalEntity::from_u64(local_entity_value as u64))
// }
// }
//
// // Protocol
// let protocol = Protocol::builder()
// .add_component::<EntityPropertyHolder>()
// .build();
// let component_kinds = protocol.component_kinds;
//
// // Write
// let mut writer = BitWriter::new();
// let mut in_1 = EntityPropertyHolder::new();
// in_1.entity_1.set(&TestEntityConverter, &1);
// in_1.write(&component_kinds, &mut writer, &TestEntityConverter);
// let bytes = writer.to_bytes();
//
// // Read
// let mut reader = BitReader::new(&bytes);
// let out_1 = component_kinds
// .read(&mut reader, &TestEntityConverter)
// .expect("should deserialize correctly")
// .to_boxed_any();
//
// let typed_out_1 = out_1.downcast_ref::<EntityPropertyHolder>().unwrap();
// assert!(in_1.entity_1.equals(&typed_out_1.entity_1));
// let global_entitys = Vec::<GlobalEntity>::from([GlobalEntity::from_u64(1)]);
// assert_eq!(in_1.entities(), global_entitys);
// assert_eq!(typed_out_1.entities(), global_entitys);
// assert_eq!(in_1.entity_1.get(&TestEntityConverter).unwrap(), 1);
// assert_eq!(in_1.entity_1.global_entity().unwrap().to_u64(), 1);
// assert_eq!(typed_out_1.entity_1.get(&TestEntityConverter).unwrap(), 1);
// assert_eq!(typed_out_1.entity_1.global_entity().unwrap().to_u64(), 1);
// }
//
// #[test]
// fn read_write_nonreplicated_replica() {
// // Protocol
// let protocol = Protocol::builder()
// .add_component::<MixedReplicationHolder>()
// .build();
// let component_kinds = protocol.component_kinds;
//
// // Write
// let mut writer = BitWriter::new();
//
// let in_1 = MixedReplicationHolder::new("hello world", "goodbye world");
//
// in_1.write(&component_kinds, &mut writer, &FakeEntityConverter);
//
// let bytes = writer.to_bytes();
//
// // Read
//
// let mut reader = BitReader::new(&bytes);
//
// let out_1 = component_kinds
// .read(&mut reader, &FakeEntityConverter)
// .expect("should deserialize correctly")
// .to_boxed_any();
//
// let typed_out_1 = out_1.downcast_ref::<MixedReplicationHolder>().unwrap();
// assert!(in_1.string_1.equals(&typed_out_1.string_1));
// assert_eq!(*in_1.string_1, "hello world".to_string());
// assert_eq!(*in_1.string_2, "goodbye world".to_string());
// assert_eq!(*typed_out_1.string_1, "hello world".to_string());
// assert_eq!(*typed_out_1.string_2, "".to_string());
// }