1
0
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:
NG (Graham)
2026-02-28 10:51:12 -05:00
parent 5d4123026d
commit 3d2b7078e4
27 changed files with 468 additions and 54 deletions

View File

@@ -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),
})
}

View File

@@ -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
}
}

View 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()
}
}

View File

@@ -0,0 +1,5 @@
mod plugin;
pub use plugin::{ValidationResultCode, VehicleValidatorPlugin};
mod c_binding;
pub use c_binding::VehicleValidatorCPlugin;

View 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;
}