register_types.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************************/
  2. /* register_types.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 "register_types.h"
  31. #include "io/resource_loader.h"
  32. #include "io/resource_saver.h"
  33. #include "nativescript.h"
  34. #include "core/os/os.h"
  35. NativeScriptLanguage *native_script_language;
  36. typedef void (*native_script_init_fn)(void *);
  37. void init_call_cb(void *p_handle, godot_string *p_proc_name, void *p_data, int p_num_args, void **args, void *r_ret) {
  38. if (p_handle == NULL) {
  39. ERR_PRINT("No valid library handle, can't call nativescript init procedure");
  40. return;
  41. }
  42. void *library_proc;
  43. Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(
  44. p_handle,
  45. *(String *)p_proc_name,
  46. library_proc,
  47. true); // we print our own message
  48. if (err != OK) {
  49. ERR_PRINT((String("GDNative procedure \"" + *(String *)p_proc_name) + "\" does not exists and can't be called").utf8().get_data());
  50. return;
  51. }
  52. native_script_init_fn fn = (native_script_init_fn)library_proc;
  53. fn(args[0]);
  54. }
  55. #ifndef NO_THREADS
  56. typedef void (*native_script_empty_callback)();
  57. void thread_call_cb(void *p_handle, godot_string *p_proc_name, void *p_data, int p_num_args, void **args, void *r_ret) {
  58. if (p_handle == NULL) {
  59. ERR_PRINT("No valid library handle, can't call nativescript thread enter/exit callback");
  60. return;
  61. }
  62. void *library_proc;
  63. Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(
  64. p_handle,
  65. *(String *)p_proc_name,
  66. library_proc,
  67. true);
  68. if (err != OK) {
  69. // it's fine if thread callbacks are not present in the library.
  70. return;
  71. }
  72. native_script_empty_callback fn = (native_script_empty_callback)library_proc;
  73. fn();
  74. }
  75. #endif // NO_THREADS
  76. ResourceFormatLoaderNativeScript *resource_loader_gdns = NULL;
  77. ResourceFormatSaverNativeScript *resource_saver_gdns = NULL;
  78. void register_nativescript_types() {
  79. native_script_language = memnew(NativeScriptLanguage);
  80. ClassDB::register_class<NativeScript>();
  81. ScriptServer::register_language(native_script_language);
  82. GDNativeCallRegistry::singleton->register_native_raw_call_type(native_script_language->_init_call_type, init_call_cb);
  83. #ifndef NO_THREADS
  84. GDNativeCallRegistry::singleton->register_native_raw_call_type(native_script_language->_thread_cb_call_type, thread_call_cb);
  85. #endif
  86. resource_saver_gdns = memnew(ResourceFormatSaverNativeScript);
  87. ResourceSaver::add_resource_format_saver(resource_saver_gdns);
  88. resource_loader_gdns = memnew(ResourceFormatLoaderNativeScript);
  89. ResourceLoader::add_resource_format_loader(resource_loader_gdns);
  90. }
  91. void unregister_nativescript_types() {
  92. memdelete(resource_loader_gdns);
  93. memdelete(resource_saver_gdns);
  94. if (native_script_language) {
  95. ScriptServer::unregister_language(native_script_language);
  96. memdelete(native_script_language);
  97. }
  98. }