use std::sync::LazyLock;
use crate::EncryptionKey;
use dashmap::DashMap;
use secure_gate::RevealSecret;
#[derive(Default)]
struct DlcRegistration {
key: Option<EncryptionKey>,
path: Option<String>,
}
static REGISTRY: LazyLock<DashMap<String, DlcRegistration>> = LazyLock::new(|| DashMap::new());
pub fn insert(dlc_id: &str, key: EncryptionKey) {
let mut entry = REGISTRY.entry(dlc_id.to_owned()).or_default();
entry.key = Some(key);
}
#[allow(unused)]
pub fn remove(dlc_id: &str) {
REGISTRY.remove(dlc_id);
}
pub struct DlcEntry {
pub key: EncryptionKey,
pub path: Option<String>,
}
pub fn get(dlc_id: &str) -> Option<EncryptionKey> {
REGISTRY.get(dlc_id).and_then(|v| {
v.key.as_ref().map(|k| {
k.with_secret(|b| {
EncryptionKey::new(*b)
})
})
})
}
pub fn get_full(dlc_id: &str) -> Option<DlcEntry> {
REGISTRY.get(dlc_id).and_then(|v| {
v.key.as_ref().map(|k| DlcEntry {
key: k.with_secret(|b| EncryptionKey::new(*b)),
path: v.path.clone(),
})
})
}
pub fn get_from_path(path: &str) -> Option<DlcEntry> {
REGISTRY.iter().find_map(|v| {
if v.value().path.as_deref() == Some(path) {
v.value().key.as_ref().map(|k| DlcEntry {
key: k.with_secret(|b| EncryptionKey::new(*b)),
path: v.value().path.clone(),
})
} else {
None
}
})
}
pub fn register_asset_path(dlc_id: &str, path: &str) {
REGISTRY
.entry(dlc_id.to_owned())
.and_modify(|e| e.path = Some(path.to_owned()))
.or_insert(DlcRegistration {
key: None,
path: Some(path.to_owned()),
});
}
pub fn iter_ids() -> impl Iterator<Item = String> {
REGISTRY.iter().map(|r| r.key().clone())
}
#[allow(unused)]
pub fn asset_path_for(dlc_id: &str) -> Option<String> {
REGISTRY
.get(dlc_id)
.and_then(|v| v.path.as_ref().map(|p| p.clone()))
}
pub(crate) fn has(dlc_id: &str, path: &str) -> bool {
if let Some(reg) = REGISTRY.get(dlc_id) {
reg.path.as_deref() == Some(path)
} else {
false
}
}
#[cfg(test)]
#[allow(unused)]
pub(crate) fn check(dlc_id: &str, path: &str) -> bool {
if let Some(reg) = REGISTRY.get(dlc_id) {
reg.path.as_deref().map_or(false, |p| p != path)
} else {
false
}
}
#[cfg(test)]
pub(crate) fn clear_all() {
REGISTRY.clear();
}