metaverse_core 0.2.1

core program for server and client IO
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
use super::session::Mailbox;
use crate::session::SendUIMessage;
use actix::{AsyncContext, Handler, Message, WrapFuture};
use benthic_protocol::messages::ui::camera_position::CameraPosition;
use benthic_protocol::messages::ui::mesh_update::{MeshType, MeshUpdate};
use benthic_protocol::messages::ui::play_animation::PlayAnimation;
use benthic_protocol::messages::ui::ui_messages::UIMessage;
use benthic_protocol::session::create_agent_animation_dir;
use benthic_protocol::skeleton::{JointName, Skeleton};
use glam::{Quat, Vec3};
use log::{error, warn};
use metaverse_avatar::animation::build_animation;
use metaverse_avatar::avatar::Avatar;
use metaverse_avatar::avatar::OutfitObject;
use metaverse_avatar::avatar_object_handler::AvatarState::FullyLoaded;
use metaverse_avatar::avatar_object_handler::{
    AvatarType, add_object_to_avatar, finalize_avatar, init_avatar,
};
use metaverse_avatar::errors::AvatarError::{self};
use metaverse_cache::agent::sqlite_update_avatar;
use metaverse_cache::avatar::cache_current_outfit;
use metaverse_messages::http::capabilities::Capability;
use metaverse_messages::udp::agent::avatar_animation::AvatarAnimation;
use metaverse_messages::udp::agent::avatar_appearance::AvatarAppearance;
use metaverse_messages::utils::object_types::ObjectType;
use metaverse_objects::avatar_asset::download_asset_objects;
use std::collections::BTreeSet;
use std::path::PathBuf;
use std::time::Duration;
use uuid::Uuid;

/// Requests Agent data from the ViewerAsset capability endpoint
///
/// These are requested from inventory objects, and not ObjectUpdate packets.
///
/// # Cause
/// - [`HandleNewAvatar`]
///
/// # Effects
///  - Dispatches an [`AddObjectToAvatar`] message on successful download
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct DownloadAgentAsset {
    /// the asset of the object to request
    pub asset_id: Uuid,
    /// the ID of the agent the asset belongs to
    pub agent_id: Uuid,
    /// the object type, used to retrieve from the capability endpoint
    pub item_type: ObjectType,
}
impl Handler<DownloadAgentAsset> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: DownloadAgentAsset, ctx: &mut Self::Context) -> Self::Result {
        let Some(session) = self.session.as_mut() else {
            return;
        };
        let db_conn = session.inventory_db_connection.clone();
        let server_endpoint = session
            .capability_urls
            .get(&Capability::ViewerAsset)
            .unwrap()
            .to_string();
        let object_type = msg.item_type;
        let asset_id = msg.asset_id;
        let agent_id = msg.agent_id;

        let addr = ctx.address();
        ctx.spawn(
            async move {
                match download_asset_objects(
                    &server_endpoint,
                    db_conn,
                    object_type,
                    asset_id,
                    agent_id,
                )
                .await
                {
                    Ok(json_path) => {
                        addr.do_send(AddObjectToAvatar {
                            object: OutfitObject::MeshObject(json_path),
                            agent_id,
                        });
                    }
                    Err(e) => {
                        error!("{:?}", e)
                    }
                };
            }
            .into_actor(self),
        );
    }
}

/// Adds an object to the avatar's outfit list
///
/// This updates the in-memory struct containing each player's avatar data, and updates
/// the global avatar skeleton if the outfit contains skeleton data, and triggers a render
/// if the avatar's outfit is finished loading in.
///
/// # Cause
/// - [`AddObjectToAvatar`]
///
/// # Effects
/// - Dispatches a [`FinalizeAvatar`] message if the avatar's outfit has all items
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct AddObjectToAvatar {
    /// ID of the agent to add the object to
    pub agent_id: Uuid,
    /// Object to add
    pub object: OutfitObject,
}
impl Handler<AddObjectToAvatar> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: AddObjectToAvatar, ctx: &mut Self::Context) -> Self::Result {
        let Some(session) = self.session.as_mut() else {
            return;
        };
        match add_object_to_avatar(session, msg.agent_id, msg.object) {
            Ok(FullyLoaded) => {
                ctx.address().do_send(FinalizeAvatar {
                    agent_id: msg.agent_id,
                });
            }
            Ok(_) => {}
            Err(e) => {
                error!("{:?}", e);
            }
        };
    }
}

