A lot more work on handling scripts properly.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-12 17:57:39 +02:00
parent 56e1237c22
commit 2aadab5a42
11 changed files with 788 additions and 175 deletions

View File

@@ -1,6 +1,7 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use std::sync::Weak;
use parking_lot::RwLock;
use std::sync::{Arc, Weak};
pub mod item_script;
pub mod script;
@@ -12,6 +13,8 @@ macro_rules! script_hook {
($hook_name: ident, $source: ident, $($parameters: expr),*) => {
let mut aggregator = $source.get_script_iterator();
while let Some(script) = aggregator.get_next() {
let lock = &mut script.get();
let script = lock.as_mut().unwrap();
if script.is_suppressed() {
continue;
}
@@ -20,47 +23,76 @@ macro_rules! script_hook {
};
}
pub trait ScriptSource {
#[derive(Default)]
pub struct ScriptSourceData {
is_initialized: bool,
scripts: Vec<ScriptWrapper>,
}
pub trait ScriptSource<'a> {
fn get_script_iterator(&self) -> ScriptAggregator {
todo!()
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;
}
ScriptAggregator::new(&lock.read().scripts as *const Vec<ScriptWrapper>)
}
fn get_script_count(&self);
fn get_script_count(&self) -> usize;
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData>;
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>);
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>);
}
pub enum ScriptWrapper {
Script(Weak<Box<dyn Script>>),
Set(Weak<ScriptSet>),
Script(Weak<RwLock<Option<Box<dyn Script>>>>),
Set(Weak<RwLock<ScriptSet>>),
}
impl From<&ScriptContainer> for ScriptWrapper {
fn from(c: &ScriptContainer) -> Self {
ScriptWrapper::Script(Arc::downgrade(c.arc()))
}
}
impl From<&Arc<RwLock<ScriptSet>>> for ScriptWrapper {
fn from(c: &Arc<RwLock<ScriptSet>>) -> Self {
ScriptWrapper::Set(Arc::downgrade(c))
}
}
pub struct ScriptAggregator {
scripts: Vec<Option<ScriptWrapper>>,
scripts: *const Vec<ScriptWrapper>,
size: i32,
index: i32,
set_index: i32,
}
impl ScriptAggregator {
pub fn new(scripts: Vec<Option<ScriptWrapper>>) -> Self {
let len = scripts.len();
Self {
scripts,
size: len as i32,
index: -1,
set_index: -1,
pub fn new(scripts: *const Vec<ScriptWrapper>) -> Self {
unsafe {
let len = scripts.as_ref().unwrap().len();
Self {
scripts,
size: len as i32,
index: -1,
set_index: -1,
}
}
}
fn increment_to_next_value(&mut self) -> bool {
if self.index != -1 {
if let Some(wrapper) = &self.scripts[self.index as usize] {
if let ScriptWrapper::Set(set) = wrapper {
if let Some(set) = set.upgrade() {
self.set_index += 1;
if self.set_index as usize >= set.count() {
self.set_index = -1;
} else {
return true;
}
let wrapper = unsafe { &self.scripts.as_ref().unwrap()[self.index as usize] };
if let ScriptWrapper::Set(set) = wrapper {
if let Some(set) = set.upgrade() {
self.set_index += 1;
if self.set_index as usize >= set.read().count() {
self.set_index = -1;
} else {
return true;
}
}
}
@@ -68,14 +100,15 @@ impl ScriptAggregator {
self.index += 1;
for index in self.index..self.size {
self.index = index;
if let Some(wrapper) = &self.scripts[index as usize] {
if let ScriptWrapper::Set(s) = wrapper {
if let Some(..) = s.upgrade() {
self.set_index = 0;
return true;
}
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(..) = script.upgrade() {
let wrapper = unsafe { &self.scripts.as_ref().unwrap()[self.index as usize] };
if let ScriptWrapper::Set(s) = wrapper {
if let Some(..) = s.upgrade() {
self.set_index = 0;
return true;
}
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(v) = script.upgrade() {
if let Some(..) = v.read().as_ref() {
return true;
}
}
@@ -85,18 +118,318 @@ impl ScriptAggregator {
false
}
pub fn get_next(&mut self) -> Option<&Box<dyn Script>> {
pub fn get_next(&mut self) -> Option<ScriptContainer> {
if !self.increment_to_next_value() {
return None;
}
return match self.scripts[self.index as usize].as_ref().unwrap() {
// We can make this unsafe as we know there is a strong reference. This is validated in
// increment_to_next_value
ScriptWrapper::Script(script) => unsafe { Some(&*script.as_ptr()) },
ScriptWrapper::Set(set) => unsafe {
let r = (&*set.as_ptr()).at(self.set_index as usize);
Some(r.as_ref())
},
};
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) => {
let lock = set.as_ptr().as_ref().unwrap();
let l = lock.read();
let sc = l.at(self.set_index as usize);
return Some(sc.clone());
}
};
}
}
pub fn reset(&mut self) {
self.index = -1;
self.set_index = -1;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dynamic_data::script_handling::script::ScriptContainer;
use crate::static_data::EffectParameter;
use crate::StringKey;
use std::any::Any;
pub struct TestScript {
name: StringKey,
test_count: usize,
}
impl TestScript {
fn new() -> Self {
Self {
name: StringKey::new("test"),
test_count: 0,
}
}
fn new_with_name(name: &str) -> Self {
Self {
name: StringKey::new(name),
test_count: 0,
}
}
}
impl Script for TestScript {
fn name(&self) -> &StringKey {
&self.name
}
fn get_suppressed_count(&self) -> usize {
0
}
fn add_suppression(&self) {}
fn remove_suppression(&self) {}
fn on_initialize(&mut self, _pars: &[EffectParameter]) {
self.test_count += 1;
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[test]
fn script_aggregator_property_iterates_single_script() {
let script = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![ScriptWrapper::from(&script)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
}
#[test]
fn script_aggregator_property_iterates_single_script_with_resets() {
let script = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![ScriptWrapper::from(&script)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
for i in 1..11 {
aggregator.reset();
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script.get_as::<TestScript>();
assert_eq!(a.test_count, i);
}
}
#[test]
fn script_aggregator_property_iterates_three_script() {
let script1 = ScriptContainer::new(Box::new(TestScript::new()));
let script2 = ScriptContainer::new(Box::new(TestScript::new()));
let script3 = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![
ScriptWrapper::from(&script1),
ScriptWrapper::from(&script2),
ScriptWrapper::from(&script3),
];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script1.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
let a = script2.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
let a = script3.get_as::<TestScript>();
assert_eq!(a.test_count, 1);
}
#[test]
fn script_aggregator_property_iterates_three_script_with_resets() {
let script1 = ScriptContainer::new(Box::new(TestScript::new()));
let script2 = ScriptContainer::new(Box::new(TestScript::new()));
let script3 = ScriptContainer::new(Box::new(TestScript::new()));
let scripts = vec![
ScriptWrapper::from(&script1),
ScriptWrapper::from(&script2),
ScriptWrapper::from(&script3),
];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
for i in 1..11 {
aggregator.reset();
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let a = script1.get_as::<TestScript>();
assert_eq!(a.test_count, i);
let a = script2.get_as::<TestScript>();
assert_eq!(a.test_count, i);
let a = script3.get_as::<TestScript>();
assert_eq!(a.test_count, i);
}
}
#[test]
fn script_aggregator_property_iterates_script_set() {
let set = Arc::new(RwLock::new(ScriptSet::default()));
let mut mut_set = set.write();
mut_set.add(Box::new(TestScript::new_with_name("test_a")));
mut_set.add(Box::new(TestScript::new_with_name("test_b")));
mut_set.add(Box::new(TestScript::new_with_name("test_c")));
// Drop so we don't have a lock on it anymore.
drop(mut_set);
let scripts = vec![ScriptWrapper::from(&set)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
for i in 1..11 {
aggregator.reset();
while let Some(v) = aggregator.get_next() {
v.get().as_mut().unwrap().on_initialize(&[]);
}
let set = set.read();
let s = set.at(0).get_as::<TestScript>();
assert_eq!(s.test_count, i);
assert_eq!(s.name().str(), "test_a");
let s = set.at(1).get_as::<TestScript>();
assert_eq!(s.test_count, i);
assert_eq!(s.name().str(), "test_b");
let s = set.at(2).get_as::<TestScript>();
assert_eq!(s.test_count, i);
assert_eq!(s.name().str(), "test_c");
}
}
#[test]
fn script_aggregator_property_iterates_script_set_when_removing_last() {
let set = Arc::new(RwLock::new(ScriptSet::default()));
let mut mut_set = set.write();
mut_set.add(Box::new(TestScript::new_with_name("test_a")));
mut_set.add(Box::new(TestScript::new_with_name("test_b")));
mut_set.add(Box::new(TestScript::new_with_name("test_c")));
// Drop so we don't have a lock on it anymore.
drop(mut_set);
let scripts = vec![ScriptWrapper::from(&set)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_a"
);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_b"
);
let mut mut_set = set.write();
mut_set.remove(&StringKey::new("test_c"));
drop(mut_set);
assert!(aggregator.get_next().is_none());
}
#[test]
fn script_aggregator_property_iterates_script_set_when_removing_middle() {
let set = Arc::new(RwLock::new(ScriptSet::default()));
let mut mut_set = set.write();
mut_set.add(Box::new(TestScript::new_with_name("test_a")));
mut_set.add(Box::new(TestScript::new_with_name("test_b")));
mut_set.add(Box::new(TestScript::new_with_name("test_c")));
// Drop so we don't have a lock on it anymore.
drop(mut_set);
let scripts = vec![ScriptWrapper::from(&set)];
let mut aggregator = ScriptAggregator::new(&scripts as *const Vec<ScriptWrapper>);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"test_a"
);
let mut mut_set = set.write();
mut_set.remove(&StringKey::new("test_b"));
drop(mut_set);
assert_eq!(
aggregator
.get_next()
.unwrap()
.get()
.as_mut()
.unwrap()
.name()
.str(),
"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(),
};
let mut aggregator = source.get_script_iterator();
aggregator.reset();
assert!(aggregator.get_next().is_none());
aggregator.reset();
source.script.set(Box::new(TestScript::new()));
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();
source.script.set(Box::new(TestScript::new()));
assert!(aggregator.get_next().is_some());
aggregator.reset();
source.script.clear();
assert!(aggregator.get_next().is_none());
}
}