register_types.cpp 12 KB

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