use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use paperpaw_sdk::builtin::PLUGIN_DIR;
use paperpaw_sdk::{Command, Commands, Env, Id, requires};
use crate::PLUGIN;
fn present(env: &Env<'_>, id: &str) -> bool {
env.plugins().get(id).is_some()
|| env
.storage()
.exists(&format!("{PLUGIN_DIR}/{id}/plugin.ini"))
}
pub(crate) fn missing(env: &Env<'_>, folder: &str, plugin: &str) -> Vec<Id> {
let path = format!("{folder}/plugin.ini");
let Ok(text) = paperpaw_sdk::read_file(env.storage(), &path, 16 * 1024) else {
return Vec::new();
};
let Ok(text) = String::from_utf8(text) else {
return Vec::new();
};
let mut section = "";
let mut value = None;
for line in text.lines().map(str::trim) {
if let Some(name) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
section = name.trim();
} else if section == "plugin"
&& let Some((key, v)) = line.split_once('=')
&& key.trim() == "requires"
{
value = Some(v.trim());
}
}
let Some(Ok(required)) = value.map(|v| requires::parse(v, plugin)) else {
return Vec::new();
};
required
.into_iter()
.filter(|r| !present(env, r.id.as_str()))
.map(|r| r.id)
.collect()
}
pub(crate) fn offer(missing: Vec<Id>, out: &mut Commands) {
if !missing.is_empty() {
out.push(Command::request_plugins(PLUGIN.clone(), missing));
}
}