Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dojo-bindgen): add namespace to unity bindgen #2155

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions crates/dojo-bindgen/src/plugins/unity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};

use async_trait::async_trait;
use cainome::parser::tokens::{Composite, CompositeType, Function, FunctionOutputKind, Token};
use dojo_world::contracts::naming;
use dojo_world::contracts::naming::{self, get_namespace_from_tag};

use crate::error::BindgenResult;
use crate::plugins::BuiltinPlugin;
Expand Down Expand Up @@ -178,35 +178,38 @@ public abstract record {}() : Enum {{",
// Token should be a model
// This will be formatted into a C# class inheriting from ModelInstance
// Fields are mapped using C# and unity SDK types
fn format_model(model: &Composite) -> String {
fn format_model(namespace: &str, model: &Composite) -> String {
let fields = model
.inners
.iter()
.map(|field| {
format!(
"[ModelField(\"{}\")]\n public {} {};",
"[ModelField(\"{}\")]\n public {} {};",
field.name,
UnityPlugin::map_type(&field.token),
field.name,
)
})
.collect::<Vec<String>>()
.join("\n\n ");
.join("\n\n ");

format!(
"
// Model definition for `{}` model
public class {} : ModelInstance {{
{}

// Start is called before the first frame update
void Start() {{
}}

// Update is called once per frame
void Update() {{
namespace {namespace} {{
// Model definition for `{}` model
public class {} : ModelInstance {{
{}

// Start is called before the first frame update
void Start() {{
}}

// Update is called once per frame
void Update() {{
}}
}}
}}

Comment on lines +181 to +212
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add validation for the namespace parameter.

The namespace parameter should be validated to ensure it is not empty or null.

fn format_model(namespace: &str, model: &Composite) -> String {
+    assert!(!namespace.is_empty(), "Namespace cannot be empty");
+    assert!(namespace.chars().all(|c| c.is_alphanumeric() || c == '_'), "Namespace can only contain alphanumeric characters and underscores");

    let fields = model
        .inners
        .iter()
        .map(|field| {
            format!(
                "[ModelField(\"{}\")]\n        public {} {};",
                field.name,
                UnityPlugin::map_type(&field.token),
                field.name,
            )
        })
        .collect::<Vec<String>>()
        .join("\n\n        ");

    format!(
        "
namespace {namespace} {{

    // Model definition for `{}` model
    public class {} : ModelInstance {{
        {}

        // Start is called before the first frame update
        void Start() {{
        }}
    
        // Update is called once per frame
        void Update() {{
        }}
    }}
}}

        ",
        model.type_path,
        model.type_name(),
        fields
    )
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn format_model(namespace: &str, model: &Composite) -> String {
let fields = model
.inners
.iter()
.map(|field| {
format!(
"[ModelField(\"{}\")]\n public {} {};",
"[ModelField(\"{}\")]\n public {} {};",
field.name,
UnityPlugin::map_type(&field.token),
field.name,
)
})
.collect::<Vec<String>>()
.join("\n\n ");
.join("\n\n ");
format!(
"
// Model definition for `{}` model
public class {} : ModelInstance {{
{}
// Start is called before the first frame update
void Start() {{
}}
// Update is called once per frame
void Update() {{
namespace {namespace} {{
// Model definition for `{}` model
public class {} : ModelInstance {{
{}
// Start is called before the first frame update
void Start() {{
}}
// Update is called once per frame
void Update() {{
}}
}}
}}
fn format_model(namespace: &str, model: &Composite) -> String {
assert!(!namespace.is_empty(), "Namespace cannot be empty");
assert!(namespace.chars().all(|c| c.is_alphanumeric() || c == '_'), "Namespace can only contain alphanumeric characters and underscores");
let fields = model
.inners
.iter()
.map(|field| {
format!(
"[ModelField(\"{}\")]\n public {} {};",
field.name,
UnityPlugin::map_type(&field.token),
field.name,
)
})
.collect::<Vec<String>>()
.join("\n\n ");
format!(
"
namespace {namespace} {{
// Model definition for `{}` model
public class {} : ModelInstance {{
{}
// Start is called before the first frame update
void Start() {{
}}
// Update is called once per frame
void Update() {{
}}
}}
}}
",
model.type_path,
model.type_name(),
fields
)
}

",
model.type_path,
model.type_name(),
Expand Down Expand Up @@ -255,7 +258,11 @@ public class {} : ModelInstance {{

out += "\n";

out += UnityPlugin::format_model(model_struct.expect("model struct not found")).as_str();
out += UnityPlugin::format_model(
&get_namespace_from_tag(&model.tag),
model_struct.expect("model struct not found"),
)
.as_str();

out
}
Expand Down
Loading