diff --git a/Cargo.lock b/Cargo.lock index 8a3d7cf..cadff05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "approx" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94" - -[[package]] -name = "autocfg" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dde43e75fd43e8a1bf86103336bc699aa8d17ad1be60c76c0bdfd4828e19b78" -dependencies = [ - "autocfg 1.1.0", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -35,47 +20,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" -[[package]] -name = "cgmath" -version = "0.16.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64a4b57c8f4e3a2e9ac07e0f6abc9c24b6fc9e1b54c3478cfb598f3d0023e51c" -dependencies = [ - "approx", - "num-traits 0.1.43", - "rand", -] - -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits 0.2.6", - "time", - "winapi", -] - -[[package]] -name = "colored" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cdb90b60f2927f8d76139c72dbde7e10c3a2bc47c8594c9c7a66529f2687c03" -dependencies = [ - "lazy_static", - "winconsole", -] - [[package]] name = "fern" -version = "0.5.9" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e69ab0d5aca163e388c3a49d284fed6c3d0810700e77c5ae2756a50ec1a4daaa" +checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee" dependencies = [ - "chrono", "log", ] @@ -85,12 +35,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - [[package]] name = "hashbrown" version = "0.12.3" @@ -109,22 +53,10 @@ version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ - "autocfg 1.1.0", + "autocfg", "hashbrown", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "libc" -version = "0.2.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" - [[package]] name = "log" version = "0.4.6" @@ -134,31 +66,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "num-integer" -version = "0.1.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" -dependencies = [ - "autocfg 0.1.8", - "num-traits 0.2.6", -] - -[[package]] -name = "num-traits" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" -dependencies = [ - "num-traits 0.2.6", -] - -[[package]] -name = "num-traits" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" - [[package]] name = "once_cell" version = "1.19.0" @@ -171,7 +78,7 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7685ca4cc0b3ad748c22ce6803e23b55b9206ef7715b965ebeaf41639238fdc" dependencies = [ - "autocfg 1.1.0", + "autocfg", "indexmap", ] @@ -211,53 +118,10 @@ dependencies = [ "proc-macro2 1.0.78", ] -[[package]] -name = "rand" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" -dependencies = [ - "fuchsia-cprng", - "libc", - "rand_core 0.3.1", - "rdrand", - "winapi", -] - -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.0", -] - -[[package]] -name = "rand_core" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" - -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rgb" -version = "0.8.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f089652ca87f5a82a62935ec6172a534066c7b97be003cc8f702ee9a7a59c92" - [[package]] name = "samp" -version = "0.1.2" -source = "git+https://github.com/ZOTTCE/samp-rs.git?rev=2e9192713bf9a77ee04e0ebbcda37d078c9c47dd#2e9192713bf9a77ee04e0ebbcda37d078c9c47dd" +version = "0.1.3" +source = "git+https://github.com/bmisiak/samp-sdk.git?rev=2a509721bc442724922076a4ae79b137708ca738#2a509721bc442724922076a4ae79b137708ca738" dependencies = [ "fern", "samp-codegen", @@ -266,8 +130,8 @@ dependencies = [ [[package]] name = "samp-codegen" -version = "0.1.1" -source = "git+https://github.com/ZOTTCE/samp-rs.git?rev=2e9192713bf9a77ee04e0ebbcda37d078c9c47dd#2e9192713bf9a77ee04e0ebbcda37d078c9c47dd" +version = "0.1.2" +source = "git+https://github.com/bmisiak/samp-sdk.git?rev=2a509721bc442724922076a4ae79b137708ca738#2a509721bc442724922076a4ae79b137708ca738" dependencies = [ "proc-macro2 0.4.30", "quote 0.6.12", @@ -291,10 +155,9 @@ dependencies = [ [[package]] name = "samp-sdk" version = "0.9.2" -source = "git+https://github.com/ZOTTCE/samp-rs.git?rev=2e9192713bf9a77ee04e0ebbcda37d078c9c47dd#2e9192713bf9a77ee04e0ebbcda37d078c9c47dd" +source = "git+https://github.com/bmisiak/samp-sdk.git?rev=2a509721bc442724922076a4ae79b137708ca738#2a509721bc442724922076a4ae79b137708ca738" dependencies = [ "bitflags", - "colored", ] [[package]] @@ -346,17 +209,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi", - "winapi", -] - [[package]] name = "unicode-ident" version = "1.0.12" @@ -368,43 +220,3 @@ name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" - -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - -[[package]] -name = "winapi" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "winconsole" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ef84b96d10db72dd980056666d7f1e7663ce93d82fa33b63e71c966f4cf5032" -dependencies = [ - "cgmath", - "lazy_static", - "rgb", - "winapi", -] diff --git a/Cargo.toml b/Cargo.toml index b36c4c1..c3a0c71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,10 +11,10 @@ publish = false crate-type = ["cdylib"] [dependencies] -samp = { git = "https://github.com/ZOTTCE/samp-rs.git", rev = "2e9192713bf9a77ee04e0ebbcda37d078c9c47dd" } +samp = { git = "https://github.com/bmisiak/samp-sdk.git", rev = "2a509721bc442724922076a4ae79b137708ca738" } slab = "0.4.2" log = "0.4.6" -fern = "0.5.9" +fern = { version = "0.6", features = [] } priority-queue = "1.3.0" once_cell = "1.19.0" fnv = "1.0.7" diff --git a/src/lib.rs b/src/lib.rs index 763c0aa..4d6091a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ #![warn(clippy::pedantic)] -use amx_arguments::{StackedCallback, VariadicAmxArguments}; +use amx_arguments::VariadicAmxArguments; use log::{error, info}; -use samp::amx::{Amx, AmxIdent}; +use samp::amx::Amx; use samp::cell::AmxString; use samp::error::{AmxError, AmxResult}; use samp::plugin::SampPlugin; -use scheduling::reschedule_timer; +use scheduling::{reschedule_timer, NextDue}; use std::convert::TryFrom; use std::time::{Duration, Instant}; @@ -122,34 +122,6 @@ enum TimerError { Executing { source: AmxError, key: usize }, } -#[allow(clippy::inline_always)] -#[inline(always)] -pub(crate) fn trigger_due_timers() { - let now = Instant::now(); - - while let Some(due_timer) = next_timer_due_for_triggering(now) { - let key = due_timer.key; - match due_timer - .bump_schedule_and(now, get_stacked_callback) - .context(TriggeringSnafu { key }) - { - Ok(callback) => { - if let Err(err) = callback.execute().context(ExecutingSnafu { key }) { - error!("{err}"); - } - } - Err(err) => error!("{err}"), - } - } -} - -pub(crate) fn get_stacked_callback(timer: &Timer) -> Result { - let amx: &'static Amx = samp::amx::get(timer.amx_identifier).ok_or(AmxError::NotFound)?; - timer - .passed_arguments - .push_onto_amx_stack(amx, timer.amx_callback_index) -} - impl SampPlugin for PreciseTimers { fn on_load(&mut self) { info!("samp-precise-timers v3 (c) Brian Misiak loaded correctly."); @@ -158,7 +130,21 @@ impl SampPlugin for PreciseTimers { #[allow(clippy::inline_always)] #[inline(always)] fn process_tick(&mut self) { - trigger_due_timers(); + let now = Instant::now(); + + while let Some(due @ NextDue { key, .. }) = next_timer_due_for_triggering(now) { + match due + .bump_schedule_and(now, Timer::stack_callback) + .context(TriggeringSnafu { key }) + { + Ok(callback) => { + if let Err(err) = callback.execute().context(ExecutingSnafu { key }) { + error!("{err}"); + } + } + Err(err) => error!("{err}"), + } + } } fn on_amx_unload(&mut self, unloaded_amx: &Amx) { @@ -178,9 +164,9 @@ samp::initialize_plugin!( let samp_logprintf = samp::plugin::logger().level(log::LevelFilter::Info); let _ = fern::Dispatch::new() - .format(|callback, message, record| { + .format(|out, message, record| { let level = record.level(); - callback.finish(format_args!("samp-precise-timers {level}: {message}")); + out.finish(format_args!("samp-precise-timers {level}: {message}")); }) .chain(samp_logprintf) .apply(); diff --git a/src/scheduling.rs b/src/scheduling.rs index fddf856..0dfb326 100644 --- a/src/scheduling.rs +++ b/src/scheduling.rs @@ -105,12 +105,6 @@ pub(crate) fn remove_timers(predicate: impl Fn(&Timer) -> bool) { }); } -fn deschedule_next_due(next_due: &NextDue) -> Result<(), TriggeringError> { - let (popped_key, _) = QUEUE.with_borrow_mut(|q| q.pop().context(DeschedulingSnafu))?; - ensure!(popped_key == next_due.key, InconsistencySnafu); - Ok(()) -} - fn change_next_trigger(key: usize, next_trigger: Instant) -> Result<(), TriggeringError> { QUEUE.with(|q| { q.try_borrow_mut() @@ -162,13 +156,16 @@ impl NextDue { prep(timer).context(StackPushSnafu) }) } else { - deschedule_next_due(self)?; + let (descheduled, _) = QUEUE.with_borrow_mut(|q| q.pop().context(DeschedulingSnafu))?; + ensure!(descheduled == self.key, InconsistencySnafu); + let timer = TIMERS.with_borrow_mut(|t| t.remove(self.key)); prep(&timer).context(StackPushSnafu) } } } +#[inline] pub(crate) fn next_timer_due_for_triggering(now: Instant) -> Option { QUEUE.with_borrow(|q| match q.peek() { Some(( @@ -212,7 +209,7 @@ mod test { }, ); let next_due = next_timer_due_for_triggering(std::time::Instant::now()); - + assert!(next_due.is_some()); } } diff --git a/src/timer.rs b/src/timer.rs index ea50dc1..7cdf014 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,6 +1,10 @@ -use crate::amx_arguments::VariadicAmxArguments; +use crate::amx_arguments::{StackedCallback, VariadicAmxArguments}; -use samp::{amx::AmxIdent, consts::AmxExecIdx}; +use samp::{ + amx::{Amx, AmxIdent}, + consts::AmxExecIdx, + error::AmxError, +}; /// The Timer struct represents a single scheduled timer #[derive(Debug, Clone)] @@ -14,4 +18,10 @@ impl Timer { pub fn was_scheduled_by_amx(&self, amx: &samp::amx::Amx) -> bool { self.amx_identifier == AmxIdent::from(amx.amx().as_ptr()) } + + pub fn stack_callback(&self) -> Result { + let amx: &'static Amx = samp::amx::get(self.amx_identifier).ok_or(AmxError::NotFound)?; + self.passed_arguments + .push_onto_amx_stack(amx, self.amx_callback_index) + } }