gdnative.cpp 10.0 KB

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