gdnative.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*************************************************************************/
  2. /* gdnative.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "gdnative.h"
  31. #include "global_constants.h"
  32. #include "io/file_access_encrypted.h"
  33. #include "os/file_access.h"
  34. #include "os/os.h"
  35. #include "project_settings.h"
  36. #include "scene/main/scene_tree.h"
  37. const String init_symbol = "gdnative_init";
  38. const String terminate_symbol = "gdnative_terminate";
  39. const String default_symbol_prefix = "godot_";
  40. // Defined in gdnative_api_struct.gen.cpp
  41. extern const godot_gdnative_core_api_struct api_struct;
  42. Map<String, Vector<Ref<GDNative> > > *GDNativeLibrary::loaded_libraries = NULL;
  43. GDNativeLibrary::GDNativeLibrary() {
  44. config_file.instance();
  45. symbol_prefix = default_symbol_prefix;
  46. if (GDNativeLibrary::loaded_libraries == NULL) {
  47. GDNativeLibrary::loaded_libraries = memnew((Map<String, Vector<Ref<GDNative> > >));
  48. }
  49. }
  50. GDNativeLibrary::~GDNativeLibrary() {
  51. }
  52. void GDNativeLibrary::_bind_methods() {
  53. ClassDB::bind_method(D_METHOD("get_config_file"), &GDNativeLibrary::get_config_file);
  54. ClassDB::bind_method(D_METHOD("get_current_library_path"), &GDNativeLibrary::get_current_library_path);
  55. ClassDB::bind_method(D_METHOD("get_current_dependencies"), &GDNativeLibrary::get_current_dependencies);
  56. ClassDB::bind_method(D_METHOD("is_current_library_statically_linked"), &GDNativeLibrary::is_current_library_statically_linked);
  57. ClassDB::bind_method(D_METHOD("should_load_once"), &GDNativeLibrary::should_load_once);
  58. ClassDB::bind_method(D_METHOD("is_singleton"), &GDNativeLibrary::is_singleton);
  59. ClassDB::bind_method(D_METHOD("get_symbol_prefix"), &GDNativeLibrary::get_symbol_prefix);
  60. ClassDB::bind_method(D_METHOD("set_load_once", "load_once"), &GDNativeLibrary::set_load_once);
  61. ClassDB::bind_method(D_METHOD("set_singleton", "singleton"), &GDNativeLibrary::set_singleton);
  62. ClassDB::bind_method(D_METHOD("set_symbol_prefix", "symbol_prefix"), &GDNativeLibrary::set_symbol_prefix);
  63. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "load_once"), "set_load_once", "should_load_once");
  64. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "singleton"), "set_singleton", "is_singleton");
  65. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "symbol_prefix"), "set_symbol_prefix", "get_symbol_prefix");
  66. }
  67. GDNative::GDNative() {
  68. native_handle = NULL;
  69. initialized = false;
  70. }
  71. GDNative::~GDNative() {
  72. }
  73. void GDNative::_bind_methods() {
  74. ClassDB::bind_method(D_METHOD("set_library", "library"), &GDNative::set_library);
  75. ClassDB::bind_method(D_METHOD("get_library"), &GDNative::get_library);
  76. ClassDB::bind_method(D_METHOD("initialize"), &GDNative::initialize);
  77. ClassDB::bind_method(D_METHOD("terminate"), &GDNative::terminate);
  78. ClassDB::bind_method(D_METHOD("call_native", "calling_type", "procedure_name", "arguments"), &GDNative::call_native);
  79. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  80. }
  81. void GDNative::set_library(Ref<GDNativeLibrary> p_library) {
  82. ERR_EXPLAIN("Tried to change library of GDNative when it is already set");
  83. ERR_FAIL_COND(library.is_valid());
  84. library = p_library;
  85. }
  86. Ref<GDNativeLibrary> GDNative::get_library() {
  87. return library;
  88. }
  89. extern "C" void _gdnative_report_version_mismatch(const godot_object *p_library, const char *p_ext, godot_gdnative_api_version p_want, godot_gdnative_api_version p_have);
  90. extern "C" void _gdnative_report_loading_error(const godot_object *p_library, const char *p_what);
  91. bool GDNative::initialize() {
  92. if (library.is_null()) {
  93. ERR_PRINT("No library set, can't initialize GDNative object");
  94. return false;
  95. }
  96. String lib_path = library->get_current_library_path();
  97. if (lib_path.empty() && !library->is_current_library_statically_linked()) {
  98. ERR_PRINT("No library set for this platform");
  99. return false;
  100. }
  101. #ifdef IPHONE_ENABLED
  102. String path = lib_path.replace("res://", "dylibs/");
  103. #else
  104. String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
  105. #endif
  106. if (library->should_load_once()) {
  107. if (GDNativeLibrary::loaded_libraries->has(lib_path)) {
  108. // already loaded. Don't load again.
  109. // copy some of the stuff instead
  110. this->native_handle = (*GDNativeLibrary::loaded_libraries)[lib_path][0]->native_handle;
  111. initialized = true;
  112. return true;
  113. }
  114. }
  115. Error err = OS::get_singleton()->open_dynamic_library(path, native_handle);
  116. if (err != OK && !library->is_current_library_statically_linked()) {
  117. return false;
  118. }
  119. void *library_init;
  120. // we cheat here a little bit. you saw nothing
  121. initialized = true;
  122. err = get_symbol(library->get_symbol_prefix() + init_symbol, library_init);
  123. initialized = false;
  124. if (err || !library_init) {
  125. if (!library->is_current_library_statically_linked())
  126. OS::get_singleton()->close_dynamic_library(native_handle);
  127. native_handle = NULL;
  128. ERR_PRINT("Failed to obtain godot_gdnative_init symbol");
  129. return false;
  130. }
  131. godot_gdnative_init_fn library_init_fpointer;
  132. library_init_fpointer = (godot_gdnative_init_fn)library_init;
  133. godot_gdnative_init_options options;
  134. options.api_struct = &api_struct;
  135. options.in_editor = Engine::get_singleton()->is_editor_hint();
  136. options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  137. options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  138. options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE);
  139. options.report_version_mismatch = &_gdnative_report_version_mismatch;
  140. options.report_loading_error = &_gdnative_report_loading_error;
  141. options.gd_native_library = (godot_object *)(get_library().ptr());
  142. options.active_library_path = (godot_string *)&path;
  143. library_init_fpointer(&options);
  144. initialized = true;
  145. if (library->should_load_once() && !GDNativeLibrary::loaded_libraries->has(lib_path)) {
  146. Vector<Ref<GDNative> > gdnatives;
  147. gdnatives.resize(1);
  148. gdnatives[0] = Ref<GDNative>(this);
  149. GDNativeLibrary::loaded_libraries->insert(lib_path, gdnatives);
  150. }
  151. return true;
  152. }
  153. bool GDNative::terminate() {
  154. if (!initialized) {
  155. ERR_PRINT("No valid library handle, can't terminate GDNative object");
  156. return false;
  157. }
  158. if (library->should_load_once()) {
  159. Vector<Ref<GDNative> > *gdnatives = &(*GDNativeLibrary::loaded_libraries)[library->get_current_library_path()];
  160. if (gdnatives->size() > 1) {
  161. // there are other GDNative's still using this library, so we actually don't terminte
  162. gdnatives->erase(Ref<GDNative>(this));
  163. initialized = false;
  164. return true;
  165. } else if (gdnatives->size() == 1) {
  166. // we're the last one, terminate!
  167. gdnatives->clear();
  168. // wew this looks scary, but all it does is remove the entry completely
  169. GDNativeLibrary::loaded_libraries->erase(GDNativeLibrary::loaded_libraries->find(library->get_current_library_path()));
  170. }
  171. }
  172. void *library_terminate;
  173. Error error = get_symbol(library->get_symbol_prefix() + terminate_symbol, library_terminate);
  174. if (error || !library_terminate) {
  175. OS::get_singleton()->close_dynamic_library(native_handle);
  176. native_handle = NULL;
  177. return true;
  178. }
  179. godot_gdnative_terminate_fn library_terminate_pointer;
  180. library_terminate_pointer = (godot_gdnative_terminate_fn)library_terminate;
  181. godot_gdnative_terminate_options options;
  182. options.in_editor = Engine::get_singleton()->is_editor_hint();
  183. library_terminate_pointer(&options);
  184. initialized = false;
  185. // GDNativeScriptLanguage::get_singleton()->initialized_libraries.erase(p_native_lib->path);
  186. OS::get_singleton()->close_dynamic_library(native_handle);
  187. native_handle = NULL;
  188. return true;
  189. }
  190. bool GDNative::is_initialized() {
  191. return initialized;
  192. }
  193. void GDNativeCallRegistry::register_native_call_type(StringName p_call_type, native_call_cb p_callback) {
  194. native_calls.insert(p_call_type, p_callback);
  195. }
  196. Vector<StringName> GDNativeCallRegistry::get_native_call_types() {
  197. Vector<StringName> call_types;
  198. call_types.resize(native_calls.size());
  199. size_t idx = 0;
  200. for (Map<StringName, native_call_cb>::Element *E = native_calls.front(); E; E = E->next(), idx++) {
  201. call_types[idx] = E->key();
  202. }
  203. return call_types;
  204. }
  205. Variant GDNative::call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments) {
  206. Map<StringName, native_call_cb>::Element *E = GDNativeCallRegistry::singleton->native_calls.find(p_native_call_type);
  207. if (!E) {
  208. ERR_PRINT((String("No handler for native call type \"" + p_native_call_type) + "\" found").utf8().get_data());
  209. return Variant();
  210. }
  211. void *procedure_handle;
  212. Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(
  213. native_handle,
  214. p_procedure_name,
  215. procedure_handle);
  216. if (err != OK || procedure_handle == NULL) {
  217. return Variant();
  218. }
  219. godot_variant result = E->get()(procedure_handle, (godot_array *)&p_arguments);
  220. return *(Variant *)&result;
  221. }
  222. Error GDNative::get_symbol(StringName p_procedure_name, void *&r_handle) {
  223. if (!initialized) {
  224. ERR_PRINT("No valid library handle, can't get symbol from GDNative object");
  225. return ERR_CANT_OPEN;
  226. }
  227. Error result = OS::get_singleton()->get_dynamic_library_symbol_handle(
  228. native_handle,
  229. p_procedure_name,
  230. r_handle,
  231. true);
  232. return result;
  233. }
  234. RES GDNativeLibraryResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  235. Ref<GDNativeLibrary> lib;
  236. lib.instance();
  237. Ref<ConfigFile> config = lib->get_config_file();
  238. Error err = config->load(p_path);
  239. if (r_error) {
  240. *r_error = err;
  241. }
  242. lib->set_singleton(config->get_value("general", "singleton", false));
  243. lib->set_load_once(config->get_value("general", "load_once", true));
  244. lib->set_symbol_prefix(config->get_value("general", "symbol_prefix", default_symbol_prefix));
  245. String entry_lib_path;
  246. {
  247. List<String> entry_keys;
  248. config->get_section_keys("entry", &entry_keys);
  249. for (List<String>::Element *E = entry_keys.front(); E; E = E->next()) {
  250. String key = E->get();
  251. Vector<String> tags = key.split(".");
  252. bool skip = false;
  253. for (int i = 0; i < tags.size(); i++) {
  254. bool has_feature = OS::get_singleton()->has_feature(tags[i]);
  255. if (!has_feature) {
  256. skip = true;
  257. break;
  258. }
  259. }
  260. if (skip) {
  261. continue;
  262. }
  263. entry_lib_path = config->get_value("entry", key);
  264. break;
  265. }
  266. }
  267. Vector<String> dependency_paths;
  268. {
  269. List<String> dependency_keys;
  270. config->get_section_keys("dependencies", &dependency_keys);
  271. for (List<String>::Element *E = dependency_keys.front(); E; E = E->next()) {
  272. String key = E->get();
  273. Vector<String> tags = key.split(".");
  274. bool skip = false;
  275. for (int i = 0; i < tags.size(); i++) {
  276. bool has_feature = OS::get_singleton()->has_feature(tags[i]);
  277. if (!has_feature) {
  278. skip = true;
  279. break;
  280. }
  281. }
  282. if (skip) {
  283. continue;
  284. }
  285. dependency_paths = config->get_value("dependencies", key);
  286. break;
  287. }
  288. }
  289. bool is_statically_linked = false;
  290. {
  291. List<String> static_linking_keys;
  292. config->get_section_keys("static_linking", &static_linking_keys);
  293. for (List<String>::Element *E = static_linking_keys.front(); E; E = E->next()) {
  294. String key = E->get();
  295. Vector<String> tags = key.split(".");
  296. bool skip = false;
  297. for (int i = 0; i < tags.size(); i++) {
  298. bool has_feature = OS::get_singleton()->has_feature(tags[i]);
  299. if (!has_feature) {
  300. skip = true;
  301. break;
  302. }
  303. }
  304. if (skip) {
  305. continue;
  306. }
  307. is_statically_linked = config->get_value("static_linking", key);
  308. break;
  309. }
  310. }
  311. lib->current_library_path = entry_lib_path;
  312. lib->current_dependencies = dependency_paths;
  313. lib->current_library_statically_linked = is_statically_linked;
  314. return lib;
  315. }
  316. void GDNativeLibraryResourceLoader::get_recognized_extensions(List<String> *p_extensions) const {
  317. p_extensions->push_back("gdnlib");
  318. }
  319. bool GDNativeLibraryResourceLoader::handles_type(const String &p_type) const {
  320. return p_type == "GDNativeLibrary";
  321. }
  322. String GDNativeLibraryResourceLoader::get_resource_type(const String &p_path) const {
  323. String el = p_path.get_extension().to_lower();
  324. if (el == "gdnlib")
  325. return "GDNativeLibrary";
  326. return "";
  327. }
  328. Error GDNativeLibraryResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  329. Ref<GDNativeLibrary> lib = p_resource;
  330. if (lib.is_null()) {
  331. return ERR_INVALID_DATA;
  332. }
  333. Ref<ConfigFile> config = lib->get_config_file();
  334. config->set_value("general", "singleton", lib->is_singleton());
  335. config->set_value("general", "load_once", lib->should_load_once());
  336. config->set_value("general", "symbol_prefix", lib->get_symbol_prefix());
  337. return config->save(p_path);
  338. }
  339. bool GDNativeLibraryResourceSaver::recognize(const RES &p_resource) const {
  340. return Object::cast_to<GDNativeLibrary>(*p_resource) != NULL;
  341. }
  342. void GDNativeLibraryResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  343. if (Object::cast_to<GDNativeLibrary>(*p_resource) != NULL) {
  344. p_extensions->push_back("gdnlib");
  345. }
  346. }