native_extension.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*************************************************************************/
  2. /* native_extension.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef NATIVE_EXTENSION_H
  31. #define NATIVE_EXTENSION_H
  32. #include "core/extension/gdnative_interface.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/object/ref_counted.h"
  35. class NativeExtension : public Resource {
  36. GDCLASS(NativeExtension, Resource)
  37. void *library = nullptr; // pointer if valid,
  38. struct Extension {
  39. ObjectNativeExtension native_extension;
  40. };
  41. Map<StringName, Extension> extension_classes;
  42. static void _register_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs);
  43. static void _register_extension_class_method(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info);
  44. static void _register_extension_class_integer_constant(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value);
  45. static void _register_extension_class_property(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativePropertyInfo *p_info, const char *p_setter, const char *p_getter);
  46. static void _register_extension_class_property_group(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_group_name, const char *p_prefix);
  47. static void _register_extension_class_property_subgroup(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_subgroup_name, const char *p_prefix);
  48. static void _register_extension_class_signal(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count);
  49. static void _unregister_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name);
  50. GDNativeInitialization initialization;
  51. int32_t level_initialized = -1;
  52. protected:
  53. static void _bind_methods();
  54. public:
  55. static String get_extension_list_config_file();
  56. Error open_library(const String &p_path, const String &p_entry_symbol);
  57. void close_library();
  58. enum InitializationLevel {
  59. INITIALIZATION_LEVEL_CORE,
  60. INITIALIZATION_LEVEL_SERVERS,
  61. INITIALIZATION_LEVEL_SCENE,
  62. INITIALIZATION_LEVEL_EDITOR,
  63. INITIALIZATION_LEVEL_DRIVER,
  64. };
  65. bool is_library_open() const;
  66. InitializationLevel get_minimum_library_initialization_level() const;
  67. void initialize_library(InitializationLevel p_level);
  68. void deinitialize_library(InitializationLevel p_level);
  69. static void initialize_native_extensions();
  70. NativeExtension();
  71. ~NativeExtension();
  72. };
  73. VARIANT_ENUM_CAST(NativeExtension::InitializationLevel)
  74. class NativeExtensionResourceLoader : public ResourceFormatLoader {
  75. public:
  76. virtual RES load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  77. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  78. virtual bool handles_type(const String &p_type) const;
  79. virtual String get_resource_type(const String &p_path) const;
  80. };
  81. #endif // NATIVEEXTENSION_H