2
0

gdextension_manager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**************************************************************************/
  2. /* gdextension_manager.cpp */
  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. #include "gdextension_manager.h"
  31. #include "core/extension/gdextension_compat_hashes.h"
  32. #include "core/io/file_access.h"
  33. #include "core/object/script_language.h"
  34. GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(const Ref<GDExtension> &p_extension) {
  35. if (level >= 0) { // Already initialized up to some level.
  36. int32_t minimum_level = p_extension->get_minimum_library_initialization_level();
  37. if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
  38. return LOAD_STATUS_NEEDS_RESTART;
  39. }
  40. // Initialize up to current level.
  41. for (int32_t i = minimum_level; i <= level; i++) {
  42. p_extension->initialize_library(GDExtension::InitializationLevel(i));
  43. }
  44. }
  45. for (const KeyValue<String, String> &kv : p_extension->class_icon_paths) {
  46. gdextension_class_icon_paths[kv.key] = kv.value;
  47. }
  48. return LOAD_STATUS_OK;
  49. }
  50. GDExtensionManager::LoadStatus GDExtensionManager::_unload_extension_internal(const Ref<GDExtension> &p_extension) {
  51. if (level >= 0) { // Already initialized up to some level.
  52. // Deinitialize down from current level.
  53. for (int32_t i = level; i >= GDExtension::INITIALIZATION_LEVEL_CORE; i--) {
  54. p_extension->deinitialize_library(GDExtension::InitializationLevel(i));
  55. }
  56. }
  57. for (const KeyValue<String, String> &kv : p_extension->class_icon_paths) {
  58. gdextension_class_icon_paths.erase(kv.key);
  59. }
  60. return LOAD_STATUS_OK;
  61. }
  62. GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &p_path) {
  63. if (gdextension_map.has(p_path)) {
  64. return LOAD_STATUS_ALREADY_LOADED;
  65. }
  66. Ref<GDExtension> extension = ResourceLoader::load(p_path);
  67. if (extension.is_null()) {
  68. return LOAD_STATUS_FAILED;
  69. }
  70. LoadStatus status = _load_extension_internal(extension);
  71. if (status != LOAD_STATUS_OK) {
  72. return status;
  73. }
  74. gdextension_map[p_path] = extension;
  75. return LOAD_STATUS_OK;
  76. }
  77. GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String &p_path) {
  78. #ifndef TOOLS_ENABLED
  79. ERR_FAIL_V_MSG(LOAD_STATUS_FAILED, "GDExtensions can only be reloaded in an editor build.");
  80. #else
  81. ERR_FAIL_COND_V_MSG(!Engine::get_singleton()->is_extension_reloading_enabled(), LOAD_STATUS_FAILED, "GDExtension reloading is disabled.");
  82. if (!gdextension_map.has(p_path)) {
  83. return LOAD_STATUS_NOT_LOADED;
  84. }
  85. Ref<GDExtension> extension = gdextension_map[p_path];
  86. ERR_FAIL_COND_V_MSG(!extension->is_reloadable(), LOAD_STATUS_FAILED, vformat("This GDExtension is not marked as 'reloadable' or doesn't support reloading: %s.", p_path));
  87. LoadStatus status;
  88. extension->prepare_reload();
  89. // Unload library if it's open. It may not be open if the developer made a
  90. // change that broke loading in a previous hot-reload attempt.
  91. if (extension->is_library_open()) {
  92. status = _unload_extension_internal(extension);
  93. if (status != LOAD_STATUS_OK) {
  94. // We need to clear these no matter what.
  95. extension->clear_instance_bindings();
  96. return status;
  97. }
  98. extension->clear_instance_bindings();
  99. extension->close_library();
  100. }
  101. Error err = GDExtensionResourceLoader::load_gdextension_resource(p_path, extension);
  102. if (err != OK) {
  103. return LOAD_STATUS_FAILED;
  104. }
  105. status = _load_extension_internal(extension);
  106. if (status != LOAD_STATUS_OK) {
  107. return status;
  108. }
  109. extension->finish_reload();
  110. return LOAD_STATUS_OK;
  111. #endif
  112. }
  113. GDExtensionManager::LoadStatus GDExtensionManager::unload_extension(const String &p_path) {
  114. if (!gdextension_map.has(p_path)) {
  115. return LOAD_STATUS_NOT_LOADED;
  116. }
  117. Ref<GDExtension> extension = gdextension_map[p_path];
  118. LoadStatus status = _unload_extension_internal(extension);
  119. if (status != LOAD_STATUS_OK) {
  120. return status;
  121. }
  122. gdextension_map.erase(p_path);
  123. return LOAD_STATUS_OK;
  124. }
  125. bool GDExtensionManager::is_extension_loaded(const String &p_path) const {
  126. return gdextension_map.has(p_path);
  127. }
  128. Vector<String> GDExtensionManager::get_loaded_extensions() const {
  129. Vector<String> ret;
  130. for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  131. ret.push_back(E.key);
  132. }
  133. return ret;
  134. }
  135. Ref<GDExtension> GDExtensionManager::get_extension(const String &p_path) {
  136. HashMap<String, Ref<GDExtension>>::Iterator E = gdextension_map.find(p_path);
  137. ERR_FAIL_COND_V(!E, Ref<GDExtension>());
  138. return E->value;
  139. }
  140. bool GDExtensionManager::class_has_icon_path(const String &p_class) const {
  141. // TODO: Check that the icon belongs to a registered class somehow.
  142. return gdextension_class_icon_paths.has(p_class);
  143. }
  144. String GDExtensionManager::class_get_icon_path(const String &p_class) const {
  145. // TODO: Check that the icon belongs to a registered class somehow.
  146. if (gdextension_class_icon_paths.has(p_class)) {
  147. return gdextension_class_icon_paths[p_class];
  148. }
  149. return "";
  150. }
  151. void GDExtensionManager::initialize_extensions(GDExtension::InitializationLevel p_level) {
  152. ERR_FAIL_COND(int32_t(p_level) - 1 != level);
  153. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  154. E.value->initialize_library(p_level);
  155. }
  156. level = p_level;
  157. }
  158. void GDExtensionManager::deinitialize_extensions(GDExtension::InitializationLevel p_level) {
  159. ERR_FAIL_COND(int32_t(p_level) != level);
  160. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  161. E.value->deinitialize_library(p_level);
  162. }
  163. level = int32_t(p_level) - 1;
  164. }
  165. #ifdef TOOLS_ENABLED
  166. void GDExtensionManager::track_instance_binding(void *p_token, Object *p_object) {
  167. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  168. if (E.value.ptr() == p_token) {
  169. if (E.value->is_reloadable()) {
  170. E.value->track_instance_binding(p_object);
  171. return;
  172. }
  173. }
  174. }
  175. }
  176. void GDExtensionManager::untrack_instance_binding(void *p_token, Object *p_object) {
  177. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  178. if (E.value.ptr() == p_token) {
  179. if (E.value->is_reloadable()) {
  180. E.value->untrack_instance_binding(p_object);
  181. return;
  182. }
  183. }
  184. }
  185. }
  186. void GDExtensionManager::_reload_all_scripts() {
  187. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  188. ScriptServer::get_language(i)->reload_all_scripts();
  189. }
  190. }
  191. #endif // TOOLS_ENABLED
  192. void GDExtensionManager::load_extensions() {
  193. Ref<FileAccess> f = FileAccess::open(GDExtension::get_extension_list_config_file(), FileAccess::READ);
  194. while (f.is_valid() && !f->eof_reached()) {
  195. String s = f->get_line().strip_edges();
  196. if (!s.is_empty()) {
  197. LoadStatus err = load_extension(s);
  198. ERR_CONTINUE_MSG(err == LOAD_STATUS_FAILED, "Error loading extension: " + s);
  199. }
  200. }
  201. OS::get_singleton()->load_platform_gdextensions();
  202. }
  203. void GDExtensionManager::reload_extensions() {
  204. #ifdef TOOLS_ENABLED
  205. bool reloaded = false;
  206. for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  207. if (!E.value->is_reloadable()) {
  208. continue;
  209. }
  210. if (E.value->has_library_changed()) {
  211. reloaded = true;
  212. reload_extension(E.value->get_path());
  213. }
  214. }
  215. if (reloaded) {
  216. emit_signal("extensions_reloaded");
  217. // Reload all scripts to clear out old references.
  218. callable_mp_static(&GDExtensionManager::_reload_all_scripts).call_deferred();
  219. }
  220. #endif
  221. }
  222. GDExtensionManager *GDExtensionManager::get_singleton() {
  223. return singleton;
  224. }
  225. void GDExtensionManager::_bind_methods() {
  226. ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
  227. ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
  228. ClassDB::bind_method(D_METHOD("unload_extension", "path"), &GDExtensionManager::unload_extension);
  229. ClassDB::bind_method(D_METHOD("is_extension_loaded", "path"), &GDExtensionManager::is_extension_loaded);
  230. ClassDB::bind_method(D_METHOD("get_loaded_extensions"), &GDExtensionManager::get_loaded_extensions);
  231. ClassDB::bind_method(D_METHOD("get_extension", "path"), &GDExtensionManager::get_extension);
  232. BIND_ENUM_CONSTANT(LOAD_STATUS_OK);
  233. BIND_ENUM_CONSTANT(LOAD_STATUS_FAILED);
  234. BIND_ENUM_CONSTANT(LOAD_STATUS_ALREADY_LOADED);
  235. BIND_ENUM_CONSTANT(LOAD_STATUS_NOT_LOADED);
  236. BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART);
  237. ADD_SIGNAL(MethodInfo("extensions_reloaded"));
  238. }
  239. GDExtensionManager *GDExtensionManager::singleton = nullptr;
  240. GDExtensionManager::GDExtensionManager() {
  241. ERR_FAIL_COND(singleton != nullptr);
  242. singleton = this;
  243. #ifndef DISABLE_DEPRECATED
  244. GDExtensionCompatHashes::initialize();
  245. #endif
  246. }
  247. GDExtensionManager::~GDExtensionManager() {
  248. #ifndef DISABLE_DEPRECATED
  249. GDExtensionCompatHashes::finalize();
  250. #endif
  251. }