gdnative.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* gdnative.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 GDNATIVE_H
  31. #define GDNATIVE_H
  32. #include "core/io/resource.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. #include "core/os/thread_safe.h"
  36. #include "gdnative/gdnative.h"
  37. #include "gdnative_api_struct.gen.h"
  38. #include "core/io/config_file.h"
  39. class GDNativeLibraryResourceLoader;
  40. class GDNative;
  41. class GDNativeLibrary : public Resource {
  42. GDCLASS(GDNativeLibrary, Resource);
  43. static Map<String, Vector<Ref<GDNative>>> loaded_libraries;
  44. friend class GDNativeLibraryResourceLoader;
  45. friend class GDNative;
  46. Ref<ConfigFile> config_file;
  47. String current_library_path;
  48. Vector<String> current_dependencies;
  49. bool singleton;
  50. bool load_once;
  51. String symbol_prefix;
  52. bool reloadable;
  53. public:
  54. virtual void reset_state() override;
  55. GDNativeLibrary();
  56. ~GDNativeLibrary();
  57. virtual bool _set(const StringName &p_name, const Variant &p_property);
  58. virtual bool _get(const StringName &p_name, Variant &r_property) const;
  59. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  60. _FORCE_INLINE_ Ref<ConfigFile> get_config_file() { return config_file; }
  61. void set_config_file(Ref<ConfigFile> p_config_file);
  62. // things that change per-platform
  63. // so there are no setters for this
  64. _FORCE_INLINE_ String get_current_library_path() const {
  65. return current_library_path;
  66. }
  67. _FORCE_INLINE_ Vector<String> get_current_dependencies() const {
  68. return current_dependencies;
  69. }
  70. // things that are a property of the library itself, not platform specific
  71. _FORCE_INLINE_ bool should_load_once() const {
  72. return load_once;
  73. }
  74. _FORCE_INLINE_ bool is_singleton() const {
  75. return singleton;
  76. }
  77. _FORCE_INLINE_ String get_symbol_prefix() const {
  78. return symbol_prefix;
  79. }
  80. _FORCE_INLINE_ bool is_reloadable() const {
  81. return reloadable;
  82. }
  83. _FORCE_INLINE_ void set_load_once(bool p_load_once) {
  84. config_file->set_value("general", "load_once", p_load_once);
  85. load_once = p_load_once;
  86. }
  87. _FORCE_INLINE_ void set_singleton(bool p_singleton) {
  88. config_file->set_value("general", "singleton", p_singleton);
  89. singleton = p_singleton;
  90. }
  91. _FORCE_INLINE_ void set_symbol_prefix(String p_symbol_prefix) {
  92. config_file->set_value("general", "symbol_prefix", p_symbol_prefix);
  93. symbol_prefix = p_symbol_prefix;
  94. }
  95. _FORCE_INLINE_ void set_reloadable(bool p_reloadable) {
  96. config_file->set_value("general", "reloadable", p_reloadable);
  97. reloadable = p_reloadable;
  98. }
  99. static void _bind_methods();
  100. };
  101. struct GDNativeCallRegistry {
  102. static GDNativeCallRegistry *singleton;
  103. inline static GDNativeCallRegistry *get_singleton() {
  104. return singleton;
  105. }
  106. inline GDNativeCallRegistry() :
  107. native_calls() {}
  108. Map<StringName, native_call_cb> native_calls;
  109. void register_native_call_type(StringName p_call_type, native_call_cb p_callback);
  110. Vector<StringName> get_native_call_types();
  111. };
  112. class GDNative : public Reference {
  113. GDCLASS(GDNative, Reference);
  114. Ref<GDNativeLibrary> library;
  115. void *native_handle;
  116. bool initialized;
  117. public:
  118. GDNative();
  119. ~GDNative();
  120. static void _bind_methods();
  121. void set_library(Ref<GDNativeLibrary> p_library);
  122. Ref<GDNativeLibrary> get_library() const;
  123. bool is_initialized() const;
  124. bool initialize();
  125. bool terminate();
  126. Variant call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments = Array());
  127. Error get_symbol(StringName p_procedure_name, void *&r_handle, bool p_optional = true) const;
  128. };
  129. class GDNativeLibraryResourceLoader : public ResourceFormatLoader {
  130. public:
  131. 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);
  132. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  133. virtual bool handles_type(const String &p_type) const;
  134. virtual String get_resource_type(const String &p_path) const;
  135. };
  136. class GDNativeLibraryResourceSaver : public ResourceFormatSaver {
  137. public:
  138. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags);
  139. virtual bool recognize(const RES &p_resource) const;
  140. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  141. };
  142. #endif // GDNATIVE_H