class_db.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index) {
  47. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class.");
  48. ClassInfo &info = classes[p_class];
  49. ERR_FAIL_COND_MSG(info.property_setget.find(p_pinfo.name) != info.property_setget.end(), "Property already exists in class.");
  50. MethodBind *setter = nullptr;
  51. if (p_setter) {
  52. setter = get_method(p_class, p_setter);
  53. ERR_FAIL_COND_MSG(!setter, "Setter method not found for property.");
  54. size_t exp_args = 1 + (p_index >= 0 ? 1 : 0);
  55. ERR_FAIL_COND_MSG(exp_args != setter->get_argument_count(), "Setter method must take a single argument.");
  56. }
  57. ERR_FAIL_COND_MSG(!p_getter, "Getter method must be specified.");
  58. MethodBind *getter = get_method(p_class, p_getter);
  59. ERR_FAIL_COND_MSG(!getter, "Getter method not found for property.");
  60. {
  61. size_t exp_args = 0 + (p_index >= 0 ? 1 : 0);
  62. ERR_FAIL_COND_MSG(exp_args != getter->get_argument_count(), "Getter method must not take any argument.");
  63. }
  64. info.property_list.push_back(p_pinfo);
  65. PropertySetGet setget;
  66. setget.setter = p_setter;
  67. setget.getter = p_getter;
  68. setget._setptr = setter;
  69. setget._getptr = getter;
  70. setget.index = p_index;
  71. setget.type = p_pinfo.type;
  72. info.property_setget[p_pinfo.name] = setget;
  73. }
  74. MethodBind *ClassDB::get_method(const char *p_class, const char *p_method) {
  75. ERR_FAIL_COND_V_MSG(classes.find(p_class) == classes.end(), nullptr, "Class not found.");
  76. ClassInfo *type = &classes[p_class];
  77. while (type) {
  78. std::unordered_map<std::string, MethodBind *>::iterator method = type->method_map.find(p_method);
  79. if (method != type->method_map.end()) {
  80. return method->second;
  81. }
  82. type = type->parent_ptr;
  83. continue;
  84. }
  85. return nullptr;
  86. }
  87. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount) {
  88. const char *instance_type = p_bind->get_instance_class();
  89. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(instance_type);
  90. if (type_it == classes.end()) {
  91. memdelete(p_bind);
  92. ERR_FAIL_V_MSG(nullptr, "Class doesn't exist.");
  93. }
  94. ClassInfo &type = type_it->second;
  95. if (type.method_map.find(method_name.name) != type.method_map.end()) {
  96. memdelete(p_bind);
  97. ERR_FAIL_V_MSG(nullptr, "Binding duplicate method.");
  98. }
  99. if (type.virtual_methods.find(method_name.name) != type.virtual_methods.end()) {
  100. memdelete(p_bind);
  101. ERR_FAIL_V_MSG(nullptr, "Method already bound as virtual.");
  102. }
  103. p_bind->set_name(method_name.name);
  104. if (method_name.args.size() > p_bind->get_argument_count()) {
  105. memdelete(p_bind);
  106. ERR_FAIL_V_MSG(nullptr, "Method definition has more arguments than the actual method.");
  107. }
  108. p_bind->set_hint_flags(p_flags);
  109. std::vector<std::string> args;
  110. args.resize(method_name.args.size());
  111. size_t arg_index = 0;
  112. for (std::string arg : method_name.args) {
  113. args[arg_index++] = arg;
  114. }
  115. p_bind->set_argument_names(args);
  116. type.method_order.push_back(p_bind);
  117. type.method_map[method_name.name] = p_bind;
  118. return p_bind;
  119. }
  120. void ClassDB::add_signal(const char *p_class, const MethodInfo &p_signal) {
  121. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  122. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  123. ClassInfo &base = type_it->second;
  124. ClassInfo *check = &base;
  125. while (check) {
  126. ERR_FAIL_COND_MSG(check->signal_map.find(p_signal.name) != check->signal_map.end(), String("Class '" + String(p_class) + "' already has signal '" + String(p_signal.name) + "'.").utf8().get_data());
  127. check = check->parent_ptr;
  128. }
  129. base.signal_map[p_signal.name] = p_signal;
  130. }
  131. void ClassDB::bind_integer_constant(const char *p_class, const char *p_enum, const char *p_name, GDNativeInt p_constant) {
  132. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  133. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  134. ClassInfo &type = type_it->second;
  135. ERR_FAIL_COND_MSG(type.constant_map.find(p_name) != type.constant_map.end(), "Constant already registered.");
  136. type.constant_map[p_name] = std::pair<std::string, GDNativeInt>{ p_enum, p_constant };
  137. type.constant_order.push_back(p_name);
  138. }
  139. GDNativeExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, const char *p_name) {
  140. const char *class_name = (const char *)p_userdata;
  141. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(class_name);
  142. ERR_FAIL_COND_V_MSG(type_it == classes.end(), nullptr, "Class doesn't exist.");
  143. ClassInfo &type = type_it->second;
  144. std::unordered_map<std::string, GDNativeExtensionClassCallVirtual>::iterator method_it = type.virtual_methods.find(p_name);
  145. if (method_it == type.virtual_methods.end()) {
  146. return nullptr;
  147. }
  148. return method_it->second;
  149. }
  150. void ClassDB::bind_virtual_method(const char *p_class, const char *p_method, GDNativeExtensionClassCallVirtual p_call) {
  151. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(p_class);
  152. ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist.");
  153. ClassInfo &type = type_it->second;
  154. ERR_FAIL_COND_MSG(type.method_map.find(p_method) != type.method_map.end(), "Method already registered as non-virtual.");
  155. ERR_FAIL_COND_MSG(type.virtual_methods.find(p_method) != type.virtual_methods.end(), "Virtual method already registered.");
  156. type.virtual_methods[p_method] = p_call;
  157. }
  158. void ClassDB::initialize(GDNativeInitializationLevel p_level) {
  159. for (const std::pair<std::string, ClassInfo> pair : classes) {
  160. const ClassInfo &cl = pair.second;
  161. if (cl.level != p_level) {
  162. continue;
  163. }
  164. GDNativeExtensionClassCreationInfo class_info = {
  165. nullptr, // GDNativeExtensionClassSet set_func;
  166. nullptr, // GDNativeExtensionClassGet get_func;
  167. nullptr, // GDNativeExtensionClassGetPropertyList get_property_list_func;
  168. nullptr, // GDNativeExtensionClassFreePropertyList free_property_list_func;
  169. nullptr, // GDNativeExtensionClassNotification notification_func;
  170. nullptr, // GDNativeExtensionClassToString to_string_func;
  171. nullptr, // GDNativeExtensionClassReference reference_func;
  172. nullptr, // GDNativeExtensionClassUnreference
  173. cl.constructor, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
  174. cl.destructor, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
  175. cl.object_instance, // GDNativeExtensionClassObjectInstance object_instance_func; /* this one is mandatory */
  176. &ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func;
  177. (void *)cl.name, //void *class_userdata;
  178. };
  179. internal::interface->classdb_register_extension_class(internal::library, cl.name, cl.parent_name, &class_info);
  180. for (MethodBind *method : cl.method_order) {
  181. GDNativeExtensionClassMethodInfo method_info = {
  182. method->get_name(), //const char *name;
  183. method, //void *method_userdata;
  184. MethodBind::bind_call, //GDNativeExtensionClassMethodCall call_func;
  185. MethodBind::bind_ptrcall, //GDNativeExtensionClassMethodPtrCall ptrcall_func;
  186. GDNATIVE_EXTENSION_METHOD_FLAGS_DEFAULT, //uint32_t method_flags; /* GDNativeExtensionClassMethodFlags */
  187. (uint32_t)method->get_argument_count(), //uint32_t argument_count;
  188. (GDNativeBool)method->has_return(), //GDNativeBool has_return_value;
  189. MethodBind::bind_get_argument_type, //(GDNativeExtensionClassMethodGetArgumentType) get_argument_type_func;
  190. 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. */
  191. MethodBind::bind_get_argument_metadata, //GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func;
  192. method->get_hint_flags(), //uint32_t default_argument_count;
  193. nullptr, //GDNativeVariantPtr *default_arguments;
  194. };
  195. internal::interface->classdb_register_extension_class_method(internal::library, cl.name, &method_info);
  196. }
  197. for (const PropertyInfo &property : cl.property_list) {
  198. GDNativePropertyInfo info = {
  199. (uint32_t)property.type, //uint32_t type;
  200. property.name, //const char *name;
  201. property.class_name, //const char *class_name;
  202. property.hint, // NONE //uint32_t hint;
  203. property.hint_string, // const char *hint_string;
  204. property.usage, // DEFAULT //uint32_t usage;
  205. };
  206. const PropertySetGet &setget = cl.property_setget.find(property.name)->second;
  207. internal::interface->classdb_register_extension_class_property(internal::library, cl.name, &info, setget.setter, setget.getter);
  208. }
  209. for (const std::pair<std::string, MethodInfo> pair : cl.signal_map) {
  210. const MethodInfo &signal = pair.second;
  211. std::vector<GDNativePropertyInfo> parameters;
  212. parameters.reserve(signal.arguments.size());
  213. for (const PropertyInfo &par : signal.arguments) {
  214. parameters.push_back(GDNativePropertyInfo{
  215. static_cast<uint32_t>(par.type), // uint32_t type;
  216. par.name, // const char *name;
  217. par.class_name, // const char *class_name;
  218. par.hint, // uint32_t hint;
  219. par.hint_string, // const char *hint_string;
  220. par.usage, // uint32_t usage;
  221. });
  222. }
  223. internal::interface->classdb_register_extension_class_signal(internal::library, cl.name, pair.first.c_str(), parameters.data(), parameters.size());
  224. }
  225. for (std::string constant : cl.constant_order) {
  226. const std::pair<std::string, GDNativeInt> &def = cl.constant_map.find(constant)->second;
  227. internal::interface->classdb_register_extension_class_integer_constant(internal::library, cl.name, def.first.c_str(), constant.c_str(), def.second);
  228. }
  229. }
  230. }
  231. void ClassDB::deinitialize(GDNativeInitializationLevel p_level) {
  232. for (const std::pair<std::string, ClassInfo> pair : classes) {
  233. const ClassInfo &cl = pair.second;
  234. if (cl.level != p_level) {
  235. continue;
  236. }
  237. internal::interface->classdb_unregister_extension_class(internal::library, cl.name);
  238. for (MethodBind *method : cl.method_order) {
  239. memdelete(method);
  240. }
  241. }
  242. }
  243. } // namespace godot