gdextension_manager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. gdextension_map[p_path] = extension;
  51. return LOAD_STATUS_OK;
  52. }
  53. GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String &p_path) {
  54. return LOAD_STATUS_OK; //TODO
  55. }
  56. GDExtensionManager::LoadStatus GDExtensionManager::unload_extension(const String &p_path) {
  57. if (!gdextension_map.has(p_path)) {
  58. return LOAD_STATUS_NOT_LOADED;
  59. }
  60. Ref<GDExtension> extension = gdextension_map[p_path];
  61. if (level >= 0) { // Already initialized up to some level.
  62. int32_t minimum_level = extension->get_minimum_library_initialization_level();
  63. if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
  64. return LOAD_STATUS_NEEDS_RESTART;
  65. }
  66. // Deinitialize down to current level.
  67. for (int32_t i = level; i >= minimum_level; i--) {
  68. extension->deinitialize_library(GDExtension::InitializationLevel(i));
  69. }
  70. }
  71. gdextension_map.erase(p_path);
  72. return LOAD_STATUS_OK;
  73. }
  74. bool GDExtensionManager::is_extension_loaded(const String &p_path) const {
  75. return gdextension_map.has(p_path);
  76. }
  77. Vector<String> GDExtensionManager::get_loaded_extensions() const {
  78. Vector<String> ret;
  79. for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  80. ret.push_back(E.key);
  81. }
  82. return ret;
  83. }
  84. Ref<GDExtension> GDExtensionManager::get_extension(const String &p_path) {
  85. HashMap<String, Ref<GDExtension>>::Iterator E = gdextension_map.find(p_path);
  86. ERR_FAIL_COND_V(!E, Ref<GDExtension>());
  87. return E->value;
  88. }
  89. void GDExtensionManager::initialize_extensions(GDExtension::InitializationLevel p_level) {
  90. ERR_FAIL_COND(int32_t(p_level) - 1 != level);
  91. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  92. E.value->initialize_library(p_level);
  93. }
  94. level = p_level;
  95. }
  96. void GDExtensionManager::deinitialize_extensions(GDExtension::InitializationLevel p_level) {
  97. ERR_FAIL_COND(int32_t(p_level) != level);
  98. for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
  99. E.value->deinitialize_library(p_level);
  100. }
  101. level = int32_t(p_level) - 1;
  102. }
  103. void GDExtensionManager::load_extensions() {
  104. Ref<FileAccess> f = FileAccess::open(GDExtension::get_extension_list_config_file(), FileAccess::READ);
  105. while (f.is_valid() && !f->eof_reached()) {
  106. String s = f->get_line().strip_edges();
  107. if (!s.is_empty()) {
  108. LoadStatus err = load_extension(s);
  109. ERR_CONTINUE_MSG(err == LOAD_STATUS_FAILED, "Error loading extension: " + s);
  110. }
  111. }
  112. }
  113. GDExtensionManager *GDExtensionManager::get_singleton() {
  114. return singleton;
  115. }
  116. void GDExtensionManager::_bind_methods() {
  117. ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
  118. ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
  119. ClassDB::bind_method(D_METHOD("unload_extension", "path"), &GDExtensionManager::unload_extension);
  120. ClassDB::bind_method(D_METHOD("is_extension_loaded", "path"), &GDExtensionManager::is_extension_loaded);
  121. ClassDB::bind_method(D_METHOD("get_loaded_extensions"), &GDExtensionManager::get_loaded_extensions);
  122. ClassDB::bind_method(D_METHOD("get_extension", "path"), &GDExtensionManager::get_extension);
  123. BIND_ENUM_CONSTANT(LOAD_STATUS_OK);
  124. BIND_ENUM_CONSTANT(LOAD_STATUS_FAILED);
  125. BIND_ENUM_CONSTANT(LOAD_STATUS_ALREADY_LOADED);
  126. BIND_ENUM_CONSTANT(LOAD_STATUS_NOT_LOADED);
  127. BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART);
  128. }
  129. GDExtensionManager *GDExtensionManager::singleton = nullptr;
  130. GDExtensionManager::GDExtensionManager() {
  131. ERR_FAIL_COND(singleton != nullptr);
  132. singleton = this;
  133. }