const STATE_MAGIC: &[u8; 4] = b"OAST";
const STATE_VERSION: u32 = 1;
#[derive(Debug)]
#[non_exhaustive]
pub enum StateLoadError {
Malformed(&'static str),
Other(String),
}
impl std::fmt::Display for StateLoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Malformed(s) => write!(f, "malformed state: {s}"),
Self::Other(s) => f.write_str(s),
}
}
}
impl std::error::Error for StateLoadError {}
#[must_use]
pub fn serialize_state(
plugin_id_hash: u64,
param_ids: &[u32],
param_values: &[f64],
extra: &[u8],
) -> Vec<u8> {
let mut data = Vec::new();
data.extend_from_slice(STATE_MAGIC);
data.extend_from_slice(&STATE_VERSION.to_le_bytes());
data.extend_from_slice(&plugin_id_hash.to_le_bytes());
let count = crate::cast::len_u32(param_ids.len());
data.extend_from_slice(&count.to_le_bytes());
for (id, value) in param_ids.iter().zip(param_values.iter()) {
data.extend_from_slice(&id.to_le_bytes());
data.extend_from_slice(&value.to_le_bytes());
}
let len = extra.len() as u64;
data.extend_from_slice(&len.to_le_bytes());
data.extend_from_slice(extra);
data
}
pub struct DeserializedState {
pub params: Vec<(u32, f64)>,
pub extra: Option<Vec<u8>>,
}
pub fn apply_state<P: crate::export::PluginExport>(plugin: &mut P, state: &DeserializedState) {
use truce_params::Params;
plugin.params().restore_values(&state.params);
plugin.params().snap_smoothers();
if let Some(extra) = &state.extra
&& let Err(e) = plugin.load_state(extra)
{
eprintln!("truce: load_state failed: {e}");
}
}
pub fn apply_params<P: truce_params::Params>(params: &P, state: &DeserializedState) {
params.restore_values(&state.params);
params.snap_smoothers();
}
#[must_use]
pub fn deserialize_state(data: &[u8], expected_plugin_id: u64) -> Option<DeserializedState> {
if data.len() < 16 {
return None;
}
if &data[0..4] != STATE_MAGIC {
return None;
}
let version = u32::from_le_bytes(data[4..8].try_into().ok()?);
if version != STATE_VERSION {
return None;
}
let plugin_id = u64::from_le_bytes(data[8..16].try_into().ok()?);
if plugin_id != expected_plugin_id {
return None;
}
let mut offset = 16;
if offset + 4 > data.len() {
return None;
}
let count = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?) as usize;
offset += 4;
let max_count = data.len().saturating_sub(offset) / 12;
let mut params = Vec::with_capacity(count.min(max_count));
for _ in 0..count {
if offset + 12 > data.len() {
return None;
}
let id = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?);
offset += 4;
let value = f64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
offset += 8;
params.push((id, value));
}
if offset + 8 > data.len() {
return None;
}
#[allow(clippy::cast_possible_truncation)]
let extra_len = u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?) as usize;
offset += 8;
let extra = if extra_len > 0 {
match offset.checked_add(extra_len) {
Some(end) if end <= data.len() => Some(data[offset..end].to_vec()),
_ => return None,
}
} else {
None
};
Some(DeserializedState { params, extra })
}
use crate::export::PluginExport;
use truce_params::Params;
#[derive(Debug)]
pub enum RestoreError {
Invalid,
LoadState(StateLoadError),
}
impl std::fmt::Display for RestoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Invalid => f.write_str("state envelope is invalid"),
Self::LoadState(e) => write!(f, "plugin load_state failed: {e}"),
}
}
}
impl std::error::Error for RestoreError {}
pub fn snapshot_plugin<P: PluginExport>(plugin: &P) -> Vec<u8> {
let (ids, values) = plugin.params().collect_values();
let extra = plugin.save_state();
serialize_state(hash_plugin_id(P::info().clap_id), &ids, &values, &extra)
}
pub fn restore_plugin<P: PluginExport>(plugin: &mut P, bytes: &[u8]) -> Result<(), RestoreError> {
let id = hash_plugin_id(P::info().clap_id);
let s = deserialize_state(bytes, id).ok_or(RestoreError::Invalid)?;
plugin.params().restore_values(&s.params);
if let Some(extra) = s.extra {
plugin.load_state(&extra).map_err(RestoreError::LoadState)?;
}
Ok(())
}
#[must_use]
pub fn shared_plugin_state_hash(info: &crate::PluginInfo) -> u64 {
hash_plugin_id(info.clap_id)
}
#[must_use]
pub fn hash_plugin_id(id: &str) -> u64 {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for byte in id.bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0100_0000_01b3); }
hash
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip_state() {
let plugin_id = hash_plugin_id("com.test.plugin");
let ids = [0u32, 1, 2];
let values = [0.5f64, 1.0, -12.0];
let extra = b"hello extra state";
let data = serialize_state(plugin_id, &ids, &values, extra);
let state = deserialize_state(&data, plugin_id).unwrap();
assert_eq!(state.params.len(), 3);
assert_eq!(state.params[0], (0, 0.5));
assert_eq!(state.params[1], (1, 1.0));
assert_eq!(state.params[2], (2, -12.0));
assert_eq!(state.extra.unwrap(), b"hello extra state");
}
#[test]
fn wrong_plugin_id_fails() {
let plugin_id = hash_plugin_id("com.test.plugin");
let data = serialize_state(plugin_id, &[], &[], &[]);
assert!(deserialize_state(&data, 12345).is_none());
}
}