mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement vehicle validation before match
This commit is contained in:
@@ -19,7 +19,7 @@ impl ChatCPlugin {
|
||||
let dll = unsafe { libloading::Library::new(file.as_ref()) }?;
|
||||
Ok(Self {
|
||||
dll,
|
||||
pretty_name: file.as_ref().to_string_lossy().to_string(),
|
||||
pretty_name: file.as_ref().display().to_string(),
|
||||
provider: std::sync::Mutex::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
pub mod chat;
|
||||
pub mod vehicle_validation;
|
||||
|
||||
pub trait Plugin: Send + Sync {}
|
||||
pub trait Plugin: Send + Sync {
|
||||
fn self_check(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
48
rc_plugins/src/vehicle_validation/c_binding.rs
Normal file
48
rc_plugins/src/vehicle_validation/c_binding.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
//! The foreign function interface implementation for validation vehicles in different shared objects/libraries.
|
||||
//use std::ffi::{CString, c_char, CStr};
|
||||
|
||||
const VALIDATE_VEHICLE_SYMBOL_NAME: &[u8] = b"oj_rc_validate_vehicle";
|
||||
const VALIDATE_VEHICLE_SYMBOL_NAME_STR: &str = "oj_rc_validate_vehicle";
|
||||
|
||||
pub struct VehicleValidatorCPlugin {
|
||||
dll: libloading::Library,
|
||||
pretty_name: String,
|
||||
}
|
||||
|
||||
impl VehicleValidatorCPlugin {
|
||||
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::VehicleValidatorPlugin for VehicleValidatorCPlugin {
|
||||
fn validate(&self, cube_data: &[u8], colour_data: &[u8]) -> super::ValidationResultCode {
|
||||
let func: libloading::Symbol<unsafe extern "C" fn(u32, *const u8, u32, *const u8) -> u8> = match unsafe { self.dll.get(VALIDATE_VEHICLE_SYMBOL_NAME) } {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to find symbol {} in library {}: {}", VALIDATE_VEHICLE_SYMBOL_NAME_STR, self.pretty_name, e);
|
||||
return super::ValidationResultCode::Invalid;
|
||||
}
|
||||
};
|
||||
let cubes_len = cube_data.len() as u32;
|
||||
let cubes = cube_data.as_ptr();
|
||||
let colour_len = colour_data.len() as u32;
|
||||
let colours = colour_data.as_ptr();
|
||||
let code = unsafe {
|
||||
func(cubes_len, cubes, colour_len, colours)
|
||||
};
|
||||
super::ValidationResultCode::from_u8(code)
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Plugin for VehicleValidatorCPlugin {
|
||||
fn self_check(&self) -> bool {
|
||||
unsafe {
|
||||
self.dll.get::<unsafe extern "C" fn(u32, *const u8, u32, *const u8) -> u8>(VALIDATE_VEHICLE_SYMBOL_NAME)
|
||||
}.is_ok()
|
||||
}
|
||||
}
|
||||
5
rc_plugins/src/vehicle_validation/mod.rs
Normal file
5
rc_plugins/src/vehicle_validation/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod plugin;
|
||||
pub use plugin::{ValidationResultCode, VehicleValidatorPlugin};
|
||||
|
||||
mod c_binding;
|
||||
pub use c_binding::VehicleValidatorCPlugin;
|
||||
25
rc_plugins/src/vehicle_validation/plugin.rs
Normal file
25
rc_plugins/src/vehicle_validation/plugin.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
#[repr(u8)]
|
||||
pub enum ValidationResultCode {
|
||||
Invalid = 0,
|
||||
Ok = 1,
|
||||
NoWeapon = 2,
|
||||
NoMovement = 3,
|
||||
Sanctioned = 4,
|
||||
}
|
||||
|
||||
impl ValidationResultCode {
|
||||
pub(super) fn from_u8(num: u8) -> Self {
|
||||
match num {
|
||||
0 => Self::Invalid,
|
||||
1 => Self::Ok,
|
||||
2 => Self::NoWeapon,
|
||||
3 => Self::NoMovement,
|
||||
4 => Self::Sanctioned,
|
||||
_ => Self::Invalid,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VehicleValidatorPlugin: crate::Plugin {
|
||||
fn validate(&self, cube_data: &[u8], colour_data: &[u8]) -> ValidationResultCode;
|
||||
}
|
||||
Reference in New Issue
Block a user