gdnative.h 6.2 KB

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