2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::{PkmnResult, StringKey};
|
2022-06-06 11:54:59 +00:00
|
|
|
use indexmap::IndexMap;
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct ScriptSet {
|
2022-06-12 15:57:39 +00:00
|
|
|
scripts: IndexMap<StringKey, ScriptContainer>,
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
|
|
|
|
impl ScriptSet {
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn add(&mut self, script: Box<dyn Script>) -> ScriptContainer {
|
|
|
|
if let Some(lock) = self.scripts.get(script.name()) {
|
2022-06-18 16:08:25 +00:00
|
|
|
if let Some(existing) = lock.get() {
|
|
|
|
let existing = existing.read();
|
|
|
|
if let Some(v) = &*existing {
|
|
|
|
v.stack();
|
|
|
|
return lock.clone();
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
self.scripts.insert(script.name().clone(), ScriptContainer::new(script));
|
2022-06-06 11:54:59 +00:00
|
|
|
self.scripts.last().unwrap().1.clone()
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:59:33 +00:00
|
|
|
pub fn stack_or_add<'b, F>(&mut self, key: &StringKey, instantiation: &'b F) -> PkmnResult<Option<ScriptContainer>>
|
2022-06-06 11:54:59 +00:00
|
|
|
where
|
2022-06-11 15:22:46 +00:00
|
|
|
F: Fn() -> PkmnResult<Option<Box<dyn Script>>>,
|
2022-06-06 11:54:59 +00:00
|
|
|
{
|
2022-06-12 15:57:39 +00:00
|
|
|
if let Some(lock) = self.scripts.get(key) {
|
2022-06-18 16:08:25 +00:00
|
|
|
if let Some(existing) = lock.get() {
|
|
|
|
let existing = existing.read();
|
|
|
|
if let Some(v) = &*existing {
|
|
|
|
v.stack();
|
|
|
|
return Ok(Some(lock.clone()));
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
let script = instantiation()?;
|
2022-06-11 15:22:46 +00:00
|
|
|
if let Some(script) = script {
|
2022-06-12 15:57:39 +00:00
|
|
|
let name = script.name().clone();
|
|
|
|
let arc = ScriptContainer::new(script);
|
2022-06-18 14:06:54 +00:00
|
|
|
self.scripts.insert(name, arc);
|
2022-06-11 15:22:46 +00:00
|
|
|
Ok(Some(self.scripts.last().unwrap().1.clone()))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn get(&self, key: &StringKey) -> Option<&ScriptContainer> {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.scripts.get(key)
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn remove(&mut self, key: &StringKey) {
|
2022-06-06 11:54:59 +00:00
|
|
|
let value = self.scripts.shift_remove(key);
|
|
|
|
if let Some(script) = value {
|
2022-06-18 16:08:25 +00:00
|
|
|
if let Some(script) = script.get() {
|
|
|
|
let script = script.read();
|
|
|
|
script.as_ref().unwrap().on_remove();
|
|
|
|
script.as_ref().unwrap().mark_for_deletion();
|
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
for script in &self.scripts {
|
2022-06-18 16:08:25 +00:00
|
|
|
if let Some(script) = script.1.get() {
|
|
|
|
let script = script.read();
|
|
|
|
script.as_ref().unwrap().on_remove();
|
|
|
|
script.as_ref().unwrap().mark_for_deletion();
|
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
self.scripts.clear();
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn has(&self, key: &StringKey) -> bool {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.scripts.contains_key(key)
|
|
|
|
}
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn at(&self, index: usize) -> &ScriptContainer {
|
2022-06-06 11:54:59 +00:00
|
|
|
&self.scripts[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.scripts.len()
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
|
|
|
|
pub(crate) fn get_underlying(&self) -> &IndexMap<StringKey, ScriptContainer> {
|
|
|
|
&self.scripts
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|