From 5762c2f53fb32d47b540981a981e9b9e3a57dc6a Mon Sep 17 00:00:00 2001 From: Karroffel Date: Sat, 4 Mar 2017 05:36:31 +0100 Subject: [PATCH] Added binding generator --- binding_generator/.gitignore | 3 + binding_generator/Cargo.lock | 88 ++++++++ binding_generator/Cargo.toml | 9 + binding_generator/src/main.rs | 191 ++++++++++++++++++ include/{godot => godot_cpp}/core/Array.h | 0 include/{godot => godot_cpp}/core/Basis.h | 0 include/{godot => godot_cpp}/core/Color.h | 2 +- include/{godot => godot_cpp}/core/CoreTypes.h | 0 include/{godot => godot_cpp}/core/Defs.h | 0 .../{godot => godot_cpp}/core/Dictionary.h | 0 include/{godot => godot_cpp}/core/Image.h | 0 .../{godot => godot_cpp}/core/InputEvent.h | 0 include/{godot => godot_cpp}/core/NodePath.h | 7 +- include/{godot => godot_cpp}/core/Plane.h | 0 .../{godot => godot_cpp}/core/PoolArrays.h | 0 include/{godot => godot_cpp}/core/Quat.h | 0 include/{godot => godot_cpp}/core/RID.h | 0 include/{godot => godot_cpp}/core/Rect2.h | 0 include/{godot => godot_cpp}/core/Rect3.h | 0 include/{godot => godot_cpp}/core/String.h | 0 include/{godot => godot_cpp}/core/Transform.h | 0 .../{godot => godot_cpp}/core/Transform2D.h | 0 include/{godot => godot_cpp}/core/Variant.h | 0 include/{godot => godot_cpp}/core/Vector2.h | 0 include/{godot => godot_cpp}/core/Vector3.h | 0 25 files changed, 298 insertions(+), 2 deletions(-) create mode 100644 binding_generator/.gitignore create mode 100644 binding_generator/Cargo.lock create mode 100644 binding_generator/Cargo.toml create mode 100644 binding_generator/src/main.rs rename include/{godot => godot_cpp}/core/Array.h (100%) rename include/{godot => godot_cpp}/core/Basis.h (100%) rename include/{godot => godot_cpp}/core/Color.h (99%) rename include/{godot => godot_cpp}/core/CoreTypes.h (100%) rename include/{godot => godot_cpp}/core/Defs.h (100%) rename include/{godot => godot_cpp}/core/Dictionary.h (100%) rename include/{godot => godot_cpp}/core/Image.h (100%) rename include/{godot => godot_cpp}/core/InputEvent.h (100%) rename include/{godot => godot_cpp}/core/NodePath.h (96%) rename include/{godot => godot_cpp}/core/Plane.h (100%) rename include/{godot => godot_cpp}/core/PoolArrays.h (100%) rename include/{godot => godot_cpp}/core/Quat.h (100%) rename include/{godot => godot_cpp}/core/RID.h (100%) rename include/{godot => godot_cpp}/core/Rect2.h (100%) rename include/{godot => godot_cpp}/core/Rect3.h (100%) rename include/{godot => godot_cpp}/core/String.h (100%) rename include/{godot => godot_cpp}/core/Transform.h (100%) rename include/{godot => godot_cpp}/core/Transform2D.h (100%) rename include/{godot => godot_cpp}/core/Variant.h (100%) rename include/{godot => godot_cpp}/core/Vector2.h (100%) rename include/{godot => godot_cpp}/core/Vector3.h (100%) diff --git a/binding_generator/.gitignore b/binding_generator/.gitignore new file mode 100644 index 0000000..d9e3259 --- /dev/null +++ b/binding_generator/.gitignore @@ -0,0 +1,3 @@ +target +.idea/ +*.iml diff --git a/binding_generator/Cargo.lock b/binding_generator/Cargo.lock new file mode 100644 index 0000000..28942e0 --- /dev/null +++ b/binding_generator/Cargo.lock @@ -0,0 +1,88 @@ +[root] +name = "binding_generator" +version = "0.1.0" +dependencies = [ + "serde 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "dtoa" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "itoa" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num-traits" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "quote" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_codegen_internals" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen_internals 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dtoa 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum dtoa 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5edd69c67b2f8e0911629b7e6b8a34cb3956613cd7c6e6414966dee349c2db4f" +"checksum itoa 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "91fd9dc2c587067de817fec4ad355e3818c3d893a78cab32a0a474c7a15bb8d5" +"checksum num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "a16a42856a256b39c6d3484f097f6713e14feacd9bfb02290917904fae46c81c" +"checksum quote 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e7b44fd83db28b83c1c58187159934906e5e955c812e211df413b76b03c909a5" +"checksum serde 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0ae9a3c8b07c09dbe43022486d55a18c629a0618d2241e49829aaef9b6d862f9" +"checksum serde_codegen_internals 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c3172bf2940b975c0e4f6ab42a511c0a4407d4f46ccef87a9d3615db5c26fa96" +"checksum serde_derive 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ecc6e0379ca933ece58302d2d3034443f06fbf38fd535857c1dc516195cbc3bf" +"checksum serde_json 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "cf37ce931677e98b4fa5e6469aaa3ab4b6228309ea33b1b22d3ec055adfc4515" +"checksum syn 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f94368aae82bb29656c98443a7026ca931a659e8d19dcdc41d6e273054e820" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" diff --git a/binding_generator/Cargo.toml b/binding_generator/Cargo.toml new file mode 100644 index 0000000..9c6a407 --- /dev/null +++ b/binding_generator/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "binding_generator" +version = "0.1.0" +authors = ["karroffel"] + +[dependencies] +serde = "*" +serde_json = "*" +serde_derive = "*" diff --git a/binding_generator/src/main.rs b/binding_generator/src/main.rs new file mode 100644 index 0000000..f7007c2 --- /dev/null +++ b/binding_generator/src/main.rs @@ -0,0 +1,191 @@ +extern crate serde_json; +#[macro_use] +extern crate serde_derive; + + +use serde_json::*; +use std::fs::File; +use std::iter::Iterator; +use std::io::prelude::*; + +#[derive(Deserialize)] +struct GodotClass { + name: String, + base_class: String, + api_type: String, + singleton: bool, + instanciable: bool, + constants: Map, + methods: Vec +} + +#[derive(Deserialize)] +struct GodotMethod { + name: String, + return_type: String, + arguments: Vec +} + +#[derive(Deserialize)] +struct GodotArgument { + name: String, + #[serde(rename = "type")] + _type: String, + default_value: String +} + +fn strip_name(s: &String) -> &str { + if s.starts_with("_") { + unsafe { s.slice_unchecked(1, s.len()) } + } else { + s.as_str() + } +} + +fn main() { + let base_dir = "/home/karroffel/Gamedev/dlscript_cpp_example/src/include/godot_cpp/"; + + let mut file = File::open("/home/karroffel/Development/api.json").unwrap(); + + let mut file_contents = String::new(); + + file.read_to_string(&mut file_contents); + + let json: Vec = serde_json::from_str::>(&file_contents).unwrap(); + + for class in json { + generate_class_binding((base_dir.to_string() + strip_name(&class.name) + ".h").as_str(), &class); + } +} + +fn generate_class_binding(filename: &str, class: &GodotClass) { + let mut file = File::create(filename).unwrap(); + + file.write_all(generate_class_content(class).as_bytes()); +} + +fn generate_class_content(class: &GodotClass) -> String { + let mut contents = String::new(); + + contents = contents + "#ifndef "; + contents = contents + strip_name(&class.name).to_string().to_uppercase().as_str() + "_H\n"; + + contents = contents + "#define "; + contents = contents + strip_name(&class.name).to_string().to_uppercase().as_str() + "_H\n\n"; + + contents = contents + "\n#include \"core/CoreTypes.h\"\n"; + + contents = contents + "\n#include \n\n\n"; + + if class.base_class != "" { + contents = contents + "\n#include \"" + strip_name(&class.base_class) + ".h\"\n\n\n"; + } + + contents = contents + "namespace godot {\n\n"; + + contents = contents + "class " + strip_name(&class.name); + + if class.base_class != "" { + contents = contents + " : public " + strip_name(&class.base_class); + } + + contents = contents + " {\n"; + + if class.base_class == "" { + contents = contents + "protected:\n\tgodot_object *__core_object;\n\n"; + } + + contents = contents + "public:\n\n"; + + // default constructor + + { + contents = contents + "\t" + strip_name(&class.name) + "() {\n"; + contents = contents + "\t\t\n"; + contents = contents + "\t}\n"; + } + + + // pointer constructor + { + contents = contents + "\t" + strip_name(&class.name) + "(godot_object *ptr) {\n"; + contents = contents + "\t\t__core_object = ptr;\n"; + contents = contents + "\t}\n\n\n"; + } + + for (name, value) in &class.constants { + contents = contents + "\tconst static int " + name.as_str() + " = " + value.as_i64().unwrap().to_string().as_str() + ";\n"; + } + + contents += "\n\n"; + + for method in &class.methods { + contents = contents + "\t" + method.return_type.as_str() + " " + method.name.as_str() + "("; + + for (i, argument) in (&method.arguments).iter().enumerate() { + if !is_primitive(&argument._type) { + contents += "const " + } + contents = contents + argument._type.as_str(); + if !is_primitive(&argument._type) { + contents += "&"; + } + contents = contents + " " + argument.name.as_str(); + if i != method.arguments.len() - 1 { + contents += ", "; + } + } + + contents = contents + ") {\n"; + + contents = contents + "\t\tstatic godot_method_bind *mb = NULL;\n" + + "\t\tif (mb == NULL) {\n" + + "\t\t\tmb = godot_method_bind_get_method(\"" + class.name.as_str() + "\", \"" + method.name.as_str() + "\");\n" + + "\t\t}\n"; + + if method.return_type != "void" { + contents = contents + "\t\t" + method.return_type.as_str() + " ret;" + "\n"; + } + + contents = contents + "\t\tconst void *args[] = {\n"; + + for argument in &method.arguments { + contents = contents + "\t\t\t"; + if is_primitive(&argument._type) { + contents = contents + "&" + argument.name.as_str(); + } else { + contents = contents + "(void *) &" + argument.name.as_str(); + } + contents = contents + ",\n"; + } + + contents = contents + "\t\t};\n"; + + contents = contents + "\t\tgodot_method_bind_ptrcall(mb, __core_object, args, " + if method.return_type == "void" { "NULL" } else { "&ret" } + ");\n"; + + if method.return_type != "void" { + contents = contents + "\t\treturn ret;\n"; + } + + contents = contents + "\t}\n\n"; + } + + contents = contents + "};\n\n"; + + contents = contents + "}\n"; + + contents = contents + "#endif\n"; + + contents +} + + +fn is_core_type(name: &String) -> bool { + let core_types = vec!("Vector2", "Vector3", "String", "Variant"); + core_types.contains(&name.as_str()) +} + +fn is_primitive(name: &String) -> bool { + let core_types = vec!("int", "bool", "real", "float"); + core_types.contains(&name.as_str()) +} \ No newline at end of file diff --git a/include/godot/core/Array.h b/include/godot_cpp/core/Array.h similarity index 100% rename from include/godot/core/Array.h rename to include/godot_cpp/core/Array.h diff --git a/include/godot/core/Basis.h b/include/godot_cpp/core/Basis.h similarity index 100% rename from include/godot/core/Basis.h rename to include/godot_cpp/core/Basis.h diff --git a/include/godot/core/Color.h b/include/godot_cpp/core/Color.h similarity index 99% rename from include/godot/core/Color.h rename to include/godot_cpp/core/Color.h index 9e7b251..d74c227 100644 --- a/include/godot/core/Color.h +++ b/include/godot_cpp/core/Color.h @@ -5,7 +5,7 @@ #include -#include +#include "String.h" namespace godot { diff --git a/include/godot/core/CoreTypes.h b/include/godot_cpp/core/CoreTypes.h similarity index 100% rename from include/godot/core/CoreTypes.h rename to include/godot_cpp/core/CoreTypes.h diff --git a/include/godot/core/Defs.h b/include/godot_cpp/core/Defs.h similarity index 100% rename from include/godot/core/Defs.h rename to include/godot_cpp/core/Defs.h diff --git a/include/godot/core/Dictionary.h b/include/godot_cpp/core/Dictionary.h similarity index 100% rename from include/godot/core/Dictionary.h rename to include/godot_cpp/core/Dictionary.h diff --git a/include/godot/core/Image.h b/include/godot_cpp/core/Image.h similarity index 100% rename from include/godot/core/Image.h rename to include/godot_cpp/core/Image.h diff --git a/include/godot/core/InputEvent.h b/include/godot_cpp/core/InputEvent.h similarity index 100% rename from include/godot/core/InputEvent.h rename to include/godot_cpp/core/InputEvent.h diff --git a/include/godot/core/NodePath.h b/include/godot_cpp/core/NodePath.h similarity index 96% rename from include/godot/core/NodePath.h rename to include/godot_cpp/core/NodePath.h index d687cc9..8fa8909 100644 --- a/include/godot/core/NodePath.h +++ b/include/godot_cpp/core/NodePath.h @@ -1,7 +1,7 @@ #ifndef NODEPATH_H #define NODEPATH_H -#include +#include "String.h" #include @@ -12,6 +12,11 @@ class NodePath { godot_node_path _node_path; public: + NodePath() + { + + } + NodePath(const String &from) { godot_node_path_new(&_node_path, (godot_string *) &from); diff --git a/include/godot/core/Plane.h b/include/godot_cpp/core/Plane.h similarity index 100% rename from include/godot/core/Plane.h rename to include/godot_cpp/core/Plane.h diff --git a/include/godot/core/PoolArrays.h b/include/godot_cpp/core/PoolArrays.h similarity index 100% rename from include/godot/core/PoolArrays.h rename to include/godot_cpp/core/PoolArrays.h diff --git a/include/godot/core/Quat.h b/include/godot_cpp/core/Quat.h similarity index 100% rename from include/godot/core/Quat.h rename to include/godot_cpp/core/Quat.h diff --git a/include/godot/core/RID.h b/include/godot_cpp/core/RID.h similarity index 100% rename from include/godot/core/RID.h rename to include/godot_cpp/core/RID.h diff --git a/include/godot/core/Rect2.h b/include/godot_cpp/core/Rect2.h similarity index 100% rename from include/godot/core/Rect2.h rename to include/godot_cpp/core/Rect2.h diff --git a/include/godot/core/Rect3.h b/include/godot_cpp/core/Rect3.h similarity index 100% rename from include/godot/core/Rect3.h rename to include/godot_cpp/core/Rect3.h diff --git a/include/godot/core/String.h b/include/godot_cpp/core/String.h similarity index 100% rename from include/godot/core/String.h rename to include/godot_cpp/core/String.h diff --git a/include/godot/core/Transform.h b/include/godot_cpp/core/Transform.h similarity index 100% rename from include/godot/core/Transform.h rename to include/godot_cpp/core/Transform.h diff --git a/include/godot/core/Transform2D.h b/include/godot_cpp/core/Transform2D.h similarity index 100% rename from include/godot/core/Transform2D.h rename to include/godot_cpp/core/Transform2D.h diff --git a/include/godot/core/Variant.h b/include/godot_cpp/core/Variant.h similarity index 100% rename from include/godot/core/Variant.h rename to include/godot_cpp/core/Variant.h diff --git a/include/godot/core/Vector2.h b/include/godot_cpp/core/Vector2.h similarity index 100% rename from include/godot/core/Vector2.h rename to include/godot_cpp/core/Vector2.h diff --git a/include/godot/core/Vector3.h b/include/godot_cpp/core/Vector3.h similarity index 100% rename from include/godot/core/Vector3.h rename to include/godot_cpp/core/Vector3.h