From bbe921590230e8b398e7623aea5a8a8e1d31db4e Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Thu, 27 Mar 2025 16:20:33 -0400 Subject: [PATCH] Add mlua for server-side scripting support This isn't hooked up to anything yet, just trying it out. --- Cargo.lock | 179 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + resources/scripts/test.lua | 3 + src/bin/kawari-world.rs | 24 ++++- src/config.rs | 4 + 5 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 resources/scripts/test.lua diff --git a/Cargo.lock b/Cargo.lock index d4cc2c2..b6166e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc" +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "axum" version = "0.8.1" @@ -125,6 +131,16 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +[[package]] +name = "bstr" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531a9155a481e2ee699d4f98f43c0ca4ff8ee1bfd55c31e9e98fb29d2b176fe0" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bytemuck" version = "1.22.0" @@ -164,12 +180,28 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "env_home" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" + [[package]] name = "equivalent" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "fallible-iterator" version = "0.3.0" @@ -388,6 +420,7 @@ dependencies = [ "bitflags 1.3.2", "md5", "minijinja", + "mlua", "physis", "rand", "rusqlite", @@ -431,6 +464,41 @@ dependencies = [ "zlib-rs", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "lua-src" +version = "547.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edaf29e3517b49b8b746701e5648ccb5785cde1c119062cbabbc5d5cd115e42" +dependencies = [ + "cc", +] + +[[package]] +name = "luajit-src" +version = "210.5.12+a4f56a4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a8e7962a5368d5f264d045a5a255e90f9aa3fc1941ae15a8d2940d42cac671" +dependencies = [ + "cc", + "which", +] + [[package]] name = "matchit" version = "0.8.4" @@ -484,6 +552,42 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "mlua" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3f763c1041eff92ffb5d7169968a327e1ed2ebfe425dac0ee5a35f29082534b" +dependencies = [ + "bstr", + "either", + "mlua-sys", + "num-traits", + "parking_lot", + "rustc-hash", +] + +[[package]] +name = "mlua-sys" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1901c1a635a22fe9250ffcc4fcc937c16b47c2e9e71adba8784af8bca1f69594" +dependencies = [ + "cc", + "cfg-if", + "lua-src", + "luajit-src", + "pkg-config", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.7" @@ -499,6 +603,29 @@ version = "1.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc" +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -598,6 +725,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" +dependencies = [ + "bitflags 2.9.0", +] + [[package]] name = "rusqlite" version = "0.34.0" @@ -618,6 +754,25 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags 2.9.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "rustversion" version = "1.0.20" @@ -630,6 +785,12 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.219" @@ -883,6 +1044,18 @@ dependencies = [ "wit-bindgen-rt", ] +[[package]] +name = "which" +version = "7.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2774c861e1f072b3aadc02f8ba886c26ad6321567ecc294c935434cad06f1283" +dependencies = [ + "either", + "env_home", + "rustix", + "winsafe", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -956,6 +1129,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" + [[package]] name = "wit-bindgen-rt" version = "0.39.0" diff --git a/Cargo.toml b/Cargo.toml index 673e2e4..27cf252 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,3 +84,6 @@ rusqlite = { version = "0.34", features = ["bundled"], default-features = false # needed for c-style bitflags # cannot upgrade to 2.0.0, breaking changes that aren't recoverable: https://github.com/bitflags/bitflags/issues/314 bitflags = { version = "1.3", default-features = false } + +# For server-side scripting +mlua = { version = "0.10", features = ["lua51", "vendored", "send"] } diff --git a/resources/scripts/test.lua b/resources/scripts/test.lua new file mode 100644 index 0000000..3ca23ee --- /dev/null +++ b/resources/scripts/test.lua @@ -0,0 +1,3 @@ +function onBeginLogin() + print("Hello, world!") +end diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 08249f0..58c8aa6 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use kawari::common::custom_ipc::{CustomIpcData, CustomIpcSegment, CustomIpcType}; use kawari::common::timestamp_secs; @@ -24,6 +24,7 @@ use kawari::world::{ }, }; use kawari::world::{PlayerData, WorldDatabase}; +use mlua::Lua; use physis::common::{Language, Platform}; use physis::gamedata::GameData; use tokio::io::AsyncReadExt; @@ -42,11 +43,23 @@ async fn main() { tracing::info!("World server started on {addr}"); let database = Arc::new(WorldDatabase::new()); + let lua = Arc::new(Mutex::new(Lua::new())); + + { + let lua = lua.lock().unwrap(); + lua.load( + std::fs::read(format!("{}/test.lua", &config.world.scripts_location)) + .expect("Failed to locate scripts directory!"), + ) + .exec() + .unwrap(); + } loop { let (socket, _) = listener.accept().await.unwrap(); let database = database.clone(); + let lua = lua.clone(); let state = PacketState { client_key: None, @@ -316,6 +329,15 @@ async fn main() { }) .await; } + + let lua = lua.lock().unwrap(); + lua.load( + r#" + onBeginLogin() + "#, + ) + .exec() + .unwrap(); } ClientZoneIpcData::FinishLoading { .. } => { tracing::info!( diff --git a/src/config.rs b/src/config.rs index c4071a4..a742d2b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -185,6 +185,9 @@ pub struct WorldConfig { pub listen_address: String, /// See the World Excel sheet. pub world_id: u16, + /// Location of the scripts directory. + /// Defaults to a sensible value if the project is self-built. + pub scripts_location: String, } impl Default for WorldConfig { @@ -193,6 +196,7 @@ impl Default for WorldConfig { port: 7100, listen_address: "127.0.0.1".to_string(), world_id: 63, // Gilgamesh + scripts_location: "resources/scripts".to_string(), } } }