gdextension_manager.cpp 17 KB

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