Skip to content

Commit

Permalink
cleanup and security, newer samp-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmisiak committed Feb 26, 2024
1 parent a75bea3 commit f3df1fb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 243 deletions.
206 changes: 9 additions & 197 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 20 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<StackedCallback, AmxError> {
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.");
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
Loading

0 comments on commit f3df1fb

Please sign in to comment.