metaverse_core 0.3.0

core program for server and client IO
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
use super::session::Mailbox;
use crate::avatar::HandleNewAvatar;
use crate::initialize::create_sub_agent_dir;
use crate::initialize::create_sub_object_dir;
use crate::session::OutgoingPacket;
use crate::session::SendUIMessage;
use crate::transport::http_handler::download_renderable_mesh;
use crate::transport::http_handler::download_texture;
use actix::AsyncContext;
use actix::ResponseFuture;
use actix::WrapFuture;
use actix::{Handler, Message};
use glam::Quat;
use glam::Vec3;
use log::info;
use log::{error, warn};
use metaverse_agent::avatar::Avatar;
use metaverse_inventory::object_update::get_object_scale_rotation_position;
use metaverse_inventory::object_update::get_object_update;
use metaverse_mesh::generate::generate_object_mesh;
use metaverse_messages::http::capabilities::Capability;
use metaverse_messages::packet::message::UIMessage;
use metaverse_messages::packet::packet::Packet;
use metaverse_messages::udp::object::improved_terse_object_update::ImprovedTerseObjectUpdate;
use metaverse_messages::udp::object::object_update::AttachItem;
use metaverse_messages::udp::object::object_update::ExtraParams;
use metaverse_messages::udp::object::object_update_cached::ObjectUpdateCached;
use metaverse_messages::udp::object::request_multiple_objects::CacheMissType;
use metaverse_messages::udp::object::request_multiple_objects::RequestMultipleObjects;
use metaverse_messages::ui::mesh_update::MeshType;
use metaverse_messages::ui::mesh_update::MeshUpdate;
use metaverse_messages::utils::object_types::ObjectType;
use metaverse_messages::utils::texture_entry::TextureEntry;
use serde::Serialize;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::PathBuf;
use uuid::Uuid;

use metaverse_inventory::object_update::insert_object_update_minimal;

/// Handles received ObjectUpdate packets.
///
/// This message contains a minimal version of the ObjectUpdate packet, and combines the
/// data for [`ObjectUpdate`] and [`ObjectUpdateCompressed`] packets into a single struct.
///
/// # Cause
/// - ObjectUpdate packet received from UDP socket from server  
///
/// # Effects
/// - Dispatches a [`Avatar`] message if the object is an avatar
/// - Dispatches a [`HandleAttachment`] message if the object is an attachment object
/// - Dispatches a [`HandlePrim`] message if the object is a prim
#[derive(Debug, Message, Clone)]
#[rtype(result = "()")]
pub struct HandleObjectUpdate {
    /// Type of the object. Required for retrieving full data from the capability endpoint
    pub object_type: ObjectType,
    /// The full ID of the object
    pub full_id: Uuid,
    /// The scene local ID of the object
    pub local_id: u32,
    /// The position of the object.
    ///
    /// If the object is a child object, this position is relative to its parent object.
    pub position: Vec3,
    /// The rotation of the object.
    ///
    /// If the object is a child object, this is used to calculate the
    /// position
    pub rotation: Quat,
    /// The scale of the object.
    pub scale: Vec3,
    /// The local ID of the obeject's parent.
    pub parent: Option<u32>,
    /// The scene local ID of the object's parent.
    ///
    /// This is used to determine the scale position and rotation if the object is part of a construction
    pub parent_id: Option<u32>,
    /// The name value of the object.
    ///
    /// This can encode extra data like attachment objects, or the avatar's name
    pub name_value: Option<String>,
    /// Extra parameters.
    ///
    /// Can contain definitions for things like sculpts (which include meshes), flexi data, light, and more.
    pub extra_params: Option<Vec<ExtraParams>>,

    /// Object's texture data
    pub texture: TextureEntry,
}

/// Begins the pipeline for handling a prim object.
///
/// Prim objects can include both mesh objects, sculpt objects, and primitive geometry objects.
/// # Cause
/// - [`HandleObjectUpdate`]
///
/// # Effects
/// - Dispatches a [`DownloadObject`] message to retrieve full object data from the server
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandlePrim {
    /// The prim object to handle
    pub object: HandleObjectUpdate,
}

/// Begins the pipeline for handling an attachment object
///
/// # Cause
/// - [`HandleObjectUpdate`]
///
/// TODO: currently a stub with no effects
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandleAttachment {
    /// The attachment object to handle
    pub object: HandleObjectUpdate,
    /// The attach item data
    pub item: AttachItem,
}

/// Message for downloading object update from its capability endpoint
///
/// This downloads the object data, writes the object to disk as json, triggers the metaverse-mesh
/// library to generate its finalized file, and then triggers a MeshUpdate
///  
/// # Cause
/// - [`HandlePrim`]
///
/// # Effects
/// - Dispatches a [`MeshUpdate`] to inform the UI of a new object
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct DownloadObject {
    /// the object data to download
    pub object: HandleObjectUpdate,
    /// The object's asset ID to retrieve from the ViewerAsset endpoint
    pub asset_id: Uuid,
    /// the object's texture ID to retrieve from the ViewerAsset endpoint  
    pub texture_id: Uuid,
    /// the object's location in space
    pub position: Vec3,
}

