A lot more work on handling scripts properly.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,21 +1,23 @@
|
||||
use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
|
||||
use crate::{PkmnResult, StringKey};
|
||||
use indexmap::IndexMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ScriptSet {
|
||||
scripts: IndexMap<StringKey, Arc<Box<dyn Script>>>,
|
||||
scripts: IndexMap<StringKey, ScriptContainer>,
|
||||
}
|
||||
|
||||
impl ScriptSet {
|
||||
pub fn add(&mut self, script: Box<dyn Script>) -> Arc<Box<dyn Script>> {
|
||||
if let Some(existing) = self.scripts.get(script.name()) {
|
||||
existing.stack();
|
||||
return existing.clone();
|
||||
pub fn add(&mut self, script: Box<dyn Script>) -> ScriptContainer {
|
||||
if let Some(lock) = self.scripts.get(script.name()) {
|
||||
let existing = lock.get();
|
||||
if let Some(v) = &*existing {
|
||||
v.stack();
|
||||
return lock.clone();
|
||||
}
|
||||
}
|
||||
let arc = Arc::new(script);
|
||||
self.scripts.insert(arc.name().clone(), arc.clone());
|
||||
self.scripts
|
||||
.insert(script.name().clone(), ScriptContainer::new(script));
|
||||
self.scripts.last().unwrap().1.clone()
|
||||
}
|
||||
|
||||
@@ -23,38 +25,42 @@ impl ScriptSet {
|
||||
&mut self,
|
||||
key: &StringKey,
|
||||
instantiation: &'b F,
|
||||
) -> PkmnResult<Option<Arc<Box<dyn Script>>>>
|
||||
) -> PkmnResult<Option<ScriptContainer>>
|
||||
where
|
||||
F: Fn() -> PkmnResult<Option<Box<dyn Script>>>,
|
||||
{
|
||||
if let Some(existing) = self.scripts.get(key) {
|
||||
existing.stack();
|
||||
return Ok(Some(existing.clone()));
|
||||
if let Some(lock) = self.scripts.get(key) {
|
||||
let existing = lock.get();
|
||||
if let Some(v) = &*existing {
|
||||
v.stack();
|
||||
return Ok(Some(lock.clone()));
|
||||
}
|
||||
}
|
||||
let script = instantiation()?;
|
||||
if let Some(script) = script {
|
||||
let arc = Arc::new(script);
|
||||
self.scripts.insert(arc.name().clone(), arc.clone());
|
||||
let name = script.name().clone();
|
||||
let arc = ScriptContainer::new(script);
|
||||
self.scripts.insert(name, arc.clone());
|
||||
Ok(Some(self.scripts.last().unwrap().1.clone()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &StringKey) -> Option<&Arc<Box<dyn Script>>> {
|
||||
pub fn get(&self, key: &StringKey) -> Option<&ScriptContainer> {
|
||||
self.scripts.get(key)
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, key: &StringKey) {
|
||||
let value = self.scripts.shift_remove(key);
|
||||
if let Some(script) = value {
|
||||
script.on_remove();
|
||||
script.get().as_ref().as_ref().unwrap().on_remove();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
for script in &self.scripts {
|
||||
script.1.on_remove();
|
||||
script.1.get().as_ref().as_ref().unwrap().on_remove();
|
||||
}
|
||||
self.scripts.clear();
|
||||
}
|
||||
@@ -63,7 +69,7 @@ impl ScriptSet {
|
||||
self.scripts.contains_key(key)
|
||||
}
|
||||
|
||||
pub fn at(&self, index: usize) -> &Arc<Box<dyn Script>> {
|
||||
pub fn at(&self, index: usize) -> &ScriptContainer {
|
||||
&self.scripts[index]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user