gdscript_cache.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*************************************************************************/
  2. /* gdscript_cache.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gdscript_cache.h"
  31. #include "core/io/file_access.h"
  32. #include "core/templates/vector.h"
  33. #include "gdscript.h"
  34. #include "gdscript_analyzer.h"
  35. #include "gdscript_parser.h"
  36. bool GDScriptParserRef::is_valid() const {
  37. return parser != nullptr;
  38. }
  39. GDScriptParserRef::Status GDScriptParserRef::get_status() const {
  40. return status;
  41. }
  42. GDScriptParser *GDScriptParserRef::get_parser() const {
  43. return parser;
  44. }
  45. Error GDScriptParserRef::raise_status(Status p_new_status) {
  46. ERR_FAIL_COND_V(parser == nullptr, ERR_INVALID_DATA);
  47. if (result != OK) {
  48. return result;
  49. }
  50. while (p_new_status > status) {
  51. switch (status) {
  52. case EMPTY:
  53. result = parser->parse(GDScriptCache::get_source_code(path), path, false);
  54. status = PARSED;
  55. break;
  56. case PARSED: {
  57. analyzer = memnew(GDScriptAnalyzer(parser));
  58. Error inheritance_result = analyzer->resolve_inheritance();
  59. status = INHERITANCE_SOLVED;
  60. if (result == OK) {
  61. result = inheritance_result;
  62. }
  63. } break;
  64. case INHERITANCE_SOLVED: {
  65. Error interface_result = analyzer->resolve_interface();
  66. status = INTERFACE_SOLVED;
  67. if (result == OK) {
  68. result = interface_result;
  69. }
  70. } break;
  71. case INTERFACE_SOLVED: {
  72. Error body_result = analyzer->resolve_body();
  73. status = FULLY_SOLVED;
  74. if (result == OK) {
  75. result = body_result;
  76. }
  77. } break;
  78. case FULLY_SOLVED: {
  79. return result;
  80. }
  81. }
  82. if (result != OK) {
  83. return result;
  84. }
  85. }
  86. return result;
  87. }
  88. GDScriptParserRef::~GDScriptParserRef() {
  89. if (parser != nullptr) {
  90. memdelete(parser);
  91. }
  92. if (analyzer != nullptr) {
  93. memdelete(analyzer);
  94. }
  95. MutexLock lock(GDScriptCache::singleton->lock);
  96. GDScriptCache::singleton->parser_map.erase(path);
  97. }
  98. GDScriptCache *GDScriptCache::singleton = nullptr;
  99. void GDScriptCache::remove_script(const String &p_path) {
  100. MutexLock lock(singleton->lock);
  101. singleton->shallow_gdscript_cache.erase(p_path);
  102. singleton->full_gdscript_cache.erase(p_path);
  103. }
  104. Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptParserRef::Status p_status, Error &r_error, const String &p_owner) {
  105. MutexLock lock(singleton->lock);
  106. Ref<GDScriptParserRef> ref;
  107. if (p_owner != String()) {
  108. singleton->dependencies[p_owner].insert(p_path);
  109. }
  110. if (singleton->parser_map.has(p_path)) {
  111. ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
  112. } else {
  113. if (!FileAccess::exists(p_path)) {
  114. r_error = ERR_FILE_NOT_FOUND;
  115. return ref;
  116. }
  117. GDScriptParser *parser = memnew(GDScriptParser);
  118. ref.instantiate();
  119. ref->parser = parser;
  120. ref->path = p_path;
  121. singleton->parser_map[p_path] = ref.ptr();
  122. }
  123. r_error = ref->raise_status(p_status);
  124. return ref;
  125. }
  126. String GDScriptCache::get_source_code(const String &p_path) {
  127. Vector<uint8_t> source_file;
  128. Error err;
  129. FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err);
  130. if (err) {
  131. ERR_FAIL_COND_V(err, "");
  132. }
  133. uint64_t len = f->get_length();
  134. source_file.resize(len + 1);
  135. uint64_t r = f->get_buffer(source_file.ptrw(), len);
  136. f->close();
  137. ERR_FAIL_COND_V(r != len, "");
  138. source_file.write[len] = 0;
  139. String source;
  140. if (source.parse_utf8((const char *)source_file.ptr())) {
  141. ERR_FAIL_V_MSG("", "Script '" + p_path + "' contains invalid unicode (UTF-8), so it was not loaded. Please ensure that scripts are saved in valid UTF-8 unicode.");
  142. }
  143. return source;
  144. }
  145. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, const String &p_owner) {
  146. MutexLock lock(singleton->lock);
  147. if (p_owner != String()) {
  148. singleton->dependencies[p_owner].insert(p_path);
  149. }
  150. if (singleton->full_gdscript_cache.has(p_path)) {
  151. return singleton->full_gdscript_cache[p_path];
  152. }
  153. if (singleton->shallow_gdscript_cache.has(p_path)) {
  154. return singleton->shallow_gdscript_cache[p_path];
  155. }
  156. Ref<GDScript> script;
  157. script.instantiate();
  158. script->set_path(p_path, true);
  159. script->set_script_path(p_path);
  160. script->load_source_code(p_path);
  161. singleton->shallow_gdscript_cache[p_path] = script.ptr();
  162. return script;
  163. }
  164. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner) {
  165. MutexLock lock(singleton->lock);
  166. if (p_owner != String()) {
  167. singleton->dependencies[p_owner].insert(p_path);
  168. }
  169. r_error = OK;
  170. if (singleton->full_gdscript_cache.has(p_path)) {
  171. return singleton->full_gdscript_cache[p_path];
  172. }
  173. Ref<GDScript> script = get_shallow_script(p_path);
  174. ERR_FAIL_COND_V(script.is_null(), Ref<GDScript>());
  175. r_error = script->load_source_code(p_path);
  176. if (r_error) {
  177. return script;
  178. }
  179. r_error = script->reload();
  180. if (r_error) {
  181. return script;
  182. }
  183. singleton->full_gdscript_cache[p_path] = script.ptr();
  184. singleton->shallow_gdscript_cache.erase(p_path);
  185. return script;
  186. }
  187. Error GDScriptCache::finish_compiling(const String &p_owner) {
  188. // Mark this as compiled.
  189. Ref<GDScript> script = get_shallow_script(p_owner);
  190. singleton->full_gdscript_cache[p_owner] = script.ptr();
  191. singleton->shallow_gdscript_cache.erase(p_owner);
  192. Set<String> depends = singleton->dependencies[p_owner];
  193. Error err = OK;
  194. for (const Set<String>::Element *E = depends.front(); E != nullptr; E = E->next()) {
  195. Error this_err = OK;
  196. // No need to save the script. We assume it's already referenced in the owner.
  197. get_full_script(E->get(), this_err);
  198. if (this_err != OK) {
  199. err = this_err;
  200. }
  201. }
  202. singleton->dependencies.erase(p_owner);
  203. return err;
  204. }
  205. GDScriptCache::GDScriptCache() {
  206. singleton = this;
  207. }
  208. GDScriptCache::~GDScriptCache() {
  209. parser_map.clear();
  210. shallow_gdscript_cache.clear();
  211. full_gdscript_cache.clear();
  212. singleton = nullptr;
  213. }