node_tree 0.15.0

An extendable scene graph made up of autonomous execution services known as nodes organized in a tree of processes. Inspired by Godot!
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
//===================================================================================================================================================================================//
//
//  /$$   /$$                 /$$                 /$$$$$$$                               
// | $$$ | $$                | $$                | $$__  $$                              
// | $$$$| $$  /$$$$$$   /$$$$$$$  /$$$$$$       | $$  \ $$  /$$$$$$   /$$$$$$$  /$$$$$$ 
// | $$ $$ $$ /$$__  $$ /$$__  $$ /$$__  $$      | $$$$$$$  |____  $$ /$$_____/ /$$__  $$
// | $$  $$$$| $$  \ $$| $$  | $$| $$$$$$$$      | $$__  $$  /$$$$$$$|  $$$$$$ | $$$$$$$$
// | $$\  $$$| $$  | $$| $$  | $$| $$_____/      | $$  \ $$ /$$__  $$ \____  $$| $$_____/
// | $$ \  $$|  $$$$$$/|  $$$$$$$|  $$$$$$$      | $$$$$$$/|  $$$$$$$ /$$$$$$$/|  $$$$$$$
// |__/  \__/ \______/  \_______/ \_______/      |_______/  \_______/|_______/  \_______/
//
//===================================================================================================================================================================================//

//?
//? Created by LunaticWyrm467 and others.
//? 
//? All code is licensed under the MIT license.
//? Feel free to reproduce, modify, and do whatever.
//?

//!
//! The `NodeBase` acts as the core of your `Node` types.
//! It handles storing references to children, parents, managing data, etc.
//!
//! Every `Node` type must contain a `base: Rc<NodeBase>` field for this reason.
//!

use std::{ rc::Rc, sync::Mutex };

use thiserror::Error;

use super::{
    logger::Log,
    node_path::{ PathSeg, NodePath },
    node_scene::NodeScene,
    node_tree_base::{ NodeTreeBase, TerminationReason },
    tree_pointer::{ TPError, Tp, TpDyn },
    tree_result::TreeResult,
    rid::RID
};

use crate::traits::{ node::Node, node_tree::NodeTree, node_getter::NodeGetter, instanceable::Instanceable };
use crate::utils::functions::ensure_unique_name;


/*
 * Node Base
 *      Error Type
 */


/// Used to articulate any errors that may occur through the usage of any node implementation.
#[derive(Error, Debug, Clone, PartialEq, Eq, Hash)]
pub enum NodeError {

    #[error("Index {index} was out of the range of this node's children count of {children_count}")]
    IndexOutOfRange { index: usize, children_count: usize },

    #[error("The path \"{0}\" is invalid")]
    InvalidPath(Box<str>),

    #[error("The root path \"{0}\" is invalid")]
    InvalidRootPath(Box<str>),

    #[error("This node has no parent")]
    NoParent,

    #[error("The following Tree Pointer error occured: {0:?}")]
    TPError(TPError)
}


/*
 * Node Status
 *      Type
 */


#[derive(Debug, Clone)]
pub enum NodeStatus {
    Normal,
    JustWarned(String),
    JustPanicked(String)
}


/*
 * Node Base
 *      Type
 */


/// Holds all of the node's internal information such as its name, children, parent, owner, and
/// owning `NodeTree`.
/// Also allows for the modification of the node's internal state.
///
/// # Note
/// Cloning this will result in a new `NodeBase` with the same name.
pub struct NodeBase {
    name:     String,
    rid:      RID,
    parent:   Option<RID>,
    owner:    Option<RID>,
    tree:     Option<*mut dyn NodeTree>,  // Lifetimes are managed by the NodeTree/Nodes
    children: Vec<RID>,
    status:   Rc<Mutex<NodeStatus>>,
    loaded:   bool,
    depth:    usize   // How far the Node is within the tree.
}

impl NodeBase {

    /// Creates a new `NodeBase` instance with no parent, owner, or owning tree.
    pub fn new(name: String) -> Self {
        NodeBase {
            name,
            rid:      RID::default(),
            parent:   None,
            owner:    None,
            tree:     None,
            children: Vec::new(),
            status:   Rc::new(Mutex::new(NodeStatus::Normal)),
            loaded:   false,
            depth:    0
        }
    }
    
