diff --git a/Cargo.lock b/Cargo.lock index 133aabf2d7..5a4060e271 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index ad93cf13cf..eb182cfaaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ members = [ "plugin/derive", "plugin/rt", "server", + "server/agent", + "server/agent/dyn", "server-cli", "voxygen", "voxygen/anim", diff --git a/server/agent/src/action_nodes.rs b/server/agent/src/action_nodes.rs index d34f594440..d17195be4c 100644 --- a/server/agent/src/action_nodes.rs +++ b/server/agent/src/action_nodes.rs @@ -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, diff --git a/server/agent/src/lib.rs b/server/agent/src/lib.rs index 09e2702834..ba7375fc09 100644 --- a/server/agent/src/lib.rs +++ b/server/agent/src/lib.rs @@ -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>> = + pub static ref LIB: Arc>> = server_dynlib::init("veloren-server-agent", "veloren-server-agent-dyn", "agent"); }