/// Message for handing improved terse object update packets
///
/// TODO: currently unimplemented
///
/// # Cause
/// - ImprovedTerseObjectUpdate packet received from UDP socket
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandleImprovedTerseObjectUpdate {
    /// The improved terse object update packet to handle
    pub improved_terse_object_update: ImprovedTerseObjectUpdate,
}

/// Message for handling ObjectUpdateCached packets
///
/// Retrieves the full object data by sending a RequestMultipleObjects packet. When the server
/// receives this packet, it replies with the ObjectUpdateCompressed packets requested
///
/// # Cause
/// - HandleObjectUpdateCached packet received from UDP socket
///
/// # Effect
/// - [`RequestMultipleObjects`] sent to server
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct HandleObjectUpdateCached {
    /// the object update cached packet to handle
    pub object_update_cached: ObjectUpdateCached,
}

impl Handler<HandleImprovedTerseObjectUpdate> for Mailbox {
    type Result = ();
    fn handle(
        &mut self,
        _msg: HandleImprovedTerseObjectUpdate,
        _ctx: &mut Self::Context,
    ) -> Self::Result {
        // TODO: unimplemented
        warn!("ImprovedTerseObjectUpdate packet received. Currently unimplemented.")
    }
}

impl Handler<HandleObjectUpdateCached> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: HandleObjectUpdateCached, ctx: &mut Self::Context) -> Self::Result {
        if let Some(session) = &self.session {
            let mut requests = Vec::new();
            for object in &msg.object_update_cached.objects {
                requests.push((CacheMissType::Normal, object.id));
            }
            let request = RequestMultipleObjects {
                session_id: session.session_id,
                agent_id: session.agent_id,
                requests,
            };

            ctx.address().do_send(OutgoingPacket {
                packet: Packet::new_request_multiple_objects(request),
            });
        }
    }
}

impl Handler<HandleObjectUpdate> for Mailbox {
    type Result = ResponseFuture<()>;
    fn handle(&mut self, msg: HandleObjectUpdate, ctx: &mut Self::Context) -> Self::Result {
        let db_pool = self.inventory_db_connection.clone();
        let addr = ctx.address();
        let msg_cloned = msg.clone();

        if self.session.is_none() {
            return Box::pin(async {});
        };
        Box::pin(async move {
            // all object updates first should be added to the db.
            // if they cannot be added, the object should be retried.

            insert_object_update_minimal(
                &db_pool,
                msg.local_id,
                msg.full_id,
                msg.object_type.clone(),
                msg.parent_id,
                msg.position,
                msg.rotation,
                msg.scale,
            )
            .await
            .unwrap_or_else(|e| {
                error!("Object Update Error: {:?}, {:?}", e, msg.full_id);
                //addr.do_send(msg.clone());
                return;
            });

            match msg.object_type {
                ObjectType::Prim => {
                    // if the msg.name_value can be parsed as an attachment, handle it as an
                    // attachment.
                    //
                    if let Some(name_value) = msg.name_value.clone() {
                        match AttachItem::parse_attach_item(name_value) {
                            Ok(item) => {
                                addr.do_send(HandleAttachment { object: msg, item });
                            }
                            Err(_) => {
                                // parsing failed, treat as generic
                                addr.do_send(HandlePrim { object: msg });
                            }
                        }
                    } else {
                        // no name_value, treat as generic
                        addr.do_send(HandlePrim { object: msg });
                    }
                }

                ObjectType::Tree
                | ObjectType::Grass
                | ObjectType::Unknown
                | ObjectType::ParticleSystem
                | ObjectType::NewTree => {
                    // TODO: unimplemented
                    warn!("Received unhandled ObjectUpdate type");
                }
                ObjectType::Avatar => {
                    if let Err(e) = create_sub_agent_dir(&msg.full_id.to_string()) {
                        warn!("Failed to create agent dir for {:?}: {:?}", msg.full_id, e);
                    }
                    // create a new avatar object in the session
                    addr.do_send(HandleNewAvatar {
                        avatar: Avatar::new(msg_cloned.full_id, msg_cloned.position),
                    });
                }
                _ => {
                    warn!("Unknown object type");
                }
            }
        })
    }
}

impl Handler<HandlePrim> for Mailbox {
    type Result = ();
    fn handle(&mut self, msg: HandlePrim, ctx: &mut Self::Context) -> Self::Result {
        if let Some(extra_params) = &msg.object.extra_params {
            for param in extra_params {
                match param {
                    ExtraParams::Sculpt(sculpt) => {
                        ctx.address().do_send(DownloadObject {
                            asset_id: sculpt.texture_id,
                            texture_id: Uuid::nil(),
                            object: msg.object.clone(),
                            position: msg.object.position,
                        });
                    }
                    _ => {
                        warn!("Recieved a non sculpt objectupdate. Currently unimplemented")
                    }
                }
            }
        };
    }
}