/// Message to spawn a new avatar
///
/// Avatars are added to the scene via ObjectUpdate packet with minimal information aside from the
/// ID. This adds those IDs to the session and begins downloading their appearances, or loads from
/// the cache.
///
/// # Cause
/// - [`HandleObjectUpdate`]
///
/// # Effect
/// - Resends a [`HandleNewAvatar`] message if the inventory is not yet loaded
/// - If the avatar is the current player's avatar
///    - Dispatches a [`CameraPosition`] message
///    - If the avatar is in the cache:
///     - Dispatches a [`LoadFromCache`] message to skip asset downloading.
///    - else:
///     - Dispatches a [`DownloadAgentAsset`] message for each object in the outfit
///     - Dispatches a [`AddObjectToAvatar`] message for each bodypart in the outfit
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandleNewAvatar(pub Avatar);
impl Handler<HandleNewAvatar> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: HandleNewAvatar, ctx: &mut Self::Context) -> Self::Result {
        let Some(session) = self.session.as_mut() else {
            return;
        };
        let addr = ctx.address();
        match init_avatar(session, &msg.0) {
            Ok(user_type) => {
                match user_type {
                    AvatarType::User => {
                        addr.do_send(SendUIMessage {
                            // handle the z-y flip
                            ui_message: UIMessage::new_camera_position(CameraPosition {
                                position: Vec3::new(
                                    msg.0.position.x,
                                    msg.0.position.z,
                                    msg.0.position.y,
                                ),
                            }),
                        });
                        let agent_id = session.agent_id;
                        let db_conn = session.inventory_db_connection.clone();
                        ctx.spawn(
                            async move {
                                match cache_current_outfit(db_conn, agent_id).await {
                                    Ok((avatar, outfit_items)) => {
                                        if addr
                                            .send(SetOutfitSize {
                                                agent_id,
                                                outfit_size: outfit_items.len(),
                                            })
                                            .await
                                            .is_err()
                                        {
                                            error!("Failed to set outfit size for {:?}", agent_id);
                                            return;
                                        }

                                        if let Some(avatar) = avatar {
                                            addr.do_send(LoadFromCache { avatar });
                                            return;
                                        }
                                        for item in outfit_items {
                                            match item.item_type {
                                                ObjectType::Object => {
                                                    addr.do_send(DownloadAgentAsset {
                                                        asset_id: item.asset_id,
                                                        item_type: item.item_type,
                                                        agent_id,
                                                    });
                                                }
                                                ObjectType::Bodypart => {
                                                    addr.do_send(AddObjectToAvatar {
                                                        object: OutfitObject::Bodypart,
                                                        agent_id,
                                                    });
                                                }
                                                ObjectType::Clothing => {
                                                    addr.do_send(AddObjectToAvatar {
                                                        object: OutfitObject::Clothing,
                                                        agent_id,
                                                    });
                                                }
                                                _ => {
                                                    addr.do_send(AddObjectToAvatar {
                                                        object: OutfitObject::Other,
                                                        agent_id,
                                                    });
                                                }
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        error!("{:?}", e)
                                    }
                                }
                            }
                            .into_actor(self),
                        );
                    }
                    AvatarType::NonUser => {
                        //TODO: handle nonuser updates
                    }
                }
            }
            Err(e) => match e {
                AvatarError::InventoryUninitialized { .. } => {
                    warn!("{:?}, Requeueing avatar download...", e);
                    ctx.notify_later(msg, Duration::from_secs(1));
                }
                e => {
                    error!("{:?}", e)
                }
            },
        };
    }
}

/// Message to finalize the avatar
///
/// This is triggered when all of the avatar's objects have loaded in. This finalizes the global
/// skeleton, writes the JSON for the full baked avatar, generates the mesh from metaverse-mesh,
/// and triggers a UI update.
///
/// # Cause
/// - [`AddObjectToAvatar`]
///
/// # Effects
/// - Dispatches a [`RenderAvatar`] message to render the finalized avatar
/// - Dispatches a [`SetAvatarGlbPath`] message to ensure the GLB path gets set
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct FinalizeAvatar {
    /// the avatar object to generate
    pub agent_id: Uuid,
}
impl Handler<FinalizeAvatar> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: FinalizeAvatar, ctx: &mut Self::Context) -> Self::Result {
        let Some(session) = self.session.as_mut() else {
            return;
        };

