Added binding generator

pull/7/head
Karroffel 2017-03-04 05:36:31 +01:00
parent 8cbebc3e5d
commit 5762c2f53f
25 changed files with 298 additions and 2 deletions

3
binding_generator/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
target
.idea/
*.iml

88
binding_generator/Cargo.lock generated Normal file
View File

@ -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"

View File

@ -0,0 +1,9 @@
[package]
name = "binding_generator"
version = "0.1.0"
authors = ["karroffel"]
[dependencies]
serde = "*"
serde_json = "*"
serde_derive = "*"

View File

@ -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<String, Value>,
methods: Vec<GodotMethod>
}
#[derive(Deserialize)]
struct GodotMethod {
name: String,
return_type: String,
arguments: Vec<GodotArgument>
}
#[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<GodotClass> = serde_json::from_str::<Vec<GodotClass>>(&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 <godot.h>\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())
}

View File

@ -5,7 +5,7 @@
#include <cmath>
#include <String.h>
#include "String.h"
namespace godot {

View File

@ -1,7 +1,7 @@
#ifndef NODEPATH_H
#define NODEPATH_H
#include <String.h>
#include "String.h"
#include <godot/godot_node_path.h>
@ -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);