impl Handler<HandleAttachment> for Mailbox {
    type Result = ResponseFuture<()>;
    fn handle(&mut self, msg: HandleAttachment, _ctx: &mut Self::Context) -> Self::Result {
        let db_pool = self.inventory_db_connection.clone();
        Box::pin(async move {
            let mut current_id = match msg.object.parent_id {
                Some(id) => id,
                None => return,
            };
            let mut visited = std::collections::HashSet::new();
            loop {
                if !visited.insert(current_id) {
                    break;
                }
                let obj = match get_object_update(&db_pool, current_id).await {
                    Ok(obj) => obj,
                    Err(_) => return,
                };
                if obj.parent_id == 0 {
                    break;
                }
                current_id = obj.parent_id;
            }
        })
    }
}

impl Handler<DownloadObject> for Mailbox {
    type Result = ();
    fn handle(&mut self, mut msg: DownloadObject, ctx: &mut Self::Context) -> Self::Result {
        if let Some(session) = self.session.as_mut() {
            let server_endpoint = session
                .capability_urls
                .get(&Capability::ViewerAsset)
                .unwrap()
                .to_string();
            let addr = ctx.address();
            let inventory_db = self.inventory_db_connection.clone();
            ctx.spawn(
                async move {
                    let base_dir = match create_sub_object_dir(&msg.asset_id.to_string()) {
                        Ok(base_dir) => base_dir,
                        Err(e) => {
                            error!("failed to create base dir: {:?}", e);
                            return;
                        }
                    };

                    let texture_id = msg.object.texture.texture_id;
                    let texture_path = base_dir.join(format!("{:?}.png", texture_id));
                    let texture_path = match download_texture(
                        ObjectType::Texture.to_string(),
                        texture_id,
                        &server_endpoint,
                        &texture_path,
                    )
                    .await
                    {
                        Ok(_) => texture_path,
                        Err(e) => {
                            error!("Failed to download prim texture: {:?} {:?}", e, texture_id);
                            PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                                .join("assets")
                                .join("benthic_default_texture.png")
                        }
                    };

                    match download_renderable_mesh(
                        msg.asset_id,
                        "name".to_string(),
                        &server_endpoint,
                        &texture_path,
                    )
                    .await
                    {
                        Ok(render_object) => {
                            // write the json
                            let json_path = match write_json(
                                &render_object,
                                msg.asset_id,
                                msg.asset_id.to_string(),
                            ) {
                                Ok(json) => json,
                                Err(e) => {
                                    error!("Failed to write json: {:?}", e);
                                    return;
                                }
                            };
                            let glb_path = base_dir.join(format!("{:?}_high.glb", msg.asset_id));
                            match generate_object_mesh(json_path, glb_path.clone()) {
                                Ok(_) => {
                                    info!("Rendering object at: {:?}", msg.asset_id)
                                }
                                Err(e) => warn!("{:?}", e),
                            };

                            // retrieve the parent's trnasforms from the db to determine the
                            // global position of a child object
                            if let Some(parent_id) = msg.object.parent_id {
                                match get_object_scale_rotation_position(&inventory_db, parent_id)
                                    .await
                                {
                                    Ok((_parent_scale, parent_rotation, parent_position)) => {
                                        let rotated_offset =
                                            parent_rotation.mul_vec3(msg.object.position);

                                        msg.object.position = parent_position + rotated_offset;
                                        msg.object.rotation = parent_rotation * msg.object.rotation;
                                    }
                                    Err(e) => {
                                        error!("{:?}", e);
                                        return;
                                    }
                                };
                            }

                            addr.do_send(SendUIMessage {
                                ui_message: UIMessage::new_mesh_update(MeshUpdate {
                                    position: msg.object.position,
                                    scale: msg.object.scale,
                                    rotation: msg.object.rotation,
                                    parent: msg.object.parent,
                                    scene_id: Some(msg.object.local_id),
                                    path: glb_path,
                                    mesh_type: MeshType::Avatar,
                                    id: None,
                                }),
                            });
                        }
                        Err(e) => {
                            error!("{:?}, {:?}", e, msg);
                        }
                    }
                }
                .into_actor(self),
            );
        }
    }
}

/// When an object is retrieved in full, the data will be written in serializable json format, to
/// create a cache. The JSON will then be sent to another crate to convert it into a 3d model that
/// can be rendered.
pub fn write_json<T: Serialize>(data: &T, asset_id: Uuid, filename: String) -> io::Result<PathBuf> {
    match create_sub_object_dir(&asset_id.to_string()) {
        Ok(mut object_dir) => match serde_json::to_string(&data) {
            Ok(json) => {
                object_dir.push(format!("{}.json", filename));
                let mut file = File::create(&object_dir).unwrap();
                file.write_all(json.as_bytes()).unwrap();
                Ok(object_dir)
            }
            Err(e) => {
                error!("Failed to serialize scene group {:?}, {:?}", filename, e);
                Err(io::Error::other(e))
            }
        },
        Err(e) => {
            error!(
                "Failed to create agent dir for {:?}. Unable to cache downloaded items.",
                e
            );
            Err(io::Error::other(e))
        }
    }
}