        let Some(avatar) = session.avatars.get_mut(&msg.agent_id) else {
            error!("Avatar {:?} not found in avatar cache", msg.agent_id);
            return;
        };

        let agent_id = msg.agent_id;
        let skeleton = avatar.skeleton.clone();
        let used_joints = avatar.used_joints.clone();
        let items = avatar.items.clone();
        let db_conn = session.inventory_db_connection.clone();
        let position = avatar.position;
        let mut avatar = avatar.clone();

        let addr = ctx.address();
        ctx.spawn(
            async move {
                match finalize_avatar(agent_id, skeleton, used_joints.clone(), items).await {
                    Ok((glb_path, skeleton)) => {
                        addr.do_send(SetAvatarGlbPath {
                            path: glb_path.clone(),
                            agent_id,
                        });
                        addr.do_send(SetAvatarFinalSkeleton {
                            final_skeleton: skeleton,
                            agent_id,
                        });
                        // edit the temp avatar for the sqlite update.
                        avatar.path = Some(glb_path.clone());
                        if let Err(e) = sqlite_update_avatar(&db_conn, avatar).await {
                            error!("{:?}", e);
                        }
                        addr.do_send(RenderAvatar {
                            message: MeshUpdate {
                                position,
                                scale: Vec3::ONE,
                                rotation: Quat::IDENTITY,
                                parent: None,
                                scene_id: None,
                                path: glb_path,
                                mesh_type: MeshType::Avatar,
                                id: Some(msg.agent_id),
                            },
                            agent_id,
                            skeleton: used_joints,
                        });
                    }
                    Err(e) => {
                        error!("{:?}", e)
                    }
                };
            }
            .into_actor(self),
        );
    }
}

#[derive(Debug, Message)]
#[rtype(result = "()")]
struct LoadFromCache {
    avatar: Avatar,
}
impl Handler<LoadFromCache> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: LoadFromCache, ctx: &mut Self::Context) -> Self::Result {
        let Some(session) = self.session.as_mut() else {
            return;
        };
        // insert the avatar to the session
        session
            .avatars
            .insert(msg.avatar.agent_id, msg.avatar.clone());

        // render the cached avatar
        ctx.address().do_send(RenderAvatar {
            message: MeshUpdate {
                position: msg.avatar.position,
                scale: Vec3::ONE,
                rotation: Quat::IDENTITY,
                parent: None,
                scene_id: None,
                path: msg.avatar.path.unwrap(),
                mesh_type: MeshType::Avatar,
                id: Some(msg.avatar.agent_id),
            },
            agent_id: msg.avatar.agent_id,
            skeleton: msg.avatar.used_joints,
        });
    }
}

/// Message to handle an updated animation
///
/// # Cause
/// - Avatar Appearance packet received from UDP socket
///
/// # Effect
/// - Dispatches a [`PlayAnimation`] message to inform the UI of a new animation
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandleNewAvatarAnimation {
    /// the avatar appearance data to handle
    pub avatar_animation: AvatarAnimation,
}
impl Handler<HandleNewAvatarAnimation> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: HandleNewAvatarAnimation, ctx: &mut Self::Context) -> Self::Result {
        let Some(session) = self.session.as_mut() else {
            return;
        };

        let avatar = match session.avatars.get(&msg.avatar_animation.sender_id) {
            Some(avatar) => {
                if !avatar.fully_loaded {
                    warn!(
                        "Animation targeting player {:?} is not yet fully loaded. Queueing animation...",
                        msg.avatar_animation.sender_id
                    );
                    ctx.notify_later(msg, Duration::from_secs(1));
                    return;
                }
                avatar
            }
            None => {
                warn!(
                    "Animation targeting player {:?}, not yet in scene. Queueing animation...",
                    msg.avatar_animation.sender_id
                );
                ctx.notify_later(msg, Duration::from_secs(1));
                return;
            }
        };

        let animations = msg.avatar_animation.animations;
        let used_joints = avatar.used_joints.clone();
        let target_skeleton = avatar.skeleton.clone();
        let Ok(out_dir) = create_agent_animation_dir(&avatar.agent_id) else {
            error!("Failed to create agent animation directory");
            return;
        };

