C#: Generate instance types for singletons

This commit is contained in:
Raul Santos 2023-07-14 16:05:48 +02:00
parent 16a93563bf
commit 6b713b1682
No known key found for this signature in database
GPG Key ID: B532473AE3A803E4
4 changed files with 72 additions and 24 deletions

View File

@ -72,7 +72,7 @@
// Types that will be skipped over (in favor of their base types) when setting up instance bindings. // Types that will be skipped over (in favor of their base types) when setting up instance bindings.
// This must be a superset of `ignored_types` in bindings_generator.cpp. // This must be a superset of `ignored_types` in bindings_generator.cpp.
const Vector<String> ignored_types = { "PhysicsServer2DExtension", "PhysicsServer3DExtension" }; const Vector<String> ignored_types = {};
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
static bool _create_project_solution_if_needed() { static bool _create_project_solution_if_needed() {

View File

@ -82,6 +82,7 @@ StringBuilder &operator<<(StringBuilder &r_sb, const char *p_cstring) {
#define CS_STATIC_METHOD_GETINSTANCE "GetPtr" #define CS_STATIC_METHOD_GETINSTANCE "GetPtr"
#define CS_METHOD_CALL "Call" #define CS_METHOD_CALL "Call"
#define CS_PROPERTY_SINGLETON "Singleton" #define CS_PROPERTY_SINGLETON "Singleton"
#define CS_SINGLETON_INSTANCE_SUFFIX "Instance"
#define CS_METHOD_INVOKE_GODOT_CLASS_METHOD "InvokeGodotClassMethod" #define CS_METHOD_INVOKE_GODOT_CLASS_METHOD "InvokeGodotClassMethod"
#define CS_METHOD_HAS_GODOT_CLASS_METHOD "HasGodotClassMethod" #define CS_METHOD_HAS_GODOT_CLASS_METHOD "HasGodotClassMethod"
#define CS_METHOD_HAS_GODOT_CLASS_SIGNAL "HasGodotClassSignal" #define CS_METHOD_HAS_GODOT_CLASS_SIGNAL "HasGodotClassSignal"
@ -116,7 +117,7 @@ StringBuilder &operator<<(StringBuilder &r_sb, const char *p_cstring) {
// Types that will be ignored by the generator and won't be available in C#. // Types that will be ignored by the generator and won't be available in C#.
// This must be kept in sync with `ignored_types` in csharp_script.cpp // This must be kept in sync with `ignored_types` in csharp_script.cpp
const Vector<String> ignored_types = { "PhysicsServer2DExtension", "PhysicsServer3DExtension" }; const Vector<String> ignored_types = {};
void BindingsGenerator::TypeInterface::postsetup_enum_type(BindingsGenerator::TypeInterface &r_enum_itype) { void BindingsGenerator::TypeInterface::postsetup_enum_type(BindingsGenerator::TypeInterface &r_enum_itype) {
// C interface for enums is the same as that of 'uint32_t'. Remember to apply // C interface for enums is the same as that of 'uint32_t'. Remember to apply
@ -653,6 +654,11 @@ void BindingsGenerator::_append_xml_constant(StringBuilder &p_xml_output, const
_append_xml_undeclared(p_xml_output, p_link_target); _append_xml_undeclared(p_xml_output, p_link_target);
} else { } else {
// Try to find the constant in the current class // Try to find the constant in the current class
if (p_target_itype->is_singleton_instance) {
// Constants and enums are declared in the static singleton class.
p_target_itype = &obj_types[p_target_itype->cname];
}
const ConstantInterface *target_iconst = find_constant_by_name(p_target_cname, p_target_itype->constants); const ConstantInterface *target_iconst = find_constant_by_name(p_target_cname, p_target_itype->constants);
if (target_iconst) { if (target_iconst) {
@ -1403,8 +1409,13 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str
if (is_derived_type && !itype.is_singleton) { if (is_derived_type && !itype.is_singleton) {
if (obj_types.has(itype.base_name)) { if (obj_types.has(itype.base_name)) {
TypeInterface base_type = obj_types[itype.base_name];
output.append(" : "); output.append(" : ");
output.append(obj_types[itype.base_name].proxy_name); output.append(base_type.proxy_name);
if (base_type.is_singleton) {
// If the type is a singleton, use the instance type.
output.append(CS_SINGLETON_INSTANCE_SUFFIX);
}
} else { } else {
ERR_PRINT("Base type '" + itype.base_name.operator String() + "' does not exist, for class '" + itype.name + "'."); ERR_PRINT("Base type '" + itype.base_name.operator String() + "' does not exist, for class '" + itype.name + "'.");
return ERR_INVALID_DATA; return ERR_INVALID_DATA;
@ -1506,14 +1517,16 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str
if (itype.is_singleton) { if (itype.is_singleton) {
// Add the type name and the singleton pointer as static fields // Add the type name and the singleton pointer as static fields
StringName instance_name = itype.name + CS_SINGLETON_INSTANCE_SUFFIX;
String instance_type_name = obj_types.has(instance_name)
? obj_types[instance_name].proxy_name
: "GodotObject";
output.append(MEMBER_BEGIN "private static GodotObject singleton;\n"); output.append(MEMBER_BEGIN "private static " + instance_type_name + " singleton;\n");
output << MEMBER_BEGIN "public static GodotObject " CS_PROPERTY_SINGLETON "\n" INDENT1 "{\n" output << MEMBER_BEGIN "public static " + instance_type_name + " " CS_PROPERTY_SINGLETON " =>\n"
<< INDENT2 "get\n" INDENT2 "{\n" INDENT3 "if (singleton == null)\n" << INDENT2 "singleton \?\?= (" + instance_type_name + ")"
<< INDENT4 "singleton = " C_METHOD_ENGINE_GET_SINGLETON "(\"" << C_METHOD_ENGINE_GET_SINGLETON "(\"" << itype.name << "\");\n";
<< itype.name
<< "\");\n" INDENT3 "return singleton;\n" INDENT2 "}\n" INDENT1 "}\n";
output.append(MEMBER_BEGIN "private static readonly StringName " BINDINGS_NATIVE_NAME_FIELD " = \""); output.append(MEMBER_BEGIN "private static readonly StringName " BINDINGS_NATIVE_NAME_FIELD " = \"");
output.append(itype.name); output.append(itype.name);
@ -1894,7 +1907,7 @@ Error BindingsGenerator::_generate_cs_property(const BindingsGenerator::TypeInte
const TypeReference &proptype_name = getter ? getter->return_type : setter->arguments.back()->get().type; const TypeReference &proptype_name = getter ? getter->return_type : setter->arguments.back()->get().type;
const TypeInterface *prop_itype = _get_type_or_null(proptype_name); const TypeInterface *prop_itype = _get_type_or_singleton_or_null(proptype_name);
ERR_FAIL_NULL_V(prop_itype, ERR_BUG); // Property type not found ERR_FAIL_NULL_V(prop_itype, ERR_BUG); // Property type not found
ERR_FAIL_COND_V_MSG(prop_itype->is_singleton, ERR_BUG, ERR_FAIL_COND_V_MSG(prop_itype->is_singleton, ERR_BUG,
@ -1983,7 +1996,7 @@ Error BindingsGenerator::_generate_cs_property(const BindingsGenerator::TypeInte
} }
Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::MethodInterface &p_imethod, int &p_method_bind_count, StringBuilder &p_output) { Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::MethodInterface &p_imethod, int &p_method_bind_count, StringBuilder &p_output) {
const TypeInterface *return_type = _get_type_or_null(p_imethod.return_type); const TypeInterface *return_type = _get_type_or_singleton_or_null(p_imethod.return_type);
ERR_FAIL_NULL_V(return_type, ERR_BUG); // Return type not found ERR_FAIL_NULL_V(return_type, ERR_BUG); // Return type not found
ERR_FAIL_COND_V_MSG(return_type->is_singleton, ERR_BUG, ERR_FAIL_COND_V_MSG(return_type->is_singleton, ERR_BUG,
@ -2004,12 +2017,17 @@ Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterf
String icall_params = method_bind_field; String icall_params = method_bind_field;
if (!p_imethod.is_static) { if (!p_imethod.is_static) {
String self_reference = "this";
if (p_itype.is_singleton) {
self_reference = CS_PROPERTY_SINGLETON;
}
if (p_itype.cs_in.size()) { if (p_itype.cs_in.size()) {
cs_in_statements << sformat(p_itype.cs_in, p_itype.c_type, "this", cs_in_statements << sformat(p_itype.cs_in, p_itype.c_type, self_reference,
String(), String(), String(), INDENT2); String(), String(), String(), INDENT2);
} }
icall_params += ", " + sformat(p_itype.cs_in_expr, "this"); icall_params += ", " + sformat(p_itype.cs_in_expr, self_reference);
} }
StringBuilder default_args_doc; StringBuilder default_args_doc;
@ -2017,7 +2035,7 @@ Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterf
// Retrieve information from the arguments // Retrieve information from the arguments
const ArgumentInterface &first = p_imethod.arguments.front()->get(); const ArgumentInterface &first = p_imethod.arguments.front()->get();
for (const ArgumentInterface &iarg : p_imethod.arguments) { for (const ArgumentInterface &iarg : p_imethod.arguments) {
const TypeInterface *arg_type = _get_type_or_null(iarg.type); const TypeInterface *arg_type = _get_type_or_singleton_or_null(iarg.type);
ERR_FAIL_NULL_V(arg_type, ERR_BUG); // Argument type not found ERR_FAIL_NULL_V(arg_type, ERR_BUG); // Argument type not found
ERR_FAIL_COND_V_MSG(arg_type->is_singleton, ERR_BUG, ERR_FAIL_COND_V_MSG(arg_type->is_singleton, ERR_BUG,
@ -2275,7 +2293,7 @@ Error BindingsGenerator::_generate_cs_signal(const BindingsGenerator::TypeInterf
// Retrieve information from the arguments // Retrieve information from the arguments
const ArgumentInterface &first = p_isignal.arguments.front()->get(); const ArgumentInterface &first = p_isignal.arguments.front()->get();
for (const ArgumentInterface &iarg : p_isignal.arguments) { for (const ArgumentInterface &iarg : p_isignal.arguments) {
const TypeInterface *arg_type = _get_type_or_null(iarg.type); const TypeInterface *arg_type = _get_type_or_singleton_or_null(iarg.type);
ERR_FAIL_NULL_V(arg_type, ERR_BUG); // Argument type not found ERR_FAIL_NULL_V(arg_type, ERR_BUG); // Argument type not found
ERR_FAIL_COND_V_MSG(arg_type->is_singleton, ERR_BUG, ERR_FAIL_COND_V_MSG(arg_type->is_singleton, ERR_BUG,
@ -2702,6 +2720,20 @@ const BindingsGenerator::TypeInterface *BindingsGenerator::_get_type_or_null(con
return nullptr; return nullptr;
} }
const BindingsGenerator::TypeInterface *BindingsGenerator::_get_type_or_singleton_or_null(const TypeReference &p_typeref) {
const TypeInterface *itype = _get_type_or_null(p_typeref);
if (itype == nullptr) {
return nullptr;
}
if (itype->is_singleton) {
StringName instance_type_name = itype->name + CS_SINGLETON_INSTANCE_SUFFIX;
itype = &obj_types.find(instance_type_name)->value;
}
return itype;
}
const String BindingsGenerator::_get_generic_type_parameters(const TypeInterface &p_itype, const List<TypeReference> &p_generic_type_parameters) { const String BindingsGenerator::_get_generic_type_parameters(const TypeInterface &p_itype, const List<TypeReference> &p_generic_type_parameters) {
if (p_generic_type_parameters.is_empty()) { if (p_generic_type_parameters.is_empty()) {
return ""; return "";
@ -2715,8 +2747,8 @@ const String BindingsGenerator::_get_generic_type_parameters(const TypeInterface
int i = 0; int i = 0;
String params = "<"; String params = "<";
for (const TypeReference &param_type : p_generic_type_parameters) { for (const TypeReference &param_type : p_generic_type_parameters) {
const TypeInterface *param_itype = _get_type_or_null(param_type); const TypeInterface *param_itype = _get_type_or_singleton_or_null(param_type);
ERR_FAIL_NULL_V(param_itype, ""); ERR_FAIL_NULL_V(param_itype, ""); // Parameter type not found
ERR_FAIL_COND_V_MSG(param_itype->is_singleton, "", ERR_FAIL_COND_V_MSG(param_itype->is_singleton, "",
"Generic type parameter is a singleton: '" + param_itype->name + "'."); "Generic type parameter is a singleton: '" + param_itype->name + "'.");
@ -2938,11 +2970,7 @@ bool BindingsGenerator::_populate_object_type_interfaces() {
itype.cs_type = itype.proxy_name; itype.cs_type = itype.proxy_name;
if (itype.is_singleton) {
itype.cs_in_expr = "GodotObject." CS_STATIC_METHOD_GETINSTANCE "(" CS_PROPERTY_SINGLETON ")";
} else {
itype.cs_in_expr = "GodotObject." CS_STATIC_METHOD_GETINSTANCE "(%0)"; itype.cs_in_expr = "GodotObject." CS_STATIC_METHOD_GETINSTANCE "(%0)";
}
itype.cs_out = "%5return (%2)%0(%1);"; itype.cs_out = "%5return (%2)%0(%1);";
@ -3346,6 +3374,19 @@ bool BindingsGenerator::_populate_object_type_interfaces() {
obj_types.insert(itype.cname, itype); obj_types.insert(itype.cname, itype);
if (itype.is_singleton) {
// Add singleton instance type.
itype.proxy_name += CS_SINGLETON_INSTANCE_SUFFIX;
itype.is_singleton = false;
itype.is_singleton_instance = true;
// Remove constants and enums, those will remain in the static class.
itype.constants.clear();
itype.enums.clear();
obj_types.insert(itype.name + CS_SINGLETON_INSTANCE_SUFFIX, itype);
}
class_list.pop_front(); class_list.pop_front();
} }

View File

@ -227,6 +227,7 @@ class BindingsGenerator {
bool is_enum = false; bool is_enum = false;
bool is_object_type = false; bool is_object_type = false;
bool is_singleton = false; bool is_singleton = false;
bool is_singleton_instance = false;
bool is_ref_counted = false; bool is_ref_counted = false;
/** /**
@ -756,6 +757,7 @@ class BindingsGenerator {
Error _populate_method_icalls_table(const TypeInterface &p_itype); Error _populate_method_icalls_table(const TypeInterface &p_itype);
const TypeInterface *_get_type_or_null(const TypeReference &p_typeref); const TypeInterface *_get_type_or_null(const TypeReference &p_typeref);
const TypeInterface *_get_type_or_singleton_or_null(const TypeReference &p_typeref);
const String _get_generic_type_parameters(const TypeInterface &p_itype, const List<TypeReference> &p_generic_type_parameters); const String _get_generic_type_parameters(const TypeInterface &p_itype, const List<TypeReference> &p_generic_type_parameters);

View File

@ -276,9 +276,14 @@ namespace Godot.Bridge
if (wrapperType != null && IsStatic(wrapperType)) if (wrapperType != null && IsStatic(wrapperType))
{ {
// A static class means this is a Godot singleton class. If an instance is needed we use GodotObject. // A static class means this is a Godot singleton class. Try to get the Instance proxy type.
wrapperType = TypeGetProxyClass($"{nativeTypeNameStr}Instance");
if (wrapperType == null)
{
// Otherwise, fallback to GodotObject.
return typeof(GodotObject); return typeof(GodotObject);
} }
}
return wrapperType; return wrapperType;
} }