luminol_config/
command_db.rs

1// Copyright (C) 2024 Melody Madeline Lyons
2//
3// This file is part of Luminol.
4//
5// Luminol is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Luminol is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Luminol.  If not, see <http://www.gnu.org/licenses/>.
17//
18//     Additional permission under GNU GPL version 3 section 7
19//
20// If you modify this Program, or any covered work, by linking or combining
21// it with Steamworks API by Valve Corporation, containing parts covered by
22// terms of the Steamworks API by Valve Corporation, the licensors of this
23// Program grant you additional permission to convey the resulting work.
24
25use 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 commands
53    default: Vec<CommandDescription>,
54    /// User defined commands
55    // FIXME: visible to user?
56    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}