Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
2c9ce46dd2
chore: Rework project structure
There likely won't be much need for multiple separate crates.
2023-05-26 23:42:01 +02:00
cec1cd8714
feat: Add global plugin singleton
To abstract the C API, this adds a global singleton that acts as the
main driver of the Rust-side implementation.
2023-05-26 23:27:44 +02:00
611f6038db
chore: Remove unused ignore files 2023-05-26 23:27:26 +02:00
138 changed files with 107 additions and 106 deletions

4
Cargo.lock generated
View file

@ -126,10 +126,6 @@ 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"

View file

@ -1,12 +1,18 @@
[workspace]
resolver = "2"
members = [
"lib/dt_p2p",
"lib/nat_traversal"
]
[package]
name = "dt_p2p"
version = "0.1.0"
edition = "2021"
[unstable]
build-std = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.144"
[build-dependencies]
bindgen = "0.65.1"
[lib]
crate-type = ["cdylib", "lib"]
# [profile.dev.package.backtrace]
# opt-level = 3

View file

@ -1,2 +0,0 @@
/target
/Cargo.lock

View file

@ -1,15 +0,0 @@
[package]
name = "dt_p2p"
version = "0.1.0"
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"]
[build-dependencies]
bindgen = "0.65.1"

View file

@ -1,53 +0,0 @@
mod stingray_sdk;
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()
}
}

View file

@ -1,2 +0,0 @@
/target
/Cargo.lock

View file

@ -1,8 +0,0 @@
[package]
name = "nat_traversal"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -1,14 +0,0 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

60
src/lib.rs Normal file
View file

@ -0,0 +1,60 @@
use std::ffi::{c_char, CString};
use std::sync::OnceLock;
mod plugin;
mod stingray_sdk;
use plugin::Plugin;
use stingray_sdk::{GetApiFunction, PluginApi, PluginApiID};
pub const PLUGIN_NAME: &str = "dt_p2p";
static PLUGIN: OnceLock<Plugin> = OnceLock::new();
#[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) {
let plugin = Plugin::new(get_engine_api);
plugin.setup_game();
PLUGIN
.set(plugin)
.expect("Failed to initalize global plugin object.");
}
#[no_mangle]
pub extern "C" fn shutdown_game() {
// Safety: The engine ensures that `setup_game` was called before this, where `PLUGIN` was
// initialized.
let plugin = unsafe { PLUGIN.get().unwrap_unchecked() };
plugin.shutdown_game();
}
#[no_mangle]
pub extern "C" fn update_game(dt: f32) {
// Safety: The engine ensures that `setup_game` was called before this, where `PLUGIN` was
// initialized.
let plugin = unsafe { PLUGIN.get().unwrap_unchecked() };
plugin.update_game(dt);
}
#[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),
update_game: Some(update_game),
shutdown_game: Some(shutdown_game),
..Default::default()
};
Box::into_raw(Box::new(api))
} else {
std::ptr::null_mut()
}
}

33
src/plugin.rs Normal file
View file

@ -0,0 +1,33 @@
use crate::stingray_sdk::{GetApiFunction, LoggingApi};
use crate::PLUGIN_NAME;
pub(crate) struct Plugin {
log: LoggingApi,
}
impl Plugin {
pub fn new(get_engine_api: GetApiFunction) -> Self {
let log = LoggingApi::get(get_engine_api);
Self { log }
}
pub fn setup_game(&self) {
self.log.info(
PLUGIN_NAME,
format!("[setup_game] Hello, world! This is {}!", PLUGIN_NAME),
);
}
pub fn shutdown_game(&self) {
self.log
.info(PLUGIN_NAME, format!("[shutdown_game] Goodbye, world!"));
}
pub fn update_game(&self, dt: f32) {}
}
impl std::fmt::Debug for Plugin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("PluginApi")
}
}

Some files were not shown because too many files have changed in this diff Show more