gdnative.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*************************************************************************/
  2. /* gdnative.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 "gdnative.h"
  31. #include "core/global_constants.h"
  32. #include "core/io/file_access_encrypted.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "scene/main/scene_tree.h"
  37. static const String init_symbol = "gdnative_init";
  38. static const String terminate_symbol = "gdnative_terminate";
  39. static const String default_symbol_prefix = "godot_";
  40. static const bool default_singleton = false;
  41. static const bool default_load_once = true;
  42. static const bool default_reloadable = true;
  43. // Defined in gdnative_api_struct.gen.cpp
  44. extern const godot_gdnative_core_api_struct api_struct;
  45. Map<String, Vector<Ref<GDNative> > > *GDNativeLibrary::loaded_libraries = NULL;
  46. GDNativeLibrary::GDNativeLibrary() {
  47. config_file.instance();
  48. symbol_prefix = default_symbol_prefix;
  49. load_once = default_load_once;
  50. singleton = default_singleton;
  51. reloadable = default_reloadable;
  52. if (GDNativeLibrary::loaded_libraries == NULL) {
  53. GDNativeLibrary::loaded_libraries = memnew((Map<String, Vector<Ref<GDNative> > >));
  54. }
  55. }
  56. GDNativeLibrary::~GDNativeLibrary() {
  57. }
  58. bool GDNativeLibrary::_set(const StringName &p_name, const Variant &p_property) {
  59. String name = p_name;
  60. if (name.begins_with("entry/")) {
  61. String key = name.substr(6, name.length() - 6);
  62. config_file->set_value("entry", key, p_property);
  63. set_config_file(config_file);
  64. return true;
  65. }
  66. if (name.begins_with("dependency/")) {
  67. String key = name.substr(11, name.length() - 11);
  68. config_file->set_value("dependencies", key, p_property);
  69. set_config_file(config_file);
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool GDNativeLibrary::_get(const StringName &p_name, Variant &r_property) const {
  75. String name = p_name;
  76. if (name.begins_with("entry/")) {
  77. String key = name.substr(6, name.length() - 6);
  78. r_property = config_file->get_value("entry", key);
  79. return true;
  80. }
  81. if (name.begins_with("dependency/")) {
  82. String key = name.substr(11, name.length() - 11);
  83. r_property = config_file->get_value("dependencies", key);
  84. return true;
  85. }
  86. return false;
  87. }
  88. void GDNativeLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  89. // set entries
  90. List<String> entry_key_list;
  91. if (config_file->has_section("entry"))
  92. config_file->get_section_keys("entry", &entry_key_list);
  93. for (List<String>::Element *E = entry_key_list.front(); E; E = E->next()) {
  94. String key = E->get();
  95. PropertyInfo prop;
  96. prop.type = Variant::STRING;
  97. prop.name = "entry/" + key;
  98. p_list->push_back(prop);
  99. }
  100. // set dependencies
  101. List<String> dependency_key_list;
  102. if (config_file->has_section("dependencies"))
  103. config_file->get_section_keys("dependencies", &dependency_key_list);
  104. for (List<String>::Element *E = dependency_key_list.front(); E; E = E->next()) {
  105. String key = E->get();
  106. PropertyInfo prop;
  107. prop.type = Variant::STRING;
  108. prop.name = "dependency/" + key;
  109. p_list->push_back(prop);
  110. }
  111. }
  112. void GDNativeLibrary::set_config_file(Ref<ConfigFile> p_config_file) {
  113. set_singleton(p_config_file->get_value("general", "singleton", default_singleton));
  114. set_load_once(p_config_file->get_value("general", "load_once", default_load_once));
  115. set_symbol_prefix(p_config_file->get_value("general", "symbol_prefix", default_symbol_prefix));
  116. set_reloadable(p_config_file->get_value("general", "reloadable", default_reloadable));
  117. String entry_lib_path;
  118. {
  119. List<String> entry_keys;
  120. if (p_config_file->has_section("entry"))
  121. p_config_file->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 = OS::get_singleton()->has_feature(tags[i]);
  128. if (!has_feature) {
  129. skip = true;
  130. break;
  131. }
  132. }
  133. if (skip) {
  134. continue;
  135. }
  136. entry_lib_path = p_config_file->get_value("entry", key);
  137. break;
  138. }
  139. }
  140. Vector<String> dependency_paths;
  141. {
  142. List<String> dependency_keys;
  143. if (p_config_file->has_section("dependencies"))
  144. p_config_file->get_section_keys("dependencies", &dependency_keys);
  145. for (List<String>::Element *E = dependency_keys.front(); E; E = E->next()) {
  146. String key = E->get();
  147. Vector<String> tags = key.split(".");
  148. bool skip = false;
  149. for (int i = 0; i < tags.size(); i++) {
  150. bool has_feature = OS::get_singleton()->has_feature(tags[i]);
  151. if (!has_feature) {
  152. skip = true;
  153. break;
  154. }
  155. }
  156. if (skip) {
  157. continue;
  158. }
  159. dependency_paths = p_config_file->get_value("dependencies", key);
  160. break;
  161. }
  162. }
  163. current_library_path = entry_lib_path;
  164. current_dependencies = dependency_paths;
  165. }
  166. void GDNativeLibrary::_bind_methods() {
  167. ClassDB::bind_method(D_METHOD("get_config_file"), &GDNativeLibrary::get_config_file);
  168. ClassDB::bind_method(D_METHOD("set_config_file", "config_file"), &GDNativeLibrary::set_config_file);
  169. ClassDB::bind_method(D_METHOD("get_current_library_path"), &GDNativeLibrary::get_current_library_path);
  170. ClassDB::bind_method(D_METHOD("get_current_dependencies"), &GDNativeLibrary::get_current_dependencies);
  171. ClassDB::bind_method(D_METHOD("should_load_once"), &GDNativeLibrary::should_load_once);
  172. ClassDB::bind_method(D_METHOD("is_singleton"), &GDNativeLibrary::is_singleton);
  173. ClassDB::bind_method(D_METHOD("get_symbol_prefix"), &GDNativeLibrary::get_symbol_prefix);
  174. ClassDB::bind_method(D_METHOD("is_reloadable"), &GDNativeLibrary::is_reloadable);
  175. ClassDB::bind_method(D_METHOD("set_load_once", "load_once"), &GDNativeLibrary::set_load_once);
  176. ClassDB::bind_method(D_METHOD("set_singleton", "singleton"), &GDNativeLibrary::set_singleton);
  177. ClassDB::bind_method(D_METHOD("set_symbol_prefix", "symbol_prefix"), &GDNativeLibrary::set_symbol_prefix);
  178. ClassDB::bind_method(D_METHOD("set_reloadable", "reloadable"), &GDNativeLibrary::set_reloadable);
  179. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "config_file", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile"), "set_config_file", "get_config_file");
  180. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "load_once"), "set_load_once", "should_load_once");
  181. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "singleton"), "set_singleton", "is_singleton");
  182. ADD_PROPERTY(PropertyInfo(Variant::STRING, "symbol_prefix"), "set_symbol_prefix", "get_symbol_prefix");
  183. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reloadable"), "set_reloadable", "is_reloadable");
  184. }
  185. GDNative::GDNative() {
  186. native_handle = NULL;
  187. initialized = false;
  188. }
  189. GDNative::~GDNative() {
  190. }
  191. void GDNative::_bind_methods() {
  192. ClassDB::bind_method(D_METHOD("set_library", "library"), &GDNative::set_library);
  193. ClassDB::bind_method(D_METHOD("get_library"), &GDNative::get_library);
  194. ClassDB::bind_method(D_METHOD("initialize"), &GDNative::initialize);
  195. ClassDB::bind_method(D_METHOD("terminate"), &GDNative::terminate);
  196. ClassDB::bind_method(D_METHOD("call_native", "calling_type", "procedure_name", "arguments"), &GDNative::call_native);
  197. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  198. }
  199. void GDNative::set_library(Ref<GDNativeLibrary> p_library) {
  200. ERR_EXPLAIN("Tried to change library of GDNative when it is already set");
  201. ERR_FAIL_COND(library.is_valid());
  202. library = p_library;
  203. }
  204. Ref<GDNativeLibrary> GDNative::get_library() const {
  205. return library;
  206. }
  207. 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);
  208. extern "C" void _gdnative_report_loading_error(const godot_object *p_library, const char *p_what);
  209. bool GDNative::initialize() {
  210. if (library.is_null()) {
  211. ERR_PRINT("No library set, can't initialize GDNative object");
  212. return false;
  213. }
  214. String lib_path = library->get_current_library_path();
  215. if (lib_path.empty()) {
  216. ERR_PRINT("No library set for this platform");
  217. return false;
  218. }
  219. #ifdef IPHONE_ENABLED
  220. // on iOS we use static linking
  221. String path = "";
  222. #elif defined(ANDROID_ENABLED)
  223. // On Android dynamic libraries are located separately from resource assets,
  224. // we should pass library name to dlopen(). The library name is flattened
  225. // during export.
  226. String path = lib_path.get_file();
  227. #elif defined(UWP_ENABLED)
  228. // On UWP we use a relative path from the app
  229. String path = lib_path.replace("res://", "");
  230. #elif defined(OSX_ENABLED)
  231. // On OSX the exported libraries are located under the Frameworks directory.
  232. // So we need to replace the library path.
  233. String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
  234. if (!FileAccess::exists(path)) {
  235. path = OS::get_singleton()->get_executable_path().get_base_dir().plus_file("../Frameworks").plus_file(lib_path.get_file());
  236. }
  237. #else
  238. String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
  239. #endif
  240. if (library->should_load_once()) {
  241. if (GDNativeLibrary::loaded_libraries->has(lib_path)) {
  242. // already loaded. Don't load again.
  243. // copy some of the stuff instead
  244. this->native_handle = (*GDNativeLibrary::loaded_libraries)[lib_path][0]->native_handle;
  245. initialized = true;
  246. return true;
  247. }
  248. }
  249. Error err = OS::get_singleton()->open_dynamic_library(path, native_handle, true);
  250. if (err != OK) {
  251. return false;
  252. }
  253. void *library_init;
  254. // we cheat here a little bit. you saw nothing
  255. initialized = true;
  256. err = get_symbol(library->get_symbol_prefix() + init_symbol, library_init, false);
  257. initialized = false;
  258. if (err || !library_init) {
  259. OS::get_singleton()->close_dynamic_library(native_handle);
  260. native_handle = NULL;
  261. ERR_PRINT("Failed to obtain godot_gdnative_init symbol");
  262. return false;
  263. }
  264. godot_gdnative_init_fn library_init_fpointer;
  265. library_init_fpointer = (godot_gdnative_init_fn)library_init;
  266. static uint64_t core_api_hash = 0;
  267. static uint64_t editor_api_hash = 0;
  268. static uint64_t no_api_hash = 0;
  269. if (!(core_api_hash || editor_api_hash || no_api_hash)) {
  270. core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  271. editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  272. no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE);
  273. }
  274. godot_gdnative_init_options options;
  275. options.api_struct = &api_struct;
  276. options.in_editor = Engine::get_singleton()->is_editor_hint();
  277. options.core_api_hash = core_api_hash;
  278. options.editor_api_hash = editor_api_hash;
  279. options.no_api_hash = no_api_hash;
  280. options.report_version_mismatch = &_gdnative_report_version_mismatch;
  281. options.report_loading_error = &_gdnative_report_loading_error;
  282. options.gd_native_library = (godot_object *)(get_library().ptr());
  283. options.active_library_path = (godot_string *)&path;
  284. library_init_fpointer(&options);
  285. initialized = true;
  286. if (library->should_load_once() && !GDNativeLibrary::loaded_libraries->has(lib_path)) {
  287. Vector<Ref<GDNative> > gdnatives;
  288. gdnatives.resize(1);
  289. gdnatives.write[0] = Ref<GDNative>(this);
  290. GDNativeLibrary::loaded_libraries->insert(lib_path, gdnatives);
  291. }
  292. return true;
  293. }
  294. bool GDNative::terminate() {
  295. if (!initialized) {
  296. ERR_PRINT("No valid library handle, can't terminate GDNative object");
  297. return false;
  298. }
  299. if (library->should_load_once()) {
  300. Vector<Ref<GDNative> > *gdnatives = &(*GDNativeLibrary::loaded_libraries)[library->get_current_library_path()];
  301. if (gdnatives->size() > 1) {
  302. // there are other GDNative's still using this library, so we actually don't terminate
  303. gdnatives->erase(Ref<GDNative>(this));
  304. initialized = false;
  305. return true;
  306. } else if (gdnatives->size() == 1) {
  307. // we're the last one, terminate!
  308. gdnatives->clear();
  309. // wew this looks scary, but all it does is remove the entry completely
  310. GDNativeLibrary::loaded_libraries->erase(GDNativeLibrary::loaded_libraries->find(library->get_current_library_path()));
  311. }
  312. }
  313. void *library_terminate;
  314. Error error = get_symbol(library->get_symbol_prefix() + terminate_symbol, library_terminate);
  315. if (error || !library_terminate) {
  316. OS::get_singleton()->close_dynamic_library(native_handle);
  317. native_handle = NULL;
  318. return true;
  319. }
  320. godot_gdnative_terminate_fn library_terminate_pointer;
  321. library_terminate_pointer = (godot_gdnative_terminate_fn)library_terminate;
  322. godot_gdnative_terminate_options options;
  323. options.in_editor = Engine::get_singleton()->is_editor_hint();
  324. library_terminate_pointer(&options);
  325. initialized = false;
  326. // GDNativeScriptLanguage::get_singleton()->initialized_libraries.erase(p_native_lib->path);
  327. OS::get_singleton()->close_dynamic_library(native_handle);
  328. native_handle = NULL;
  329. return true;
  330. }
  331. bool GDNative::is_initialized() const {
  332. return initialized;
  333. }
  334. void GDNativeCallRegistry::register_native_call_type(StringName p_call_type, native_call_cb p_callback) {
  335. native_calls.insert(p_call_type, p_callback);
  336. }
  337. Vector<StringName> GDNativeCallRegistry::get_native_call_types() {
  338. Vector<StringName> call_types;
  339. call_types.resize(native_calls.size());
  340. size_t idx = 0;
  341. for (Map<StringName, native_call_cb>::Element *E = native_calls.front(); E; E = E->next(), idx++) {
  342. call_types.write[idx] = E->key();
  343. }
  344. return call_types;
  345. }
  346. Variant GDNative::call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments) {
  347. Map<StringName, native_call_cb>::Element *E = GDNativeCallRegistry::singleton->native_calls.find(p_native_call_type);
  348. if (!E) {
  349. ERR_PRINT((String("No handler for native call type \"" + p_native_call_type) + "\" found").utf8().get_data());
  350. return Variant();
  351. }
  352. void *procedure_handle;
  353. Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(
  354. native_handle,
  355. p_procedure_name,
  356. procedure_handle);
  357. if (err != OK || procedure_handle == NULL) {
  358. return Variant();
  359. }
  360. godot_variant result = E->get()(procedure_handle, (godot_array *)&p_arguments);
  361. Variant res = *(Variant *)&result;
  362. godot_variant_destroy(&result);
  363. return res;
  364. }
  365. Error GDNative::get_symbol(StringName p_procedure_name, void *&r_handle, bool p_optional) const {
  366. if (!initialized) {
  367. ERR_PRINT("No valid library handle, can't get symbol from GDNative object");
  368. return ERR_CANT_OPEN;
  369. }
  370. Error result = OS::get_singleton()->get_dynamic_library_symbol_handle(
  371. native_handle,
  372. p_procedure_name,
  373. r_handle,
  374. p_optional);
  375. return result;
  376. }
  377. RES GDNativeLibraryResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  378. Ref<GDNativeLibrary> lib;
  379. lib.instance();
  380. Ref<ConfigFile> config = lib->get_config_file();
  381. Error err = config->load(p_path);
  382. if (r_error) {
  383. *r_error = err;
  384. }
  385. lib->set_config_file(config);
  386. return lib;
  387. }
  388. void GDNativeLibraryResourceLoader::get_recognized_extensions(List<String> *p_extensions) const {
  389. p_extensions->push_back("gdnlib");
  390. }
  391. bool GDNativeLibraryResourceLoader::handles_type(const String &p_type) const {
  392. return p_type == "GDNativeLibrary";
  393. }
  394. String GDNativeLibraryResourceLoader::get_resource_type(const String &p_path) const {
  395. String el = p_path.get_extension().to_lower();
  396. if (el == "gdnlib")
  397. return "GDNativeLibrary";
  398. return "";
  399. }
  400. Error GDNativeLibraryResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  401. Ref<GDNativeLibrary> lib = p_resource;
  402. if (lib.is_null()) {
  403. return ERR_INVALID_DATA;
  404. }
  405. Ref<ConfigFile> config = lib->get_config_file();
  406. config->set_value("general", "singleton", lib->is_singleton());
  407. config->set_value("general", "load_once", lib->should_load_once());
  408. config->set_value("general", "symbol_prefix", lib->get_symbol_prefix());
  409. config->set_value("general", "reloadable", lib->is_reloadable());
  410. return config->save(p_path);
  411. }
  412. bool GDNativeLibraryResourceSaver::recognize(const RES &p_resource) const {
  413. return Object::cast_to<GDNativeLibrary>(*p_resource) != NULL;
  414. }
  415. void GDNativeLibraryResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  416. if (Object::cast_to<GDNativeLibrary>(*p_resource) != NULL) {
  417. p_extensions->push_back("gdnlib");
  418. }
  419. }