        let sender_id = msg.avatar_animation.sender_id;
        let addr = ctx.address();
        ctx.spawn(
            async move {
                match build_animation(animations, used_joints, target_skeleton, out_dir).await {
                    Ok(mesh_paths) => {
                        for path in mesh_paths {
                            addr.do_send(SendUIMessage {
                                ui_message: UIMessage::new_play_animation(PlayAnimation {
                                    player_id: sender_id,
                                    animation_path: path,
                                }),
                            });
                        }
                    }
                    Err(e) => {
                        error!("{:?}", e)
                    }
                };
            }
            .into_actor(self),
        );
    }
}

/// Message to handle an updated avatar appearance
///
/// TODO: currently a stub
///
/// # Cause
/// - Avatar Appearance packet received from UDP socket
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandleNewAvatarAppearance {
    /// the avatar appearance data to handle
    pub avatar_appearance: AvatarAppearance,
}
impl Handler<HandleNewAvatarAppearance> for Mailbox {
    type Result = ();
    fn handle(
        &mut self,
        _msg: HandleNewAvatarAppearance,
        _ctx: &mut Self::Context,
    ) -> Self::Result {
        // TODO: implement this. AvatarAppearance packets change skeleton joint positions to change
        // the appearance of the avatar.
        warn!("AvatarAppearance packet received. Currently unimplemented");
    }
}

/// Helper message to ensure all avatars are marked as fully loaded once the mesh render is triggered.
///
/// # Cause
/// - [`FinalizeAvatar`]
/// - [`LoadFromCache`]
///
/// # Effects
/// - Dispatches a [`MeshUpdate`] message to inform the UI of an update
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct RenderAvatar {
    message: MeshUpdate,
    agent_id: Uuid,
    skeleton: BTreeSet<JointName>,
}
impl Handler<RenderAvatar> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: RenderAvatar, ctx: &mut Self::Context) -> Self::Result {
        if let Some(session) = self.session.as_mut()
            && let Some(avatar) = session.avatars.get_mut(&msg.agent_id)
        {
            avatar.used_joints = msg.skeleton;
            let addr = ctx.address();

            addr.do_send(SendUIMessage {
                ui_message: UIMessage::new_mesh_update(msg.message),
            });

            avatar.fully_loaded = true;
        }
    }
}

/// Message to set the outfit size of the avatar
///
/// This must be set in order to trigger a render. Without this, the avatar doesn't know when the
/// outfit is finished loading in.
///
/// # Cause
/// - [`HandleNewAvatar`]
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct SetOutfitSize {
    /// agent ID to set the outfit size of
    pub agent_id: Uuid,
    /// size of the outfit
    pub outfit_size: usize,
}
impl Handler<SetOutfitSize> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: SetOutfitSize, _ctx: &mut Self::Context) -> Self::Result {
        if let Some(session) = self.session.as_mut()
            && let Some(avatar) = session.avatars.get_mut(&msg.agent_id)
        {
            avatar.outfit_size = msg.outfit_size;
        }
    }
}

/// Simple helper function to ensure that the avatar's GLB path is set correctly
///
/// #Cause
/// - [`FinalizeAvatar`]
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct SetAvatarGlbPath {
    /// GLB path
    pub path: PathBuf,
    /// Agent ID to set
    pub agent_id: Uuid,
}
impl Handler<SetAvatarGlbPath> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: SetAvatarGlbPath, _ctx: &mut Self::Context) -> Self::Result {
        if let Some(session) = self.session.as_mut()
            && let Some(avatar) = session.avatars.get_mut(&msg.agent_id)
        {
            avatar.path = Some(msg.path)
        }
    }
}
/// Simple helper function to ensure that the avatar's skeleton is updated after finalizing
///
/// #Cause
/// - [`FinalizeAvatar`]
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct SetAvatarFinalSkeleton {
    /// Skeleton
    pub final_skeleton: Skeleton,
    /// Agent ID to set
    pub agent_id: Uuid,
}
impl Handler<SetAvatarFinalSkeleton> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: SetAvatarFinalSkeleton, _ctx: &mut Self::Context) -> Self::Result {
        if let Some(session) = self.session.as_mut()
            && let Some(avatar) = session.avatars.get_mut(&msg.agent_id)
        {
            avatar.skeleton = msg.final_skeleton
        }
    }
}