mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add configurable team selection for #44
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
pub mod chat;
|
||||
pub mod vehicle_validation;
|
||||
pub mod team_selection;
|
||||
|
||||
pub trait Plugin: Send + Sync {
|
||||
fn self_check(&self) -> bool {
|
||||
|
||||
57
rc_plugins/src/team_selection/c_binding.rs
Normal file
57
rc_plugins/src/team_selection/c_binding.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
//! The foreign function interface implementation for assigning a team to a player entering a match in different shared objects/libraries.
|
||||
use std::ffi::{CString, c_char};
|
||||
|
||||
const SELECT_TEAM_SYMBOL_NAME: &[u8] = b"oj_rc_select_team";
|
||||
const SELECT_TEAM_SYMBOL_NAME_STR: &str = "oj_rc_select_team";
|
||||
|
||||
pub struct TeamSelectorCPlugin {
|
||||
dll: libloading::Library,
|
||||
pretty_name: String,
|
||||
}
|
||||
|
||||
impl TeamSelectorCPlugin {
|
||||
pub fn new(file: impl AsRef<std::path::Path>) -> Result<Self, libloading::Error> {
|
||||
let dll = unsafe { libloading::Library::new(file.as_ref()) }?;
|
||||
Ok(Self {
|
||||
dll,
|
||||
pretty_name: file.as_ref().display().to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl super::TeamSelector for TeamSelectorCPlugin {
|
||||
fn select_team(&self, game: &str, index: usize, user_id: Option<i32>, group: Option<String>) -> u8 {
|
||||
let func: libloading::Symbol<unsafe extern "C" fn(*const c_char, u64, *const i32, *const c_char) -> u8> = match unsafe { self.dll.get(SELECT_TEAM_SYMBOL_NAME) } {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to find symbol {} in library {}: {}", SELECT_TEAM_SYMBOL_NAME_STR, self.pretty_name, e);
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
let game_c = CString::new(game).unwrap_or_default();
|
||||
let index_c = index as u64;
|
||||
let user_id_c = if let Some(user_id) = &user_id {
|
||||
std::ptr::from_ref(user_id)
|
||||
} else {
|
||||
std::ptr::null()
|
||||
};
|
||||
let group_c = group.map(|group| CString::new(group).unwrap_or_default());
|
||||
unsafe {
|
||||
func(
|
||||
game_c.as_ptr(),
|
||||
index_c,
|
||||
user_id_c,
|
||||
group_c.map(|x| x.as_ptr())
|
||||
.unwrap_or(std::ptr::null()),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Plugin for TeamSelectorCPlugin {
|
||||
fn self_check(&self) -> bool {
|
||||
unsafe {
|
||||
self.dll.get::<unsafe extern "C" fn(*const c_char, u64, *const i32, *const c_char) -> u8>(SELECT_TEAM_SYMBOL_NAME)
|
||||
}.is_ok()
|
||||
}
|
||||
}
|
||||
5
rc_plugins/src/team_selection/mod.rs
Normal file
5
rc_plugins/src/team_selection/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod plugin;
|
||||
pub use plugin::TeamSelector;
|
||||
|
||||
mod c_binding;
|
||||
pub use c_binding::TeamSelectorCPlugin;
|
||||
3
rc_plugins/src/team_selection/plugin.rs
Normal file
3
rc_plugins/src/team_selection/plugin.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub trait TeamSelector: crate::Plugin {
|
||||
fn select_team(&self, game: &str, index: usize, user_id: Option<i32>, group: Option<String>) -> u8;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
//! The foreign function interface implementation for validation vehicles in different shared objects/libraries.
|
||||
//! The foreign function interface implementation for validating vehicles in different shared objects/libraries.
|
||||
//use std::ffi::{CString, c_char, CStr};
|
||||
|
||||
const VALIDATE_VEHICLE_SYMBOL_NAME: &[u8] = b"oj_rc_validate_vehicle";
|
||||
|
||||
Reference in New Issue
Block a user