gdextension_manager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/extension/gdextension_library_loader.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/object/script_language.h"
  36. GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(const Ref<GDExtension> &p_extension, bool p_first_load) {
  37. if (level >= 0) { // Already initialized up to some level.
  38. int32_t minimum_level = 0;
  39. if (!p_first_load) {
  40. minimum_level = p_extension->get_minimum_library_initialization_level();
  41. if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
  42. return LOAD_STATUS_NEEDS_RESTART;
  43. }
  44. }
  45. // Initialize up to current level.
  46. for (int32_t i = minimum_level; i <= level; i++) {
  47. p_extension->initialize_library(GDExtension::InitializationLevel(i));
  48. }
  49. }
  50. for (const KeyValue<String, String> &kv : p_extension->class_icon_paths) {
  51. gdextension_class_icon_paths[kv.key] = kv.value;
  52. }
  53. #ifdef TOOLS_ENABLED
  54. // Signals that a new extension is loaded so GDScript can register new class names.
  55. emit_signal("extension_loaded", p_extension);
  56. #endif
  57. return LOAD_STATUS_OK;
  58. }
  59. GDExtensionManager::LoadStatus GDExtensionManager::_unload_extension_internal(const Ref<GDExtension> &p_extension) {
  60. #ifdef TOOLS_ENABLED
  61. // Signals that a new extension is unloading so GDScript can unregister class names.
  62. emit_signal("extension_unloading", p_extension);
  63. #endif
  64. if (level >= 0) { // Already initialized up to some level.
  65. // Deinitialize down from current level.
  66. for (int32_t i = level; i >= GDExtension::INITIALIZATION_LEVEL_CORE; i--) {
  67. p_extension->deinitialize_library(GDExtension::InitializationLevel(i));
  68. }
  69. }
  70. for (const KeyValue<String, String> &kv : p_extension->class_icon_paths) {
  71. gdextension_class_icon_paths.erase(kv.key);
  72. }
  73. return LOAD_STATUS_OK;
  74. }
  75. GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &p_path) {
  76. Ref<GDExtensionLibraryLoader> loader;
  77. loader.instantiate();
  78. return GDExtensionManager::get_singleton()->load_extension_with_loader(p_path, loader);
  79. }
  80. GDExtensionManager::LoadStatus GDExtensionManager::load_extension_with_loader(const String &p_path, const Ref<GDExtensionLoader> &p_loader) {
  81. DEV_ASSERT(p_loader.is_valid());
  82. if (gdextension_map.has(p_path)) {
  83. return LOAD_STATUS_ALREADY_LOADED;
  84. }
  85. Ref<GDExtension> extension;
  86. extension.instantiate();
  87. Error err = extension->open_library(p_path, p_loader);
  88. if (err != OK) {
  89. return LOAD_STATUS_FAILED;
  90. }
  91. LoadStatus status = _load_extension_internal(extension, true);
  92. if (status != LOAD_STATUS_OK) {
  93. return status;
  94. }
  95. extension->set_path(p_path);
  96. gdextension_map[p_path] = extension;
  97. return LOAD_STATUS_OK;
  98. }
  99. GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String &p_path) {
  100. #ifndef TOOLS_ENABLED
  101. ERR_FAIL_V_MSG(LOAD_STATUS_FAILED, "GDExtensions can only be reloaded in an editor build.");
  102. #else
  103. ERR_FAIL_COND_V_MSG(!Engine::get_singleton()->is_extension_reloading_enabled(), LOAD_STATUS_FAILED, "GDExtension reloading is disabled.");
  104. if (!gdextension_map.has(p_path)) {
  105. return LOAD_STATUS_NOT_LOADED;
  106. }
  107. Ref<GDExtension> extension = gdextension_map[p_path];
  108. 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));
  109. LoadStatus status;
  110. extension->prepare_reload();
  111. // Unload library if it's open. It may not be open if the developer made a
  112. // change that broke loading in a previous hot-reload attempt.
  113. if (extension->is_library_open()) {
  114. status = _unload_extension_internal(extension);
  115. if (status != LOAD_STATUS_OK) {
  116. // We need to clear these no matter what.
  117. extension->clear_instance_bindings();
  118. return status;
  119. }
  120. extension->clear_instance_bindings();
  121. extension->close_library();
  122. }
  123. Error err = extension->open_library(p_path, extension->loader);
  124. if (err != OK) {
  125. return LOAD_STATUS_FAILED;
  126. }
  127. status = _load_extension_internal(extension, false);
  128. if (status != LOAD_STATUS_OK) {
  129. return status;
  130. }
  131. extension->finish_reload();
  132. return LOAD_STATUS_OK;
  133. #endif
  134. }
  135. GDExtensionManager::LoadStatus GDExtensionManager::unload_extension(const String &p_path) {
  136. if (!gdextension_map.has(p_path)) {
  137. return LOAD_STATUS_NOT_LOADED;
  138. }
  139. Ref<GDExtension> extension = gdextension_map[p_path];
  140. LoadStatus status = _unload_extension_internal(extension);
  141. if (status != LOAD_STATUS_OK) {
  142. return status;
  143. }
  144. gdextension_map.erase(p_path);
  145. return LOAD_STATUS_OK;
  146. }
  147. bool GDExtensionManager::is_extension_loaded(const String &p_path) const {
  148. return gdextension_map.has(p_path);
  149. }
  150. Vector<String> GDExtensionManager::get_loaded_extensions() const {
  151. Vector<String> ret;
  152. for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  153. ret.push_back(E.key);
  154. }
  155. return ret;
  156. }
  157. Ref<GDExtension> GDExtensionManager::get_extension(const String &p_path) {
  158. HashMap<String, Ref<GDExtension>>::Iterator E = gdextension_map.find(p_path);
  159. ERR_FAIL_COND_V(!E, Ref<GDExtension>());
  160. return E->value;
  161. }
  162. bool GDExtensionManager::class_has_icon_path(const String &p_class) const {
  163. // TODO: Check that the icon belongs to a registered class somehow.
  164. return gdextension_class_icon_paths.has(p_class);
  165. }
  166. String GDExtensionManager::class_get_icon_path(const String &p_class) const {
  167. // TODO: Check that the icon belongs to a registered class somehow.
  168. if (gdextension_class_icon_paths.has(p_class)) {
  169. return gdextension_class_icon_paths[p_class];
  170. }
  171. return "";
  172. }
  173. void GDExtensionManager::initialize_extensions(GDExtension::InitializationLevel p_level) {
  174. ERR_FAIL_COND(int32_t(p_level) - 1 != level);
  175. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  176. E.value->initialize_library(p_level);
  177. }
  178. level = p_level;
  179. }
  180. void GDExtensionManager::deinitialize_extensions(GDExtension::InitializationLevel p_level) {
  181. ERR_FAIL_COND(int32_t(p_level) != level);
  182. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  183. E.value->deinitialize_library(p_level);
  184. }
  185. level = int32_t(p_level) - 1;
  186. }
  187. #ifdef TOOLS_ENABLED
  188. void GDExtensionManager::track_instance_binding(void *p_token, Object *p_object) {
  189. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  190. if (E.value.ptr() == p_token) {
  191. if (E.value->is_reloadable()) {
  192. E.value->track_instance_binding(p_object);
  193. return;
  194. }
  195. }
  196. }
  197. }
  198. void GDExtensionManager::untrack_instance_binding(void *p_token, Object *p_object) {
  199. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  200. if (E.value.ptr() == p_token) {
  201. if (E.value->is_reloadable()) {
  202. E.value->untrack_instance_binding(p_object);
  203. return;
  204. }
  205. }
  206. }
  207. }
  208. void GDExtensionManager::_reload_all_scripts() {
  209. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  210. ScriptServer::get_language(i)->reload_all_scripts();
  211. }
  212. }
  213. #endif // TOOLS_ENABLED
  214. void GDExtensionManager::load_extensions() {
  215. Ref<FileAccess> f = FileAccess::open(GDExtension::get_extension_list_config_file(), FileAccess::READ);
  216. while (f.is_valid() && !f->eof_reached()) {
  217. String s = f->get_line().strip_edges();
  218. if (!s.is_empty()) {
  219. LoadStatus err = load_extension(s);
  220. ERR_CONTINUE_MSG(err == LOAD_STATUS_FAILED, vformat("Error loading extension: '%s'.", s));
  221. }
  222. }
  223. OS::get_singleton()->load_platform_gdextensions();
  224. }
  225. void GDExtensionManager::reload_extensions() {
  226. #ifdef TOOLS_ENABLED
  227. bool reloaded = false;
  228. for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  229. if (!E.value->is_reloadable()) {
  230. continue;
  231. }
  232. if (E.value->has_library_changed()) {
  233. reloaded = true;
  234. reload_extension(E.value->get_path());
  235. }
  236. }
  237. if (reloaded) {
  238. emit_signal("extensions_reloaded");
  239. // Reload all scripts to clear out old references.
  240. callable_mp_static(&GDExtensionManager::_reload_all_scripts).call_deferred();
  241. }
  242. #endif
  243. }
  244. bool GDExtensionManager::ensure_extensions_loaded(const HashSet<String> &p_extensions) {
  245. Vector<String> extensions_added;
  246. Vector<String> extensions_removed;
  247. for (const String &E : p_extensions) {
  248. if (!is_extension_loaded(E)) {
  249. extensions_added.push_back(E);
  250. }
  251. }
  252. Vector<String> loaded_extensions = get_loaded_extensions();
  253. for (const String &loaded_extension : loaded_extensions) {
  254. if (!p_extensions.has(loaded_extension)) {
  255. // The extension may not have a .gdextension file.
  256. const Ref<GDExtension> extension = GDExtensionManager::get_singleton()->get_extension(loaded_extension);
  257. if (!extension->get_loader()->library_exists()) {
  258. extensions_removed.push_back(loaded_extension);
  259. }
  260. }
  261. }
  262. String extension_list_config_file = GDExtension::get_extension_list_config_file();
  263. if (p_extensions.size()) {
  264. if (extensions_added.size() || extensions_removed.size()) {
  265. // Extensions were added or removed.
  266. Ref<FileAccess> f = FileAccess::open(extension_list_config_file, FileAccess::WRITE);
  267. for (const String &E : p_extensions) {
  268. f->store_line(E);
  269. }
  270. }
  271. } else {
  272. if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) {
  273. // Extensions were removed.
  274. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  275. da->remove(extension_list_config_file);
  276. }
  277. }
  278. bool needs_restart = false;
  279. for (const String &extension : extensions_added) {
  280. GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->load_extension(extension);
  281. if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  282. needs_restart = true;
  283. }
  284. }
  285. for (const String &extension : extensions_removed) {
  286. GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->unload_extension(extension);
  287. if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  288. needs_restart = true;
  289. }
  290. }
  291. #ifdef TOOLS_ENABLED
  292. if (extensions_added.size() || extensions_removed.size()) {
  293. // Emitting extensions_reloaded so EditorNode can reload Inspector and regenerate documentation.
  294. emit_signal("extensions_reloaded");
  295. // Reload all scripts to clear out old references.
  296. callable_mp_static(&GDExtensionManager::_reload_all_scripts).call_deferred();
  297. }
  298. #endif
  299. return needs_restart;
  300. }
  301. GDExtensionManager *GDExtensionManager::get_singleton() {
  302. return singleton;
  303. }
  304. void GDExtensionManager::_bind_methods() {
  305. ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
  306. ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
  307. ClassDB::bind_method(D_METHOD("unload_extension", "path"), &GDExtensionManager::unload_extension);
  308. ClassDB::bind_method(D_METHOD("is_extension_loaded", "path"), &GDExtensionManager::is_extension_loaded);
  309. ClassDB::bind_method(D_METHOD("get_loaded_extensions"), &GDExtensionManager::get_loaded_extensions);
  310. ClassDB::bind_method(D_METHOD("get_extension", "path"), &GDExtensionManager::get_extension);
  311. BIND_ENUM_CONSTANT(LOAD_STATUS_OK);
  312. BIND_ENUM_CONSTANT(LOAD_STATUS_FAILED);
  313. BIND_ENUM_CONSTANT(LOAD_STATUS_ALREADY_LOADED);
  314. BIND_ENUM_CONSTANT(LOAD_STATUS_NOT_LOADED);
  315. BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART);
  316. ADD_SIGNAL(MethodInfo("extensions_reloaded"));
  317. ADD_SIGNAL(MethodInfo("extension_loaded", PropertyInfo(Variant::OBJECT, "extension", PROPERTY_HINT_RESOURCE_TYPE, "GDExtension")));
  318. ADD_SIGNAL(MethodInfo("extension_unloading", PropertyInfo(Variant::OBJECT, "extension", PROPERTY_HINT_RESOURCE_TYPE, "GDExtension")));
  319. }
  320. GDExtensionManager *GDExtensionManager::singleton = nullptr;
  321. GDExtensionManager::GDExtensionManager() {
  322. ERR_FAIL_COND(singleton != nullptr);
  323. singleton = this;
  324. #ifndef DISABLE_DEPRECATED
  325. GDExtensionCompatHashes::initialize();
  326. #endif
  327. }
  328. GDExtensionManager::~GDExtensionManager() {
  329. if (singleton == this) {
  330. singleton = nullptr;
  331. }
  332. #ifndef DISABLE_DEPRECATED
  333. GDExtensionCompatHashes::finalize();
  334. #endif
  335. }