Skip to content

Commit

Permalink
feat(dojo-bindgen): add namespace to unity bindgen (#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo authored Jul 8, 2024
1 parent 617fafb commit f9256ea
Showing 1 changed file with 22 additions and 15 deletions.
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() {{
}}
}}
}}
",
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

0 comments on commit f9256ea

Please sign in to comment.