diff --git a/README.md b/README.md index a0e4571..2708e8d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Install on `Cargo.toml` as `build-dependencies` and set up `bindgen::Builder` on ```toml [build-dependencies] -csbindgen = "1.5.0" +csbindgen = "1.6.0" ``` ### Rust to C#. diff --git a/csbindgen-tests/src/lib.rs b/csbindgen-tests/src/lib.rs index 40baa59..99c4f58 100644 --- a/csbindgen-tests/src/lib.rs +++ b/csbindgen-tests/src/lib.rs @@ -73,6 +73,37 @@ mod lz4_ffi; // println!("{}", str); // } +#[allow(non_camel_case_types)] +pub type png_byte = ::std::os::raw::c_uchar; + +#[allow(non_camel_case_types)] +pub type JPH_ContactPoints = [u128; 65usize]; +#[allow(non_camel_case_types)] +pub type JPH_ContactPoints2 = [u128; 65]; + +#[repr(C)] +#[allow(unused)] +#[allow(non_snake_case)] +pub struct JPH_ContactManifold { + pub mPenetrationDepth: f32, + pub mWorldSpaceContactPointsOn1: JPH_ContactPoints, + pub mWorldSpaceContactPointsOn2: JPH_ContactPoints, + pub mWorldSpaceContactPointsOn3: JPH_ContactPoints2, + pub mWorldSpaceContactPointsOn4: [u128; 65], + pub mWorldSpaceContactPointsOn5: [u32; 65], + pub png_name: [png_byte; 5usize], +} + +#[no_mangle] +#[allow(unused)] +#[allow(non_snake_case)] +pub extern "C" fn JPH_PruneContactPoints( + ioContactPointsOn1: *mut JPH_ContactPoints, + ioContactPointsOn2: *mut JPH_ContactManifold, +) +{ + todo!(); +} /// my comment! diff --git a/csbindgen/Cargo.toml b/csbindgen/Cargo.toml index 800c0ba..176f4f8 100644 --- a/csbindgen/Cargo.toml +++ b/csbindgen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "csbindgen" -version = "1.5.0" +version = "1.6.0" edition = "2021" authors = [ "Yoshifumi Kawai ", diff --git a/csbindgen/src/alias_map.rs b/csbindgen/src/alias_map.rs index ae9479f..0c690a2 100644 --- a/csbindgen/src/alias_map.rs +++ b/csbindgen/src/alias_map.rs @@ -54,10 +54,6 @@ impl AliasMap { None => name.clone(), } } - - pub fn iter(&self) -> std::collections::hash_map::Iter { - self.type_aliases.iter() - } } #[cfg(test)] diff --git a/csbindgen/src/emitter.rs b/csbindgen/src/emitter.rs index c616375..4eb6e7a 100644 --- a/csbindgen/src/emitter.rs +++ b/csbindgen/src/emitter.rs @@ -224,8 +224,16 @@ pub fn emit_csharp( "".to_string() }; - structs_string - .push_str(format!(" {}public {} {}", attr, type_name, escape_name(field.name.as_str())).as_str()); + structs_string.push_str( + format!( + " {}public {} {}", + attr, + type_name, + escape_name(field.name.as_str()) + ) + .as_str(), + ); + if let TypeKind::FixedArray(digits, _) = &field.rust_type.type_kind { let mut digits = digits.clone(); if digits == "0" { @@ -233,7 +241,23 @@ pub fn emit_csharp( }; structs_string.push_str(format!("[{}]", digits).as_str()); + } else { + let alias_resolved_field = + match aliases.get_mapped_value(&field.rust_type.type_name) { + Some(x) => x, + None => field.rust_type.clone(), + }; + + if let TypeKind::FixedArray(digits, _) = &alias_resolved_field.type_kind { + let mut digits = digits.clone(); + if digits == "0" { + digits = "1".to_string(); // 0 fixed array is not allowed in C# + }; + + structs_string.push_str(format!("[{}]", digits).as_str()); + } } + structs_string.push_str_ln(";"); } structs_string.push_str_ln(" }"); diff --git a/csbindgen/src/type_meta.rs b/csbindgen/src/type_meta.rs index 394c546..8e83c8e 100644 --- a/csbindgen/src/type_meta.rs +++ b/csbindgen/src/type_meta.rs @@ -257,17 +257,28 @@ impl RustType { match &self.type_kind { TypeKind::FixedArray(_, _) => { - sb.push_str("fixed "); + if emit_from_struct { + sb.push_str("fixed "); - let type_name = type_csharp_string.as_str(); - let type_name = match type_name { + let type_name = type_csharp_string.as_str(); + let type_name = match type_name { // C# fixed allow types "bool" | "byte" | "short" | "int" | "long" | "char" | "sbyte" | "ushort" | "uint" | "ulong" | "float" | "double" => type_name.to_owned(), _ => format!("byte/* {}, this length is invalid so must keep pointer and can't edit from C# */", type_name) }; - sb.push_str(type_name.as_str()); + sb.push_str(type_name.as_str()); + } else { + let type_name = type_csharp_string.as_str(); + sb.push_str( + format!( + "void/* {}[] */", + type_name + ) + .as_str(), + ); + } } TypeKind::Function(parameters, return_type) => { if emit_from_struct && !options.csharp_use_function_pointer { diff --git a/dotnet-sandbox/NativeMethods.cs b/dotnet-sandbox/NativeMethods.cs index 79663f1..3be320d 100644 --- a/dotnet-sandbox/NativeMethods.cs +++ b/dotnet-sandbox/NativeMethods.cs @@ -12,6 +12,9 @@ namespace CsBindgen { const string __DllName = "csbindgen_tests"; + [DllImport(__DllName, EntryPoint = "JPH_PruneContactPoints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void JPH_PruneContactPoints(void/* UInt128[] */* ioContactPointsOn1, JPH_ContactManifold* ioContactPointsOn2); + /// my comment! [DllImport(__DllName, EntryPoint = "comment_one", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern void comment_one(); @@ -141,6 +144,18 @@ namespace CsBindgen } + [StructLayout(LayoutKind.Sequential)] + internal unsafe partial struct JPH_ContactManifold + { + public float mPenetrationDepth; + public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn1[65]; + public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn2[65]; + public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn3[65]; + public fixed byte/* UInt128, this length is invalid so must keep pointer and can't edit from C# */ mWorldSpaceContactPointsOn4[65]; + public fixed uint mWorldSpaceContactPointsOn5[65]; + public fixed byte png_name[5]; + } + [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct NfcCard {