mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-07-22 04:06:44 +08:00
chore: establish development baseline
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "bat-ffi"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "staticlib"]
|
||||
|
||||
[dependencies]
|
||||
bat-cas-engine = { path = "../bat-cas-engine" }
|
||||
bat-assetbundle = { path = "../bat-assetbundle" }
|
||||
bat-patch = { path = "../bat-patch" }
|
||||
|
||||
anyhow.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
# FFI 绑定
|
||||
libc = "0.2"
|
||||
|
||||
[build-dependencies]
|
||||
cbindgen = "0.27"
|
||||
@@ -0,0 +1,50 @@
|
||||
//! # BAT FFI
|
||||
//!
|
||||
//! Go 和 Rust 之间的 FFI 绑定层
|
||||
//!
|
||||
//! 导出 C ABI 接口供 Go 通过 CGO 调用
|
||||
|
||||
#![warn(clippy::all)]
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::os::raw::c_char;
|
||||
|
||||
/// FFI 版本号
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
/// 获取版本号(C ABI)
|
||||
#[no_mangle]
|
||||
pub extern "C" fn bat_version() -> *const c_char {
|
||||
CString::new(VERSION).unwrap().into_raw()
|
||||
}
|
||||
|
||||
/// 释放 C 字符串内存
|
||||
///
|
||||
/// # Safety
|
||||
/// 调用者必须确保:
|
||||
/// - `s` 是通过 `bat_version` 返回的有效指针
|
||||
/// - `s` 只被释放一次
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn bat_free_string(s: *mut c_char) {
|
||||
if !s.is_null() {
|
||||
let _ = CString::from_raw(s);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::ffi::CStr;
|
||||
|
||||
#[test]
|
||||
fn test_ffi_version() {
|
||||
let version_ptr = bat_version();
|
||||
assert!(!version_ptr.is_null());
|
||||
|
||||
unsafe {
|
||||
let version_cstr = CStr::from_ptr(version_ptr);
|
||||
let version = version_cstr.to_str().unwrap();
|
||||
assert!(!version.is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user