gdextension_manager.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/io/file_access.h"
  32. GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &p_path) {
  33. if (gdextension_map.has(p_path)) {
  34. return LOAD_STATUS_ALREADY_LOADED;
  35. }
  36. Ref<GDExtension> extension = ResourceLoader::load(p_path);
  37. if (extension.is_null()) {
  38. return LOAD_STATUS_FAILED;
  39. }
  40. if (level >= 0) { // Already initialized up to some level.
  41. int32_t minimum_level = extension->get_minimum_library_initialization_level();
  42. if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
  43. return LOAD_STATUS_NEEDS_RESTART;
  44. }
  45. // Initialize up to current level.
  46. for (int32_t i = minimum_level; i <= level; i++) {
  47. extension->initialize_library(GDExtension::InitializationLevel(i));
  48. }
  49. }
  50. for (const KeyValue<String, String> &kv : extension->class_icon_paths) {
  51. gdextension_class_icon_paths[kv.key] = kv.value;
  52. }
  53. gdextension_map[p_path] = extension;
  54. return LOAD_STATUS_OK;
  55. }
  56. GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String &p_path) {
  57. return LOAD_STATUS_OK; //TODO
  58. }
  59. GDExtensionManager::LoadStatus GDExtensionManager::unload_extension(const String &p_path) {
  60. if (!gdextension_map.has(p_path)) {
  61. return LOAD_STATUS_NOT_LOADED;
  62. }
  63. Ref<GDExtension> extension = gdextension_map[p_path];
  64. if (level >= 0) { // Already initialized up to some level.
  65. int32_t minimum_level = extension->get_minimum_library_initialization_level();
  66. if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
  67. return LOAD_STATUS_NEEDS_RESTART;
  68. }
  69. // Deinitialize down to current level.
  70. for (int32_t i = level; i >= minimum_level; i--) {
  71. extension->deinitialize_library(GDExtension::InitializationLevel(i));
  72. }
  73. }
  74. for (const KeyValue<String, String> &kv : extension->class_icon_paths) {
  75. gdextension_class_icon_paths.erase(kv.key);
  76. }
  77. gdextension_map.erase(p_path);
  78. return LOAD_STATUS_OK;
  79. }
  80. bool GDExtensionManager::is_extension_loaded(const String &p_path) const {
  81. return gdextension_map.has(p_path);
  82. }
  83. Vector<String> GDExtensionManager::get_loaded_extensions() const {
  84. Vector<String> ret;
  85. for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  86. ret.push_back(E.key);
  87. }
  88. return ret;
  89. }
  90. Ref<GDExtension> GDExtensionManager::get_extension(const String &p_path) {
  91. HashMap<String, Ref<GDExtension>>::Iterator E = gdextension_map.find(p_path);
  92. ERR_FAIL_COND_V(!E, Ref<GDExtension>());
  93. return E->value;
  94. }
  95. bool GDExtensionManager::class_has_icon_path(const String &p_class) const {
  96. // TODO: Check that the icon belongs to a registered class somehow.
  97. return gdextension_class_icon_paths.has(p_class);
  98. }
  99. String GDExtensionManager::class_get_icon_path(const String &p_class) const {
  100. // TODO: Check that the icon belongs to a registered class somehow.
  101. if (gdextension_class_icon_paths.has(p_class)) {
  102. return gdextension_class_icon_paths[p_class];
  103. }
  104. return "";
  105. }
  106. void GDExtensionManager::initialize_extensions(GDExtension::InitializationLevel p_level) {
  107. ERR_FAIL_COND(int32_t(p_level) - 1 != level);
  108. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  109. E.value->initialize_library(p_level);
  110. }
  111. level = p_level;
  112. }
  113. void GDExtensionManager::deinitialize_extensions(GDExtension::InitializationLevel p_level) {
  114. ERR_FAIL_COND(int32_t(p_level) != level);
  115. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  116. E.value->deinitialize_library(p_level);
  117. }
  118. level = int32_t(p_level) - 1;
  119. }
  120. void GDExtensionManager::load_extensions() {
  121. Ref<FileAccess> f = FileAccess::open(GDExtension::get_extension_list_config_file(), FileAccess::READ);
  122. while (f.is_valid() && !f->eof_reached()) {
  123. String s = f->get_line().strip_edges();
  124. if (!s.is_empty()) {
  125. LoadStatus err = load_extension(s);
  126. ERR_CONTINUE_MSG(err == LOAD_STATUS_FAILED, "Error loading extension: " + s);
  127. }
  128. }
  129. }
  130. GDExtensionManager *GDExtensionManager::get_singleton() {
  131. return singleton;
  132. }
  133. void GDExtensionManager::_bind_methods() {
  134. ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
  135. ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
  136. ClassDB::bind_method(D_METHOD("unload_extension", "path"), &GDExtensionManager::unload_extension);
  137. ClassDB::bind_method(D_METHOD("is_extension_loaded", "path"), &GDExtensionManager::is_extension_loaded);
  138. ClassDB::bind_method(D_METHOD("get_loaded_extensions"), &GDExtensionManager::get_loaded_extensions);
  139. ClassDB::bind_method(D_METHOD("get_extension", "path"), &GDExtensionManager::get_extension);
  140. BIND_ENUM_CONSTANT(LOAD_STATUS_OK);
  141. BIND_ENUM_CONSTANT(LOAD_STATUS_FAILED);
  142. BIND_ENUM_CONSTANT(LOAD_STATUS_ALREADY_LOADED);
  143. BIND_ENUM_CONSTANT(LOAD_STATUS_NOT_LOADED);
  144. BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART);
  145. }
  146. GDExtensionManager *GDExtensionManager::singleton = nullptr;
  147. GDExtensionManager::GDExtensionManager() {
  148. ERR_FAIL_COND(singleton != nullptr);
  149. singleton = this;
  150. }