register_types.cpp 4.7 KB

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