From fa9ffd9002a77219d9c57a3608eb0bcf89664283 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 25 May 2023 23:39:52 +0200 Subject: [PATCH] feat: Implement basic plugin API --- Cargo.lock | 258 +++++++++++++++++++++++++++++++++ lib/dt_p2p/Cargo.toml | 1 + lib/dt_p2p/build.rs | 7 + lib/dt_p2p/src/lib.rs | 53 ++++++- lib/dt_p2p/src/stingray_sdk.rs | 78 +++++++++- 5 files changed, 393 insertions(+), 4 deletions(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f54786e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,258 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bindgen" +version = "0.65.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfdf7b466f9a4903edc73f95d6d2bcd5baf8ae620638762244d3f60143643cc5" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clang-sys" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "dt_p2p" +version = "0.1.0" +dependencies = [ + "bindgen", + "libc", +] + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + +[[package]] +name = "libc" +version = "0.2.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" + +[[package]] +name = "libloading" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" +dependencies = [ + "cfg-if", + "winapi", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nat_traversal" +version = "0.1.0" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + +[[package]] +name = "prettyplease" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b69d39aab54d069e7f2fe8cb970493e7834601ca2d8c65fd7bbd183578080d1" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81ca098a9821bd52d6b24fd8b10bd081f47d39c22778cafaa75a2857a62c6390" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "shlex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" + +[[package]] +name = "syn" +version = "2.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" + +[[package]] +name = "which" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +dependencies = [ + "either", + "libc", + "once_cell", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/lib/dt_p2p/Cargo.toml b/lib/dt_p2p/Cargo.toml index 97ff094..e3197e6 100644 --- a/lib/dt_p2p/Cargo.toml +++ b/lib/dt_p2p/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +libc = "0.2.144" [lib] crate-type = ["cdylib", "lib"] diff --git a/lib/dt_p2p/build.rs b/lib/dt_p2p/build.rs index 9cfda19..29e2022 100644 --- a/lib/dt_p2p/build.rs +++ b/lib/dt_p2p/build.rs @@ -10,6 +10,7 @@ fn main() { let bindings = bindgen::Builder::default() .header(HEADER_NAME) + .rustified_enum("PluginApiID") .clang_arg("-Istingray_sdk/") .parse_callbacks(Box::new(bindgen::CargoCallbacks)) .generate() @@ -19,4 +20,10 @@ fn main() { bindings .write_to_file(out_path.join("bindings.rs")) .expect("Couldn't write bindings!"); + + if cfg!(debug_assertions) { + bindings + .write_to_file(out_path.join("../../../bindings.rs")) + .expect("Couldn't write bindings to debug path"); + } } diff --git a/lib/dt_p2p/src/lib.rs b/lib/dt_p2p/src/lib.rs index 9ebdc78..148bb9f 100644 --- a/lib/dt_p2p/src/lib.rs +++ b/lib/dt_p2p/src/lib.rs @@ -1,6 +1,53 @@ mod stingray_sdk; -#[cfg(test)] -mod tests { - use super::*; +use std::ffi::c_char; +use std::ffi::CString; + +use stingray_sdk::GetApiFunction; +use stingray_sdk::PluginApi; +use stingray_sdk::PluginApiID; + +use crate::stingray_sdk::LoggingApi; + +const PLUGIN_NAME: &str = "dt_p2p"; + +#[no_mangle] +pub extern "C" fn get_name() -> *const c_char { + let s = CString::new(PLUGIN_NAME).expect("Failed to create CString from plugin name"); + s.as_ptr() +} + +#[no_mangle] +pub extern "C" fn setup_game(get_engine_api: GetApiFunction) { + println!("setup_game"); + + let log = LoggingApi::get(get_engine_api); + + log.info( + PLUGIN_NAME, + format!("Hello, world! This is {}!", PLUGIN_NAME), + ); +} + +#[no_mangle] +pub extern "C" fn shutdown_game() { + println!("shutdown_game"); + + // log.info(PLUGIN_NAME, format!("Goodbye, world!", PLUGIN_NAME)); +} + +#[no_mangle] +pub extern "C" fn get_plugin_api(id: PluginApiID) -> *mut PluginApi { + if id == PluginApiID::PLUGIN_API_ID { + let api = PluginApi { + get_name: Some(get_name), + setup_game: Some(setup_game), + shutdown_game: Some(shutdown_game), + ..Default::default() + }; + + Box::into_raw(Box::new(api)) + } else { + std::ptr::null_mut() + } } diff --git a/lib/dt_p2p/src/stingray_sdk.rs b/lib/dt_p2p/src/stingray_sdk.rs index b6ccf39..841c092 100644 --- a/lib/dt_p2p/src/stingray_sdk.rs +++ b/lib/dt_p2p/src/stingray_sdk.rs @@ -4,4 +4,80 @@ #![allow(clippy::type_complexity)] #![allow(unused)] -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +mod bindings { + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +} + +use std::ffi::CString; +use std::os::raw::c_char; +use std::os::raw::c_void; + +pub use bindings::GetApiFunction; +pub use bindings::PluginApi; +pub use bindings::PluginApiID; + +impl std::default::Default for PluginApi { + fn default() -> Self { + Self { + version: 65, + flags: 3, + fn_0: std::ptr::null_mut(), + fn_1: std::ptr::null_mut(), + fn_2: std::ptr::null_mut(), + fn_3: std::ptr::null_mut(), + fn_4: std::ptr::null_mut(), + fn_5: std::ptr::null_mut(), + setup_game: None, + update_game: None, + shutdown_game: None, + fn_9: std::ptr::null_mut(), + fn_10: std::ptr::null_mut(), + fn_11: std::ptr::null_mut(), + get_name: None, + } + } +} + +fn get_engine_api(f: GetApiFunction, id: PluginApiID) -> *mut c_void { + let f = if cfg!(debug_assertions) { + f.expect("'GetApiFunction' is always passed by the engine") + } else { + // `Option::unwrap` still generates several instructions in + // optimized code. + unsafe { f.unwrap_unchecked() } + }; + + unsafe { f(id as u32) } +} + +pub struct LoggingApi { + info: unsafe extern "C" fn(*const c_char, *const c_char), + warning: unsafe extern "C" fn(*const c_char, *const c_char), + error: unsafe extern "C" fn(*const c_char, *const c_char), +} + +impl LoggingApi { + pub fn get(f: GetApiFunction) -> Self { + let api = unsafe { + let api = get_engine_api(f, PluginApiID::LOGGING_API_ID); + api as *mut bindings::LoggingApi + }; + + unsafe { + Self { + info: (*api).info.unwrap_unchecked(), + warning: (*api).warning.unwrap_unchecked(), + error: (*api).error.unwrap_unchecked(), + } + } + } + + pub fn info(&self, system: impl Into>, message: impl Into>) { + let f = self.info; + let system = CString::new(system).expect("Invalid CString"); + let message = CString::new(message).expect("Invalid CString"); + unsafe { + f(system.as_ptr(), message.as_ptr()); + } + } +}