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", "veloren-server-dynlib",
] ]
[[package]]
name = "veloren-server-agent-dyn"
version = "0.1.0"
dependencies = [
"veloren-server-agent",
]
[[package]] [[package]]
name = "veloren-server-cli" name = "veloren-server-cli"
version = "0.13.0" version = "0.13.0"

View File

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

View File

@ -40,6 +40,12 @@ use rand::{thread_rng, Rng};
use specs::Entity as EcsEntity; use specs::Entity as EcsEntity;
use vek::*; use vek::*;
#[cfg(feature = "use-dyn-lib")]
use {
crate::LIB,
std::ffi::CStr,
};
impl<'a> AgentData<'a> { impl<'a> AgentData<'a> {
//////////////////////////////////////// ////////////////////////////////////////
// Action Nodes // Action Nodes
@ -698,8 +704,51 @@ impl<'a> AgentData<'a> {
controller: &mut Controller, controller: &mut Controller,
tgt_data: &TargetData, tgt_data: &TargetData,
read_data: &ReadData, 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, rng: &mut impl Rng,
) { ) {
#[cfg(feature = "use-dyn-lib")]
let rng = &mut thread_rng();
let tool_tactic = |tool_kind| match tool_kind { let tool_tactic = |tool_kind| match tool_kind {
ToolKind::Bow => Tactic::Bow, ToolKind::Bow => Tactic::Bow,
ToolKind::Staff => Tactic::Staff, ToolKind::Staff => Tactic::Staff,

View File

@ -1,10 +1,6 @@
#[cfg(all(feature = "be-dyn-lib", feature = "use-dyn-lib"))] #[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"); 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 action_nodes;
pub mod attack; pub mod attack;
pub mod consts; pub mod consts;
@ -12,14 +8,11 @@ pub mod data;
pub mod util; pub mod util;
#[cfg(feature = "use-dyn-lib")] #[cfg(feature = "use-dyn-lib")]
use { use {lazy_static::lazy_static, server_dynlib::LoadedLib, std::sync::Arc, std::sync::Mutex};
lazy_static::lazy_static, server_dynlib::LoadedLib, std::ffi::CStr, std::sync::Arc,
std::sync::Mutex,
};
#[cfg(feature = "use-dyn-lib")] #[cfg(feature = "use-dyn-lib")]
lazy_static! { 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"); server_dynlib::init("veloren-server-agent", "veloren-server-agent-dyn", "agent");
} }