1use std::hash::Hash;
2
3use log::{info, warn};
4use naia_serde::{BitCounter, BitReader, BitWrite, BitWriter, Serde, SerdeErr};
5
6use crate::world::local::local_entity::OwnedLocalEntity;
7use crate::{
8 world::entity::{
9 entity_converters::{
10 EntityAndGlobalEntityConverter, LocalEntityAndGlobalEntityConverter,
11 LocalEntityAndGlobalEntityConverterMut,
12 },
13 global_entity::GlobalEntity,
14 },
15 EntityAuthAccessor, PropertyMutator, RemoteEntity,
16};
17
18#[derive(Clone)]
19enum EntityRelation {
20 HostCreated(HostCreatedRelation),
21 RemoteCreated(RemoteCreatedRelation),
22 RemoteWaiting(RemoteWaitingRelation),
23 RemotePublic(RemotePublicRelation),
24 Delegated(DelegatedRelation),
25 Local(LocalRelation),
26 Invalid,
27}
28
29impl EntityRelation {
30 fn clone_delegated(&self) -> Option<DelegatedRelation> {
31 match self {
32 EntityRelation::Delegated(inner) => Some(inner.clone()),
33 _ => None,
34 }
35 }
36 fn clone_public(&self) -> Option<RemotePublicRelation> {
37 match self {
38 EntityRelation::RemotePublic(inner) => Some(inner.clone()),
39 _ => None,
40 }
41 }
42 fn name(&self) -> &str {
43 match self {
44 EntityRelation::HostCreated(_) => "HostOwned",
45 EntityRelation::RemoteCreated(_) => "RemoteOwned",
46 EntityRelation::RemoteWaiting(_) => "RemoteWaiting",
47 EntityRelation::RemotePublic(_) => "RemotePublic",
48 EntityRelation::Delegated(_) => "Delegated",
49 EntityRelation::Local(_) => "Local",
50 EntityRelation::Invalid => "Invalid",
51 }
52 }
53 fn write(
54 &self,
55 writer: &mut dyn BitWrite,
56 converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
57 ) {
58 match self {
59 EntityRelation::HostCreated(inner) => {
60 inner.write(writer, converter);
61 }
62 EntityRelation::RemotePublic(inner) => {
63 inner.write(writer, converter);
64 }
65 EntityRelation::Delegated(inner) => {
66 inner.write(writer, converter);
67 }
68 EntityRelation::RemoteCreated(_)
69 | EntityRelation::RemoteWaiting(_)
70 | EntityRelation::Local(_)
71 | EntityRelation::Invalid => {
72 panic!(
73 "EntityProperty of inner type: `{:}` should never be written.",
74 self.name()
75 );
76 }
77 }
78 }
79 fn set_mutator(&mut self, mutator: &PropertyMutator) {
80 match self {
81 EntityRelation::HostCreated(inner) => {
82 inner.set_mutator(mutator);
83 }
84 EntityRelation::RemoteCreated(_)
85 | EntityRelation::RemoteWaiting(_)
86 | EntityRelation::RemotePublic(_)
87 | EntityRelation::Local(_)
88 | EntityRelation::Delegated(_)
89 | EntityRelation::Invalid => {
90 panic!(
91 "EntityProperty of inner type: `{:}` cannot call set_mutator()",
92 self.name()
93 );
94 }
95 }
96 }
97 fn bit_length(&self, converter: &mut dyn LocalEntityAndGlobalEntityConverterMut) -> u32 {
98 match self {
99 EntityRelation::HostCreated(inner) => inner.bit_length(converter),
100 EntityRelation::Delegated(inner) => inner.bit_length(converter),
101 EntityRelation::RemotePublic(inner) => inner.bit_length(converter),
102 EntityRelation::RemoteCreated(_)
103 | EntityRelation::RemoteWaiting(_)
104 | EntityRelation::Local(_)
105 | EntityRelation::Invalid => {
106 panic!(
107 "EntityProperty of inner type: `{:}` should never be written, so no need for their bit length.", self.name()
108 );
109 }
110 }
111 }
112 fn get<E: Copy + Eq + Hash + Sync + Send>(
113 &self,
114 converter: &dyn EntityAndGlobalEntityConverter<E>,
115 ) -> Option<E> {
116 let inner_global_entity = self.get_global_entity();
117
118 if let Some(global_entity) = inner_global_entity {
119 if let Ok(world_entity) = converter.global_entity_to_entity(&global_entity) {
120 return Some(world_entity);
121 } else {
122 warn!("Could not find World Entity from Global Entity `{:?}`, in order to get the EntityRelation value!", global_entity);
123 return None;
124 }
125 }
126 warn!("Could not get EntityRelation value, because EntityRelation has no GlobalEntity!");
127 None
128 }
129
130 fn set<E: Copy + Eq + Hash + Sync + Send>(
131 &mut self,
132 converter: &dyn EntityAndGlobalEntityConverter<E>,
133 entity: &E,
134 ) {
135 match self {
136 EntityRelation::HostCreated(inner) => {
137 inner.set(converter, entity);
138 }
139 EntityRelation::Local(inner) => {
140 inner.set(converter, entity);
141 }
142 EntityRelation::Delegated(inner) => {
143 inner.set(converter, entity);
144 }
145 EntityRelation::RemoteCreated(_)
146 | EntityRelation::RemoteWaiting(_)
147 | EntityRelation::RemotePublic(_)
148 | EntityRelation::Invalid => {
149 panic!("Remote EntityProperty should never be set manually.");
150 }
151 }
152 }
153 fn set_to_none(&mut self) {
154 match self {
155 EntityRelation::HostCreated(inner) => {
156 inner.set_to_none();
157 }
158 EntityRelation::Local(inner) => {
159 inner.set_to_none();
160 }
161 EntityRelation::Delegated(inner) => {
162 inner.set_to_none();
163 }
164 EntityRelation::RemoteCreated(_)
165 | EntityRelation::RemoteWaiting(_)
166 | EntityRelation::RemotePublic(_)
167 | EntityRelation::Invalid => {
168 panic!("Remote EntityProperty should never be set manually.");
169 }
170 }
171 }
172 fn mirror(&mut self, other: &EntityProperty) {
173 match self {
174 EntityRelation::HostCreated(inner) => match &other.inner {
175 EntityRelation::HostCreated(other_inner) => {
176 inner.set_global_entity(&other_inner.global_entity);
177 }
178 EntityRelation::RemoteCreated(other_inner) => {
179 inner.set_global_entity(&other_inner.global_entity);
180 }
181 EntityRelation::RemotePublic(other_inner) => {
182 inner.set_global_entity(&other_inner.global_entity);
183 }
184 EntityRelation::Local(other_inner) => {
185 inner.set_global_entity(&other_inner.global_entity);
186 }
187 EntityRelation::Delegated(other_inner) => {
188 inner.set_global_entity(&other_inner.global_entity);
189 }
190 EntityRelation::RemoteWaiting(_) => {
191 inner.mirror_waiting();
192 }
193 EntityRelation::Invalid => {
194 panic!("Invalid EntityProperty should never be mirrored.");
195 }
196 },
197 EntityRelation::Local(inner) => match &other.inner {
198 EntityRelation::HostCreated(other_inner) => {
199 inner.set_global_entity(&other_inner.global_entity);
200 }
201 EntityRelation::RemoteCreated(other_inner) => {
202 inner.set_global_entity(&other_inner.global_entity);
203 }
204 EntityRelation::RemotePublic(other_inner) => {
205 inner.set_global_entity(&other_inner.global_entity);
206 }
207 EntityRelation::Local(other_inner) => {
208 inner.set_global_entity(&other_inner.global_entity);
209 }
210 EntityRelation::Delegated(other_inner) => {
211 inner.set_global_entity(&other_inner.global_entity);
212 }
213 EntityRelation::RemoteWaiting(_) => {
214 inner.mirror_waiting();
215 }
216 EntityRelation::Invalid => {
217 panic!("Invalid EntityProperty should never be mirrored.");
218 }
219 },
220 EntityRelation::Delegated(inner) => match &other.inner {
221 EntityRelation::HostCreated(other_inner) => {
222 inner.set_global_entity(&other_inner.global_entity);
223 }
224 EntityRelation::RemoteCreated(other_inner) => {
225 inner.set_global_entity(&other_inner.global_entity);
226 }
227 EntityRelation::RemotePublic(other_inner) => {
228 inner.set_global_entity(&other_inner.global_entity);
229 }
230 EntityRelation::Local(other_inner) => {
231 inner.set_global_entity(&other_inner.global_entity);
232 }
233 EntityRelation::Delegated(other_inner) => {
234 inner.set_global_entity(&other_inner.global_entity);
235 }
236 EntityRelation::RemoteWaiting(_) => {
237 inner.mirror_waiting();
238 }
239 EntityRelation::Invalid => {
240 panic!("Invalid EntityProperty should never be mirrored.");
241 }
242 },
243 EntityRelation::RemoteCreated(_)
244 | EntityRelation::RemoteWaiting(_)
245 | EntityRelation::RemotePublic(_) => {
246 panic!("Remote EntityProperty should never be set manually.");
247 }
248 EntityRelation::Invalid => {
249 panic!("Invalid EntityProperty should never be set manually.");
250 }
251 }
252 }
253 fn waiting_remote_entity(&self) -> Option<RemoteEntity> {
254 match self {
255 EntityRelation::HostCreated(_)
256 | EntityRelation::RemoteCreated(_)
257 | EntityRelation::RemotePublic(_)
258 | EntityRelation::Local(_)
259 | EntityRelation::Delegated(_)
260 | EntityRelation::Invalid => None,
261 EntityRelation::RemoteWaiting(inner) => Some(inner.remote_entity),
262 }
263 }
264 pub fn write_local_entity(
265 &self,
266 converter: &dyn LocalEntityAndGlobalEntityConverter,
267 writer: &mut BitWriter,
268 ) {
269 match self {
270 EntityRelation::RemoteCreated(inner) => {
271 inner.write_local_entity(converter, writer);
272 }
273 EntityRelation::RemotePublic(inner) => {
274 inner.write_local_entity(converter, writer);
275 }
276 EntityRelation::Delegated(inner) => {
277 inner.write_local_entity(converter, writer);
278 }
279 EntityRelation::HostCreated(_)
280 | EntityRelation::RemoteWaiting(_)
281 | EntityRelation::Local(_)
282 | EntityRelation::Invalid => {
283 panic!(
284 "This type of EntityProperty: `{:?}` can't use this method",
285 self.name()
286 );
287 }
288 }
289 }
290
291 fn get_global_entity(&self) -> Option<GlobalEntity> {
292 match self {
293 EntityRelation::HostCreated(inner) => inner.global_entity,
294 EntityRelation::RemoteCreated(inner) => inner.global_entity,
295 EntityRelation::RemotePublic(inner) => inner.global_entity,
296 EntityRelation::Local(inner) => inner.global_entity,
297 EntityRelation::Delegated(inner) => inner.global_entity,
298 EntityRelation::RemoteWaiting(_) | EntityRelation::Invalid => None,
299 }
300 }
301}
302
303#[derive(Clone)]
305pub struct EntityProperty {
306 inner: EntityRelation,
307}
308
309impl EntityProperty {
310 pub fn new_for_message() -> Self {
313 Self {
314 inner: EntityRelation::HostCreated(HostCreatedRelation::new()),
315 }
316 }
317
318 pub fn new_for_component(mutator_index: u8) -> Self {
321 Self {
322 inner: EntityRelation::HostCreated(HostCreatedRelation::with_mutator(mutator_index)),
323 }
324 }
325
326 pub fn new_read(
329 reader: &mut BitReader,
330 converter: &dyn LocalEntityAndGlobalEntityConverter,
331 ) -> Result<Self, SerdeErr> {
332 let exists = bool::de(reader)?;
333 if exists {
334 let local_entity = OwnedLocalEntity::de(reader)?;
336
337 let redirected_entity = converter.apply_entity_redirect(&local_entity);
341
342 if let Ok(global_entity) = redirected_entity.convert_to_global(converter) {
345 let mut new_impl = RemoteCreatedRelation::new_empty();
346 new_impl.global_entity = Some(global_entity);
347
348 let new_self = Self {
349 inner: EntityRelation::RemoteCreated(new_impl),
350 };
351
352 Ok(new_self)
353 } else if let OwnedLocalEntity::Remote { .. } = redirected_entity {
354 let new_impl = RemoteWaitingRelation::new(redirected_entity.take_remote());
355
356 let new_self = Self {
357 inner: EntityRelation::RemoteWaiting(new_impl),
358 };
359
360 Ok(new_self)
361 } else {
362 Ok(Self {
363 inner: EntityRelation::Invalid,
364 })
365 }
366 } else {
367 let mut new_impl = RemoteCreatedRelation::new_empty();
368 new_impl.global_entity = None;
369
370 let new_self = Self {
371 inner: EntityRelation::RemoteCreated(new_impl),
372 };
373
374 Ok(new_self)
375 }
376 }
377
378 pub fn read_write(reader: &mut BitReader, writer: &mut BitWriter) -> Result<(), SerdeErr> {
380 let exists = bool::de(reader)?;
381 exists.ser(writer);
382 if exists {
383 OwnedLocalEntity::de(reader)?.ser(writer);
384 }
385 Ok(())
386 }
387
388 pub fn read(
390 &mut self,
391 reader: &mut BitReader,
392 converter: &dyn LocalEntityAndGlobalEntityConverter,
393 ) -> Result<(), SerdeErr> {
394 let exists = bool::de(reader)?;
395 let local_entity_opt = if exists {
396 Some(OwnedLocalEntity::de(reader)?)
397 } else {
398 None
399 };
400
401 let eval = (
402 self.inner.clone_public(),
403 self.inner.clone_delegated(),
404 local_entity_opt,
405 local_entity_opt.map(|local_entity| local_entity.convert_to_global(converter)),
406 );
407 self.inner = match eval {
408 (None, None, None, None) => {
409 EntityRelation::RemoteCreated(RemoteCreatedRelation::new_empty())
410 }
411 (None, None, Some(local_entity), Some(Err(_))) => {
412 info!("1 setting inner to RemoteWaiting");
413 EntityRelation::RemoteWaiting(RemoteWaitingRelation::new(
414 local_entity.take_remote(),
415 ))
416 }
417 (None, None, Some(_), Some(Ok(global_entity))) => EntityRelation::RemoteCreated(
418 RemoteCreatedRelation::new_with_value(Some(global_entity)),
419 ),
420 (Some(public_relation), None, None, None) => EntityRelation::RemotePublic(
421 RemotePublicRelation::new(None, public_relation.index, &public_relation.mutator),
422 ),
423 (Some(public_relation), None, Some(local_entity), Some(Err(_))) => {
424 EntityRelation::RemoteWaiting(RemoteWaitingRelation::new_public(
425 local_entity.take_remote(),
426 public_relation.index,
427 &public_relation.mutator,
428 ))
429 }
430 (Some(public_relation), None, Some(_), Some(Ok(global_entity))) => {
431 EntityRelation::RemotePublic(RemotePublicRelation::new(
432 Some(global_entity),
433 public_relation.index,
434 &public_relation.mutator,
435 ))
436 }
437 (None, Some(delegated_relation), None, None) => {
438 EntityRelation::Delegated(delegated_relation.read_none())
439 }
440 (None, Some(delegated_relation), Some(local_entity), Some(Err(_))) => {
441 info!("3 setting inner to RemoteWaiting");
442 EntityRelation::RemoteWaiting(RemoteWaitingRelation::new_delegated(
443 local_entity.take_remote(),
444 &delegated_relation.auth_accessor,
445 &delegated_relation.mutator,
446 delegated_relation.index,
447 ))
448 }
449 (None, Some(delegate_relation), Some(_), Some(Ok(global_entity))) => {
450 EntityRelation::Delegated(delegate_relation.read_some(global_entity))
451 }
452 _ => {
453 panic!("This shouldn't be possible. Unknown read case for EntityProperty.")
454 }
455 };
456
457 Ok(())
458 }
459
460 pub fn waiting_complete(&mut self, converter: &dyn LocalEntityAndGlobalEntityConverter) {
462 match &mut self.inner {
463 EntityRelation::RemoteCreated(_)
464 | EntityRelation::RemotePublic(_)
465 | EntityRelation::Delegated(_) => {
466 }
471 EntityRelation::RemoteWaiting(inner) => {
472 let new_global_entity = {
473 let owned_entity = inner.remote_entity.copy_to_owned();
476 let redirected_entity = converter.apply_entity_redirect(&owned_entity);
477
478 if let Ok(global_entity) = redirected_entity.convert_to_global(converter) {
479 Some(global_entity)
480 } else {
481 panic!("Error completing waiting EntityProperty! Could not convert RemoteEntity to GlobalEntity! Original: {:?}, Redirected: {:?}",
482 owned_entity, redirected_entity);
483 }
484 };
485
486 if let Some((index, mutator)) = &inner.will_publish {
487 if let Some(accessor) = &inner.will_delegate {
488 let mut new_impl =
490 DelegatedRelation::new(new_global_entity, accessor, mutator, *index);
491 new_impl.global_entity = new_global_entity;
492 self.inner = EntityRelation::Delegated(new_impl);
493 } else {
494 let new_impl =
496 RemotePublicRelation::new(new_global_entity, *index, mutator);
497 self.inner = EntityRelation::RemotePublic(new_impl);
498 }
499 } else {
500 let mut new_impl = RemoteCreatedRelation::new_empty();
502 new_impl.global_entity = new_global_entity;
503 self.inner = EntityRelation::RemoteCreated(new_impl);
504 }
505 }
506 EntityRelation::HostCreated(_) | EntityRelation::Local(_) | EntityRelation::Invalid => {
507 panic!(
508 "Can't complete EntityProperty of type: `{:?}`!",
509 self.inner.name()
510 );
511 }
512 }
513 }
514
515 pub fn remote_publish(&mut self, mutator_index: u8, mutator: &PropertyMutator) {
517 match &mut self.inner {
518 EntityRelation::RemoteCreated(inner) => {
519 let inner_value = inner.global_entity;
520 self.inner = EntityRelation::RemotePublic(RemotePublicRelation::new(
521 inner_value,
522 mutator_index,
523 mutator,
524 ));
525 }
526 EntityRelation::RemoteWaiting(inner) => {
527 inner.remote_publish(mutator_index, mutator);
528 }
529 EntityRelation::HostCreated(_)
530 | EntityRelation::RemotePublic(_)
531 | EntityRelation::Local(_)
532 | EntityRelation::Delegated(_)
533 | EntityRelation::Invalid => {
534 panic!(
535 "EntityProperty of type: `{:?}` should never be made public twice.",
536 self.inner.name()
537 );
538 }
539 }
540 }
541
542 pub fn remote_unpublish(&mut self) {
544 match &mut self.inner {
545 EntityRelation::RemotePublic(inner) => {
546 let inner_value = inner.global_entity;
547 self.inner = EntityRelation::RemoteCreated(RemoteCreatedRelation {
548 global_entity: inner_value,
549 });
550 }
551 EntityRelation::RemoteWaiting(inner) => {
552 inner.remote_unpublish();
553 }
554 EntityRelation::HostCreated(_)
555 | EntityRelation::RemoteCreated(_)
556 | EntityRelation::Local(_)
557 | EntityRelation::Delegated(_)
558 | EntityRelation::Invalid => {
559 panic!(
560 "EntityProperty of type: `{:?}` should never be unpublished.",
561 self.inner.name()
562 );
563 }
564 }
565 }
566
567 pub fn enable_delegation(
569 &mut self,
570 accessor: &EntityAuthAccessor,
571 mutator_opt: Option<(u8, &PropertyMutator)>,
572 ) {
573 let inner_value = self.inner.get_global_entity();
574
575 let (mutator_index, mutator) = {
576 if let Some((mutator_index, mutator)) = mutator_opt {
577 match &mut self.inner {
579 EntityRelation::RemoteCreated(_) => (mutator_index, mutator),
580 EntityRelation::RemoteWaiting(inner) => {
581 inner.remote_delegate(accessor);
582 return;
583 }
584 EntityRelation::Local(_)
585 | EntityRelation::RemotePublic(_)
586 | EntityRelation::HostCreated(_)
587 | EntityRelation::Delegated(_)
588 | EntityRelation::Invalid => {
589 panic!(
590 "EntityProperty of type `{:?}` should never enable delegation.",
591 self.inner.name()
592 );
593 }
594 }
595 } else {
596 match &mut self.inner {
598 EntityRelation::HostCreated(inner) => (
599 inner.index,
600 inner
601 .mutator
602 .as_ref()
603 .expect("should have a mutator by now"),
604 ),
605 EntityRelation::RemotePublic(inner) => (inner.index, &inner.mutator),
606 EntityRelation::Local(_)
607 | EntityRelation::RemoteCreated(_)
608 | EntityRelation::RemoteWaiting(_)
609 | EntityRelation::Delegated(_)
610 | EntityRelation::Invalid => {
611 panic!(
612 "EntityProperty of type `{:?}` should never enable delegation.",
613 self.inner.name()
614 );
615 }
616 }
617 }
618 };
619
620 self.inner = EntityRelation::Delegated(DelegatedRelation::new(
621 inner_value,
622 accessor,
623 mutator,
624 mutator_index,
625 ));
626 }
627
628 pub fn disable_delegation(&mut self) {
630 match &mut self.inner {
631 EntityRelation::Delegated(inner) => {
632 let inner_value = inner.global_entity;
633 let mut new_inner = HostCreatedRelation::with_mutator(inner.index);
634 new_inner.set_mutator(&inner.mutator);
635 new_inner.global_entity = inner_value;
636 self.inner = EntityRelation::HostCreated(new_inner);
637 }
638 EntityRelation::RemoteWaiting(inner) => {
639 inner.remote_undelegate();
640 }
641 EntityRelation::HostCreated(_)
642 | EntityRelation::RemoteCreated(_)
643 | EntityRelation::RemotePublic(_)
644 | EntityRelation::Local(_)
645 | EntityRelation::Invalid => {
646 panic!(
647 "EntityProperty of type: `{:?}` should never disable delegation.",
648 self.inner.name()
649 );
650 }
651 }
652 }
653
654 pub fn localize(&mut self) {
656 match &mut self.inner {
657 EntityRelation::HostCreated(inner) => {
658 let inner_value = inner.global_entity;
659 self.inner = EntityRelation::Local(LocalRelation::new(inner_value));
660 }
661 EntityRelation::Delegated(inner) => {
662 let inner_value = inner.global_entity;
663 self.inner = EntityRelation::Local(LocalRelation::new(inner_value));
664 }
665 EntityRelation::RemoteCreated(_)
666 | EntityRelation::RemotePublic(_)
667 | EntityRelation::RemoteWaiting(_)
668 | EntityRelation::Local(_)
669 | EntityRelation::Invalid => {
670 panic!(
671 "EntityProperty of type: `{:?}` should never be made local.",
672 self.inner.name()
673 );
674 }
675 }
676 }
677
678 pub fn set_mutator(&mut self, mutator: &PropertyMutator) {
682 self.inner.set_mutator(mutator);
683 }
684
685 pub fn bit_length(&self, converter: &mut dyn LocalEntityAndGlobalEntityConverterMut) -> u32 {
689 self.inner.bit_length(converter)
690 }
691
692 pub fn write(
694 &self,
695 writer: &mut dyn BitWrite,
696 converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
697 ) {
698 self.inner.write(writer, converter);
699 }
700
701 pub fn get<E: Copy + Eq + Hash + Sync + Send>(
703 &self,
704 converter: &dyn EntityAndGlobalEntityConverter<E>,
705 ) -> Option<E> {
706 self.inner.get(converter)
707 }
708
709 pub fn get_inner(&self) -> Option<GlobalEntity> {
711 self.inner.get_global_entity()
712 }
713
714 pub fn set<E: Copy + Eq + Hash + Sync + Send>(
716 &mut self,
717 converter: &dyn EntityAndGlobalEntityConverter<E>,
718 entity: &E,
719 ) {
720 self.inner.set(converter, entity);
721 }
722
723 pub fn set_to_none(&mut self) {
725 self.inner.set_to_none();
726 }
727
728 pub fn mirror(&mut self, other: &EntityProperty) {
730 self.inner.mirror(other);
731 }
732
733 pub fn waiting_remote_entity(&self) -> Option<RemoteEntity> {
735 self.inner.waiting_remote_entity()
736 }
737
738 pub fn write_local_entity(
741 &self,
742 converter: &dyn LocalEntityAndGlobalEntityConverter,
743 writer: &mut BitWriter,
744 ) {
745 self.inner.write_local_entity(converter, writer);
746 }
747}
748
749#[derive(Clone)]
751struct HostCreatedRelation {
752 global_entity: Option<GlobalEntity>,
753 mutator: Option<PropertyMutator>,
754 index: u8,
755}
756
757impl HostCreatedRelation {
758 pub fn new() -> Self {
759 Self {
760 global_entity: None,
761 mutator: None,
762 index: 0,
763 }
764 }
765
766 pub fn with_mutator(mutate_index: u8) -> Self {
767 Self {
768 global_entity: None,
769 mutator: None,
770 index: mutate_index,
771 }
772 }
773
774 pub fn set_mutator(&mut self, mutator: &PropertyMutator) {
775 self.mutator = Some(mutator.clone_new());
776 }
777
778 pub fn write(
779 &self,
780 writer: &mut dyn BitWrite,
781 converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
782 ) {
783 let Some(global_entity) = &self.global_entity else {
784 false.ser(writer);
785 return;
786 };
787
788 let Ok(owned_local_entity) = converter.get_or_reserve_entity(global_entity) else {
791 false.ser(writer);
792 return;
793 };
794
795 let reversed_local_entity = owned_local_entity.to_reversed();
800
801 true.ser(writer);
802 reversed_local_entity.ser(writer);
803 }
804
805 pub fn bit_length(&self, converter: &mut dyn LocalEntityAndGlobalEntityConverterMut) -> u32 {
806 let mut bit_counter = BitCounter::new(0, 0, u32::MAX);
807 self.write(&mut bit_counter, converter);
808 bit_counter.bits_needed()
809 }
810
811 pub fn set<E: Copy + Eq + Hash + Sync + Send>(
812 &mut self,
813 converter: &dyn EntityAndGlobalEntityConverter<E>,
814 world_entity: &E,
815 ) {
816 if let Ok(new_global_entity) = converter.entity_to_global_entity(world_entity) {
817 self.global_entity = Some(new_global_entity);
818 self.mutate();
819 } else {
820 warn!("Could not find Global Entity from World Entity, in order to set the EntityRelation value!");
821 }
822 }
823
824 pub fn set_to_none(&mut self) {
825 self.global_entity = None;
826 self.mutate();
827 }
828
829 pub fn mirror_waiting(&mut self) {
830 self.global_entity = None;
831 self.mutate();
832 }
833
834 pub fn set_global_entity(&mut self, other_global_entity: &Option<GlobalEntity>) {
835 self.global_entity = *other_global_entity;
836 self.mutate();
837 }
838
839 fn mutate(&mut self) {
840 let _success = if let Some(mutator) = &mut self.mutator {
841 mutator.mutate(self.index)
842 } else {
843 false
844 };
845 }
846}
847
848#[derive(Clone, Debug)]
850struct RemoteCreatedRelation {
851 global_entity: Option<GlobalEntity>,
852}
853
854impl RemoteCreatedRelation {
855 fn new_empty() -> Self {
856 Self {
857 global_entity: None,
858 }
859 }
860
861 fn new_with_value(global_entity: Option<GlobalEntity>) -> Self {
862 Self { global_entity }
863 }
864
865 pub fn write_local_entity(
866 &self,
867 converter: &dyn LocalEntityAndGlobalEntityConverter,
868 writer: &mut BitWriter,
869 ) {
870 let Some(global_entity) = &self.global_entity else {
871 false.ser(writer);
872 return;
873 };
874 let Ok(owned_entity) = converter.global_entity_to_owned_entity(global_entity) else {
875 warn!("Could not find Local Entity from Global Entity, in order to write the EntityRelation value! This should not happen.");
876 false.ser(writer);
877 return;
878 };
879 true.ser(writer);
880 owned_entity.ser(writer);
881 }
882}
883
884#[derive(Clone)]
886struct RemoteWaitingRelation {
887 remote_entity: RemoteEntity,
888 will_publish: Option<(u8, PropertyMutator)>,
889 will_delegate: Option<EntityAuthAccessor>,
890}
891
892impl RemoteWaitingRelation {
893 fn new(remote_entity: RemoteEntity) -> Self {
894 Self {
895 remote_entity,
896 will_publish: None,
897 will_delegate: None,
898 }
899 }
900 fn new_public(remote_entity: RemoteEntity, index: u8, mutator: &PropertyMutator) -> Self {
901 Self {
902 remote_entity,
903 will_publish: Some((index, mutator.clone_new())),
904 will_delegate: None,
905 }
906 }
907 fn new_delegated(
908 local_entity: RemoteEntity,
909 auth_accessor: &EntityAuthAccessor,
910 mutator: &PropertyMutator,
911 index: u8,
912 ) -> Self {
913 Self {
914 remote_entity: local_entity,
915 will_publish: Some((index, mutator.clone_new())),
916 will_delegate: Some(auth_accessor.clone()),
917 }
918 }
919 pub(crate) fn remote_publish(&mut self, index: u8, mutator: &PropertyMutator) {
920 self.will_publish = Some((index, mutator.clone_new()));
921 }
922 pub(crate) fn remote_unpublish(&mut self) {
923 self.will_publish = None;
924 }
925 pub(crate) fn remote_delegate(&mut self, accessor: &EntityAuthAccessor) {
926 self.will_delegate = Some(accessor.clone());
927 }
928 pub(crate) fn remote_undelegate(&mut self) {
929 self.will_delegate = None;
930 }
931}
932
933#[derive(Clone)]
935struct RemotePublicRelation {
936 global_entity: Option<GlobalEntity>,
937 mutator: PropertyMutator,
938 index: u8,
939}
940
941impl RemotePublicRelation {
942 pub fn new(global_entity: Option<GlobalEntity>, index: u8, mutator: &PropertyMutator) -> Self {
943 Self {
944 global_entity,
945 mutator: mutator.clone_new(),
946 index,
947 }
948 }
949
950 pub fn bit_length(&self, converter: &mut dyn LocalEntityAndGlobalEntityConverterMut) -> u32 {
951 let mut bit_counter = BitCounter::new(0, 0, u32::MAX);
952 self.write(&mut bit_counter, converter);
953 bit_counter.bits_needed()
954 }
955
956 pub fn write(
957 &self,
958 writer: &mut dyn BitWrite,
959 converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
960 ) {
961 let Some(global_entity) = &self.global_entity else {
962 false.ser(writer);
963 return;
964 };
965 let Ok(local_entity) = converter.get_or_reserve_entity(global_entity) else {
966 false.ser(writer);
967 return;
968 };
969
970 let reversed_local_entity = local_entity.to_reversed();
973
974 true.ser(writer);
975 reversed_local_entity.ser(writer);
976 }
977
978 pub fn write_local_entity(
979 &self,
980 converter: &dyn LocalEntityAndGlobalEntityConverter,
981 writer: &mut BitWriter,
982 ) {
983 let Some(global_entity) = &self.global_entity else {
984 false.ser(writer);
985 return;
986 };
987 let Ok(owned_entity) = converter.global_entity_to_owned_entity(global_entity) else {
988 warn!("Could not find Local Entity from Global Entity, in order to write the EntityRelation value! This should not happen.");
989 false.ser(writer);
990 return;
991 };
992 true.ser(writer);
993 owned_entity.ser(writer);
994 }
995}
996
997#[derive(Clone)]
999struct DelegatedRelation {
1000 global_entity: Option<GlobalEntity>,
1001 auth_accessor: EntityAuthAccessor,
1002 mutator: PropertyMutator,
1003 index: u8,
1004}
1005
1006impl DelegatedRelation {
1007 pub fn new(
1009 global_entity: Option<GlobalEntity>,
1010 auth_accessor: &EntityAuthAccessor,
1011 mutator: &PropertyMutator,
1012 index: u8,
1013 ) -> Self {
1014 Self {
1015 global_entity,
1016 auth_accessor: auth_accessor.clone(),
1017 mutator: mutator.clone_new(),
1018 index,
1019 }
1020 }
1021
1022 pub fn set<E: Copy + Eq + Hash + Sync + Send>(
1023 &mut self,
1024 converter: &dyn EntityAndGlobalEntityConverter<E>,
1025 world_entity: &E,
1026 ) {
1027 if let Ok(new_global_entity) = converter.entity_to_global_entity(world_entity) {
1028 self.global_entity = Some(new_global_entity);
1029 self.mutate();
1030 } else {
1031 warn!("Could not find Global Entity from World Entity, in order to set the EntityRelation value!");
1032 }
1033 }
1034
1035 pub fn set_to_none(&mut self) {
1036 self.global_entity = None;
1037 self.mutate();
1038 }
1039
1040 pub fn set_global_entity(&mut self, other_global_entity: &Option<GlobalEntity>) {
1041 self.global_entity = *other_global_entity;
1042 self.mutate();
1043 }
1044
1045 pub fn mirror_waiting(&mut self) {
1046 self.global_entity = None;
1047 self.mutate();
1048 }
1049
1050 pub fn read_none(mut self) -> Self {
1051 if self.can_read() {
1052 self.global_entity = None;
1053 self.mutate();
1054 }
1055
1056 self
1057 }
1058
1059 pub fn read_some(mut self, global_entity: GlobalEntity) -> Self {
1060 if self.can_read() {
1061 self.global_entity = Some(global_entity);
1062 self.mutate();
1063 }
1064
1065 self
1066 }
1067
1068 pub fn bit_length(&self, converter: &mut dyn LocalEntityAndGlobalEntityConverterMut) -> u32 {
1069 if !self.can_write() {
1070 panic!("Must have Authority over Entity before performing this operation.");
1071 }
1072 let mut bit_counter = BitCounter::new(0, 0, u32::MAX);
1073 self.write(&mut bit_counter, converter);
1074 bit_counter.bits_needed()
1075 }
1076
1077 pub fn write(
1078 &self,
1079 writer: &mut dyn BitWrite,
1080 converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
1081 ) {
1082 if !self.can_write() {
1083 panic!("Must have Authority over Entity before performing this operation.");
1084 }
1085
1086 let Some(global_entity) = &self.global_entity else {
1087 false.ser(writer);
1088 return;
1089 };
1090 let Ok(local_entity) = converter.get_or_reserve_entity(global_entity) else {
1091 false.ser(writer);
1092 return;
1093 };
1094
1095 let reversed_local_entity = local_entity.to_reversed();
1098
1099 true.ser(writer);
1100 reversed_local_entity.ser(writer);
1101 }
1102
1103 pub fn write_local_entity(
1104 &self,
1105 converter: &dyn LocalEntityAndGlobalEntityConverter,
1106 writer: &mut BitWriter,
1107 ) {
1108 let Some(global_entity) = &self.global_entity else {
1109 false.ser(writer);
1110 return;
1111 };
1112 let Ok(host_entity) = converter.global_entity_to_owned_entity(global_entity) else {
1113 warn!("Could not find Local Entity from Global Entity, in order to write the EntityRelation value! This should not happen.");
1114 false.ser(writer);
1115 return;
1116 };
1117 true.ser(writer);
1118 host_entity.ser(writer);
1119 }
1120
1121 fn mutate(&mut self) {
1122 if !self.can_mutate() {
1123 panic!("Must request authority to mutate a Delegated EntityProperty.");
1124 }
1125 let _success = self.mutator.mutate(self.index);
1126 }
1127
1128 fn can_mutate(&self) -> bool {
1129 self.auth_accessor.auth_status().can_mutate()
1130 }
1131
1132 fn can_read(&self) -> bool {
1133 self.auth_accessor.auth_status().can_read()
1134 }
1135
1136 fn can_write(&self) -> bool {
1137 self.auth_accessor.auth_status().can_write()
1138 }
1139}
1140
1141#[derive(Clone, Debug)]
1143struct LocalRelation {
1144 global_entity: Option<GlobalEntity>,
1145}
1146
1147impl LocalRelation {
1148 pub fn new(global_entity: Option<GlobalEntity>) -> Self {
1149 Self { global_entity }
1150 }
1151
1152 pub fn set<E: Copy + Eq + Hash + Sync + Send>(
1153 &mut self,
1154 converter: &dyn EntityAndGlobalEntityConverter<E>,
1155 world_entity: &E,
1156 ) {
1157 if let Ok(new_global_entity) = converter.entity_to_global_entity(world_entity) {
1158 self.global_entity = Some(new_global_entity);
1159 } else {
1160 warn!("Could not find Global Entity from World Entity, in order to set the EntityRelation value!");
1161 }
1162 }
1163
1164 pub fn set_to_none(&mut self) {
1165 self.global_entity = None;
1166 }
1167
1168 pub fn mirror_waiting(&mut self) {
1169 self.global_entity = None;
1170 }
1171
1172 pub fn set_global_entity(&mut self, other_global_entity: &Option<GlobalEntity>) {
1173 self.global_entity = *other_global_entity;
1174 }
1175}