From c1c7d3af8479e2bb879bc9325da2952cf6db6493 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Sat, 14 Jun 2025 02:06:18 +0800 Subject: Move crate nogamepads-c-bindings to ./core/bindings/lang/clang --- core/bindings/c_lang/Cargo.toml | 12 -- core/bindings/c_lang/headers/ngpd_player.h | 39 ------- core/bindings/c_lang/src/data/mod.rs | 1 - core/bindings/c_lang/src/data/ngpd_player.rs | 139 ----------------------- core/bindings/c_lang/src/lib.rs | 1 - core/bindings/lang/clang/Cargo.toml | 12 ++ core/bindings/lang/clang/headers/ngpd_player.h | 39 +++++++ core/bindings/lang/clang/src/data/mod.rs | 1 + core/bindings/lang/clang/src/data/ngpd_player.rs | 139 +++++++++++++++++++++++ core/bindings/lang/clang/src/lib.rs | 1 + 10 files changed, 192 insertions(+), 192 deletions(-) delete mode 100644 core/bindings/c_lang/Cargo.toml delete mode 100644 core/bindings/c_lang/headers/ngpd_player.h delete mode 100644 core/bindings/c_lang/src/data/mod.rs delete mode 100644 core/bindings/c_lang/src/data/ngpd_player.rs delete mode 100644 core/bindings/c_lang/src/lib.rs create mode 100644 core/bindings/lang/clang/Cargo.toml create mode 100644 core/bindings/lang/clang/headers/ngpd_player.h create mode 100644 core/bindings/lang/clang/src/data/mod.rs create mode 100644 core/bindings/lang/clang/src/data/ngpd_player.rs create mode 100644 core/bindings/lang/clang/src/lib.rs (limited to 'core') diff --git a/core/bindings/c_lang/Cargo.toml b/core/bindings/c_lang/Cargo.toml deleted file mode 100644 index a0ee61f..0000000 --- a/core/bindings/c_lang/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "nogamepads-c-bindings" -edition = "2024" -version.workspace = true - -[dependencies] -nogamepads-core = { path = "../../../core" } -libc = "0.2.172" - -[lib] -name = "nogamepads_c" -crate-type = ["cdylib"] diff --git a/core/bindings/c_lang/headers/ngpd_player.h b/core/bindings/c_lang/headers/ngpd_player.h deleted file mode 100644 index 00fbe5b..0000000 --- a/core/bindings/c_lang/headers/ngpd_player.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef PLAYER_FFI_H -#define PLAYER_FFI_H - -#include -#include - -typedef struct FfiAccount { - char* id; - char* player_hash; -} FfiAccount; - -typedef struct FfiCustomize { - char* nickname; - int32_t color_hue; - double color_saturation; - double color_value; -} FfiCustomize; - -typedef struct FfiPlayer { - FfiAccount account; - FfiCustomize* customize; -} FfiPlayer; - -#ifdef __cplusplus -extern "C" { -#endif - -FfiPlayer* player_register(const char* id, const char* password); -bool player_check(const FfiPlayer* player, const char* password); -void player_set_nickname(FfiPlayer* player, const char* nickname); -void player_set_hue(FfiPlayer* player, int hue); -void player_set_hsv(FfiPlayer* player, int hue, double saturation, double value); -void player_free(FfiPlayer* player); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/core/bindings/c_lang/src/data/mod.rs b/core/bindings/c_lang/src/data/mod.rs deleted file mode 100644 index 32af6d8..0000000 --- a/core/bindings/c_lang/src/data/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod ngpd_player; \ No newline at end of file diff --git a/core/bindings/c_lang/src/data/ngpd_player.rs b/core/bindings/c_lang/src/data/ngpd_player.rs deleted file mode 100644 index 2ab615c..0000000 --- a/core/bindings/c_lang/src/data/ngpd_player.rs +++ /dev/null @@ -1,139 +0,0 @@ -use std::ffi::{CStr, CString}; -use std::os::raw::{c_char, c_double, c_int}; -use std::ptr; -use nogamepads_core::data::player::player_data::{Account, Customize, Player}; - -#[repr(C)] -pub struct FfiAccount { - id: *mut c_char, - player_hash: *mut c_char, -} - -#[repr(C)] -pub struct FfiCustomize { - nickname: *mut c_char, - color_hue: c_int, - color_saturation: c_double, - color_value: c_double, -} - -#[repr(C)] -pub struct FfiPlayer { - account: FfiAccount, - customize: *mut FfiCustomize, -} - -impl From for FfiAccount { - fn from(acc: Account) -> Self { - FfiAccount { - id: CString::new(acc.id).unwrap().into_raw(), - player_hash: CString::new(acc.player_hash).unwrap().into_raw(), - } - } -} - -impl From for FfiPlayer { - fn from(player: Player) -> Self { - let account = FfiAccount::from(player.account); - let customize = player.customize.map(|c| { - Box::into_raw(Box::new(FfiCustomize { - nickname: CString::new(c.nickname).unwrap().into_raw(), - color_hue: c.color_hue, - color_saturation: c.color_saturation, - color_value: c.color_value, - })) - }).unwrap_or(ptr::null_mut()); - - FfiPlayer { account, customize } - } -} - -impl TryFrom<&FfiPlayer> for Player { - type Error = (); - - fn try_from(ffi: &FfiPlayer) -> Result { - let account = Account { - id: unsafe { CStr::from_ptr(ffi.account.id) }.to_str().map_err(|_| ())?.to_owned(), - player_hash: unsafe { CStr::from_ptr(ffi.account.player_hash) }.to_str().map_err(|_| ())?.to_owned(), - }; - - let customize = if !ffi.customize.is_null() { - let c = unsafe { &*ffi.customize }; - Some(Customize { - nickname: unsafe { CStr::from_ptr(c.nickname) }.to_str().map_err(|_| ())?.to_owned(), - color_hue: c.color_hue, - color_saturation: c.color_saturation, - color_value: c.color_value, - }) - } else { - None - }; - - Ok(Player { account, customize }) - } -} - -#[unsafe(no_mangle)] -pub extern "C" fn player_register(id: *const c_char, password: *const c_char) -> *mut FfiPlayer { - let id_str = unsafe { CStr::from_ptr(id) }.to_string_lossy(); - let pass_str = unsafe { CStr::from_ptr(password) }.to_string_lossy(); - - let player = Player::register(id_str.into_owned(), pass_str.into_owned()); - Box::into_raw(Box::new(FfiPlayer::from(player))) -} - -#[unsafe(no_mangle)] -pub extern "C" fn player_check(player: *const FfiPlayer, password: *const c_char) -> bool { - let player = unsafe { &*player }; - let pass_str = unsafe { CStr::from_ptr(password) }.to_string_lossy(); - - Player::try_from(player) - .map(|p| p.check(pass_str.into_owned())) - .unwrap_or(false) -} - -#[unsafe(no_mangle)] -pub extern "C" fn player_set_nickname(player: *mut FfiPlayer, nickname: *const c_char) { - let nickname_str = unsafe { CStr::from_ptr(nickname) }.to_string_lossy().into_owned(); - let mut rust_player : Player = unsafe { (&*player).try_into().unwrap() }; - - rust_player.nickname(&nickname_str); - *unsafe { &mut *player } = FfiPlayer::from(rust_player); -} - -#[unsafe(no_mangle)] -pub extern "C" fn player_set_hue(player: *mut FfiPlayer, hue: c_int) { - let mut rust_player : Player = unsafe { (&*player).try_into().unwrap() }; - - rust_player.hue(hue); - *unsafe { &mut *player } = FfiPlayer::from(rust_player); -} - -#[unsafe(no_mangle)] -pub extern "C" fn player_set_hsv( - player: *mut FfiPlayer, hue: c_int, saturation: c_double, value: c_double -) { - let mut rust_player : Player = unsafe { (&*player).try_into().unwrap() }; - - rust_player.hsv(hue, saturation, value); - *unsafe { &mut *player } = FfiPlayer::from(rust_player); -} - -#[unsafe(no_mangle)] -pub extern "C" fn player_free(player: *mut FfiPlayer) { - if player.is_null() { return; } - - let ffi_player = unsafe { Box::from_raw(player) }; - - // Free account strings - unsafe { - let _ = CString::from_raw(ffi_player.account.id); - let _ = CString::from_raw(ffi_player.account.player_hash); - } - - // Free customize if present - if !ffi_player.customize.is_null() { - let ffi_custom = unsafe { Box::from_raw(ffi_player.customize) }; - unsafe { let _ = CString::from_raw(ffi_custom.nickname); }; - } -} \ No newline at end of file diff --git a/core/bindings/c_lang/src/lib.rs b/core/bindings/c_lang/src/lib.rs deleted file mode 100644 index 12e35bb..0000000 --- a/core/bindings/c_lang/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod data; \ No newline at end of file diff --git a/core/bindings/lang/clang/Cargo.toml b/core/bindings/lang/clang/Cargo.toml new file mode 100644 index 0000000..34d1fed --- /dev/null +++ b/core/bindings/lang/clang/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "nogamepads-c-bindings" +edition = "2024" +version.workspace = true + +[dependencies] +nogamepads-core = { path = "./../../../../core" } +libc = "0.2.172" + +[lib] +name = "nogamepads_c" +crate-type = ["cdylib"] diff --git a/core/bindings/lang/clang/headers/ngpd_player.h b/core/bindings/lang/clang/headers/ngpd_player.h new file mode 100644 index 0000000..00fbe5b --- /dev/null +++ b/core/bindings/lang/clang/headers/ngpd_player.h @@ -0,0 +1,39 @@ +#ifndef PLAYER_FFI_H +#define PLAYER_FFI_H + +#include +#include + +typedef struct FfiAccount { + char* id; + char* player_hash; +} FfiAccount; + +typedef struct FfiCustomize { + char* nickname; + int32_t color_hue; + double color_saturation; + double color_value; +} FfiCustomize; + +typedef struct FfiPlayer { + FfiAccount account; + FfiCustomize* customize; +} FfiPlayer; + +#ifdef __cplusplus +extern "C" { +#endif + +FfiPlayer* player_register(const char* id, const char* password); +bool player_check(const FfiPlayer* player, const char* password); +void player_set_nickname(FfiPlayer* player, const char* nickname); +void player_set_hue(FfiPlayer* player, int hue); +void player_set_hsv(FfiPlayer* player, int hue, double saturation, double value); +void player_free(FfiPlayer* player); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/core/bindings/lang/clang/src/data/mod.rs b/core/bindings/lang/clang/src/data/mod.rs new file mode 100644 index 0000000..32af6d8 --- /dev/null +++ b/core/bindings/lang/clang/src/data/mod.rs @@ -0,0 +1 @@ +pub mod ngpd_player; \ No newline at end of file diff --git a/core/bindings/lang/clang/src/data/ngpd_player.rs b/core/bindings/lang/clang/src/data/ngpd_player.rs new file mode 100644 index 0000000..2ab615c --- /dev/null +++ b/core/bindings/lang/clang/src/data/ngpd_player.rs @@ -0,0 +1,139 @@ +use std::ffi::{CStr, CString}; +use std::os::raw::{c_char, c_double, c_int}; +use std::ptr; +use nogamepads_core::data::player::player_data::{Account, Customize, Player}; + +#[repr(C)] +pub struct FfiAccount { + id: *mut c_char, + player_hash: *mut c_char, +} + +#[repr(C)] +pub struct FfiCustomize { + nickname: *mut c_char, + color_hue: c_int, + color_saturation: c_double, + color_value: c_double, +} + +#[repr(C)] +pub struct FfiPlayer { + account: FfiAccount, + customize: *mut FfiCustomize, +} + +impl From for FfiAccount { + fn from(acc: Account) -> Self { + FfiAccount { + id: CString::new(acc.id).unwrap().into_raw(), + player_hash: CString::new(acc.player_hash).unwrap().into_raw(), + } + } +} + +impl From for FfiPlayer { + fn from(player: Player) -> Self { + let account = FfiAccount::from(player.account); + let customize = player.customize.map(|c| { + Box::into_raw(Box::new(FfiCustomize { + nickname: CString::new(c.nickname).unwrap().into_raw(), + color_hue: c.color_hue, + color_saturation: c.color_saturation, + color_value: c.color_value, + })) + }).unwrap_or(ptr::null_mut()); + + FfiPlayer { account, customize } + } +} + +impl TryFrom<&FfiPlayer> for Player { + type Error = (); + + fn try_from(ffi: &FfiPlayer) -> Result { + let account = Account { + id: unsafe { CStr::from_ptr(ffi.account.id) }.to_str().map_err(|_| ())?.to_owned(), + player_hash: unsafe { CStr::from_ptr(ffi.account.player_hash) }.to_str().map_err(|_| ())?.to_owned(), + }; + + let customize = if !ffi.customize.is_null() { + let c = unsafe { &*ffi.customize }; + Some(Customize { + nickname: unsafe { CStr::from_ptr(c.nickname) }.to_str().map_err(|_| ())?.to_owned(), + color_hue: c.color_hue, + color_saturation: c.color_saturation, + color_value: c.color_value, + }) + } else { + None + }; + + Ok(Player { account, customize }) + } +} + +#[unsafe(no_mangle)] +pub extern "C" fn player_register(id: *const c_char, password: *const c_char) -> *mut FfiPlayer { + let id_str = unsafe { CStr::from_ptr(id) }.to_string_lossy(); + let pass_str = unsafe { CStr::from_ptr(password) }.to_string_lossy(); + + let player = Player::register(id_str.into_owned(), pass_str.into_owned()); + Box::into_raw(Box::new(FfiPlayer::from(player))) +} + +#[unsafe(no_mangle)] +pub extern "C" fn player_check(player: *const FfiPlayer, password: *const c_char) -> bool { + let player = unsafe { &*player }; + let pass_str = unsafe { CStr::from_ptr(password) }.to_string_lossy(); + + Player::try_from(player) + .map(|p| p.check(pass_str.into_owned())) + .unwrap_or(false) +} + +#[unsafe(no_mangle)] +pub extern "C" fn player_set_nickname(player: *mut FfiPlayer, nickname: *const c_char) { + let nickname_str = unsafe { CStr::from_ptr(nickname) }.to_string_lossy().into_owned(); + let mut rust_player : Player = unsafe { (&*player).try_into().unwrap() }; + + rust_player.nickname(&nickname_str); + *unsafe { &mut *player } = FfiPlayer::from(rust_player); +} + +#[unsafe(no_mangle)] +pub extern "C" fn player_set_hue(player: *mut FfiPlayer, hue: c_int) { + let mut rust_player : Player = unsafe { (&*player).try_into().unwrap() }; + + rust_player.hue(hue); + *unsafe { &mut *player } = FfiPlayer::from(rust_player); +} + +#[unsafe(no_mangle)] +pub extern "C" fn player_set_hsv( + player: *mut FfiPlayer, hue: c_int, saturation: c_double, value: c_double +) { + let mut rust_player : Player = unsafe { (&*player).try_into().unwrap() }; + + rust_player.hsv(hue, saturation, value); + *unsafe { &mut *player } = FfiPlayer::from(rust_player); +} + +#[unsafe(no_mangle)] +pub extern "C" fn player_free(player: *mut FfiPlayer) { + if player.is_null() { return; } + + let ffi_player = unsafe { Box::from_raw(player) }; + + // Free account strings + unsafe { + let _ = CString::from_raw(ffi_player.account.id); + let _ = CString::from_raw(ffi_player.account.player_hash); + } + + // Free customize if present + if !ffi_player.customize.is_null() { + let ffi_custom = unsafe { Box::from_raw(ffi_player.customize) }; + unsafe { let _ = CString::from_raw(ffi_custom.nickname); }; + } +} \ No newline at end of file diff --git a/core/bindings/lang/clang/src/lib.rs b/core/bindings/lang/clang/src/lib.rs new file mode 100644 index 0000000..12e35bb --- /dev/null +++ b/core/bindings/lang/clang/src/lib.rs @@ -0,0 +1 @@ +pub mod data; \ No newline at end of file -- cgit