    /// Returns a `Tp<T>` pointer for itself.
    ///
    /// # Panics
    /// The correct type (type of `self`) will have to be provided, or this function will panic!
    /// Panics also if this Node is not connected to a `NodeTree`.
    pub fn this<S: Node>(&self) -> Tp<S> {
        if self.tree.is_none() {
            panic!("Cannot get a tree pointer to a node that is not in a `NodeTree`!");
        }

        unsafe {
            Tp::new(self.tree.unwrap_unchecked(), self.rid, self.rid).expect("Called `this` with the wrong type provided")
        }
    }
    
    /// Returns a `TpDyn` pointer for itself.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn this_dyn(&self) -> TpDyn {
        if self.tree.is_none() {
            panic!("Cannot get a tree pointer to a node that is not in a `NodeTree`!");
        }
        
        // SAFETY:
        // There are no invariants that could result in this being unwrapped on an `Err` value.
        unsafe {
            TpDyn::new(self.tree.unwrap_unchecked(), self.rid, self.rid).unwrap_unchecked()
        }
    }

    /// Gets the name of the node.
    /// Each name is unique within the context of the parent's children vector.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Sets the name of the node.
    /// If the name is not unique among the node's siblings, then it will be made into a unique name.
    pub fn set_name(&mut self, name: &str) {
        if let (Some(parent), Some(tree)) = (self.parent, self.tree()) {
            let     parent:    &dyn Node    = unsafe { tree.get_node(parent).unwrap_unchecked() };
            let     siblings:  &[String]    = &parent.children().iter().map(|a| a.name().to_string()).collect::<Vec<_>>();

            unsafe {
                self.set_name_unchecked(&ensure_unique_name(name, siblings));
            }
        } else {
            unsafe {
                self.set_name_unchecked(name);
            }
        }
    }

    /// Registers this node as a singleton.
    /// Returns whether the name was set successfully.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn register_as_singleton(&mut self, name: String) -> bool {
        let rid: RID = self.rid;
        match self.tree_mut() {
            None       => panic!("Cannot register a node that is not apart of the Nodetree as a singleton!"),
            Some(tree) => tree.register_as_singleton(rid, name).unwrap()
        }
    }

    /// Adds a child to the node, automatically renaming it if its name is not unique in the
    /// node's children vector.
    ///
    /// # Note
    /// `_ready()` will automatically be propogated through the added child node.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn add_child<I: Instanceable>(&mut self, child: I) {
        child.iterate(|parent, node, is_owner| {
            if let Some(parent) = parent {
                unsafe {
                    let parent: &mut dyn Node = &mut *parent;
                    parent.add_child_from_ptr(node, is_owner, false);
                }
            } else {
                unsafe {
                    self.add_child_from_ptr(node, is_owner, false);
                }
            }
        });
    }

    /// Adds a child to the node via a passed in pointer, automatically renaming it if its
    /// name is not unique in the node's children vector.
    ///
    /// Returns the child's new RID.
    ///
    /// # Arguments
    /// Aside from the raw pointer to the child itself, this function takes in two booleans for
    /// whether if this node marks the owner of a new scene branch, and if this added node does not
    /// call its ready() function respectively.
    ///
    /// # Note
    /// `_ready()` will automatically be propogated through the added child node.
    ///
    /// # Safety
    /// Cannot guarantee that the raw pointer that is passed in is valid.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub unsafe fn add_child_from_ptr(&mut self, child_ptr: *mut dyn Node, owner_is_self: bool, ignore_ready: bool) -> RID {
        if self.tree.is_none() {
            panic!("Cannot add a child to a node that is not in a `NodeTree`!");
        }

        // Ensure that the child's name within the context of this node's children is unique.
        let names_of_children: &[String] = &self.children().iter().map(|c| c.name().to_string()).collect::<Vec<_>>();
        let child_name:        &str      = unsafe { &*child_ptr }.name();
        let unique_name:       String    = ensure_unique_name(child_name, names_of_children);

        // Add the child to this node's children and connect it to its parent and owner nodes,
        // as well as the root tree structure's reference.
        let child_rid: RID = unsafe {
            let owner_rid:  RID               = self.owner.unwrap_unchecked();
            let parent_rid: RID               = self.rid;
            let new_depth:  usize             = self.depth() + 1; 
            let tree_raw:   *mut dyn NodeTree = self.tree.unwrap_unchecked();
            let tree:       &mut dyn NodeTree = self.tree_mut().unwrap_unchecked();
            
            let rid:   RID           = tree.register_node(child_ptr);
            let child: &mut dyn Node = tree.get_node_mut(rid).unwrap_unchecked();

            child.set_name_unchecked(&unique_name);
            child.set_parent(parent_rid);
            child.set_owner(if owner_is_self { rid } else { owner_rid });
            child.set_tree(tree_raw);
            child.set_depth(new_depth);   // This is the only place where depth is updated.
            
            child.set_rid(rid);
            rid
        };
        self.children.push(child_rid);
        
        // Call the `ready()` function for the child as long as the call to ready() is not ignored
        // or circumvented..
        if !ignore_ready {
            unsafe {
                let child: &mut dyn Node = self.tree_mut().unwrap_unchecked().get_node_mut(child_rid).unwrap_unchecked();
                if child.has_just_loaded() {
                    child.loaded();
                    child.mark_as_final();
                }
                child.ready();

            }
        }
        
        // Print the debug information on the child to the console.
        let child: &dyn Node = unsafe { self.tree().unwrap_unchecked().get_node(child_rid).unwrap_unchecked() };
        self.post(Log::Debug(&format!("Node \"{}\" added to the scene as the child of \"{}\"! Unique ID of \"{}\" generated!", child.name(), self.name(), child.rid)));
        child_rid
    }

    /// Removes a child but it does not destroy it, disconnecting from its parent.
    /// Both the child and its children will be disconnected from the tree and their owners.
    /// This will return whether the child node was successfully removed or not.
    ///
    /// # Note
    /// This will result in all removed nodes having their `terminal()` function called with the
    /// reason `RemovedAsChild`.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn remove_child(&mut self, name: &str) -> bool {
        if self.tree.is_none() {
            panic!("Cannot add a child to a node that is not in a `NodeTree`!");
        }

        // TODO:
        // This function could be cleaned up a bit...
        
        // Locate a child node that has the same name. If there is no matching node, then exist
        // early.
        let child: Option<(usize, TpDyn)> = self.children()
            .into_iter()
            .enumerate()
            .find(|(_, c)| c.name() == name);
        if child.is_none() {
            self.post(Log::Warn(&format!("Attempted to remove invalid node of name \"{}\" from node \"{}\"!", name, self.name())));
            return false;
        }

        let (
            child_idx,
            child_name,
            connected
        ): (usize, String, Vec<RID>) = unsafe {
            child.map(|(idx, child)| (
                    idx,
                    child.name().to_string(),
                    child.top_down(true)
            )).unwrap_unchecked()
        };

        self.children.remove(child_idx);
        for (idx, queued_rid) in connected.into_iter().enumerate() { unsafe { 
            let _is_root_child: bool          = idx == 0; // TODO: Use this to save children nodes!
            let queued_node:    &mut dyn Node = self.tree_mut().unwrap_unchecked().get_node_mut(queued_rid).unwrap_unchecked();
            
            queued_node.terminal(TerminationReason::RemovedAsChild);
            queued_node.disconnnect_parent();
            queued_node.disconnnect_owner();
            queued_node.disconnnect_tree();

            self.tree_mut().unwrap_unchecked().unregister_node(queued_rid);
        }}

        self.post(Log::Debug(&format!("Removed child node \"{}\" from parent node \"{}\"!", child_name, self.name())));
        true 
    }

    /// Returns a `Tp<T>` pointer to a child at the given index.
    /// If there is no child at the given index, or if the wrong type is given, then `Err` will be returned.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn get_child<T: Node>(&self, i: usize) -> TreeResult<Tp<T>, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }

        if i >= self.num_children() {
            unsafe {
                TreeResult::new(self.tree.unwrap_unchecked(), self.rid, Err(NodeError::IndexOutOfRange { index: i, children_count: self.num_children() }))
            }
        } else {
            unsafe {
                Tp::new(self.tree.unwrap_unchecked(), self.rid, self.children[i])
                    .map_err(|err| NodeError::TPError(err))
            }
        }
    }
    
    /// Returns a `TpDyn` pointer to a child at the given index.
    /// If there is no child at the given index then `Err` will be returned.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn get_child_dyn(&self, i: usize) -> TreeResult<TpDyn, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }

        if i >= self.num_children() {
            unsafe {
                TreeResult::new(self.tree.unwrap_unchecked(), self.rid, Err(NodeError::IndexOutOfRange { index: i, children_count: self.num_children() }))
            }
        } else {
            unsafe {
                TpDyn::new(self.tree.unwrap_unchecked(), self.rid, self.children[i])
                    .map_err(|err| NodeError::TPError(err))
            }
        }
    }

    /// Gets a vector of `DynTp` to describe this node's children.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn children(&self) -> Vec<TpDyn> {
        if self.tree().is_none() {
            panic!("Cannot get children from a node that is not a part of a NodeTree!");
        }

        self.children.iter().map(|&c| unsafe { TpDyn::new(self.tree.unwrap_unchecked(), self.rid, c).unwrap_unchecked() }).collect()
    }
    
    /// Gets a `Tp<T>` or a Tree Pointer to a given `Node` via either a `NodePath`, a `&str`, or a
    /// String (the latter two may be used to denote Singletons).
    /// Returns `Err` if the address is invalid or if the referenced `Node` is not of the type
    /// `T`.
    ///
    /// # Usage
    /// Please check the documentation of `NodePath` for the proper syntax.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn get_node<T: Node>(&self, path: impl NodeGetter) -> TreeResult<Tp<T>, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }
        let path_str: String = format!("{path:?}");

        match unsafe { self.tree().unwrap_unchecked() }.get_node_rid(path, Some(self.rid)) {
            Some(node_rid) => {
                unsafe {
                    Tp::new(self.tree.unwrap_unchecked(), self.rid, node_rid)
                        .map_err(|err| NodeError::TPError(err))
                }
            },
            None => unsafe {
                TreeResult::new(self.tree.unwrap_unchecked(), self.rid, Err(NodeError::InvalidPath(path_str.into())))
            }
        }
    }

    /// Gets a `TpDyn` or a Dynamic Tree Pointer to a given `Node` via either a `NodePath`, a `&str`, or a
    /// String (the latter two may be used to denote Singletons).
    /// Returns `Err` if the address is invalid.
    ///
    /// # Usage
    /// Please check the documentation of `NodePath` for the proper syntax.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn get_node_dyn(&self, path: impl NodeGetter) -> TreeResult<TpDyn, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }
        let path_str: String = format!("{path:?}");

        match unsafe { self.tree().unwrap_unchecked() }.get_node_rid(path, Some(self.rid)) {
            Some(node_rid) => {
                unsafe {
                    TpDyn::new(self.tree.unwrap_unchecked(), self.rid, node_rid)
                        .map_err(|err| NodeError::TPError(err))
                }
            },
            None => unsafe {
                TreeResult::new(self.tree.unwrap_unchecked(), self.rid, Err(NodeError::InvalidRootPath(path_str.into())))
            }
        }
    }

    /// Gets a node's `RID` given a `NodePath` that is respective to this node as the root.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn get_node_raw(&self, mut path: NodePath) -> Option<RID> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }

        let next_node: Option<PathSeg> = path.pop_front();
        match next_node {
            Some(target) => {
                match target {
                    PathSeg::Node(target_node) => {
                        for child in self.children() {
                            if *child.name() == *target_node {
                                return child.get_node_raw(path);
                            }
                        }
                        None
                    },
                    PathSeg::This => {
                        self.get_node_raw(path)
                    },
                    PathSeg::Parent => {
                        if let Some(parent) = self.parent_dyn().to_option() {
                            parent.get_node_raw(path)
                        } else {
                            None
                        }
                    }
                }
            },
            None => Some(self.rid())
        }
    }

    /// Produces a normal top-down order iteration of all of the nodes connected to this node.
    /// This is used to handle a lot of the scene tree behaviour.
    /// If 'contains_self' is true, then the list will contain this node as well.
    ///
    /// # Note
    /// Nodes that are at the beginning of the children vector will be prioritized.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn top_down(&self, contains_self: bool) -> Vec<RID> {
        let mut iter: Vec<RID> = if contains_self { vec![self.rid()] } else { Vec::new() };
        self.top_down_tail(&mut iter, vec![self.rid()]);
        iter
    }

    /// The tail end recursive function for the `top_down` method.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    fn top_down_tail(&self, iter: &mut Vec<RID>, layer: Vec<RID>) {
        if self.tree().is_none() {
            panic!("Cannot get nodes from a node that is not a part of a NodeTree!");
        }
        
        let new_layer: Vec<RID> = unsafe { self.tree().unwrap_unchecked() }.get_all_valid_nodes(&layer)
            .into_iter()
            .flat_map(|node| node.children.to_owned() )
            .collect();
        if new_layer.is_empty() {
            return;
        }
        iter.append(&mut new_layer.clone());

        self.top_down_tail(iter, new_layer)        
    }

    /// Gets this Node's absolute `NodePath` to the root of the tree.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn get_absolute_path(&self) -> NodePath {
        let mut path: String = String::new();
        self.get_absolute_path_tail(&mut path);
        NodePath::from_str(&path)
    }

    /// The recursive tail for the `get_absolute_path` function.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    fn get_absolute_path_tail(&self, path: &mut String) {
        if self.tree().is_none() {
            panic!("Cannot get nodes from a node that is not a part of a NodeTree!");
        }

        *path = self.name().to_string() + &(if path.is_empty() { String::new() } else { "/".to_string() + path });
        if !self.is_root() {
            unsafe {
                self.tree().unwrap_unchecked().get_node(self.parent.unwrap_unchecked()).unwrap_unchecked().get_absolute_path_tail(path);
            }
        }
    }

    /// Attempts to post a log to the logger.
    /// If this node has a unique identifier accessible by name, then that will be used as the
    /// node's identifier in the log.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn post(&self, log: Log) {
        unsafe {
            match &log {
                Log::Warn(str)  => self.set_status(NodeStatus::JustWarned(str.to_string())),
                Log::Panic(str) => self.set_status(NodeStatus::JustPanicked(str.to_string())),
                _               => ()
            }
        }

        let rid: RID = self.rid();
        match self.tree_mut() {
            Some(root) => {
                root.post(rid, log);
            },
            None => panic!("Cannot post to log on a disconnected node!")
        }
    }

    /// Destroys the Node, removing it from any connected parent or children.
    /// If this is the root node, then the destruction of this node will result in the program
    /// itself terminating.
    ///
    /// # Note
    /// This will result in all removed nodes having their `terminal()` function called with the
    /// reason `Freed`.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn free(&mut self) {
        if self.tree().is_none() {
            panic!("Cannot free a node that is not a part of a NodeTree! Instead, simply let the unbound Node drop out of scope or use drop()!");
        }
        
        // Call the terminal function on this node,
        // before removing it and all it's children nodes from the NodeTree.
        for node in self.top_down(true) {
            let is_self: bool              = node == self.rid;
            let tree:    &mut NodeTreeBase = unsafe { self.tree_mut().unwrap_unchecked() };

            unsafe {
                tree.get_node_mut(self.rid).unwrap_unchecked().terminal(TerminationReason::Freed); // Has to be called externally!
            }

            // Remove the reference of this node from its parent if it has a parent.
            if is_self {
                if let Some(parent) = self.parent {
                    unsafe {
                        let rid:       RID           = self.rid;
                        let parent:    &mut dyn Node = self.tree_mut().unwrap_unchecked().get_node_mut(parent).unwrap_unchecked();
                        let child_idx: usize         = parent.children.iter().position(|&c_rid| c_rid == rid).unwrap_unchecked();

                        parent.children.remove(child_idx);
                    }
                }
            }

            unsafe {
                tree.unregister_node(node);
            }
        }

        // If this is the root node, terminate the NodeTree.
        if self.is_root() {
            self.tree_mut().unwrap().terminate();
        }
    }

    /// Saves this node and all of the nodes below it as a `NodeScene`, which can then be
    /// reinstanced somewhere else OR be written to the disk.
    ///
    /// # Note
    /// All data in every `NodeBase` will either be destroyed or be represented in the `NodeScene`'s
    /// representation.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn save_as_branch(&self) -> NodeScene {
        if self.tree().is_none() {
            panic!("Cannot free a node that is not a part of a NodeTree! Instead, simply let the unbound Node drop out of scope or use drop()!");
        }
        self.save_as_branch_tail()
    }

    /// The recursive tail function for `save_as_branch`.
    fn save_as_branch_tail(&self) -> NodeScene {
        
        // Create the root `NodeScene` structure using this node as the root owner.
        let     root:  Box<dyn Node> = unsafe { (&*self.tree.unwrap_unchecked()).get_node(self.rid).unwrap_unchecked() }.clone_as_instance();
        let mut scene: NodeScene     = NodeScene::new_dyn(root);

        // For each child, append their representation of a node scene.
        for &child in &self.children {
            let child: &dyn Node = unsafe { (&*self.tree.unwrap_unchecked()).get_node(child).unwrap_unchecked() };
            if child.is_owner() {
                scene.append_as_owner(child.save_as_branch_tail());
            } else {
                scene.append(child.save_as_branch_tail());
            }
        }
        scene
    }

    /// Sets the name of the node without checking if the name is unique.
    ///
    /// # Safety
    /// Setting this manually with a name that is already present in another sibling could cause
    /// hard to find bugs.
    pub unsafe fn set_name_unchecked(&mut self, name: &str) {
        self.name = name.to_string();
    }

    /// Gets the unique `RID` (resource ID) of the node.
    /// Each `RID` is unique within the context of the entire `NodeTree`.
    pub fn rid(&self) -> RID {
        self.rid
    }

    /// Sets the unique `RID` of the node.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn set_rid(&mut self, rid: RID) {
        self.rid = rid;
    }

    /// Gets a reference to the owning `NodeTree` structure, which controls the entire tree.
    /// This will return `None` if the node is not connected to the `NodeTree`.
    pub fn tree(&self) -> Option<&dyn NodeTree> {
        unsafe {
            self.tree.map(|x| &*x)
        }
    }

    /// Gets a mutable reference to the owning `NodeTree` structure, which controls the entire tree.
    /// This will return `None` if the node is not connected to the `NodeTree`.
    pub fn tree_mut(&self) -> Option<&mut dyn NodeTree> {
        unsafe {
            self.tree.map(|x| &mut *x)
        }
    }

    /// Sets the reference to the owning `NodeTree` structure.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn set_tree(&mut self, tree: *mut dyn NodeTree) {
        self.tree = Some(tree);
    }

    /// Disconnects the `NodeTree` from this node.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn disconnnect_tree(&mut self) {
        self.tree = None;
    }

    /// Gets the `Tp<T>` owner of the node. Returns `Err` if `T` does not match the owner's type.
    /// The owner is different from the parent. The owner can be thought as the root of the scene
    /// that this node is a part of, rather than the node's actual parent.
    /// In other words, if you had a node tree that looked like this:
    /// ```text
    /// ... <Higher Nodes>
    /// â•°NodeA <Root of Saved Scene>
    ///  ├NodeB
    ///  â•°NodeC
    ///   â•°NodeD
    ///```
    /// And you were to call `owner()` on `NodeD`, you would get `NodeA`.
    ///
    /// # Note
    /// You can only have an owner on a node that is a part of a node tree.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn owner<T: Node>(&self) -> TreeResult<Tp<T>, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }
        
        unsafe {
            Tp::new(self.tree.unwrap_unchecked(), self.rid, self.owner.unwrap_unchecked())
                .map_err(|err| NodeError::TPError(err))
        }
    }

    /// Gets a `TpDyn` pointer to the owner of the node.
    /// The owner is different from the parent. The owner can be thought as the root of the scene
    /// that this node is a part of, rather than the node's actual parent.
    /// In other words, if you had a node tree that looked like this:
    /// ```text
    /// ... <Higher Nodes>
    /// â•°NodeA <Root of Saved Scene>
    ///  ├NodeB
    ///  â•°NodeC
    ///   â•°NodeD
    ///```
    /// And you were to call `owner()` on `NodeD`, you would get `NodeA`.
    ///
    /// # Note
    /// You can only have an owner on a node that is a part of a node tree.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn owner_dyn(&self) -> TpDyn {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }
        
        unsafe {
            TpDyn::new(self.tree.unwrap_unchecked(), self.rid, self.owner.unwrap_unchecked()).unwrap_unchecked()
        }
    }

    /// Sets the owner of the node.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn set_owner(&mut self, owner: RID) {
        self.owner = Some(owner);
    }

    /// Disconnects this node's owner from this node.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn disconnnect_owner(&mut self) {
        self.owner = None;
    }

    /// Gets a `Tp<T>` pointer to the direct parent of this node, if the node has one.
    /// Returns `Err` if there is no parent or if `T` does not match the parent's type.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn parent<T: Node>(&self) -> TreeResult<Tp<T>, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }
        
        match self.parent {
            Some(parent) => {
                unsafe {
                    Tp::new(self.tree.unwrap_unchecked(), self.rid, parent)
                        .map_err(|err| NodeError::TPError(err))
                }
            },
            None => unsafe {
                TreeResult::new(self.tree.unwrap_unchecked(), self.rid, Err(NodeError::NoParent))
            }
        }
    }
    
    /// Gets a `TpDyn` pointer to the direct parent of this node, if the node has one.
    /// Returns `Err` if there is no parent.
    ///
    /// # Panics
    /// Panics if this Node is not connected to a `NodeTree`.
    pub fn parent_dyn(&self) -> TreeResult<TpDyn, NodeError> {
        if self.tree().is_none() {
            panic!("Cannot get a node from a node that is not a part of a NodeTree!");
        }
        
        match self.parent {
            Some(parent) => {
                unsafe {
                    TpDyn::new(self.tree.unwrap_unchecked(), self.rid, parent)
                        .map_err(|err| NodeError::TPError(err))
                }
            },
            None => unsafe {
                TreeResult::new(self.tree.unwrap_unchecked(), self.rid, Err(NodeError::NoParent))
            }
        }
    }

    /// Sets the parent of this node.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn set_parent(&mut self, parent: RID) {
        self.parent = Some(parent);
    }

    /// Disconnects this node's parent from this node.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn disconnnect_parent(&mut self) {
        self.parent = None;
    }

    /// Gets the node's status.
    pub fn status(&self) -> NodeStatus {
        self.status.lock().unwrap().to_owned()
    }

    /// Sets the node's status.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn set_status(&self, status: NodeStatus) {
        *self.status.lock().unwrap() = status;
    }

    /// Gets the node's depth.
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Sets the node's depth.
    ///
    /// # Safety
    /// This should NOT be called manually.
    pub unsafe fn set_depth(&mut self, depth: usize) {
        self.depth = depth;
    }

    /// Returns if this node is a part of the node tree.
    /// If this is false, then it is expected behaviour that this node does not have an owner or
    /// parent.
    pub fn in_tree(&self) -> bool {
        self.tree().is_some()
    }

    /// Returns if this node is not a part of the node tree.
    /// If this is true, then it is expected behaviour that this node does not have an owner or
    /// parent.
    pub fn is_stray(&self) -> bool {
        self.tree().is_none()
    }

    /// Returns true if this node is the root node of the node tree.
    ///
    /// # Note
    /// This will return false if the node is not apart of a node tree.
    pub fn is_root(&self) -> bool {
        self.parent.is_none() && self.in_tree()
    }

    /// Returns true if this node is an owner of a scene.
    ///
    /// # Note
    /// This will return false if the node is not apart of a node tree.
    pub fn is_owner(&self) -> bool {
        match self.owner {
            Some(owner) => self.rid == owner,
            None        => false
        }
    }

    /// Returns the number of children this node has.
    pub fn num_children(&self) -> usize {
        self.children().len()
    }

    /// Returns true if this node has no children.
    pub fn childless(&self) -> bool {
        self.num_children() == 0
    }

    /// Marks this node as just having been recently loaded from the disk.
    /// 
    /// # Safety
    /// This should not be used manually.
    pub unsafe fn mark_as_loaded(&mut self) {
        self.loaded = true;
    }

    /// Marks this node as having been fully loaded.
    /// 
    /// # Safety
    /// This should not be used manually.
    pub unsafe fn mark_as_final(&mut self) {
        self.loaded = false;
    }

    /// Returns if this node has just been loaded from the disk.
    pub fn has_just_loaded(&self) -> bool {
        self.loaded
    }
}

impl std::fmt::Debug for NodeBase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("Inner[{}] {{ ", self.name))?;
        
        if let Some(parent) = &self.parent {
            f.write_str(&format!("Parent: {}, ", parent/*.name()*/))?;
        }
        if let Some(owner) = &self.owner {
            f.write_str(&format!("Owner: {}, ", owner/*.name()*/))?;
        }

        f.write_str(&format!("Connected to Tree: {}, ", self.tree.is_some()))?;
        f.write_str(&format!("Children: {:?}, ", &self.children))?;
        f.write_str(&format!("Depth: {} ", self.depth))?;
        f.write_str("}")?;

        Ok(())
    }
}

impl Clone for NodeBase {
    fn clone(&self) -> Self {
        Self::new(self.name.clone())
    }
}