From edc249647461201db3b3c62cae667521e3a03ee8 Mon Sep 17 00:00:00 2001 From: Karroffel Date: Sat, 4 Mar 2017 18:08:44 +0100 Subject: [PATCH] added proper singleton suport --- binding_generator/src/main.rs | 61 ++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/binding_generator/src/main.rs b/binding_generator/src/main.rs index 836e398..f30b448 100644 --- a/binding_generator/src/main.rs +++ b/binding_generator/src/main.rs @@ -92,6 +92,20 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents += forward_declares; + let core_obj_name = { + let mut name = String::new(); + if class.singleton { + name = name + "___static_object_" + strip_name(&class.name); + } else { + name = name + "__core_object"; + }; + name + }; + + if class.singleton { + contents = contents + "\n\nstatic godot_object *" + core_obj_name.as_str() + ";\n\n"; + } + contents = contents + "\n\n\nclass " + strip_name(&class.name); if class.base_class != "" { @@ -101,7 +115,13 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + " {\n"; if class.base_class == "" { - contents = contents + "protected:\n\tgodot_object *__core_object;\n\n"; + contents = contents + "protected:\n\tgodot_object *__core_object = 0;\n\n"; + } + + if class.singleton { + contents = contents + "private:\n"; + contents = contents + "\tstatic void ___singleton_init() { " + + core_obj_name.as_str() + " = godot_global_get_singleton(\"" + strip_name(&class.name) + "\"); }\n\n"; } contents = contents + "public:\n\n"; @@ -111,7 +131,7 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri { contents = contents + "\t" + strip_name(&class.name) + "() {\n"; contents = contents + "\t\t\n"; - contents = contents + "\t}\n"; + contents = contents + "\t}\n\n"; } @@ -122,6 +142,11 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + "\t}\n\n\n"; } + if class.instanciable { + contents = contents + "\tstatic " + strip_name(&class.name) + "& _new();\n"; + contents = contents + "\tvoid _destroy();\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"; } @@ -129,7 +154,7 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents += "\n\n"; for method in &class.methods { - contents = contents + "\t" + strip_name(&method.return_type) + (if !is_core_type(&method.return_type) && !is_primitive(&method.return_type) { " &" } else { " " }) + method.name.as_str() + "("; + contents = contents + "\t" + (if class.singleton { "static " } else { "" }) + strip_name(&method.return_type) + (if !is_core_type(&method.return_type) && !is_primitive(&method.return_type) { " &" } else { " " }) + method.name.as_str() + "("; for (i, argument) in (&method.arguments).iter().enumerate() { if !is_primitive(&argument._type) { @@ -146,6 +171,12 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + ") {\n"; + if class.singleton { + contents = contents + "\t\tif (" + core_obj_name.as_str() + " == 0) {\n"; + contents = contents + "\t\t\t___singleton_init();\n"; + contents = contents + "\t\t}\n\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" @@ -172,7 +203,7 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri 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"; + contents = contents + "\t\tgodot_method_bind_ptrcall(mb, " + core_obj_name.as_str() + ", args, " + if method.return_type == "void" { "NULL" } else { "&ret" } + ");\n"; if !is_primitive(&method.return_type) && !is_core_type(&method.return_type) { contents = contents + "\t\treturn reinterpret_cast<" + strip_name(&method.return_type) + "&>(ret);\n"; @@ -187,6 +218,28 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + "}\n"; + + + if class.instanciable { + + contents = contents + "#include \"ClassDB.h\"\n"; + + contents = contents + "namespace godot {\n\n"; + + contents = contents + "" + strip_name(&class.name) + "& " + strip_name(&class.name) + "::_new() {\n"; + contents = contents + "\tObject *ptr = ClassDB::instance(\"" + class.name.as_str() + "\");\n"; + contents = contents + "\treturn reinterpret_cast<" + strip_name(&class.name) + "&>(ptr);"; + contents = contents + "}\n\n"; + + contents = contents + "void " + strip_name(&class.name) + "::_destroy() {\n"; + contents = contents + "\tgodot_object_destroy(__core_object);\n"; + contents = contents + "}\n\n\n"; + + contents = contents + "}\n"; + } + + + contents = contents + "#endif\n"; contents