plasma-prp 0.1.0

Read, write, inspect, and manipulate Plasma engine PRP files used by Myst Online: Uru Live
Documentation
//! hsKeyedObject — base class for all objects in the Plasma key registry.
//!
//! Every persistent object in Plasma inherits from hsKeyedObject, which provides
//! a plKey (identity handle) and Read/Write that serializes the self-key.
//!
//! C++ ref: pnKeyedObject/hsKeyedObject.h/.cpp

use std::io::Read;

use anyhow::Result;

use super::key::Key;
use super::uoid::{Uoid, read_key_uoid};

/// Trait for types that have a key identity in the Plasma object system.
///
/// Corresponds to hsKeyedObject in C++.
pub trait KeyedObject {
    /// Get this object's key.
    fn key(&self) -> &Key;

    /// Get a mutable reference to this object's key.
    fn key_mut(&mut self) -> &mut Key;

    /// Set this object's key.
    fn set_key(&mut self, key: Key);
}

/// Read the hsKeyedObject portion of a stream — just the self-key reference.
/// Returns the Uoid of the self-key.
pub fn read_keyed_object(reader: &mut impl Read) -> Result<Option<Uoid>> {
    read_key_uoid(reader)
}

/// Common base data for keyed objects — stores the key.
#[derive(Debug, Clone, Default)]
pub struct KeyedObjectData {
    pub key: Key,
}

impl KeyedObjectData {
    pub fn new() -> Self {
        Self { key: Key::null() }
    }

    pub fn with_key(key: Key) -> Self {
        Self { key }
    }
}

impl KeyedObject for KeyedObjectData {
    fn key(&self) -> &Key {
        &self.key
    }

    fn key_mut(&mut self) -> &mut Key {
        &mut self.key
    }

    fn set_key(&mut self, key: Key) {
        self.key = key;
    }
}