gdnative.cpp 11 KB

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