gdnative.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 = "godot_gdnative_init";
  38. const String terminate_symbol = "godot_gdnative_terminate";
  39. // Defined in gdnative_api_struct.gen.cpp
  40. extern const godot_gdnative_api_struct api_struct;
  41. String GDNativeLibrary::platform_names[NUM_PLATFORMS + 1] = {
  42. "X11_32bit",
  43. "X11_64bit",
  44. "Windows_32bit",
  45. "Windows_64bit",
  46. "OSX",
  47. "Android",
  48. "iOS_32bit",
  49. "iOS_64bit",
  50. "WebAssembly",
  51. ""
  52. };
  53. String GDNativeLibrary::platform_lib_ext[NUM_PLATFORMS + 1] = {
  54. "so",
  55. "so",
  56. "dll",
  57. "dll",
  58. "dylib",
  59. "so",
  60. "dylib",
  61. "dylib",
  62. "wasm",
  63. ""
  64. };
  65. GDNativeLibrary::Platform GDNativeLibrary::current_platform =
  66. #if defined(X11_ENABLED)
  67. (sizeof(void *) == 8 ? X11_64BIT : X11_32BIT);
  68. #elif defined(WINDOWS_ENABLED)
  69. (sizeof(void *) == 8 ? WINDOWS_64BIT : WINDOWS_32BIT);
  70. #elif defined(OSX_ENABLED)
  71. OSX;
  72. #elif defined(IPHONE_ENABLED)
  73. (sizeof(void *) == 8 ? IOS_64BIT : IOS_32BIT);
  74. #elif defined(ANDROID_ENABLED)
  75. ANDROID;
  76. #elif defined(JAVASCRIPT_ENABLED)
  77. WASM;
  78. #else
  79. NUM_PLATFORMS;
  80. #endif
  81. GDNativeLibrary::GDNativeLibrary()
  82. : library_paths(), singleton_gdnative(false) {
  83. }
  84. GDNativeLibrary::~GDNativeLibrary() {
  85. }
  86. void GDNativeLibrary::_bind_methods() {
  87. ClassDB::bind_method(D_METHOD("set_library_path", "platform", "path"), &GDNativeLibrary::set_library_path);
  88. ClassDB::bind_method(D_METHOD("get_library_path", "platform"), &GDNativeLibrary::get_library_path);
  89. ClassDB::bind_method(D_METHOD("get_active_library_path"), &GDNativeLibrary::get_active_library_path);
  90. ClassDB::bind_method(D_METHOD("is_singleton_gdnative"), &GDNativeLibrary::is_singleton_gdnative);
  91. ClassDB::bind_method(D_METHOD("set_singleton_gdnative", "singleton"), &GDNativeLibrary::set_singleton_gdnative);
  92. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "singleton_gdnative"), "set_singleton_gdnative", "is_singleton_gdnative");
  93. }
  94. bool GDNativeLibrary::_set(const StringName &p_name, const Variant &p_value) {
  95. String name = p_name;
  96. if (name.begins_with("platform/")) {
  97. set_library_path(name.get_slice("/", 1), p_value);
  98. return true;
  99. }
  100. return false;
  101. }
  102. bool GDNativeLibrary::_get(const StringName &p_name, Variant &r_ret) const {
  103. String name = p_name;
  104. if (name.begins_with("platform/")) {
  105. r_ret = get_library_path(name.get_slice("/", 1));
  106. return true;
  107. }
  108. return false;
  109. }
  110. void GDNativeLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  111. for (int i = 0; i < NUM_PLATFORMS; i++) {
  112. p_list->push_back(PropertyInfo(Variant::STRING,
  113. "platform/" + platform_names[i],
  114. PROPERTY_HINT_FILE,
  115. "*." + platform_lib_ext[i]));
  116. }
  117. }
  118. void GDNativeLibrary::set_library_path(StringName p_platform, String p_path) {
  119. int i;
  120. for (i = 0; i <= NUM_PLATFORMS; i++) {
  121. if (i == NUM_PLATFORMS) break;
  122. if (platform_names[i] == p_platform) {
  123. break;
  124. }
  125. }
  126. if (i == NUM_PLATFORMS) {
  127. ERR_EXPLAIN(String("No such platform: ") + p_platform);
  128. ERR_FAIL();
  129. }
  130. library_paths[i] = p_path;
  131. }
  132. String GDNativeLibrary::get_library_path(StringName p_platform) const {
  133. int i;
  134. for (i = 0; i <= NUM_PLATFORMS; i++) {
  135. if (i == NUM_PLATFORMS) break;
  136. if (platform_names[i] == p_platform) {
  137. break;
  138. }
  139. }
  140. if (i == NUM_PLATFORMS) {
  141. ERR_EXPLAIN(String("No such platform: ") + p_platform);
  142. ERR_FAIL_V("");
  143. }
  144. return library_paths[i];
  145. }
  146. String GDNativeLibrary::get_active_library_path() const {
  147. if (GDNativeLibrary::current_platform != NUM_PLATFORMS) {
  148. return library_paths[GDNativeLibrary::current_platform];
  149. }
  150. return "";
  151. }
  152. GDNative::GDNative() {
  153. native_handle = NULL;
  154. }
  155. GDNative::~GDNative() {
  156. }
  157. extern "C" void _api_anchor();
  158. void GDNative::_compile_dummy_for_api() {
  159. _api_anchor();
  160. }
  161. void GDNative::_bind_methods() {
  162. ClassDB::bind_method(D_METHOD("set_library", "library"), &GDNative::set_library);
  163. ClassDB::bind_method(D_METHOD("get_library"), &GDNative::get_library);
  164. ClassDB::bind_method(D_METHOD("initialize"), &GDNative::initialize);
  165. ClassDB::bind_method(D_METHOD("terminate"), &GDNative::terminate);
  166. // TODO(karroffel): get_native_(raw_)call_types binding?
  167. // TODO(karroffel): make this a varargs function?
  168. ClassDB::bind_method(D_METHOD("call_native", "procedure_name", "arguments"), &GDNative::call_native);
  169. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  170. }
  171. void GDNative::set_library(Ref<GDNativeLibrary> p_library) {
  172. ERR_EXPLAIN("Tried to change library of GDNative when it is already set");
  173. ERR_FAIL_COND(library.is_valid());
  174. library = p_library;
  175. }
  176. Ref<GDNativeLibrary> GDNative::get_library() {
  177. return library;
  178. }
  179. bool GDNative::initialize() {
  180. if (library.is_null()) {
  181. ERR_PRINT("No library set, can't initialize GDNative object");
  182. return false;
  183. }
  184. String lib_path = library->get_active_library_path();
  185. if (lib_path.empty()) {
  186. ERR_PRINT("No library set for this platform");
  187. return false;
  188. }
  189. #ifdef IPHONE_ENABLED
  190. String path = lib_path.replace("res://", "dylibs/");
  191. #else
  192. String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
  193. #endif
  194. Error err = OS::get_singleton()->open_dynamic_library(path, native_handle);
  195. if (err != OK) {
  196. return false;
  197. }
  198. void *library_init;
  199. err = OS::get_singleton()->get_dynamic_library_symbol_handle(
  200. native_handle,
  201. init_symbol,
  202. library_init);
  203. if (err || !library_init) {
  204. OS::get_singleton()->close_dynamic_library(native_handle);
  205. native_handle = NULL;
  206. ERR_PRINT("Failed to obtain godot_gdnative_init symbol");
  207. return false;
  208. }
  209. godot_gdnative_init_fn library_init_fpointer;
  210. library_init_fpointer = (godot_gdnative_init_fn)library_init;
  211. godot_gdnative_init_options options;
  212. options.api_struct = &api_struct;
  213. options.in_editor = Engine::get_singleton()->is_editor_hint();
  214. options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  215. options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  216. options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE);
  217. options.gd_native_library = (godot_object *)(get_library().ptr());
  218. options.active_library_path = (godot_string *)&path;
  219. library_init_fpointer(&options);
  220. return true;
  221. }
  222. bool GDNative::terminate() {
  223. if (native_handle == NULL) {
  224. ERR_PRINT("No valid library handle, can't terminate GDNative object");
  225. return false;
  226. }
  227. void *library_terminate;
  228. Error error = OS::get_singleton()->get_dynamic_library_symbol_handle(
  229. native_handle,
  230. terminate_symbol,
  231. library_terminate);
  232. if (error) {
  233. OS::get_singleton()->close_dynamic_library(native_handle);
  234. native_handle = NULL;
  235. return true;
  236. }
  237. godot_gdnative_terminate_fn library_terminate_pointer;
  238. library_terminate_pointer = (godot_gdnative_terminate_fn)library_terminate;
  239. // TODO(karroffel): remove this? Should be part of NativeScript, not
  240. // GDNative IMO
  241. godot_gdnative_terminate_options options;
  242. options.in_editor = Engine::get_singleton()->is_editor_hint();
  243. library_terminate_pointer(&options);
  244. // GDNativeScriptLanguage::get_singleton()->initialized_libraries.erase(p_native_lib->path);
  245. OS::get_singleton()->close_dynamic_library(native_handle);
  246. native_handle = NULL;
  247. return true;
  248. }
  249. bool GDNative::is_initialized() {
  250. return (native_handle != NULL);
  251. }
  252. void GDNativeCallRegistry::register_native_call_type(StringName p_call_type, native_call_cb p_callback) {
  253. native_calls.insert(p_call_type, p_callback);
  254. }
  255. void GDNativeCallRegistry::register_native_raw_call_type(StringName p_raw_call_type, native_raw_call_cb p_callback) {
  256. native_raw_calls.insert(p_raw_call_type, p_callback);
  257. }
  258. Vector<StringName> GDNativeCallRegistry::get_native_call_types() {
  259. Vector<StringName> call_types;
  260. call_types.resize(native_calls.size());
  261. size_t idx = 0;
  262. for (Map<StringName, native_call_cb>::Element *E = native_calls.front(); E; E = E->next(), idx++) {
  263. call_types[idx] = E->key();
  264. }
  265. return call_types;
  266. }
  267. Vector<StringName> GDNativeCallRegistry::get_native_raw_call_types() {
  268. Vector<StringName> call_types;
  269. call_types.resize(native_raw_calls.size());
  270. size_t idx = 0;
  271. for (Map<StringName, native_raw_call_cb>::Element *E = native_raw_calls.front(); E; E = E->next(), idx++) {
  272. call_types[idx] = E->key();
  273. }
  274. return call_types;
  275. }
  276. Variant GDNative::call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments) {
  277. Map<StringName, native_call_cb>::Element *E = GDNativeCallRegistry::singleton->native_calls.find(p_native_call_type);
  278. if (!E) {
  279. ERR_PRINT((String("No handler for native call type \"" + p_native_call_type) + "\" found").utf8().get_data());
  280. return Variant();
  281. }
  282. String procedure_name = p_procedure_name;
  283. godot_variant result = E->get()(native_handle, (godot_string *)&procedure_name, (godot_array *)&p_arguments);
  284. return *(Variant *)&result;
  285. }
  286. void GDNative::call_native_raw(StringName p_raw_call_type, StringName p_procedure_name, void *data, int num_args, void **args, void *r_return) {
  287. Map<StringName, native_raw_call_cb>::Element *E = GDNativeCallRegistry::singleton->native_raw_calls.find(p_raw_call_type);
  288. if (!E) {
  289. ERR_PRINT((String("No handler for native raw call type \"" + p_raw_call_type) + "\" found").utf8().get_data());
  290. return;
  291. }
  292. String procedure_name = p_procedure_name;
  293. E->get()(native_handle, (godot_string *)&procedure_name, data, num_args, args, r_return);
  294. }