#![allow(non_snake_case)]
pub(crate) mod prelude {
pub(crate) use viable::vtable;
pub(crate) use crate::interface::common::PlayerInfo;
pub(crate) use std::os::raw::{
c_char, c_double, c_float, c_int, c_long, c_uchar, c_uint, c_ushort, c_void
};
pub(crate) use crate::userdata::Vector;
}
mod common;
mod cvar;
mod engine;
mod lua;
mod materials;
mod mdl;
mod net;
mod panel;
mod client;
pub use cvar::{CVar, ConVar};
pub use engine::{EngineClient, EngineServer};
pub use lua::{LuaInterface, LuaObject, LuaShared};
pub use materials::MaterialSystem;
pub use mdl::{MdlCache, MdlCacheNotify};
pub use net::{NetChannelInfo, NetChannel, NetChannelHandler, NetMessage, CNetChan};
pub use panel::Panel;
pub use client::Client;
use crate::try_cstr;
use libloading::{Library, Symbol};
use std::ffi::c_void;
pub type CreateInterfaceFn =
extern "system" fn(pName: *const i8, pReturnCode: *mut i32) -> *mut c_void;
pub unsafe fn get_interface_handle(file: &str) -> Result<CreateInterfaceFn, libloading::Error> {
let lib = Library::new(file)?;
let sym: Symbol<CreateInterfaceFn> = lib.get(b"CreateInterface\0".as_ref())?;
Ok(*sym)
}
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Libloading error: {0}")]
Libloading(#[from] libloading::Error),
#[error("Failed to convert interface to c string. {0}")]
BadCString(#[from] std::ffi::NulError),
#[error("Couldn't find factory of interface {0}!")]
FactoryNotFound(String),
#[error("Failure in CreateInterface of interface {1}; Code [{0}]")]
CreateInterface(i32, String),
#[error("Failed to get interface {0} as mutable")]
IFaceMut(String),
#[error("Failed to get object as mutable")]
AsMut
}
pub fn get_from_interface(iface: &str, factory: CreateInterfaceFn) -> Result<*mut (), Error> {
let mut status = 0;
let interface = try_cstr!(iface)?;
let result = factory(interface.as_ptr(), &mut status);
if status == 0 && !result.is_null() {
Ok(result as *mut ())
} else {
Err(Error::FactoryNotFound(iface.to_owned()))
}
}