interoptopus icon indicating copy to clipboard operation
interoptopus copied to clipboard

Separating namespaces and generating the common one

Open fakkoweb opened this issue 7 months ago • 1 comments

I have followed the reference project and trying out the service pattern I am however having troubles understanding:

  • how you define a "namespace" (where you specify what Rust file code goes into which?)
  • how do you get to generate bindings for the "Common" or needed stuff

Here is my case: I have reused only the SimpleService from the reference project and then added it to the generation list. What happens is that I get the SimpleService generated, but don't get the common.cs being generated on the side, so it ends up with types like SliceMutu32 not being found in C#.

Here is my generation code, inspired from the backend_csharp_ui.rs for Unity.

    let namespace_mappings = NamespaceMappings::new("MyCompany.Unity")
        .add("common", "MyCompany.Unity.Common");

    let output_folder_str = to_folder.to_str().unwrap();

    println!("Generating folder: {}", output_folder_str);
    
    for namespace_id in from_inventory.namespaces() {
        
        let output_file_name = format!("UnityRustPlugin.{}.cs", namespace_id).replace("..", ".");
        let output_file_path = format!("{}/{}", output_folder_str, output_file_name);
        println!("Generating path: {}", output_file_path);
        
        let write_types = if namespace_id.is_empty() {
            WriteTypes::Namespace
        } else {
            WriteTypes::NamespaceAndInteroptopusGlobal
        };

        // Configure C# generation optimized for Unity
        let unity_bindings_config = Config {
            // Comment to put on top of the generated file
            file_header_comment: HEADER_COMMENT.to_string(),
            // Name of static class for Interop methods, e.g., `Interop`.
            class: "UnityRustPlugin".to_string(),
            // Name of static class for constants, e.g., `Interop`. If [None] then [Self.class] is used
            // class_constants: Option<String>,
            dll_name: "unityrustplugin".to_string(),
            // Maps which namespace id belongs into which FQN (e.g., "common" => "MyCompany.Common").
            namespace_mappings: namespace_mappings.clone(),
            // Namespace ID of _this_ namespace to write (default "").
            namespace_id: namespace_id.clone(),
            // Sets the visibility access modifiers for generated types.
            visibility_types: CSharpVisibility::ForceInternal,
            // Whether, say, a `x: [u8; 3]` should become 3 `x0: u8, ...` instead.
            // If this is not set, interop generation with arrays in structs will fail. This is a somewhat
            // open issue w.r.t Unity-sans-unsafe support and feedback would be greatly welcome!
            // Default: TRUE
            unroll_struct_arrays: true,
            // Which types to write.
            write_types: write_types,
            // If enabled bindings will use C# `unsafe` for increased performance; but will need to be enabled in C# project settings to work.
            use_unsafe: Unsafe::UnsafePlatformMemCpy,
            // Generate functions and field names matching C# conventions, instead of mapping them 1:1 with Rust.
            rename_symbols: false,
            // Also generate markers for easier debugging. Default: FALSE
            //debug: true,
            // If signatures that normally use arrays should instead use span and readonly span.
            // Requires use_unsafe, as pinning spans requires the fixed keyword in C#.
            param_slice_type: ParamSliceType::Array,
            ..Config::default()
        };

        Generator::new(unity_bindings_config, unityrustplugin::my_inventory())
            .add_overload_writer(DotNet::new())
            .add_overload_writer(Unity::new())
            .write_file(output_file_path).expect("Unable to generate bindings cs file.");
        }

From the logs I see that loop only runs once and there is no "common" namespace.

Thank you for your much needed input!

fakkoweb avatar Jul 22 '24 08:07 fakkoweb