gdnative.cpp 18 KB

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