Complete refactor of the FFI to use handles instead of pointers.
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,37 +1,36 @@
|
||||
use anyhow::Result;
|
||||
use std::fmt;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::sync::Arc;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use crate::defines::LevelInt;
|
||||
use crate::static_data::GrowthRate;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
use crate::StringKey;
|
||||
|
||||
/// A library to store all growth rates.
|
||||
pub trait GrowthRateLibrary: Debug + ValueIdentifiable {
|
||||
pub trait GrowthRateLibrary: Debug {
|
||||
/// Calculates the level for a given growth key name and a certain experience.
|
||||
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt>;
|
||||
/// Calculates the experience for a given growth key name and a certain level.
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
|
||||
/// Adds a new growth rate with a name and value.
|
||||
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
|
||||
fn add_growth_rate(&self, key: &StringKey, value: Arc<dyn GrowthRate>);
|
||||
}
|
||||
|
||||
/// A library to store all growth rates.
|
||||
pub struct GrowthRateLibraryImpl {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying data structure.
|
||||
growth_rates: HashMap<StringKey, Box<dyn GrowthRate>>,
|
||||
growth_rates: RwLock<HashMap<StringKey, Arc<dyn GrowthRate>>>,
|
||||
}
|
||||
|
||||
impl GrowthRateLibraryImpl {
|
||||
/// Instantiates a new growth rate library with a capacity.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
growth_rates: HashMap::with_capacity(capacity),
|
||||
growth_rates: RwLock::new(HashMap::with_capacity(capacity)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +40,7 @@ impl GrowthRateLibrary for GrowthRateLibraryImpl {
|
||||
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt> {
|
||||
Ok(self
|
||||
.growth_rates
|
||||
.read()
|
||||
.get(growth_rate)
|
||||
.ok_or_else(|| anyhow::anyhow!("No growth rate found with key {}", growth_rate.to_string()))?
|
||||
.calculate_level(experience))
|
||||
@@ -48,20 +48,15 @@ impl GrowthRateLibrary for GrowthRateLibraryImpl {
|
||||
/// Calculates the experience for a given growth key name and a certain level.
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32> {
|
||||
self.growth_rates
|
||||
.read()
|
||||
.get(growth_rate)
|
||||
.ok_or_else(|| anyhow::anyhow!("No growth rate found with key {}", growth_rate.to_string()))?
|
||||
.calculate_experience(level)
|
||||
}
|
||||
|
||||
/// Adds a new growth rate with a name and value.
|
||||
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>) {
|
||||
self.growth_rates.insert(key.clone(), value);
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for GrowthRateLibraryImpl {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
fn add_growth_rate(&self, key: &StringKey, value: Arc<dyn GrowthRate>) {
|
||||
self.growth_rates.write().insert(key.clone(), value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +82,7 @@ pub mod tests {
|
||||
let w = &mut lib;
|
||||
w.add_growth_rate(
|
||||
&"test_growthrate".into(),
|
||||
Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])),
|
||||
Arc::new(LookupGrowthRate::new(vec![0, 5, 10, 100])),
|
||||
);
|
||||
// Drops borrow as mut
|
||||
|
||||
@@ -100,12 +95,7 @@ pub mod tests {
|
||||
impl GrowthRateLibrary for GrowthRateLibrary {
|
||||
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt>;
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
|
||||
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
|
||||
}
|
||||
impl ValueIdentifiable for GrowthRateLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier{
|
||||
ValueIdentifier::new(0)
|
||||
}
|
||||
fn add_growth_rate(&self, key: &StringKey, value: Arc<dyn GrowthRate>);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user