gdextension_manager.cpp 15 KB

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