|
@@ -1506,6 +1506,10 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
|
|
|
result.append(f"\tGDEXTENSION_CLASS({class_name}, {inherits})")
|
|
|
result.append("")
|
|
|
|
|
|
+ if is_singleton:
|
|
|
+ result.append(f"\tstatic {class_name} *singleton;")
|
|
|
+ result.append("")
|
|
|
+
|
|
|
result.append("public:")
|
|
|
result.append("")
|
|
|
|
|
@@ -1586,6 +1590,11 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
|
|
|
|
|
|
result.append("\t}")
|
|
|
result.append("")
|
|
|
+
|
|
|
+ if is_singleton:
|
|
|
+ result.append(f"\t~{class_name}();")
|
|
|
+ result.append("")
|
|
|
+
|
|
|
result.append("public:")
|
|
|
|
|
|
# Special cases.
|
|
@@ -1735,6 +1744,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
|
|
|
|
|
|
result.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
|
|
|
result.append("")
|
|
|
+ result.append("#include <godot_cpp/core/class_db.hpp>")
|
|
|
result.append("#include <godot_cpp/core/engine_ptrcall.hpp>")
|
|
|
result.append("#include <godot_cpp/core/error_macros.hpp>")
|
|
|
result.append("")
|
|
@@ -1749,9 +1759,10 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
|
|
|
result.append("")
|
|
|
|
|
|
if is_singleton:
|
|
|
+ result.append(f"{class_name} *{class_name}::singleton = nullptr;")
|
|
|
+ result.append("")
|
|
|
result.append(f"{class_name} *{class_name}::get_singleton() {{")
|
|
|
# We assume multi-threaded access is OK because each assignment will assign the same value every time
|
|
|
- result.append(f"\tstatic {class_name} *singleton = nullptr;")
|
|
|
result.append("\tif (unlikely(singleton == nullptr)) {")
|
|
|
result.append(
|
|
|
f"\t\tGDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton({class_name}::get_class_static()._native_ptr());"
|
|
@@ -1765,11 +1776,22 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
|
|
|
result.append("#ifdef DEBUG_ENABLED")
|
|
|
result.append("\t\tERR_FAIL_NULL_V(singleton, nullptr);")
|
|
|
result.append("#endif // DEBUG_ENABLED")
|
|
|
+ result.append("\t\tif (likely(singleton)) {")
|
|
|
+ result.append(f"\t\t\tClassDB::_register_engine_singleton({class_name}::get_class_static(), singleton);")
|
|
|
+ result.append("\t\t}")
|
|
|
result.append("\t}")
|
|
|
result.append("\treturn singleton;")
|
|
|
result.append("}")
|
|
|
result.append("")
|
|
|
|
|
|
+ result.append(f"{class_name}::~{class_name}() {{")
|
|
|
+ result.append("\tif (singleton == this) {")
|
|
|
+ result.append(f"\t\tClassDB::_unregister_engine_singleton({class_name}::get_class_static());")
|
|
|
+ result.append("\t\tsingleton = nullptr;")
|
|
|
+ result.append("\t}")
|
|
|
+ result.append("}")
|
|
|
+ result.append("")
|
|
|
+
|
|
|
if "methods" in class_api:
|
|
|
for method in class_api["methods"]:
|
|
|
if method["is_virtual"]:
|