register_types.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 used to discover singleton gdnative files
  48. static void actual_discoverer_handler();
  49. class GDNativeSingletonDiscover : public Object {
  50. // GDCLASS(GDNativeSingletonDiscover, Object)
  51. virtual String get_class() const {
  52. // okay, this is a really dirty hack.
  53. // We're overriding get_class so we can connect it to a signal
  54. // This works because get_class is a virtual method, so we don't
  55. // need to register a new class to ClassDB just for this one
  56. // little signal.
  57. actual_discoverer_handler();
  58. return "Object";
  59. }
  60. };
  61. static Set<String> get_gdnative_singletons(EditorFileSystemDirectory *p_dir) {
  62. Set<String> file_paths;
  63. // check children
  64. for (int i = 0; i < p_dir->get_file_count(); i++) {
  65. String file_name = p_dir->get_file(i);
  66. String file_type = p_dir->get_file_type(i);
  67. if (file_type != "GDNativeLibrary") {
  68. continue;
  69. }
  70. Ref<GDNativeLibrary> lib = ResourceLoader::load(p_dir->get_file_path(i));
  71. if (lib.is_valid() && lib->is_singleton()) {
  72. file_paths.insert(p_dir->get_file_path(i));
  73. }
  74. }
  75. // check subdirectories
  76. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  77. Set<String> paths = get_gdnative_singletons(p_dir->get_subdir(i));
  78. for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
  79. file_paths.insert(E->get());
  80. }
  81. }
  82. return file_paths;
  83. }
  84. static void actual_discoverer_handler() {
  85. EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->get_filesystem();
  86. Set<String> file_paths = get_gdnative_singletons(dir);
  87. bool changed = false;
  88. Array current_files;
  89. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  90. current_files = ProjectSettings::get_singleton()->get("gdnative/singletons");
  91. }
  92. Array files;
  93. files.resize(file_paths.size());
  94. int i = 0;
  95. for (Set<String>::Element *E = file_paths.front(); E; i++, E = E->next()) {
  96. if (!current_files.has(E->get())) {
  97. changed = true;
  98. }
  99. files.set(i, E->get());
  100. }
  101. // Check for removed files
  102. if (!changed) {
  103. for (int j = 0; j < current_files.size(); j++) {
  104. if (!file_paths.has(current_files[j])) {
  105. changed = true;
  106. break;
  107. }
  108. }
  109. }
  110. if (changed) {
  111. ProjectSettings::get_singleton()->set("gdnative/singletons", files);
  112. ProjectSettings::get_singleton()->save();
  113. }
  114. }
  115. static GDNativeSingletonDiscover *discoverer = NULL;
  116. class GDNativeExportPlugin : public EditorExportPlugin {
  117. protected:
  118. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features);
  119. };
  120. struct LibrarySymbol {
  121. const char *name;
  122. bool is_required;
  123. };
  124. void GDNativeExportPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  125. if (p_type != "GDNativeLibrary") {
  126. return;
  127. }
  128. Ref<GDNativeLibrary> lib = ResourceLoader::load(p_path);
  129. if (lib.is_null()) {
  130. return;
  131. }
  132. Ref<ConfigFile> config = lib->get_config_file();
  133. {
  134. List<String> entry_keys;
  135. config->get_section_keys("entry", &entry_keys);
  136. for (List<String>::Element *E = entry_keys.front(); E; E = E->next()) {
  137. String key = E->get();
  138. Vector<String> tags = key.split(".");
  139. bool skip = false;
  140. for (int i = 0; i < tags.size(); i++) {
  141. bool has_feature = p_features.has(tags[i]);
  142. if (!has_feature) {
  143. skip = true;
  144. break;
  145. }
  146. }
  147. if (skip) {
  148. continue;
  149. }
  150. String entry_lib_path = config->get_value("entry", key);
  151. add_shared_object(entry_lib_path, tags);
  152. }
  153. }
  154. {
  155. List<String> dependency_keys;
  156. config->get_section_keys("dependencies", &dependency_keys);
  157. for (List<String>::Element *E = dependency_keys.front(); E; E = E->next()) {
  158. String key = E->get();
  159. Vector<String> tags = key.split(".");
  160. bool skip = false;
  161. for (int i = 0; i < tags.size(); i++) {
  162. bool has_feature = p_features.has(tags[i]);
  163. if (!has_feature) {
  164. skip = true;
  165. break;
  166. }
  167. }
  168. if (skip) {
  169. continue;
  170. }
  171. Vector<String> dependency_paths = config->get_value("dependencies", key);
  172. for (int i = 0; i < dependency_paths.size(); i++) {
  173. add_shared_object(dependency_paths[i], tags);
  174. }
  175. }
  176. }
  177. if (p_features.has("iOS")) {
  178. // Register symbols in the "fake" dynamic lookup table, because dlsym does not work well on iOS.
  179. LibrarySymbol expected_symbols[] = {
  180. { "gdnative_init", true },
  181. { "gdnative_terminate", false },
  182. { "nativescript_init", false },
  183. { "nativescript_frame", false },
  184. { "nativescript_thread_enter", false },
  185. { "nativescript_thread_exit", false },
  186. { "gdnative_singleton", false }
  187. };
  188. String declare_pattern = "extern \"C\" void $name(void)$weak;\n";
  189. String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
  190. "extern void add_ios_init_callback(void (*cb)());\n";
  191. String linker_flags = "";
  192. for (unsigned int i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
  193. String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
  194. String code = declare_pattern.replace("$name", full_name);
  195. code = code.replace("$weak", expected_symbols[i].is_required ? "" : " __attribute__((weak))");
  196. additional_code += code;
  197. if (!expected_symbols[i].is_required) {
  198. if (linker_flags.length() > 0) {
  199. linker_flags += " ";
  200. }
  201. linker_flags += "-Wl,-U,_" + full_name;
  202. }
  203. }
  204. additional_code += String("void $prefixinit() {\n").replace("$prefix", lib->get_symbol_prefix());
  205. String register_pattern = " if (&$name) register_dynamic_symbol((char *)\"$name\", (void *)$name);\n";
  206. for (unsigned int i = 0; i < sizeof(expected_symbols) / sizeof(expected_symbols[0]); ++i) {
  207. String full_name = lib->get_symbol_prefix() + expected_symbols[i].name;
  208. additional_code += register_pattern.replace("$name", full_name);
  209. }
  210. additional_code += "}\n";
  211. additional_code += String("struct $prefixstruct {$prefixstruct() {add_ios_init_callback($prefixinit);}};\n").replace("$prefix", lib->get_symbol_prefix());
  212. additional_code += String("$prefixstruct $prefixstruct_instance;\n").replace("$prefix", lib->get_symbol_prefix());
  213. add_ios_cpp_code(additional_code);
  214. add_ios_linker_flags(linker_flags);
  215. }
  216. }
  217. static void editor_init_callback() {
  218. GDNativeLibrarySingletonEditor *library_editor = memnew(GDNativeLibrarySingletonEditor);
  219. library_editor->set_name(TTR("GDNative"));
  220. ProjectSettingsEditor::get_singleton()->get_tabs()->add_child(library_editor);
  221. discoverer = memnew(GDNativeSingletonDiscover);
  222. EditorFileSystem::get_singleton()->connect("filesystem_changed", discoverer, "get_class");
  223. Ref<GDNativeExportPlugin> export_plugin;
  224. export_plugin.instance();
  225. EditorExport::get_singleton()->add_export_plugin(export_plugin);
  226. EditorNode::get_singleton()->add_editor_plugin(memnew(GDNativeLibraryEditorPlugin(EditorNode::get_singleton())));
  227. }
  228. #endif
  229. static godot_variant cb_standard_varcall(void *p_procedure_handle, godot_array *p_args) {
  230. godot_gdnative_procedure_fn proc;
  231. proc = (godot_gdnative_procedure_fn)p_procedure_handle;
  232. return proc(p_args);
  233. }
  234. GDNativeCallRegistry *GDNativeCallRegistry::singleton;
  235. Vector<Ref<GDNative> > singleton_gdnatives;
  236. Ref<GDNativeLibraryResourceLoader> resource_loader_gdnlib;
  237. Ref<GDNativeLibraryResourceSaver> resource_saver_gdnlib;
  238. void register_gdnative_types() {
  239. #ifdef TOOLS_ENABLED
  240. EditorNode::add_init_callback(editor_init_callback);
  241. #endif
  242. ClassDB::register_class<GDNativeLibrary>();
  243. ClassDB::register_class<GDNative>();
  244. resource_loader_gdnlib.instance();
  245. ResourceLoader::add_resource_format_loader(resource_loader_gdnlib);
  246. resource_saver_gdnlib.instance();
  247. ResourceSaver::add_resource_format_saver(resource_saver_gdnlib);
  248. GDNativeCallRegistry::singleton = memnew(GDNativeCallRegistry);
  249. GDNativeCallRegistry::singleton->register_native_call_type("standard_varcall", cb_standard_varcall);
  250. register_net_types();
  251. register_arvr_types();
  252. register_nativescript_types();
  253. register_pluginscript_types();
  254. register_videodecoder_types();
  255. // run singletons
  256. Array singletons = Array();
  257. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  258. singletons = ProjectSettings::get_singleton()->get("gdnative/singletons");
  259. }
  260. singleton_gdnatives.resize(singletons.size());
  261. for (int i = 0; i < singletons.size(); i++) {
  262. String path = singletons[i];
  263. Ref<GDNativeLibrary> lib = ResourceLoader::load(path);
  264. singleton_gdnatives.write[i].instance();
  265. singleton_gdnatives.write[i]->set_library(lib);
  266. if (!singleton_gdnatives.write[i]->initialize()) {
  267. // Can't initialize. Don't make a native_call then
  268. continue;
  269. }
  270. void *proc_ptr;
  271. Error err = singleton_gdnatives[i]->get_symbol(
  272. lib->get_symbol_prefix() + "gdnative_singleton",
  273. proc_ptr);
  274. if (err != OK) {
  275. ERR_PRINT((String("No godot_gdnative_singleton in \"" + singleton_gdnatives[i]->get_library()->get_current_library_path()) + "\" found").utf8().get_data());
  276. } else {
  277. ((void (*)())proc_ptr)();
  278. }
  279. }
  280. }
  281. void unregister_gdnative_types() {
  282. for (int i = 0; i < singleton_gdnatives.size(); i++) {
  283. if (singleton_gdnatives[i].is_null()) {
  284. continue;
  285. }
  286. if (!singleton_gdnatives[i]->is_initialized()) {
  287. continue;
  288. }
  289. singleton_gdnatives.write[i]->terminate();
  290. }
  291. singleton_gdnatives.clear();
  292. unregister_videodecoder_types();
  293. unregister_pluginscript_types();
  294. unregister_nativescript_types();
  295. unregister_arvr_types();
  296. unregister_net_types();
  297. memdelete(GDNativeCallRegistry::singleton);
  298. #ifdef TOOLS_ENABLED
  299. if (Engine::get_singleton()->is_editor_hint() && discoverer != NULL) {
  300. memdelete(discoverer);
  301. }
  302. #endif
  303. ResourceLoader::remove_resource_format_loader(resource_loader_gdnlib);
  304. resource_loader_gdnlib.unref();
  305. ResourceSaver::remove_resource_format_saver(resource_saver_gdnlib);
  306. resource_saver_gdnlib.unref();
  307. // This is for printing out the sizes of the core types
  308. /*
  309. print_line(String("array:\t") + itos(sizeof(Array)));
  310. print_line(String("basis:\t") + itos(sizeof(Basis)));
  311. print_line(String("color:\t") + itos(sizeof(Color)));
  312. print_line(String("dict:\t" ) + itos(sizeof(Dictionary)));
  313. print_line(String("node_path:\t") + itos(sizeof(NodePath)));
  314. print_line(String("plane:\t") + itos(sizeof(Plane)));
  315. print_line(String("poolarray:\t") + itos(sizeof(PoolByteArray)));
  316. print_line(String("quat:\t") + itos(sizeof(Quat)));
  317. print_line(String("rect2:\t") + itos(sizeof(Rect2)));
  318. print_line(String("aabb:\t") + itos(sizeof(AABB)));
  319. print_line(String("rid:\t") + itos(sizeof(RID)));
  320. print_line(String("string:\t") + itos(sizeof(String)));
  321. print_line(String("transform:\t") + itos(sizeof(Transform)));
  322. print_line(String("transfo2D:\t") + itos(sizeof(Transform2D)));
  323. print_line(String("variant:\t") + itos(sizeof(Variant)));
  324. print_line(String("vector2:\t") + itos(sizeof(Vector2)));
  325. print_line(String("vector3:\t") + itos(sizeof(Vector3)));
  326. */
  327. }