use luminol_data::commands::CommandDescription;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use super::RMVer;
static XP_DEFAULT: Lazy<Vec<CommandDescription>> = Lazy::new(|| {
ron::from_str(include_str!("commands/xp.ron")).expect(
"failed to statically load the default commands for rpg maker xp. please report this bug",
)
});
static VX_DEFAULT: Lazy<Vec<CommandDescription>> = Lazy::new(|| {
ron::from_str(include_str!("commands/vx.ron")).expect(
"failed to statically load the default commands for rpg maker vx. please report this bug",
)
});
static ACE_DEFAULT: Lazy<Vec<CommandDescription>> = Lazy::new(|| {
ron::from_str(include_str!("commands/ace.ron")).expect(
"failed to statically load the default commands for rpg maker vx ace. please report this bug",
)
});
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct CommandDB {
default: Vec<CommandDescription>,
pub user: Vec<CommandDescription>,
}
impl CommandDB {
pub fn new(ver: RMVer) -> Self {
Self {
default: match ver {
RMVer::XP => &*XP_DEFAULT,
RMVer::VX => &*VX_DEFAULT,
RMVer::Ace => &*ACE_DEFAULT,
}
.clone(),
user: vec![],
}
}
pub fn get(&self, code: u16) -> Option<&CommandDescription> {
self.user
.iter()
.find(|c| c.code == code)
.or_else(|| self.default.iter().find(|c| c.code == code))
}
pub fn iter(&self) -> impl Iterator<Item = &CommandDescription> {
self.default.iter().chain(self.user.iter())
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut CommandDescription> {
self.default.iter_mut().chain(self.user.iter_mut())
}
pub fn len(&self) -> usize {
self.default.len() + self.user.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}