diff --git a/README.md b/README.md index e3302d5..261f68f 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ using System.Runtime.InteropServices; namespace CsBindgen { - public static unsafe partial class NativeMethods + internal static unsafe partial class NativeMethods { const string __DllName = "liblz4"; @@ -126,7 +126,7 @@ using System.Runtime.InteropServices; namespace CsBindgen { - public static unsafe partial class NativeMethods + internal static unsafe partial class NativeMethods { const string __DllName = "nativelib"; @@ -148,6 +148,7 @@ csbindgen::Builder::default() .rust_file_header("use super::lz4;") .rust_method_type_path("lz4") .csharp_class_name("LibLz4") + .csharp_class_accessibility("public") .csharp_namespace("CsBindgen") .csharp_dll_name("csbindgen_tests") .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") @@ -182,7 +183,7 @@ using System.Runtime.InteropServices; namespace {csharp_namespace} { - public static unsafe partial class {csharp_class_name} + {csharp_class_accessibility} static unsafe partial class {csharp_class_name} { #if {csharp_dll_name_if(if_symbol,...)} const string __DllName = "{csharp_dll_name_if(...,if_dll_name)}"; @@ -207,7 +208,7 @@ Adjust `rust_file_header` and `rust_method_type_path` to match your module confi If the file path to be loaded needs to be changed depending on the operating system, the following load code can be used. ```csharp -public static unsafe partial class NativeMethods +internal static unsafe partial class NativeMethods { // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cross-platform // Library path will search diff --git a/csbindgen-tests/build.rs b/csbindgen-tests/build.rs index 708ed4c..3144313 100644 --- a/csbindgen-tests/build.rs +++ b/csbindgen-tests/build.rs @@ -52,6 +52,7 @@ fn main() { .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") .csharp_entry_point_prefix("csbindgen_") .csharp_method_prefix("") + .csharp_class_accessibility("public") .csharp_c_long_convert("int") .csharp_c_ulong_convert("uint") .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") diff --git a/csbindgen/Cargo.toml b/csbindgen/Cargo.toml index 0472631..6bb74f6 100644 --- a/csbindgen/Cargo.toml +++ b/csbindgen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "csbindgen" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = [ "Yoshifumi Kawai ", diff --git a/csbindgen/src/builder.rs b/csbindgen/src/builder.rs index e0b5c78..a22f61b 100644 --- a/csbindgen/src/builder.rs +++ b/csbindgen/src/builder.rs @@ -21,6 +21,7 @@ pub struct BindgenOptions { pub csharp_namespace: String, pub csharp_class_name: String, pub csharp_dll_name: String, + pub csharp_class_accessibility: String, pub csharp_entry_point_prefix: String, pub csharp_method_prefix: String, pub csharp_c_long_convert: String, @@ -44,6 +45,7 @@ impl Default for Builder { csharp_dll_name: "".to_string(), csharp_entry_point_prefix: "".to_string(), csharp_method_prefix: "".to_string(), + csharp_class_accessibility: "internal".to_string(), csharp_c_long_convert: "int".to_string(), csharp_c_ulong_convert: "uint".to_string(), csharp_if_symbol: "".to_string(), @@ -135,6 +137,13 @@ impl Builder { self } + /// configure C# class accessibility, default is internal + /// `{csharp_class_accessibility} static unsafe partial class NativeMethods` + pub fn csharp_class_accessibility>(mut self, csharp_class_accessibility: T) -> Builder { + self.options.csharp_class_accessibility = csharp_class_accessibility.into(); + self + } + /// configure c_long to {csharp_c_long_convert} type, /// default is `int`. pub fn csharp_c_long_convert>(mut self, csharp_c_long_convert: T) -> Builder { diff --git a/csbindgen/src/emitter.rs b/csbindgen/src/emitter.rs index aa2402f..8c7af83 100644 --- a/csbindgen/src/emitter.rs +++ b/csbindgen/src/emitter.rs @@ -87,6 +87,7 @@ pub fn emit_csharp( let namespace = &options.csharp_namespace; let class_name = &options.csharp_class_name; let method_prefix = &options.csharp_method_prefix; + let accessibility = &options.csharp_class_accessibility; let dll_name = match options.csharp_if_symbol.as_str() { "" => format!(" const string __DllName = \"{}\";", options.csharp_dll_name), @@ -176,7 +177,7 @@ using System.Runtime.InteropServices; namespace {namespace} {{ - public static unsafe partial class {class_name} + {accessibility} static unsafe partial class {class_name} {{ {dll_name} diff --git a/dotnet-sandbox/Program.cs b/dotnet-sandbox/Program.cs index 2aee533..4f681ab 100644 --- a/dotnet-sandbox/Program.cs +++ b/dotnet-sandbox/Program.cs @@ -11,7 +11,7 @@ unsafe var z = LibRust.my_add(100, 200); Console.WriteLine(z); - var s = LibLz4.LZ4_versionString(); + var s = CsBindgen.LibLz4.LZ4_versionString(); var ss = new string((sbyte*)s); Console.WriteLine(ss); @@ -33,7 +33,6 @@ unsafe } -namespace CsBindgen { public static unsafe partial class LibLz4 { diff --git a/dotnet-sandbox/method_call.cs b/dotnet-sandbox/method_call.cs index bb535a6..af21cb2 100644 --- a/dotnet-sandbox/method_call.cs +++ b/dotnet-sandbox/method_call.cs @@ -7,7 +7,7 @@ using System.Runtime.InteropServices; namespace CsBindgen { - public static unsafe partial class LibRust + internal static unsafe partial class LibRust { const string __DllName = "csbindgen_tests";