Skip to content

Commit

Permalink
keep the reference of runtime object instead of consuming, facilitati…
Browse files Browse the repository at this point in the history
…ng communication between registered modules
  • Loading branch information
Sreyas-Sreelal committed Aug 21, 2024
1 parent a816a0a commit 4e79491
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 69 deletions.
5 changes: 4 additions & 1 deletion omp-gdk/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::events::Events;
use std::sync::{Arc, Mutex};

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<dyn Events + 'static>>> = None;
pub static mut Runtime: Option<Vec<Box<OMPRSModule>>> = None;

#[doc(hidden)]
pub static mut __terminate_event_chain: bool = false;
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.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.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.on_actor_stream_out(
script.lock().unwrap().on_actor_stream_out(
Actor::new(*(*(*args).list).actor),
Player::new(*(*(*args).list).forPlayer),
);
Expand Down
20 changes: 16 additions & 4 deletions omp-gdk/src/scripting/checkpoints/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ pub unsafe extern "C" fn OMPRS_OnPlayerEnterCheckpoint(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_player_enter_checkpoint(Player::new(*(*(*args).list).player));
script
.lock()
.unwrap()
.on_player_enter_checkpoint(Player::new(*(*(*args).list).player));
}
}

Expand All @@ -27,7 +30,10 @@ pub unsafe extern "C" fn OMPRS_OnPlayerLeaveCheckpoint(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_player_leave_checkpoint(Player::new(*(*(*args).list).player));
script
.lock()
.unwrap()
.on_player_leave_checkpoint(Player::new(*(*(*args).list).player));
}
}

Expand All @@ -42,7 +48,10 @@ pub unsafe extern "C" fn OMPRS_OnPlayerEnterRaceCheckpoint(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_player_enter_race_checkpoint(Player::new(*(*(*args).list).player));
script
.lock()
.unwrap()
.on_player_enter_race_checkpoint(Player::new(*(*(*args).list).player));
}
}

Expand All @@ -57,6 +66,9 @@ pub unsafe extern "C" fn OMPRS_OnPlayerLeaveRaceCheckpoint(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_player_leave_race_checkpoint(Player::new(*(*(*args).list).player));
script
.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.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.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.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.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.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.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.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.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
7 changes: 5 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.on_player_selected_menu_row(
script.lock().unwrap().on_player_selected_menu_row(
Player::new(*(*(*args).list).player),
*(*(*args).list).row,
);
Expand All @@ -29,6 +29,9 @@ pub struct OnPlayerExitedMenuArgs {
pub unsafe extern "C" fn OMPRS_OnPlayerExitedMenu(args: *const EventArgs<OnPlayerExitedMenuArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_player_exited_menu(Player::new(*(*(*args).list).player));
script
.lock()
.unwrap()
.on_player_exited_menu(Player::new(*(*(*args).list).player));
}
}
7 changes: 5 additions & 2 deletions omp-gdk/src/scripting/models/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ pub unsafe extern "C" fn OMPRS_OnPlayerFinishedDownloading(
) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_player_finished_downloading(Player::new(*(*(*args).list).player));
script
.lock()
.unwrap()
.on_player_finished_downloading(Player::new(*(*(*args).list).player));
}
}

Expand All @@ -31,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.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
13 changes: 8 additions & 5 deletions omp-gdk/src/scripting/objects/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ pub struct OnObjectMoveArgs {
pub unsafe extern "C" fn OMPRS_OnObjectMove(args: *const EventArgs<OnObjectMoveArgs>) {
let scripts = crate::runtime::Runtime.as_mut().unwrap();
for script in scripts.iter_mut() {
script.on_object_moved(Object::new(*(*(*args).list).object));
script
.lock()
.unwrap()
.on_object_moved(Object::new(*(*(*args).list).object));
}
}

Expand All @@ -28,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.on_player_object_moved(
script.lock().unwrap().on_player_object_moved(
Player::new(*(*(*args).list).player),
PlayerObject::new(
*(*(*args).list).object,
Expand All @@ -55,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.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 @@ -110,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.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 @@ -154,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.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.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 4e79491

Please sign in to comment.