register_types.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "register_types.h"
  31. #include "gdnative/gdnative.h"
  32. #include "gdnative.h"
  33. #include "arvr/register_types.h"
  34. #include "nativescript/register_types.h"
  35. #include "net/register_types.h"
  36. #include "pluginscript/register_types.h"
  37. #include "videodecoder/register_types.h"
  38. #include "core/engine.h"
  39. #include "core/io/resource_loader.h"
  40. #include "core/io/resource_saver.h"
  41. #include "core/os/os.h"
  42. #include "core/project_settings.h"
  43. #ifdef TOOLS_ENABLED
  44. #include "editor/editor_node.h"
  45. #include "gdnative_library_editor_plugin.h"
  46. #include "gdnative_library_singleton_editor.h"
  47. class GDNativeExportPlugin : public EditorExportPlugin {
  48. protected:
  49. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features);
  50. };
  51. struct LibrarySymbol {
  52. const char *name;
  53. bool is_required;
  54. };
  55. void GDNativeExportPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  56. if (p_type != "GDNativeLibrary") {
  57. return;
  58. }
  59. Ref<GDNativeLibrary> lib = ResourceLoader::load(p_path);
  60. if (lib.is_null()) {
  61. return;
  62. }
  63. Ref<ConfigFile> config = lib->get_config_file();
  64. {
  65. List<String> entry_keys;
  66. config->get_section_keys("entry", &entry_keys);
  67. for (List<String>::Element *E = entry_keys.front(); E; E = E->next()) {
  68. String key = E->get();
  69. Vector<String> tags = key.split(".");
  70. bool skip = false;
  71. for (int i = 0; i < tags.size(); i++) {
  72. bool has_feature = p_features.has(tags[i]);
  73. if (!has_feature) {
  74. skip = true;
  75. break;
  76. }
  77. }
  78. if (skip) {
  79. continue;
  80. }
  81. String entry_lib_path = config->get_value("entry", key);
  82. add_shared_object(entry_lib_path, tags);
  83. }
  84. }
  85. {
  86. List<String> dependency_keys;
  87. config->get_section_keys("dependencies", &dependency_keys);
  88. for (List<String>::Element *E = dependency_keys.front(); E; E = E->next()) {
  89. String key = E->get();
  90. Vector<String> tags = key.split(".");
  91. bool skip = false;
  92. for (int i = 0; i < tags.size(); i++) {
  93. bool has_feature = p_features.has(tags[i]);
  94. if (!has_feature) {
  95. skip = true;
  96. break;
  97. }
  98. }
  99. if (skip) {
  100. continue;
  101. }
  102. Vector<String> dependency_paths = config->get_value("dependencies", key);
  103. for (int i = 0; i < dependency_paths.size(); i++) {
  104. add_shared_object(dependency_paths[i], tags);
  105. }
  106. }
  107. }
  108. if (p_features.has("iOS")) {
  109. // Register symbols in the "fake" dynamic lookup table, because dlsym does not work well on iOS.
  110. LibrarySymbol expected_symbols[] = {
  111. { "gdnative_init", true },
  112. { "gdnative_terminate", false },
  113. { "nativescript_init", false },
  114. { "nativescript_frame", false },
  115. { "nativescript_thread_enter", false },
  116. { "nativescript_thread_exit", false },
  117. { "gdnative_singleton", false }
  118. };
  119. String declare_pattern = "extern \"C\" void $name(void)$weak;\n";
  120. String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
  121. "extern void add_ios_init_callback(void (*cb)());\n";
  122. String linker_flags = "";
  123. for (unsigned int i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
  124. String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
  125. String code = declare_pattern.replace("$name", full_name);
  126. code = code.replace("$weak", expected_symbols[i].is_required ? "" : " __attribute__((weak))");
  127. additional_code += code;
  128. if (!expected_symbols[i].is_required) {
  129. if (linker_flags.length() > 0) {
  130. linker_flags += " ";
  131. }
  132. linker_flags += "-Wl,-U,_" + full_name;
  133. }
  134. }
  135. additional_code += String("void $prefixinit() {\n").replace("$prefix", lib->get_symbol_prefix());
  136. String register_pattern = " if (&$name) register_dynamic_symbol((char *)\"$name\", (void *)$name);\n";
  137. for (unsigned int i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
  138. String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
  139. additional_code += register_pattern.replace("$name", full_name);
  140. }
  141. additional_code += "}\n";
  142. additional_code += String("struct $prefixstruct {$prefixstruct() {add_ios_init_callback($prefixinit);}};\n").replace("$prefix", lib->get_symbol_prefix());
  143. additional_code += String("$prefixstruct $prefixstruct_instance;\n").replace("$prefix", lib->get_symbol_prefix());
  144. add_ios_cpp_code(additional_code);
  145. add_ios_linker_flags(linker_flags);
  146. }
  147. }
  148. static void editor_init_callback() {
  149. GDNativeLibrarySingletonEditor *library_editor = memnew(GDNativeLibrarySingletonEditor);
  150. library_editor->set_name(TTR("GDNative"));
  151. ProjectSettingsEditor::get_singleton()->get_tabs()->add_child(library_editor);
  152. Ref<GDNativeExportPlugin> export_plugin;
  153. export_plugin.instance();
  154. EditorExport::get_singleton()->add_export_plugin(export_plugin);
  155. EditorNode::get_singleton()->add_editor_plugin(memnew(GDNativeLibraryEditorPlugin(EditorNode::get_singleton())));
  156. }
  157. #endif
  158. static godot_variant cb_standard_varcall(void *p_procedure_handle, godot_array *p_args) {
  159. godot_gdnative_procedure_fn proc;
  160. proc = (godot_gdnative_procedure_fn)p_procedure_handle;
  161. return proc(p_args);
  162. }
  163. GDNativeCallRegistry *GDNativeCallRegistry::singleton;
  164. Vector<Ref<GDNative> > singleton_gdnatives;
  165. Ref<GDNativeLibraryResourceLoader> resource_loader_gdnlib;
  166. Ref<GDNativeLibraryResourceSaver> resource_saver_gdnlib;
  167. void register_gdnative_types() {
  168. #ifdef TOOLS_ENABLED
  169. EditorNode::add_init_callback(editor_init_callback);
  170. #endif
  171. ClassDB::register_class<GDNativeLibrary>();
  172. ClassDB::register_class<GDNative>();
  173. resource_loader_gdnlib.instance();
  174. ResourceLoader::add_resource_format_loader(resource_loader_gdnlib);
  175. resource_saver_gdnlib.instance();
  176. ResourceSaver::add_resource_format_saver(resource_saver_gdnlib);
  177. GDNativeCallRegistry::singleton = memnew(GDNativeCallRegistry);
  178. GDNativeCallRegistry::singleton->register_native_call_type("standard_varcall", cb_standard_varcall);
  179. register_net_types();
  180. register_arvr_types();
  181. register_nativescript_types();
  182. register_pluginscript_types();
  183. register_videodecoder_types();
  184. // run singletons
  185. Array singletons = Array();
  186. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  187. singletons = ProjectSettings::get_singleton()->get("gdnative/singletons");
  188. }
  189. Array excluded = Array();
  190. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons_disabled")) {
  191. excluded = ProjectSettings::get_singleton()->get("gdnative/singletons_disabled");
  192. }
  193. for (int i = 0; i < singletons.size(); i++) {
  194. String path = singletons[i];
  195. if (excluded.has(path))
  196. continue;
  197. Ref<GDNativeLibrary> lib = ResourceLoader::load(path);
  198. Ref<GDNative> singleton;
  199. singleton.instance();
  200. singleton->set_library(lib);
  201. if (!singleton->initialize()) {
  202. // Can't initialize. Don't make a native_call then
  203. continue;
  204. }
  205. void *proc_ptr;
  206. Error err = singleton->get_symbol(
  207. lib->get_symbol_prefix() + "gdnative_singleton",
  208. proc_ptr);
  209. if (err != OK) {
  210. ERR_PRINT((String("No godot_gdnative_singleton in \"" + singleton->get_library()->get_current_library_path()) + "\" found").utf8().get_data());
  211. } else {
  212. singleton_gdnatives.push_back(singleton);
  213. ((void (*)())proc_ptr)();
  214. }
  215. }
  216. }
  217. void unregister_gdnative_types() {
  218. for (int i = 0; i < singleton_gdnatives.size(); i++) {
  219. if (singleton_gdnatives[i].is_null()) {
  220. continue;
  221. }
  222. if (!singleton_gdnatives[i]->is_initialized()) {
  223. continue;
  224. }
  225. singleton_gdnatives.write[i]->terminate();
  226. }
  227. singleton_gdnatives.clear();
  228. unregister_videodecoder_types();
  229. unregister_pluginscript_types();
  230. unregister_nativescript_types();
  231. unregister_arvr_types();
  232. unregister_net_types();
  233. memdelete(GDNativeCallRegistry::singleton);
  234. ResourceLoader::remove_resource_format_loader(resource_loader_gdnlib);
  235. resource_loader_gdnlib.unref();
  236. ResourceSaver::remove_resource_format_saver(resource_saver_gdnlib);
  237. resource_saver_gdnlib.unref();
  238. // This is for printing out the sizes of the core types
  239. /*
  240. print_line(String("array:\t") + itos(sizeof(Array)));
  241. print_line(String("basis:\t") + itos(sizeof(Basis)));
  242. print_line(String("color:\t") + itos(sizeof(Color)));
  243. print_line(String("dict:\t" ) + itos(sizeof(Dictionary)));
  244. print_line(String("node_path:\t") + itos(sizeof(NodePath)));
  245. print_line(String("plane:\t") + itos(sizeof(Plane)));
  246. print_line(String("poolarray:\t") + itos(sizeof(PoolByteArray)));
  247. print_line(String("quat:\t") + itos(sizeof(Quat)));
  248. print_line(String("rect2:\t") + itos(sizeof(Rect2)));
  249. print_line(String("aabb:\t") + itos(sizeof(AABB)));
  250. print_line(String("rid:\t") + itos(sizeof(RID)));
  251. print_line(String("string:\t") + itos(sizeof(String)));
  252. print_line(String("transform:\t") + itos(sizeof(Transform)));
  253. print_line(String("transfo2D:\t") + itos(sizeof(Transform2D)));
  254. print_line(String("variant:\t") + itos(sizeof(Variant)));
  255. print_line(String("vector2:\t") + itos(sizeof(Vector2)));
  256. print_line(String("vector3:\t") + itos(sizeof(Vector3)));
  257. */
  258. }