register_types.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ResourceFormatLoaderNativeScript *resource_loader_gdns = NULL;
  55. ResourceFormatSaverNativeScript *resource_saver_gdns = NULL;
  56. void register_nativescript_types() {
  57. native_script_language = memnew(NativeScriptLanguage);
  58. ClassDB::register_class<NativeScript>();
  59. ScriptServer::register_language(native_script_language);
  60. GDNativeCallRegistry::singleton->register_native_raw_call_type(native_script_language->_init_call_type, init_call_cb);
  61. resource_saver_gdns = memnew(ResourceFormatSaverNativeScript);
  62. ResourceSaver::add_resource_format_saver(resource_saver_gdns);
  63. resource_loader_gdns = memnew(ResourceFormatLoaderNativeScript);
  64. ResourceLoader::add_resource_format_loader(resource_loader_gdns);
  65. }
  66. void unregister_nativescript_types() {
  67. memdelete(resource_loader_gdns);
  68. memdelete(resource_saver_gdns);
  69. if (native_script_language) {
  70. ScriptServer::unregister_language(native_script_language);
  71. memdelete(native_script_language);
  72. }
  73. }