class_db.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include <godot_cpp/core/class_db.hpp>
  31. #include <godot_cpp/core/error_macros.hpp>
  32. #include <godot_cpp/godot.hpp>
  33. #include <godot_cpp/core/memory.hpp>
  34. #include <algorithm>
  35. namespace godot {
  36. std::unordered_map<std::string, ClassDB::ClassInfo> ClassDB::classes;
  37. GDNativeInitializationLevel ClassDB::current_level = GDNATIVE_INITIALIZATION_CORE;
  38. MethodDefinition D_METHOD(const char *p_name) {
  39. return MethodDefinition(p_name);
  40. }
  41. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) {
  42. MethodDefinition method(p_name);
  43. method.args.push_front(p_arg1);
  44. return method;
  45. }
  46. void ClassDB::add_property_group(const char *p_class, const char *p_name, const char *p_prefix) {
  47. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class.");
  48. internal::gdn_interface->classdb_register_extension_class_property_group(internal::library, p_class, p_name, p_prefix);
  49. }
  50. void ClassDB::add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix) {
  51. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class.");
  52. internal::gdn_interface->classdb_register_extension_class_property_subgroup(internal::library, p_class, p_name, p_prefix);
  53. }
  54. void ClassDB::add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index) {
  55. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class.");
  56. ClassInfo &info = classes[p_class];
  57. ERR_FAIL_COND_MSG(info.property_names.find(p_pinfo.name) != info.property_names.end(), "Property already exists in class.");
  58. MethodBind *setter = nullptr;
  59. if (p_setter) {
  60. setter = get_method(p_class, p_setter);
  61. ERR_FAIL_COND_MSG(!setter, "Setter method not found for property.");
  62. size_t exp_args = 1 + (p_index >= 0 ? 1 : 0);
  63. ERR_FAIL_COND_MSG(exp_args != setter->get_argument_count(), "Setter method must take a single argument.");
  64. }
  65. ERR_FAIL_COND_MSG(!p_getter, "Getter method must be specified.");
  66. MethodBind *getter = get_method(p_class, p_getter);
  67. ERR_FAIL_COND_MSG(!getter, "Getter method not found for property.");
  68. {
  69. size_t exp_args = 0 + (p_index >= 0 ? 1 : 0);
  70. ERR_FAIL_COND_MSG(exp_args != getter->get_argument_count(), "Getter method must not take any argument.");
  71. }
  72. // register property with plugin
  73. info.property_names.insert(p_pinfo.name);
  74. // register with Godot
  75. GDNativePropertyInfo prop_info = {
  76. (uint32_t)p_pinfo.type, //uint32_t type;
  77. p_pinfo.name, //const char *name;
  78. p_pinfo.class_name, //const char *class_name;
  79. p_pinfo.hint, // NONE //uint32_t hint;
  80. p_pinfo.hint_string, // const char *hint_string;
  81. p_pinfo.usage, // DEFAULT //uint32_t usage;
  82. };
  83. PropertySetGet setget;
  84. setget.setter = p_setter;
  85. setget.getter = p_getter;
  86. setget._setptr = setter;
  87. setget._getptr = getter;
  88. setget.index = p_index;
  89. setget.type = p_pinfo.type;
  90. internal::gdn_interface->classdb_register_extension_class_property(internal::library, info.name, &prop_info, setget.setter, setget.getter);
  91. }
  92. MethodBind *ClassDB::get_method(const char *p_class, const char *p_method) {
  93. ERR_FAIL_COND_V_MSG(classes.find(p_class) == classes.end(), nullptr, "Class not found.");
  94. ClassInfo *type = &classes[p_class];
  95. while (type) {
  96. std::unordered_map<std::string, MethodBind *>::iterator method = type->method_map.find(p_method);
  97. if (method != type->method_map.end()) {
  98. return method->second;
  99. }
  100. type = type->parent_ptr;
  101. continue;
  102. }
  103. return nullptr;
  104. }
  105. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount) {
  106. const char *instance_type = p_bind->get_instance_class();
  107. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(instance_type);
  108. if (type_it == classes.end()) {
  109. memdelete(p_bind);
  110. ERR_FAIL_V_MSG(nullptr, "Class doesn't exist.");
  111. }
  112. ClassInfo &type = type_it->second;
  113. if (type.method_map.find(method_name.name) != type.method_map.end()) {
  114. memdelete(p_bind);
  115. ERR_FAIL_V_MSG(nullptr, "Binding duplicate method.");
  116. }
  117. if (type.virtual_methods.find(method_name.name) != type.virtual_methods.end()) {
  118. memdelete(p_bind);
  119. ERR_FAIL_V_MSG(nullptr, "Method already bound as virtual.");
  120. }
  121. p_bind->set_name(method_name.name);
  122. if (method_name.args.size() > p_bind->get_argument_count()) {
  123. memdelete(p_bind);
  124. ERR_FAIL_V_MSG(nullptr, "Method definition has more arguments than the actual method.");
  125. }
  126. p_bind->set_hint_flags(p_flags);
  127. std::vector<std::string> args;
  128. args.resize(method_name.args.size());
  129. size_t arg_index = 0;
  130. for (std::string arg : method_name.args) {
  131. args[arg_index++] = arg;
  132. }
  133. p_bind->set_argument_names(args);
  134. // register our method bind within our plugin
  135. type.method_map[method_name.name] = p_bind;
  136. // and register with godot
  137. bind_method_godot(type.name, p_bind);
  138. return p_bind;
  139. }
  140. void ClassDB::bind_method_godot(const char *p_class_name, MethodBind *p_method) {
  141. GDNativeExtensionClassMethodInfo method_info = {
  142. p_method->get_name(), //const char *name;
  143. p_method, //void *method_userdata;
  144. MethodBind::bind_call, //GDNativeExtensionClassMethodCall call_func;
  145. MethodBind::bind_ptrcall, //GDNativeExtensionClassMethodPtrCall ptrcall_func;
  146. GDNATIVE_EXTENSION_METHOD_FLAGS_DEFAULT, //uint32_t method_flags; /* GDNativeExtensionClassMethodFlags */
  147. (uint32_t)p_method->get_argument_count(), //uint32_t argument_count;
  148. (GDNativeBool)p_method->has_return(), //GDNativeBool has_return_value;
  149. MethodBind::bind_get_argument_type, //(GDNativeExtensionClassMethodGetArgumentType) get_argument_type_func;
  150. MethodBind::bind_get_argument_info, //GDNativeExtensionClassMethodGetArgumentInfo get_argument_info_func; /* name and hint information for the argument can be omitted in release builds. Class name should always be present if it applies. */
  151. MethodBind::bind_get_argument_metadata, //GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func;
  152. p_method->get_hint_flags(), //uint32_t default_argument_count;
  153. nullptr, //GDNativeVariantPtr *default_arguments;
  154. };
  155. internal::gdn_interface->classdb_register_extension_class_method(internal::library, p_class_name, &method_info);
  156. }
  157. void ClassDB::add_signal(const char *p_class, const MethodInfo &p_signal) {
  158. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  159. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  160. ClassInfo &cl = type_it->second;
  161. // Check if this signal is already register
  162. ClassInfo *check = &cl;
  163. while (check) {
  164. ERR_FAIL_COND_MSG(check->signal_names.find(p_signal.name) != check->signal_names.end(), String("Class '" + String(p_class) + "' already has signal '" + String(p_signal.name) + "'.").utf8().get_data());
  165. check = check->parent_ptr;
  166. }
  167. // register our signal in our plugin
  168. cl.signal_names.insert(p_signal.name);
  169. // register our signal in godot
  170. std::vector<GDNativePropertyInfo> parameters;
  171. parameters.reserve(p_signal.arguments.size());
  172. for (const PropertyInfo &par : p_signal.arguments) {
  173. parameters.push_back(GDNativePropertyInfo{
  174. static_cast<uint32_t>(par.type), // uint32_t type;
  175. par.name, // const char *name;
  176. par.class_name, // const char *class_name;
  177. par.hint, // uint32_t hint;
  178. par.hint_string, // const char *hint_string;
  179. par.usage, // uint32_t usage;
  180. });
  181. }
  182. internal::gdn_interface->classdb_register_extension_class_signal(internal::library, cl.name, p_signal.name, parameters.data(), parameters.size());
  183. }
  184. void ClassDB::bind_integer_constant(const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value) {
  185. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class_name);
  186. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  187. ClassInfo &type = type_it->second;
  188. // check if it already exists
  189. ERR_FAIL_COND_MSG(type.constant_names.find(p_constant_name) != type.constant_names.end(), "Constant already registered.");
  190. // register it with our plugin (purely to check for duplicates)
  191. type.constant_names.insert(p_constant_name);
  192. // Register it with Godot
  193. internal::gdn_interface->classdb_register_extension_class_integer_constant(internal::library, p_class_name, p_enum_name, p_constant_name, p_constant_value);
  194. }
  195. GDNativeExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, const char *p_name) {
  196. const char *class_name = (const char *)p_userdata;
  197. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(class_name);
  198. ERR_FAIL_COND_V_MSG(type_it == classes.end(), nullptr, "Class doesn't exist.");
  199. ClassInfo &type = type_it->second;
  200. std::unordered_map<std::string, GDNativeExtensionClassCallVirtual>::iterator method_it = type.virtual_methods.find(p_name);
  201. if (method_it == type.virtual_methods.end()) {
  202. return nullptr;
  203. }
  204. return method_it->second;
  205. }
  206. void ClassDB::bind_virtual_method(const char *p_class, const char *p_method, GDNativeExtensionClassCallVirtual p_call) {
  207. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  208. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  209. ClassInfo &type = type_it->second;
  210. ERR_FAIL_COND_MSG(type.method_map.find(p_method) != type.method_map.end(), "Method already registered as non-virtual.");
  211. ERR_FAIL_COND_MSG(type.virtual_methods.find(p_method) != type.virtual_methods.end(), "Virtual method already registered.");
  212. type.virtual_methods[p_method] = p_call;
  213. }
  214. void ClassDB::initialize_class(const ClassInfo &p_cl) {
  215. }
  216. void ClassDB::initialize(GDNativeInitializationLevel p_level) {
  217. for (const std::pair<std::string, ClassInfo> pair : classes) {
  218. const ClassInfo &cl = pair.second;
  219. if (cl.level != p_level) {
  220. continue;
  221. }
  222. // Nothing to do here for now...
  223. }
  224. }
  225. void ClassDB::deinitialize(GDNativeInitializationLevel p_level) {
  226. for (const std::pair<std::string, ClassInfo> pair : classes) {
  227. const ClassInfo &cl = pair.second;
  228. if (cl.level != p_level) {
  229. continue;
  230. }
  231. internal::gdn_interface->classdb_unregister_extension_class(internal::library, cl.name);
  232. for (auto method : cl.method_map) {
  233. memdelete(method.second);
  234. }
  235. }
  236. }
  237. } // namespace godot