From f657a9c2056d4e5692ddbc19217b654277300c75 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Sat, 14 Jun 2025 04:29:52 +0800 Subject: Update CBindings --- core/bindings/lang/clang/Generate.toml | 11 -- core/bindings/lang/clang/build.rs | 67 +++++--- .../lang/clang/configures/nogamepads_data.toml | 17 ++ core/bindings/lang/clang/headers/nogamepads.h | 181 --------------------- core/bindings/lang/clang/headers/nogamepads_data.h | 181 +++++++++++++++++++++ 5 files changed, 245 insertions(+), 212 deletions(-) delete mode 100644 core/bindings/lang/clang/Generate.toml create mode 100644 core/bindings/lang/clang/configures/nogamepads_data.toml delete mode 100644 core/bindings/lang/clang/headers/nogamepads.h create mode 100644 core/bindings/lang/clang/headers/nogamepads_data.h (limited to 'core') diff --git a/core/bindings/lang/clang/Generate.toml b/core/bindings/lang/clang/Generate.toml deleted file mode 100644 index 08d8b00..0000000 --- a/core/bindings/lang/clang/Generate.toml +++ /dev/null @@ -1,11 +0,0 @@ -language = "C" -include_guard = "NOGAMEPADS_H" -header = "// Auto generated by cbindgen 0.29.0" -cpp_compat = true - -[parse] -parse_deps = true -include = ["*"] - -[fn] -prefix = "ngp_" \ No newline at end of file diff --git a/core/bindings/lang/clang/build.rs b/core/bindings/lang/clang/build.rs index c43ee7f..190501e 100644 --- a/core/bindings/lang/clang/build.rs +++ b/core/bindings/lang/clang/build.rs @@ -1,34 +1,61 @@ fn main() { - // Get the project root directory let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); let root_path = std::path::Path::new(&manifest_dir); - // Input configuration file path - let config_path = root_path.join("Generate.toml"); + // Input configuration directory path + let config_dir = root_path.join("configures"); // Output directory path (headers folder in the crate directory) let headers_dir = root_path.join("headers"); - // Create the headers directory (if it does not exist) - std::fs::create_dir_all(&headers_dir).expect("Failed to create headers directory"); - - // Output file path - let output_file = headers_dir.join("nogamepads.h"); + // Clear existing headers directory (if it exists) + if headers_dir.exists() { + std::fs::remove_dir_all(&headers_dir).expect("Failed to remove headers directory"); + } - // Load configuration from Generate.toml - let config = cbindgen::Config::from_file(&config_path) - .expect("Failed to parse Generate.toml configuration"); - - // Generate header file - cbindgen::Builder::new() - .with_crate(manifest_dir) - .with_config(config) - .generate() - .expect("Failed to generate C header file") - .write_to_file(&output_file); + // Create a new headers directory + std::fs::create_dir_all(&headers_dir).expect("Failed to create headers directory"); - println!("cargo:rerun-if-changed={}", config_path.display()); + // Find all TOML files in configures directory + let config_files = std::fs::read_dir(&config_dir) + .expect("Failed to read configures directory") + .filter_map(|entry| { + let entry = entry.expect("Failed to read directory entry"); + let path = entry.path(); + if path.is_file() && path.extension().map_or(false, |ext| ext == "toml") { + Some(path) + } else { + None + } + }); + + // Process each configuration file + for config_path in config_files { + // Extract filename without extension + let file_stem = config_path.file_stem() + .and_then(|s| s.to_str()) + .expect("Invalid filename") + .to_string(); + + // Set output file path + let output_file = headers_dir.join(format!("{}.h", file_stem)); + + // Load configuration from current TOML file + let config = cbindgen::Config::from_file(&config_path) + .expect(&format!("Failed to parse configuration: {}", config_path.display())); + + // Generate header file + cbindgen::Builder::new() + .with_crate(&manifest_dir) + .with_config(config) + .generate() + .expect(&format!("Failed to generate header for: {}", config_path.display())) + .write_to_file(&output_file); + } + + // Print rerun instructions + println!("cargo:rerun-if-changed={}", config_dir.display()); println!("cargo:rerun-if-changed=src/mycode.rs"); println!("cargo:rerun-if-changed=build.rs"); } \ No newline at end of file diff --git a/core/bindings/lang/clang/configures/nogamepads_data.toml b/core/bindings/lang/clang/configures/nogamepads_data.toml new file mode 100644 index 0000000..56d645d --- /dev/null +++ b/core/bindings/lang/clang/configures/nogamepads_data.toml @@ -0,0 +1,17 @@ +language = "C" +include_guard = "NOGAMEPADS_DATA_H" +header = "// Auto generated by cbindgen 0.29.0" +cpp_compat = true + +[parse] +parse_deps = true +include = [ + "string_converter", + + "ngpd_game_info", + "ngpd_message", + "ngpd_player" +] + +[fn] +prefix = "ngpd_data_" \ No newline at end of file diff --git a/core/bindings/lang/clang/headers/nogamepads.h b/core/bindings/lang/clang/headers/nogamepads.h deleted file mode 100644 index 3242a44..0000000 --- a/core/bindings/lang/clang/headers/nogamepads.h +++ /dev/null @@ -1,181 +0,0 @@ -// Auto generated by cbindgen 0.29.0 - -#ifndef NOGAMEPADS_H -#define NOGAMEPADS_H - -#include -#include -#include -#include - -typedef enum FfiConnectionMessageTag { - Join, - RequestGameInfos, - RequestLayoutConfigure, - RequestSkinPackage, - Ready, - Err, -} FfiConnectionMessageTag; - -typedef enum FfiConnectionResponseMessageTag { - GameInfos, - Deny, - Fail, - Ok, - Welcome, - Err, -} FfiConnectionResponseMessageTag; - -typedef enum FfiControlMessageTag { - Msg, - Pressed, - Released, - Axis, - Dir, - Exit, - Err, - End, -} FfiControlMessageTag; - -typedef enum FfiExitReason { - Exit, - GameOver, - ServerClosed, - YouAreKicked, - YouAreBanned, - Err, -} FfiExitReason; - -typedef enum FfiGameMessageTag { - EventTrigger, - Msg, - LetExit, - Err, - End, -} FfiGameMessageTag; - -typedef enum FfiJoinFailedMessage { - ContainIdenticalPlayer, - PlayerBanned, - GameLocked, - UnknownError, -} FfiJoinFailedMessage; - -typedef struct FfiAccount { - char *id; - char *player_hash; -} FfiAccount; - -typedef struct FfiCustomize { - char *nickname; - int color_hue; - double color_saturation; - double color_value; -} FfiCustomize; - -typedef struct FfiPlayer { - struct FfiAccount account; - struct FfiCustomize *customize; -} FfiPlayer; - -typedef struct FfiKeyAndAxis { - uint8_t key; - double axis; -} FfiKeyAndAxis; - -typedef struct FfiKeyAndDirection { - uint8_t key; - double x; - double y; -} FfiKeyAndDirection; - -typedef union FfiControlMessageUnion { - char *message; - uint8_t key; - struct FfiKeyAndAxis key_and_axis; - struct FfiKeyAndDirection key_and_direction; -} FfiControlMessageUnion; - -typedef struct FfiControlMessage { - enum FfiControlMessageTag tag; - union FfiControlMessageUnion data; -} FfiControlMessage; - -typedef union FfiGameMessageUnion { - uint8_t key; - char *message; - enum FfiExitReason exit_reason; -} FfiGameMessageUnion; - -typedef struct FfiGameMessage { - enum FfiGameMessageTag tag; - union FfiGameMessageUnion data; -} FfiGameMessage; - -typedef union FfiConnectionMessageUnion { - struct FfiPlayer player; -} FfiConnectionMessageUnion; - -typedef struct FfiConnectionMessage { - enum FfiConnectionMessageTag tag; - union FfiConnectionMessageUnion data; -} FfiConnectionMessage; - -typedef struct KeyValuePair { - char *key; - char *value; -} KeyValuePair; - -typedef struct FfiGameInfo { - struct KeyValuePair *data; - uintptr_t len; - uintptr_t cap; -} FfiGameInfo; - -typedef union FfiConnectionResponseMessageUnion { - struct FfiGameInfo game_info; - enum FfiJoinFailedMessage failed_message; -} FfiConnectionResponseMessageUnion; - -typedef struct FfiConnectionResponseMessage { - enum FfiConnectionResponseMessageTag tag; - union FfiConnectionResponseMessageUnion data; -} FfiConnectionResponseMessage; - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -ngp_ void free_c_string(char *ptr); - -ngp_ struct FfiPlayer *player_register(const char *id, const char *password); - -ngp_ bool player_check(const struct FfiPlayer *player, const char *password); - -ngp_ void player_set_nickname(struct FfiPlayer *player, const char *nickname); - -ngp_ void player_set_hue(struct FfiPlayer *player, int hue); - -ngp_ void player_set_hsv(struct FfiPlayer *player, int hue, double saturation, double value); - -ngp_ void player_free(struct FfiPlayer *player); - -ngp_ void free_control_message(struct FfiControlMessage msg); - -ngp_ void free_game_message(struct FfiGameMessage msg); - -ngp_ void free_exit_reason(enum FfiExitReason msg); - -ngp_ void free_connection_message(struct FfiConnectionMessage msg); - -ngp_ void free_connection_response_message(struct FfiConnectionResponseMessage msg); - -ngp_ void free_join_failed_message(enum FfiJoinFailedMessage msg); - -ngp_ void free_game_info(struct FfiGameInfo map); - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus - -#endif /* NOGAMEPADS_H */ diff --git a/core/bindings/lang/clang/headers/nogamepads_data.h b/core/bindings/lang/clang/headers/nogamepads_data.h new file mode 100644 index 0000000..6d9f1ad --- /dev/null +++ b/core/bindings/lang/clang/headers/nogamepads_data.h @@ -0,0 +1,181 @@ +// Auto generated by cbindgen 0.29.0 + +#ifndef NOGAMEPADS_DATA_H +#define NOGAMEPADS_DATA_H + +#include +#include +#include +#include + +typedef enum FfiConnectionMessageTag { + Join, + RequestGameInfos, + RequestLayoutConfigure, + RequestSkinPackage, + Ready, + Err, +} FfiConnectionMessageTag; + +typedef enum FfiConnectionResponseMessageTag { + GameInfos, + Deny, + Fail, + Ok, + Welcome, + Err, +} FfiConnectionResponseMessageTag; + +typedef enum FfiControlMessageTag { + Msg, + Pressed, + Released, + Axis, + Dir, + Exit, + Err, + End, +} FfiControlMessageTag; + +typedef enum FfiExitReason { + Exit, + GameOver, + ServerClosed, + YouAreKicked, + YouAreBanned, + Err, +} FfiExitReason; + +typedef enum FfiGameMessageTag { + EventTrigger, + Msg, + LetExit, + Err, + End, +} FfiGameMessageTag; + +typedef enum FfiJoinFailedMessage { + ContainIdenticalPlayer, + PlayerBanned, + GameLocked, + UnknownError, +} FfiJoinFailedMessage; + +typedef struct FfiAccount { + char *id; + char *player_hash; +} FfiAccount; + +typedef struct FfiCustomize { + char *nickname; + int color_hue; + double color_saturation; + double color_value; +} FfiCustomize; + +typedef struct FfiPlayer { + struct FfiAccount account; + struct FfiCustomize *customize; +} FfiPlayer; + +typedef struct FfiKeyAndAxis { + uint8_t key; + double axis; +} FfiKeyAndAxis; + +typedef struct FfiKeyAndDirection { + uint8_t key; + double x; + double y; +} FfiKeyAndDirection; + +typedef union FfiControlMessageUnion { + char *message; + uint8_t key; + struct FfiKeyAndAxis key_and_axis; + struct FfiKeyAndDirection key_and_direction; +} FfiControlMessageUnion; + +typedef struct FfiControlMessage { + enum FfiControlMessageTag tag; + union FfiControlMessageUnion data; +} FfiControlMessage; + +typedef union FfiGameMessageUnion { + uint8_t key; + char *message; + enum FfiExitReason exit_reason; +} FfiGameMessageUnion; + +typedef struct FfiGameMessage { + enum FfiGameMessageTag tag; + union FfiGameMessageUnion data; +} FfiGameMessage; + +typedef union FfiConnectionMessageUnion { + struct FfiPlayer player; +} FfiConnectionMessageUnion; + +typedef struct FfiConnectionMessage { + enum FfiConnectionMessageTag tag; + union FfiConnectionMessageUnion data; +} FfiConnectionMessage; + +typedef struct KeyValuePair { + char *key; + char *value; +} KeyValuePair; + +typedef struct FfiGameInfo { + struct KeyValuePair *data; + uintptr_t len; + uintptr_t cap; +} FfiGameInfo; + +typedef union FfiConnectionResponseMessageUnion { + struct FfiGameInfo game_info; + enum FfiJoinFailedMessage failed_message; +} FfiConnectionResponseMessageUnion; + +typedef struct FfiConnectionResponseMessage { + enum FfiConnectionResponseMessageTag tag; + union FfiConnectionResponseMessageUnion data; +} FfiConnectionResponseMessage; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +ngpd_data_ void free_c_string(char *ptr); + +ngpd_data_ struct FfiPlayer *player_register(const char *id, const char *password); + +ngpd_data_ bool player_check(const struct FfiPlayer *player, const char *password); + +ngpd_data_ void player_set_nickname(struct FfiPlayer *player, const char *nickname); + +ngpd_data_ void player_set_hue(struct FfiPlayer *player, int hue); + +ngpd_data_ void player_set_hsv(struct FfiPlayer *player, int hue, double saturation, double value); + +ngpd_data_ void player_free(struct FfiPlayer *player); + +ngpd_data_ void free_control_message(struct FfiControlMessage msg); + +ngpd_data_ void free_game_message(struct FfiGameMessage msg); + +ngpd_data_ void free_exit_reason(enum FfiExitReason msg); + +ngpd_data_ void free_connection_message(struct FfiConnectionMessage msg); + +ngpd_data_ void free_connection_response_message(struct FfiConnectionResponseMessage msg); + +ngpd_data_ void free_join_failed_message(enum FfiJoinFailedMessage msg); + +ngpd_data_ void free_game_info(struct FfiGameInfo map); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* NOGAMEPADS_DATA_H */ -- cgit