Dynamic library compiles, but still panics

This commit is contained in:
Sam 2022-09-15 22:34:44 -04:00
parent 525630c37a
commit ddb56bd560
4 changed files with 60 additions and 9 deletions

7
Cargo.lock generated
View File

@ -6829,6 +6829,13 @@ dependencies = [
"veloren-server-dynlib",
]
[[package]]
name = "veloren-server-agent-dyn"
version = "0.1.0"
dependencies = [
"veloren-server-agent",
]
[[package]]
name = "veloren-server-cli"
version = "0.13.0"

View File

@ -15,6 +15,8 @@ members = [
"plugin/derive",
"plugin/rt",
"server",
"server/agent",
"server/agent/dyn",
"server-cli",
"voxygen",
"voxygen/anim",

View File

@ -40,6 +40,12 @@ use rand::{thread_rng, Rng};
use specs::Entity as EcsEntity;
use vek::*;
#[cfg(feature = "use-dyn-lib")]
use {
crate::LIB,
std::ffi::CStr,
};
impl<'a> AgentData<'a> {
////////////////////////////////////////
// Action Nodes
@ -698,8 +704,51 @@ impl<'a> AgentData<'a> {
controller: &mut Controller,
tgt_data: &TargetData,
read_data: &ReadData,
#[cfg(not(feature = "use-dyn-lib"))]
rng: &mut impl Rng,
#[cfg(feature = "use-dyn-lib")]
_rng: &mut impl Rng,
) {
#[cfg(not(feature = "use-dyn-lib"))]
{
self.attack_inner(agent, controller, tgt_data, read_data, rng);
}
#[cfg(feature = "use-dyn-lib")]
{
let lock = LIB.lock().unwrap();
let lib = &lock.as_ref().unwrap().lib;
const ATTACK_FN: &[u8] = b"attack_inner\0";
let attack_fn: server_dynlib::Symbol<
fn(&Self, &mut Agent, &mut Controller, &TargetData, &ReadData),
> = unsafe { lib.get(ATTACK_FN) }.unwrap_or_else(|e| {
panic!(
"Trying to use: {} but had error: {:?}",
CStr::from_bytes_with_nul(ATTACK_FN)
.map(CStr::to_str)
.unwrap()
.unwrap(),
e
)
});
attack_fn(self, agent, controller, tgt_data, read_data);
}
}
#[cfg_attr(feature = "be-dyn-lib", export_name = "attack_inner")]
pub fn attack_inner(
&self,
agent: &mut Agent,
controller: &mut Controller,
tgt_data: &TargetData,
read_data: &ReadData,
#[cfg(not(feature = "use-dyn-lib"))]
rng: &mut impl Rng,
) {
#[cfg(feature = "use-dyn-lib")]
let rng = &mut thread_rng();
let tool_tactic = |tool_kind| match tool_kind {
ToolKind::Bow => Tactic::Bow,
ToolKind::Staff => Tactic::Staff,

View File

@ -1,10 +1,6 @@
#[cfg(all(feature = "be-dyn-lib", feature = "use-dyn-lib"))]
compile_error!("Can't use both \"be-dyn-lib\" and \"use-dyn-lib\" features at once");
#[cfg(all(target_os = "windows", feature = "be-dyn-lib"))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
pub mod action_nodes;
pub mod attack;
pub mod consts;
@ -12,14 +8,11 @@ pub mod data;
pub mod util;
#[cfg(feature = "use-dyn-lib")]
use {
lazy_static::lazy_static, server_dynlib::LoadedLib, std::ffi::CStr, std::sync::Arc,
std::sync::Mutex,
};
use {lazy_static::lazy_static, server_dynlib::LoadedLib, std::sync::Arc, std::sync::Mutex};
#[cfg(feature = "use-dyn-lib")]
lazy_static! {
static ref LIB: Arc<Mutex<Option<LoadedLib>>> =
pub static ref LIB: Arc<Mutex<Option<LoadedLib>>> =
server_dynlib::init("veloren-server-agent", "veloren-server-agent-dyn", "agent");
}