Skip to content

Commit

Permalink
use mutex instead of Rc and Cell as callbacks might be called from on…
Browse files Browse the repository at this point in the history
…e another from CAPI component or omp server (related #6)
  • Loading branch information
Sreyas-Sreelal committed Nov 18, 2024
1 parent 75848ef commit a7ab30d
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 70 deletions.
4 changes: 2 additions & 2 deletions omp-gdk/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::events::Events;
use std::{cell::RefCell, rc::Rc};
use std::sync::{Arc, Mutex};

type OMPRSModule = Rc<RefCell<dyn Events + 'static>>;
type OMPRSModule = Arc<Mutex<dyn Events + 'static>>;

/// Runtime global object that implements all the callbacks and gamemode data
pub static mut Runtime: Option<Vec<Box<OMPRSModule>>> = None;
Expand Down
6 changes: 3 additions & 3 deletions omp-gdk/src/scripting/actors/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerGiveDamageActor(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_give_damage_actor(
script.lock().unwrap().on_player_give_damage_actor(
Player::new(*(*(*args).list).player),
Actor::new(*(*(*args).list).actor),
*(*(*args).list).amount,
Expand All @@ -40,7 +40,7 @@ pub struct OnActorStreamInArgs {
pub unsafe extern "C" fn OMPRS_OnActorStreamIn(args: *const EventArgs<OnActorStreamInArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_actor_stream_in(
script.lock().unwrap().on_actor_stream_in(
Actor::new(*(*(*args).list).actor),
Player::new(*(*(*args).list).forPlayer),
);
Expand All @@ -57,7 +57,7 @@ pub struct OnActorStreamOutArgs {
pub unsafe extern "C" fn OMPRS_OnActorStreamOut(args: *const EventArgs<OnActorStreamOutArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_actor_stream_out(
script.lock().unwrap().on_actor_stream_out(
Actor::new(*(*(*args).list).actor),
Player::new(*(*(*args).list).forPlayer),
);
Expand Down
12 changes: 8 additions & 4 deletions omp-gdk/src/scripting/checkpoints/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub unsafe extern "C" fn OMPRS_OnPlayerEnterCheckpoint(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_player_enter_checkpoint(Player::new(*(*(*args).list).player));
}
}
Expand All @@ -30,7 +31,8 @@ pub unsafe extern "C" fn OMPRS_OnPlayerLeaveCheckpoint(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_player_leave_checkpoint(Player::new(*(*(*args).list).player));
}
}
Expand All @@ -47,7 +49,8 @@ pub unsafe extern "C" fn OMPRS_OnPlayerEnterRaceCheckpoint(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_player_enter_race_checkpoint(Player::new(*(*(*args).list).player));
}
}
Expand All @@ -64,7 +67,8 @@ pub unsafe extern "C" fn OMPRS_OnPlayerLeaveRaceCheckpoint(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_player_leave_race_checkpoint(Player::new(*(*(*args).list).player));
}
}
2 changes: 1 addition & 1 deletion omp-gdk/src/scripting/classes/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerRequestClass(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
let mut ret = false;
for script in scripts.iter_mut() {
ret = script.borrow_mut().on_player_request_class(
ret = script.lock().unwrap().on_player_request_class(
Player::new(*(*(*args).list).player),
*(*(*args).list).classId,
);
Expand Down
6 changes: 3 additions & 3 deletions omp-gdk/src/scripting/core/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct OnTickArgs {
pub unsafe extern "C" fn OMPRS_OnTick(args: *const EventArgs<OnTickArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_tick(*(*(*args).list).elapsed);
script.lock().unwrap().on_tick(*(*(*args).list).elapsed);
}
}

Expand All @@ -25,7 +25,7 @@ pub unsafe extern "C" fn OMPRS_OnConsoleText(args: *const EventArgs<OnConsoleTex
let scripts = crate::runtime::Runtime.as_mut().unwrap();
let mut ret = false;
for script in scripts.iter_mut() {
ret = script.borrow_mut().on_console_text(
ret = script.lock().unwrap().on_console_text(
(*(*(*args).list).command).get_data(),
(*(*(*args).list).parameters).get_data(),
);
Expand All @@ -51,7 +51,7 @@ pub unsafe extern "C" fn OMPRS_OnRconLoginAttempt(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
let mut ret = false;
for script in scripts.iter_mut() {
ret = script.borrow_mut().on_rcon_login_attempt(
ret = script.lock().unwrap().on_rcon_login_attempt(
(*(*(*args).list).address).get_data(),
(*(*(*args).list).password).get_data(),
*(*(*args).list).success,
Expand Down
2 changes: 1 addition & 1 deletion omp-gdk/src/scripting/dialogs/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct OnDialogResponseArgs {
pub unsafe extern "C" fn OMPRS_OnDialogResponse(args: *const EventArgs<OnDialogResponseArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_dialog_response(
script.lock().unwrap().on_dialog_response(
Player::new(*(*(*args).list).player),
*(*(*args).list).dialogId,
transmute(*(*(*args).list).response),
Expand Down
6 changes: 3 additions & 3 deletions omp-gdk/src/scripting/gangzones/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerEnterGangZone(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_enter_gang_zone(
script.lock().unwrap().on_player_enter_gang_zone(
Player::new(*(*(*args).list).player),
GangZone::new(*(*(*args).list).zone),
);
Expand All @@ -34,7 +34,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerLeaveGangZone(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_leave_gang_zone(
script.lock().unwrap().on_player_leave_gang_zone(
Player::new(*(*(*args).list).player),
GangZone::new(*(*(*args).list).zone),
);
Expand All @@ -53,7 +53,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerClickGangZone(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_click_gang_zone(
script.lock().unwrap().on_player_click_gang_zone(
Player::new(*(*(*args).list).player),
GangZone::new(*(*(*args).list).zone),
);
Expand Down
5 changes: 3 additions & 2 deletions omp-gdk/src/scripting/menus/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerSelectedMenuRow(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_selected_menu_row(
script.lock().unwrap().on_player_selected_menu_row(
Player::new(*(*(*args).list).player),
*(*(*args).list).row,
);
Expand All @@ -30,7 +30,8 @@ pub unsafe extern "C" fn OMPRS_OnPlayerExitedMenu(args: *const EventArgs<OnPlaye
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_player_exited_menu(Player::new(*(*(*args).list).player));
}
}
5 changes: 3 additions & 2 deletions omp-gdk/src/scripting/models/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub unsafe extern "C" fn OMPRS_OnPlayerFinishedDownloading(
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_player_finished_downloading(Player::new(*(*(*args).list).player));
}
}
Expand All @@ -33,7 +34,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerRequestDownload(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_request_download(
script.lock().unwrap().on_player_request_download(
Player::new(*(*(*args).list).player),
transmute(*(*(*args).list).model_type),
*(*(*args).list).checksum,
Expand Down
11 changes: 6 additions & 5 deletions omp-gdk/src/scripting/objects/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub unsafe extern "C" fn OMPRS_OnObjectMove(args: *const EventArgs<OnObjectMoveA
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script
.borrow_mut()
.lock()
.unwrap()
.on_object_moved(Object::new(*(*(*args).list).object));
}
}
Expand All @@ -30,7 +31,7 @@ pub struct OnPlayerObjectMoveArgs {
pub unsafe extern "C" fn OMPRS_OnPlayerObjectMove(args: *const EventArgs<OnPlayerObjectMoveArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_object_moved(
script.lock().unwrap().on_player_object_moved(
Player::new(*(*(*args).list).player),
PlayerObject::new(
*(*(*args).list).object,
Expand All @@ -57,7 +58,7 @@ pub struct OnPlayerEditObjectArgs {
pub unsafe extern "C" fn OMPRS_OnPlayerEditObject(args: *const EventArgs<OnPlayerEditObjectArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_edit_object(
script.lock().unwrap().on_player_edit_object(
Player::new(*(*(*args).list).player),
Object::new(*(*(*args).list).object),
transmute(*(*(*args).list).response),
Expand Down Expand Up @@ -112,7 +113,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerEditAttachedObject(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_edit_attached_object(
script.lock().unwrap().on_player_edit_attached_object(
Player::new(*(*(*args).list).player),
*(*(*args).list).index,
*(*(*args).list).saved,
Expand Down Expand Up @@ -156,7 +157,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerSelectObject(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_select_object(
script.lock().unwrap().on_player_select_object(
Player::new(*(*(*args).list).player),
Object::new(*(*(*args).list).object),
*(*(*args).list).model,
Expand Down
2 changes: 1 addition & 1 deletion omp-gdk/src/scripting/pickups/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub unsafe extern "C" fn OMPRS_OnPlayerPickUpPickup(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.borrow_mut().on_player_pick_up_pickup(
script.lock().unwrap().on_player_pick_up_pickup(
Player::new(*(*(*args).list).player),
Pickup::new(*(*(*args).list).pickup),
);
Expand Down
Loading

0 comments on commit a7ab30d

Please sign in to comment.