2023-12-31 04:32:12 +01:00
|
|
|
use csflow::{memflow::Address, enums::PlayerType, structs::{GlobalVars, GameRules}, traits::MemoryClass};
|
2023-11-28 12:30:44 +01:00
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
|
pub enum CachedEntity {
|
|
|
|
|
Bomb {ptr: Address},
|
|
|
|
|
Player {ptr: Address, player_type: PlayerType},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Cache {
|
|
|
|
|
timestamp: std::time::Instant,
|
2023-12-31 04:32:12 +01:00
|
|
|
valid: bool,
|
2023-11-28 12:30:44 +01:00
|
|
|
entity_cache: Vec<CachedEntity>,
|
|
|
|
|
entity_list: Address,
|
2023-12-31 04:32:12 +01:00
|
|
|
globals: GlobalVars,
|
|
|
|
|
gamerules: GameRules,
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Cache {
|
|
|
|
|
pub fn is_valid(&self) -> bool {
|
2023-12-31 04:32:12 +01:00
|
|
|
if self.valid {
|
2024-01-02 01:08:25 +01:00
|
|
|
if self.timestamp.elapsed() > std::time::Duration::from_secs(10) {
|
2023-12-31 04:32:12 +01:00
|
|
|
log::info!("Invalidated cache! Reason: time");
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-11-28 12:30:44 +01:00
|
|
|
|
2023-12-31 04:32:12 +01:00
|
|
|
true
|
|
|
|
|
} else { false }
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_invalid() -> Cache {
|
|
|
|
|
Cache {
|
|
|
|
|
timestamp: std::time::Instant::now().checked_sub(std::time::Duration::from_millis(500)).unwrap(),
|
2023-12-31 04:32:12 +01:00
|
|
|
valid: false,
|
2023-11-28 12:30:44 +01:00
|
|
|
entity_cache: Vec::new(),
|
|
|
|
|
entity_list: Address::null(),
|
2023-12-31 04:32:12 +01:00
|
|
|
globals: GlobalVars::new(Address::null()),
|
|
|
|
|
gamerules: GameRules::new(Address::null()),
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-31 04:32:12 +01:00
|
|
|
pub fn invalidate(&mut self) {
|
|
|
|
|
self.valid = false;
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-31 04:32:12 +01:00
|
|
|
pub fn entity_cache(&mut self) -> Vec<CachedEntity> {
|
|
|
|
|
self.entity_cache.clone()
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn entity_list(&self) -> Address {
|
|
|
|
|
self.entity_list
|
|
|
|
|
}
|
2023-12-31 04:32:12 +01:00
|
|
|
|
|
|
|
|
pub fn globals(&self) -> GlobalVars {
|
|
|
|
|
self.globals
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn gamerules(&self) -> GameRules {
|
|
|
|
|
self.gamerules
|
|
|
|
|
}
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct CacheBuilder {
|
|
|
|
|
entity_cache: Option<Vec<CachedEntity>>,
|
2023-12-31 04:32:12 +01:00
|
|
|
entity_list: Option<Address>,
|
|
|
|
|
globals: Option<GlobalVars>,
|
|
|
|
|
gamerules: Option<GameRules>
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CacheBuilder {
|
|
|
|
|
pub fn new() -> CacheBuilder {
|
|
|
|
|
CacheBuilder {
|
|
|
|
|
entity_cache: None,
|
|
|
|
|
entity_list: None,
|
2023-12-31 04:32:12 +01:00
|
|
|
globals: None,
|
|
|
|
|
gamerules: None,
|
2023-11-28 12:30:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn entity_cache(mut self, entity_cache: Vec<CachedEntity>) -> CacheBuilder {
|
|
|
|
|
self.entity_cache = Some(entity_cache);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-31 04:32:12 +01:00
|
|
|
pub fn entity_list(mut self, entity_list: Address) -> CacheBuilder {
|
|
|
|
|
self.entity_list = Some(entity_list);
|
2023-11-28 12:30:44 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-31 04:32:12 +01:00
|
|
|
pub fn globals(mut self, globals: GlobalVars) -> CacheBuilder {
|
|
|
|
|
self.globals = Some(globals);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn gamerules(mut self, gamerules: GameRules) -> CacheBuilder {
|
|
|
|
|
self.gamerules = Some(gamerules);
|
2023-11-28 12:30:44 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn build(self) -> anyhow::Result<Cache> {
|
|
|
|
|
Ok(Cache {
|
|
|
|
|
timestamp: std::time::Instant::now(),
|
2023-12-31 04:32:12 +01:00
|
|
|
valid: true,
|
2023-11-28 12:30:44 +01:00
|
|
|
entity_cache: self.entity_cache.ok_or(anyhow::anyhow!("entity_cache not set on builder"))?,
|
|
|
|
|
entity_list: self.entity_list.ok_or(anyhow::anyhow!("entity_list not set on builder"))?,
|
2023-12-31 04:32:12 +01:00
|
|
|
globals: self.globals.ok_or(anyhow::anyhow!("globals not set on builder"))?,
|
|
|
|
|
gamerules: self.gamerules.ok_or(anyhow::anyhow!("gamerules not set on builder"))?,
|
2023-11-28 12:30:44 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|