gdnative.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* gdnative.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_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_ 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. load_once = p_load_once;
  84. }
  85. _FORCE_INLINE_ void set_singleton(bool p_singleton) {
  86. singleton = p_singleton;
  87. }
  88. _FORCE_INLINE_ void set_symbol_prefix(String p_symbol_prefix) {
  89. symbol_prefix = p_symbol_prefix;
  90. }
  91. _FORCE_INLINE_ void set_reloadable(bool p_reloadable) {
  92. reloadable = p_reloadable;
  93. }
  94. static void _bind_methods();
  95. };
  96. struct GDNativeCallRegistry {
  97. static GDNativeCallRegistry *singleton;
  98. inline static GDNativeCallRegistry *get_singleton() {
  99. return singleton;
  100. }
  101. inline GDNativeCallRegistry() :
  102. native_calls() {}
  103. Map<StringName, native_call_cb> native_calls;
  104. void register_native_call_type(StringName p_call_type, native_call_cb p_callback);
  105. Vector<StringName> get_native_call_types();
  106. };
  107. class GDNative : public Reference {
  108. GDCLASS(GDNative, Reference)
  109. Ref<GDNativeLibrary> library;
  110. void *native_handle;
  111. bool initialized;
  112. public:
  113. GDNative();
  114. ~GDNative();
  115. static void _bind_methods();
  116. void set_library(Ref<GDNativeLibrary> p_library);
  117. Ref<GDNativeLibrary> get_library() const;
  118. bool is_initialized() const;
  119. bool initialize();
  120. bool terminate();
  121. Variant call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments = Array());
  122. Error get_symbol(StringName p_procedure_name, void *&r_handle, bool p_optional = true) const;
  123. };
  124. class GDNativeLibraryResourceLoader : public ResourceFormatLoader {
  125. GDCLASS(GDNativeLibraryResourceLoader, ResourceFormatLoader)
  126. public:
  127. virtual RES load(const String &p_path, const String &p_original_path, Error *r_error);
  128. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  129. virtual bool handles_type(const String &p_type) const;
  130. virtual String get_resource_type(const String &p_path) const;
  131. };
  132. class GDNativeLibraryResourceSaver : public ResourceFormatSaver {
  133. GDCLASS(GDNativeLibraryResourceSaver, ResourceFormatSaver)
  134. public:
  135. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags);
  136. virtual bool recognize(const RES &p_resource) const;
  137. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  138. };
  139. #endif // GDNATIVE_H