2022-06-12 15:57:39 +00:00
|
|
|
use std::sync::{Arc, Weak};
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use item_script::*;
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use script::*;
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use script_set::*;
|
|
|
|
#[doc(inline)]
|
2022-07-01 15:07:22 +00:00
|
|
|
pub use volatile_scripts_owner::*;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2022-07-01 16:20:16 +00:00
|
|
|
/// Scripts that are used for item usage
|
2022-06-19 19:34:08 +00:00
|
|
|
mod item_script;
|
2022-07-01 16:20:16 +00:00
|
|
|
/// Scripts that are used to change how a battle functions.
|
2022-06-19 19:34:08 +00:00
|
|
|
mod script;
|
2022-07-01 16:20:16 +00:00
|
|
|
/// Collections of scripts.
|
2022-06-19 19:34:08 +00:00
|
|
|
mod script_set;
|
2022-07-01 16:20:16 +00:00
|
|
|
/// A trait to add volatile scripts to any object.
|
2022-07-01 15:07:22 +00:00
|
|
|
mod volatile_scripts_owner;
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// This macro runs a script function on a given ScriptSource, and all its parents. It will ensure
|
|
|
|
/// to only run the script function if it is not suppressed, and can take any amount of parameters.
|
2022-06-03 14:35:18 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! script_hook {
|
|
|
|
($hook_name: ident, $source: ident, $($parameters: expr),*) => {
|
|
|
|
let mut aggregator = $source.get_script_iterator();
|
2022-06-18 17:44:13 +00:00
|
|
|
while let Some(script_container) = aggregator.get_next() {
|
|
|
|
let script = script_container.get();
|
2022-06-18 16:08:25 +00:00
|
|
|
if let Some(script) = script {
|
|
|
|
if let Some(script) = script.read().as_deref() {
|
|
|
|
if !script.is_suppressed() {
|
|
|
|
script.$hook_name($($parameters),*);
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// This macro runs a script function on all scripts in a Vec of scripts. It will ensure it only
|
|
|
|
/// runs the script function if it is not suppressed, and can take any amount of parameters.
|
2022-06-16 15:59:33 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! run_scripts {
|
|
|
|
($hook_name: ident, $source: ident, $($parameters: expr),*) => {
|
|
|
|
for script in $source {
|
|
|
|
match script {
|
|
|
|
ScriptWrapper::Script(s) => {
|
|
|
|
if let Some(s) = s.upgrade() {
|
2022-06-18 16:08:25 +00:00
|
|
|
let s = s.read();
|
|
|
|
if let Some(s) = s.deref() {
|
2022-06-16 15:59:33 +00:00
|
|
|
if !s.is_suppressed() {
|
|
|
|
s.$hook_name($($parameters),*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ScriptWrapper::Set(s) => {
|
2022-06-19 10:07:54 +00:00
|
|
|
if let Some(set) = s.upgrade() {
|
|
|
|
let current_scripts = set.get_owning_iterator();
|
|
|
|
for s in current_scripts {
|
|
|
|
if let Some(s) = s.get() {
|
2022-06-18 16:08:25 +00:00
|
|
|
let s = s.read();
|
|
|
|
if let Some(s) = s.deref() {
|
2022-06-19 10:07:54 +00:00
|
|
|
if !s.is_suppressed() && set.has(s.name()) {
|
2022-06-18 16:08:25 +00:00
|
|
|
s.$hook_name($($parameters),*);
|
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The script source data is the basic data required for any script source.
|
2022-06-12 16:41:49 +00:00
|
|
|
#[derive(Default, Debug)]
|
2022-06-12 15:57:39 +00:00
|
|
|
pub struct ScriptSourceData {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Whether or not the data has been initialized yet.
|
2022-06-12 15:57:39 +00:00
|
|
|
is_initialized: bool,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A list that references all possible scripts on this source, and it's parents.
|
2022-06-12 15:57:39 +00:00
|
|
|
scripts: Vec<ScriptWrapper>,
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A script source is a struct on which we can trigger scripts to be run from.
|
2022-06-12 15:57:39 +00:00
|
|
|
pub trait ScriptSource<'a> {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets an iterator over all the scripts that are relevant to this script source. If the data
|
|
|
|
/// has not been initialised, it will do so here.
|
|
|
|
fn get_script_iterator(&self) -> ScriptIterator {
|
2022-06-12 15:57:39 +00:00
|
|
|
let lock = self.get_script_source_data();
|
|
|
|
if !lock.read().is_initialized {
|
|
|
|
let mut data = lock.write();
|
|
|
|
data.scripts = Vec::with_capacity(self.get_script_count());
|
|
|
|
self.collect_scripts(&mut data.scripts);
|
|
|
|
data.is_initialized = true;
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
ScriptIterator::new(&lock.read().scripts as *const Vec<ScriptWrapper>)
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
|
|
|
|
/// The number of scripts that are expected to be relevant for this source. This generally is
|
|
|
|
/// the number of its own scripts + the number of scripts for any parents.
|
2022-06-12 15:57:39 +00:00
|
|
|
fn get_script_count(&self) -> usize;
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Returns the underlying data required for us to be a script source.
|
2022-06-12 15:57:39 +00:00
|
|
|
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData>;
|
2022-07-01 15:07:22 +00:00
|
|
|
/// This should add all scripts belonging to this source to the scripts Vec, disregarding its
|
|
|
|
/// potential parents.
|
2022-06-12 15:57:39 +00:00
|
|
|
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>);
|
2022-07-01 15:07:22 +00:00
|
|
|
/// This should add all scripts that are relevant to the source the the scripts Vec, including
|
|
|
|
/// everything that belongs to its parents.
|
2022-06-12 15:57:39 +00:00
|
|
|
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>);
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Enum to store both ScriptSets and sets in a single value.
|
2022-06-12 16:41:49 +00:00
|
|
|
#[derive(Debug)]
|
2022-06-06 11:54:59 +00:00
|
|
|
pub enum ScriptWrapper {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A reference to a single script.
|
2022-06-18 16:41:23 +00:00
|
|
|
Script(Weak<RwLock<Option<Arc<dyn Script>>>>),
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A reference to a ScriptSet.
|
2022-06-19 10:07:54 +00:00
|
|
|
Set(Weak<ScriptSet>),
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&ScriptContainer> for ScriptWrapper {
|
|
|
|
fn from(c: &ScriptContainer) -> Self {
|
|
|
|
ScriptWrapper::Script(Arc::downgrade(c.arc()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 10:07:54 +00:00
|
|
|
impl From<&Arc<ScriptSet>> for ScriptWrapper {
|
|
|
|
fn from(c: &Arc<ScriptSet>) -> Self {
|
2022-06-12 15:57:39 +00:00
|
|
|
ScriptWrapper::Set(Arc::downgrade(c))
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// This struct allows for the iteration over scripts.
|
|
|
|
pub struct ScriptIterator {
|
|
|
|
/// A pointer to the vector of ScriptWrappers. This can be a pointer, as we know it remains valid
|
|
|
|
/// while we're using it.
|
2022-06-12 15:57:39 +00:00
|
|
|
scripts: *const Vec<ScriptWrapper>,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The current index in the scripts.
|
2022-06-03 14:35:18 +00:00
|
|
|
index: i32,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// If we're currently inside a set, the current index inside the set.
|
2022-06-03 14:35:18 +00:00
|
|
|
set_index: i32,
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
impl ScriptIterator {
|
|
|
|
/// Instantiates an iterator.
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn new(scripts: *const Vec<ScriptWrapper>) -> Self {
|
2022-07-01 15:07:22 +00:00
|
|
|
Self {
|
|
|
|
scripts,
|
|
|
|
index: -1,
|
|
|
|
set_index: -1,
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Move to the next valid value in the scripts.
|
2022-06-03 14:35:18 +00:00
|
|
|
fn increment_to_next_value(&mut self) -> bool {
|
|
|
|
if self.index != -1 {
|
2022-07-01 15:07:22 +00:00
|
|
|
let wrapper = unsafe { &(*self.scripts)[self.index as usize] };
|
2022-06-12 15:57:39 +00:00
|
|
|
if let ScriptWrapper::Set(set) = wrapper {
|
|
|
|
if let Some(set) = set.upgrade() {
|
|
|
|
self.set_index += 1;
|
2022-06-19 10:07:54 +00:00
|
|
|
if self.set_index as usize >= set.count() {
|
2022-06-12 15:57:39 +00:00
|
|
|
self.set_index = -1;
|
|
|
|
} else {
|
|
|
|
return true;
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.index += 1;
|
2022-07-01 15:07:22 +00:00
|
|
|
let len = (unsafe { &*self.scripts }).len() as i32;
|
|
|
|
for index in self.index..len {
|
2022-06-03 14:35:18 +00:00
|
|
|
self.index = index;
|
2022-06-12 15:57:39 +00:00
|
|
|
let wrapper = unsafe { &self.scripts.as_ref().unwrap()[self.index as usize] };
|
|
|
|
if let ScriptWrapper::Set(s) = wrapper {
|
2022-06-17 17:53:33 +00:00
|
|
|
if let Some(set) = s.upgrade() {
|
2022-06-19 10:07:54 +00:00
|
|
|
if set.count() > 0 {
|
2022-06-17 17:53:33 +00:00
|
|
|
self.set_index = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
} else if let ScriptWrapper::Script(script) = wrapper {
|
|
|
|
if let Some(v) = script.upgrade() {
|
|
|
|
if let Some(..) = v.read().as_ref() {
|
2022-06-06 11:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets the next valid script. If none is found, returns None.
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn get_next(&mut self) -> Option<ScriptContainer> {
|
2022-06-03 14:35:18 +00:00
|
|
|
if !self.increment_to_next_value() {
|
|
|
|
return None;
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
unsafe {
|
|
|
|
return match &self.scripts.as_ref().unwrap()[self.index as usize] {
|
|
|
|
// increment_to_next_value
|
|
|
|
ScriptWrapper::Script(script) => Some(script.upgrade().unwrap().into()),
|
|
|
|
ScriptWrapper::Set(set) => {
|
2022-06-19 10:07:54 +00:00
|
|
|
let set = set.upgrade().unwrap();
|
|
|
|
let sc = set.at(self.set_index as usize);
|
2022-07-01 16:20:16 +00:00
|
|
|
return Some(sc);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Resets the iterator to the start.
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.index = -1;
|
|
|
|
self.set_index = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-06-19 19:34:08 +00:00
|
|
|
use std::any::Any;
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::script::ScriptContainer;
|
|
|
|
use crate::static_data::EffectParameter;
|
|
|
|
use crate::StringKey;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
|
|
|
use super::*;
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
pub struct TestScript {
|
|
|
|
name: StringKey,
|
2022-06-18 16:08:25 +00:00
|
|
|
is_marked_for_deletion: AtomicBool,
|
|
|
|
suppressed_count: AtomicUsize,
|
|
|
|
test_count: AtomicUsize,
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestScript {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
name: StringKey::new("test"),
|
2022-06-18 16:08:25 +00:00
|
|
|
is_marked_for_deletion: Default::default(),
|
|
|
|
suppressed_count: AtomicUsize::new(0),
|
|
|
|
test_count: AtomicUsize::new(0),
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn new_with_name(name: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
name: StringKey::new(name),
|
2022-06-18 16:08:25 +00:00
|
|
|
is_marked_for_deletion: Default::default(),
|
|
|
|
suppressed_count: AtomicUsize::new(0),
|
|
|
|
test_count: AtomicUsize::new(0),
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Script for TestScript {
|
|
|
|
fn name(&self) -> &StringKey {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:08:25 +00:00
|
|
|
fn get_marked_for_deletion(&self) -> &AtomicBool {
|
|
|
|
&self.is_marked_for_deletion
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:08:25 +00:00
|
|
|
fn get_suppressed_count(&self) -> &AtomicUsize {
|
|
|
|
&self.suppressed_count
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_suppression(&self) {}
|
2022-06-12 15:57:39 +00:00
|
|
|
|
2022-06-18 16:08:25 +00:00
|
|
|
fn remove_suppression(&self) {}
|
2022-06-12 15:57:39 +00:00
|
|
|
|
2022-06-18 16:08:25 +00:00
|
|
|
fn on_initialize(&self, _pars: &[EffectParameter]) {
|
|
|
|
self.test_count.fetch_add(1, Ordering::SeqCst);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
2022-06-18 16:08:25 +00:00
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_single_script() {
|
2022-06-18 16:41:23 +00:00
|
|
|
let script = ScriptContainer::new(Arc::new(TestScript::new()));
|
2022-06-12 15:57:39 +00:00
|
|
|
let scripts = vec![ScriptWrapper::from(&script)];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
while let Some(v) = aggregator.get_next() {
|
2022-06-18 16:08:25 +00:00
|
|
|
v.get().unwrap().read().as_ref().unwrap().on_initialize(&[]);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
let a = script.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), 1);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_single_script_with_resets() {
|
2022-06-18 16:41:23 +00:00
|
|
|
let script = ScriptContainer::new(Arc::new(TestScript::new()));
|
2022-06-12 15:57:39 +00:00
|
|
|
let scripts = vec![ScriptWrapper::from(&script)];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
for i in 1..11 {
|
|
|
|
aggregator.reset();
|
|
|
|
while let Some(v) = aggregator.get_next() {
|
2022-06-18 16:08:25 +00:00
|
|
|
v.get().unwrap().read().as_ref().unwrap().on_initialize(&[]);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
let a = script.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_three_script() {
|
2022-06-18 16:41:23 +00:00
|
|
|
let script1 = ScriptContainer::new(Arc::new(TestScript::new()));
|
|
|
|
let script2 = ScriptContainer::new(Arc::new(TestScript::new()));
|
|
|
|
let script3 = ScriptContainer::new(Arc::new(TestScript::new()));
|
2022-06-12 15:57:39 +00:00
|
|
|
let scripts = vec![
|
|
|
|
ScriptWrapper::from(&script1),
|
|
|
|
ScriptWrapper::from(&script2),
|
|
|
|
ScriptWrapper::from(&script3),
|
|
|
|
];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
while let Some(v) = aggregator.get_next() {
|
2022-06-18 16:08:25 +00:00
|
|
|
v.get().unwrap().read().as_ref().unwrap().on_initialize(&[]);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
let a = script1.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), 1);
|
2022-06-12 15:57:39 +00:00
|
|
|
let a = script2.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), 1);
|
2022-06-12 15:57:39 +00:00
|
|
|
let a = script3.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), 1);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_three_script_with_resets() {
|
2022-06-18 16:41:23 +00:00
|
|
|
let script1 = ScriptContainer::new(Arc::new(TestScript::new()));
|
|
|
|
let script2 = ScriptContainer::new(Arc::new(TestScript::new()));
|
|
|
|
let script3 = ScriptContainer::new(Arc::new(TestScript::new()));
|
2022-06-12 15:57:39 +00:00
|
|
|
let scripts = vec![
|
|
|
|
ScriptWrapper::from(&script1),
|
|
|
|
ScriptWrapper::from(&script2),
|
|
|
|
ScriptWrapper::from(&script3),
|
|
|
|
];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
for i in 1..11 {
|
|
|
|
aggregator.reset();
|
|
|
|
while let Some(v) = aggregator.get_next() {
|
2022-06-18 16:08:25 +00:00
|
|
|
v.get().unwrap().read().as_ref().unwrap().on_initialize(&[]);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
let a = script1.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
let a = script2.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
let a = script3.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(a.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_script_set() {
|
2022-06-19 10:07:54 +00:00
|
|
|
let set = Arc::new(ScriptSet::default());
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_a")));
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_b")));
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_c")));
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
let scripts = vec![ScriptWrapper::from(&set)];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
for i in 1..11 {
|
|
|
|
aggregator.reset();
|
|
|
|
while let Some(v) = aggregator.get_next() {
|
2022-06-18 16:08:25 +00:00
|
|
|
v.get().unwrap().read().as_ref().unwrap().on_initialize(&[]);
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
2022-06-19 10:07:54 +00:00
|
|
|
let s = set.at(0);
|
|
|
|
let s = s.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(s.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
assert_eq!(s.name().str(), "test_a");
|
2022-06-19 10:07:54 +00:00
|
|
|
let s = set.at(1);
|
|
|
|
let s = s.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(s.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
assert_eq!(s.name().str(), "test_b");
|
2022-06-19 10:07:54 +00:00
|
|
|
let s = set.at(2);
|
|
|
|
let s = s.get_as::<TestScript>();
|
2022-06-18 16:08:25 +00:00
|
|
|
assert_eq!(s.test_count.load(Ordering::Relaxed), i);
|
2022-06-12 15:57:39 +00:00
|
|
|
assert_eq!(s.name().str(), "test_c");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_script_set_when_removing_last() {
|
2022-06-19 10:07:54 +00:00
|
|
|
let set = Arc::new(ScriptSet::default());
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_a")));
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_b")));
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_c")));
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
let scripts = vec![ScriptWrapper::from(&set)];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
assert_eq!(
|
2022-06-18 16:08:25 +00:00
|
|
|
aggregator
|
|
|
|
.get_next()
|
|
|
|
.unwrap()
|
|
|
|
.get()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.name()
|
|
|
|
.str(),
|
2022-06-12 15:57:39 +00:00
|
|
|
"test_a"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-06-18 16:08:25 +00:00
|
|
|
aggregator
|
|
|
|
.get_next()
|
|
|
|
.unwrap()
|
|
|
|
.get()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.name()
|
|
|
|
.str(),
|
2022-06-12 15:57:39 +00:00
|
|
|
"test_b"
|
|
|
|
);
|
|
|
|
|
2022-06-19 10:07:54 +00:00
|
|
|
set.remove(&StringKey::new("test_c"));
|
2022-06-12 15:57:39 +00:00
|
|
|
assert!(aggregator.get_next().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_aggregator_property_iterates_script_set_when_removing_middle() {
|
2022-06-19 10:07:54 +00:00
|
|
|
let set = Arc::new(ScriptSet::default());
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_a")));
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_b")));
|
|
|
|
set.add(Arc::new(TestScript::new_with_name("test_c")));
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
let scripts = vec![ScriptWrapper::from(&set)];
|
2022-07-01 15:07:22 +00:00
|
|
|
let mut aggregator = ScriptIterator::new(&scripts as *const Vec<ScriptWrapper>);
|
2022-06-12 15:57:39 +00:00
|
|
|
assert_eq!(
|
2022-06-18 16:08:25 +00:00
|
|
|
aggregator
|
|
|
|
.get_next()
|
|
|
|
.unwrap()
|
|
|
|
.get()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.name()
|
|
|
|
.str(),
|
2022-06-12 15:57:39 +00:00
|
|
|
"test_a"
|
|
|
|
);
|
|
|
|
|
2022-06-19 10:07:54 +00:00
|
|
|
set.remove(&StringKey::new("test_b"));
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-06-18 16:08:25 +00:00
|
|
|
aggregator
|
|
|
|
.get_next()
|
|
|
|
.unwrap()
|
|
|
|
.get()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.name()
|
|
|
|
.str(),
|
2022-06-12 15:57:39 +00:00
|
|
|
"test_c"
|
|
|
|
);
|
|
|
|
assert!(aggregator.get_next().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TestScriptSource {
|
|
|
|
pub data: RwLock<ScriptSourceData>,
|
|
|
|
pub script: ScriptContainer,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ScriptSource<'a> for TestScriptSource {
|
|
|
|
fn get_script_count(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
|
|
|
|
&self.data
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
|
|
|
scripts.push((&self.script).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
|
|
|
self.get_own_scripts(scripts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_source_set_script_then_rerun() {
|
|
|
|
let source = TestScriptSource {
|
|
|
|
data: RwLock::new(ScriptSourceData::default()),
|
|
|
|
script: ScriptContainer::default(),
|
2022-06-03 14:35:18 +00:00
|
|
|
};
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
let mut aggregator = source.get_script_iterator();
|
|
|
|
aggregator.reset();
|
|
|
|
assert!(aggregator.get_next().is_none());
|
|
|
|
aggregator.reset();
|
2022-06-18 16:41:23 +00:00
|
|
|
source.script.set(Arc::new(TestScript::new()));
|
2022-06-12 15:57:39 +00:00
|
|
|
assert!(aggregator.get_next().is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn script_source_clear_script_then_rerun() {
|
|
|
|
let source = TestScriptSource {
|
|
|
|
data: RwLock::new(ScriptSourceData::default()),
|
|
|
|
script: ScriptContainer::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut aggregator = source.get_script_iterator();
|
2022-06-18 16:41:23 +00:00
|
|
|
source.script.set(Arc::new(TestScript::new()));
|
2022-06-12 15:57:39 +00:00
|
|
|
assert!(aggregator.get_next().is_some());
|
|
|
|
aggregator.reset();
|
|
|
|
source.script.clear();
|
|
|
|
assert!(aggregator.get_next().is_none());
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|