class_db.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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.utf8().get_data()) != 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.utf8().get_data());
  74. // register with Godot
  75. GDNativePropertyInfo prop_info = {
  76. static_cast<uint32_t>(p_pinfo.type), // uint32_t type;
  77. _alloc_and_copy_cstr(p_pinfo.name.utf8().get_data()), // const char *name;
  78. _alloc_and_copy_cstr(p_pinfo.class_name.utf8().get_data()), // const char *class_name;
  79. p_pinfo.hint, // NONE //uint32_t hint;
  80. _alloc_and_copy_cstr(p_pinfo.hint_string.utf8().get_data()), // 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. memfree(const_cast<char *>(prop_info.name));
  92. memfree(const_cast<char *>(prop_info.class_name));
  93. memfree(const_cast<char *>(prop_info.hint_string));
  94. }
  95. MethodBind *ClassDB::get_method(const char *p_class, const char *p_method) {
  96. ERR_FAIL_COND_V_MSG(classes.find(p_class) == classes.end(), nullptr, "Class not found.");
  97. ClassInfo *type = &classes[p_class];
  98. while (type) {
  99. std::unordered_map<std::string, MethodBind *>::iterator method = type->method_map.find(p_method);
  100. if (method != type->method_map.end()) {
  101. return method->second;
  102. }
  103. type = type->parent_ptr;
  104. continue;
  105. }
  106. return nullptr;
  107. }
  108. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount) {
  109. const char *instance_type = p_bind->get_instance_class();
  110. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(instance_type);
  111. if (type_it == classes.end()) {
  112. memdelete(p_bind);
  113. ERR_FAIL_V_MSG(nullptr, "Class doesn't exist.");
  114. }
  115. ClassInfo &type = type_it->second;
  116. if (type.method_map.find(method_name.name) != type.method_map.end()) {
  117. memdelete(p_bind);
  118. ERR_FAIL_V_MSG(nullptr, "Binding duplicate method.");
  119. }
  120. if (type.virtual_methods.find(method_name.name) != type.virtual_methods.end()) {
  121. memdelete(p_bind);
  122. ERR_FAIL_V_MSG(nullptr, "Method already bound as virtual.");
  123. }
  124. p_bind->set_name(method_name.name);
  125. if (method_name.args.size() > p_bind->get_argument_count()) {
  126. memdelete(p_bind);
  127. ERR_FAIL_V_MSG(nullptr, "Method definition has more arguments than the actual method.");
  128. }
  129. p_bind->set_hint_flags(p_flags);
  130. std::vector<std::string> args;
  131. args.resize(method_name.args.size());
  132. size_t arg_index = 0;
  133. for (std::string arg : method_name.args) {
  134. args[arg_index++] = arg;
  135. }
  136. p_bind->set_argument_names(args);
  137. std::vector<Variant> defvals;
  138. defvals.resize(p_defcount);
  139. for (int i = 0; i < p_defcount; i++) {
  140. defvals[i] = *static_cast<const Variant *>(p_defs[i]);
  141. }
  142. p_bind->set_default_arguments(defvals);
  143. p_bind->set_hint_flags(p_flags);
  144. // register our method bind within our plugin
  145. type.method_map[method_name.name] = p_bind;
  146. // and register with godot
  147. bind_method_godot(type.name, p_bind);
  148. return p_bind;
  149. }
  150. void ClassDB::bind_method_godot(const char *p_class_name, MethodBind *p_method) {
  151. std::vector<GDNativeVariantPtr> def_args;
  152. const std::vector<Variant> &def_args_val = p_method->get_default_arguments();
  153. def_args.resize(def_args_val.size());
  154. for (int i = 0; i < def_args_val.size(); i++) {
  155. def_args[i] = (GDNativeVariantPtr)&def_args_val[i];
  156. }
  157. GDNativeExtensionClassMethodInfo method_info = {
  158. p_method->get_name(), // const char *name;
  159. p_method, // void *method_userdata;
  160. MethodBind::bind_call, // GDNativeExtensionClassMethodCall call_func;
  161. MethodBind::bind_ptrcall, // GDNativeExtensionClassMethodPtrCall ptrcall_func;
  162. p_method->get_hint_flags(), // uint32_t method_flags; /* GDNativeExtensionClassMethodFlags */
  163. (uint32_t)p_method->get_argument_count(), // uint32_t argument_count;
  164. (GDNativeBool)p_method->has_return(), // GDNativeBool has_return_value;
  165. MethodBind::bind_get_argument_type, //(GDNativeExtensionClassMethodGetArgumentType) get_argument_type_func;
  166. 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. */
  167. MethodBind::bind_get_argument_metadata, // GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func;
  168. (uint32_t)p_method->get_default_argument_count(), // uint32_t default_argument_count;
  169. def_args.data(), // GDNativeVariantPtr *default_arguments;
  170. };
  171. internal::gdn_interface->classdb_register_extension_class_method(internal::library, p_class_name, &method_info);
  172. }
  173. void ClassDB::add_signal(const char *p_class, const MethodInfo &p_signal) {
  174. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  175. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  176. ClassInfo &cl = type_it->second;
  177. // Check if this signal is already register
  178. ClassInfo *check = &cl;
  179. while (check) {
  180. 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());
  181. check = check->parent_ptr;
  182. }
  183. // register our signal in our plugin
  184. cl.signal_names.insert(p_signal.name);
  185. // register our signal in godot
  186. std::vector<GDNativePropertyInfo> parameters;
  187. parameters.reserve(p_signal.arguments.size());
  188. for (const PropertyInfo &par : p_signal.arguments) {
  189. parameters.push_back(GDNativePropertyInfo{
  190. static_cast<uint32_t>(par.type), // uint32_t type;
  191. _alloc_and_copy_cstr(par.name.utf8().get_data()), // const char *name;
  192. _alloc_and_copy_cstr(par.class_name.utf8().get_data()), // const char *class_name;
  193. par.hint, // NONE //uint32_t hint;
  194. _alloc_and_copy_cstr(par.hint_string.utf8().get_data()), // const char *hint_string;
  195. par.usage, // DEFAULT //uint32_t usage;
  196. });
  197. }
  198. internal::gdn_interface->classdb_register_extension_class_signal(internal::library, cl.name, p_signal.name, parameters.data(), parameters.size());
  199. for (GDNativePropertyInfo &par : parameters) {
  200. memfree(const_cast<char *>(par.name));
  201. memfree(const_cast<char *>(par.class_name));
  202. memfree(const_cast<char *>(par.hint_string));
  203. }
  204. }
  205. void ClassDB::bind_integer_constant(const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value, bool p_is_bitfield) {
  206. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class_name);
  207. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  208. ClassInfo &type = type_it->second;
  209. // check if it already exists
  210. ERR_FAIL_COND_MSG(type.constant_names.find(p_constant_name) != type.constant_names.end(), "Constant already registered.");
  211. // register it with our plugin (purely to check for duplicates)
  212. type.constant_names.insert(p_constant_name);
  213. // Register it with Godot
  214. internal::gdn_interface->classdb_register_extension_class_integer_constant(internal::library, p_class_name, p_enum_name, p_constant_name, p_constant_value, p_is_bitfield);
  215. }
  216. GDNativeExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, const char *p_name) {
  217. const char *class_name = (const char *)p_userdata;
  218. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(class_name);
  219. ERR_FAIL_COND_V_MSG(type_it == classes.end(), nullptr, "Class doesn't exist.");
  220. ClassInfo &type = type_it->second;
  221. std::unordered_map<std::string, GDNativeExtensionClassCallVirtual>::iterator method_it = type.virtual_methods.find(p_name);
  222. if (method_it == type.virtual_methods.end()) {
  223. return nullptr;
  224. }
  225. return method_it->second;
  226. }
  227. void ClassDB::bind_virtual_method(const char *p_class, const char *p_method, GDNativeExtensionClassCallVirtual p_call) {
  228. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  229. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  230. ClassInfo &type = type_it->second;
  231. ERR_FAIL_COND_MSG(type.method_map.find(p_method) != type.method_map.end(), "Method already registered as non-virtual.");
  232. ERR_FAIL_COND_MSG(type.virtual_methods.find(p_method) != type.virtual_methods.end(), "Virtual method already registered.");
  233. type.virtual_methods[p_method] = p_call;
  234. }
  235. void ClassDB::initialize_class(const ClassInfo &p_cl) {
  236. }
  237. void ClassDB::initialize(GDNativeInitializationLevel p_level) {
  238. for (const std::pair<std::string, ClassInfo> pair : classes) {
  239. const ClassInfo &cl = pair.second;
  240. if (cl.level != p_level) {
  241. continue;
  242. }
  243. // Nothing to do here for now...
  244. }
  245. }
  246. void ClassDB::deinitialize(GDNativeInitializationLevel p_level) {
  247. for (const std::pair<std::string, ClassInfo> pair : classes) {
  248. const ClassInfo &cl = pair.second;
  249. if (cl.level != p_level) {
  250. continue;
  251. }
  252. internal::gdn_interface->classdb_unregister_extension_class(internal::library, cl.name);
  253. for (auto method : cl.method_map) {
  254. memdelete(method.second);
  255. }
  256. }
  257. }
  258. } // namespace godot