luminol_config/
command_db.rs1use luminol_data::commands::CommandDescription;
26use once_cell::sync::Lazy;
27
28use serde::{Deserialize, Serialize};
29
30use super::RMVer;
31
32static XP_DEFAULT: Lazy<Vec<CommandDescription>> = Lazy::new(|| {
33 ron::from_str(include_str!("commands/xp.ron")).expect(
34 "failed to statically load the default commands for rpg maker xp. please report this bug",
35 )
36});
37
38static VX_DEFAULT: Lazy<Vec<CommandDescription>> = Lazy::new(|| {
39 ron::from_str(include_str!("commands/vx.ron")).expect(
40 "failed to statically load the default commands for rpg maker vx. please report this bug",
41 )
42});
43
44static ACE_DEFAULT: Lazy<Vec<CommandDescription>> = Lazy::new(|| {
45 ron::from_str(include_str!("commands/ace.ron")).expect(
46 "failed to statically load the default commands for rpg maker vx ace. please report this bug",
47 )
48});
49
50#[derive(Deserialize, Serialize, Debug, Clone)]
51pub struct CommandDB {
52 default: Vec<CommandDescription>,
54 pub user: Vec<CommandDescription>,
57}
58
59impl CommandDB {
60 pub fn new(ver: RMVer) -> Self {
61 Self {
62 default: match ver {
63 RMVer::XP => &*XP_DEFAULT,
64 RMVer::VX => &*VX_DEFAULT,
65 RMVer::Ace => &*ACE_DEFAULT,
66 }
67 .clone(),
68 user: vec![],
69 }
70 }
71
72 pub fn get(&self, code: u16) -> Option<&CommandDescription> {
73 self.user
74 .iter()
75 .find(|c| c.code == code)
76 .or_else(|| self.default.iter().find(|c| c.code == code))
77 }
78
79 pub fn iter(&self) -> impl Iterator<Item = &CommandDescription> {
80 self.default.iter().chain(self.user.iter())
81 }
82
83 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut CommandDescription> {
84 self.default.iter_mut().chain(self.user.iter_mut())
85 }
86
87 pub fn len(&self) -> usize {
88 self.default.len() + self.user.len()
89 }
90
91 pub fn is_empty(&self) -> bool {
92 self.len() == 0
93 }
94}