gdscript_cache.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/os/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. Error result = OK;
  48. while (p_new_status > status) {
  49. switch (status) {
  50. case EMPTY:
  51. result = parser->parse(GDScriptCache::get_source_code(path), path, false);
  52. status = PARSED;
  53. break;
  54. case PARSED: {
  55. analyzer = memnew(GDScriptAnalyzer(parser));
  56. Error inheritance_result = analyzer->resolve_inheritance();
  57. status = INHERITANCE_SOLVED;
  58. if (result == OK) {
  59. result = inheritance_result;
  60. }
  61. } break;
  62. case INHERITANCE_SOLVED: {
  63. Error interface_result = analyzer->resolve_interface();
  64. status = INTERFACE_SOLVED;
  65. if (result == OK) {
  66. result = interface_result;
  67. }
  68. } break;
  69. case INTERFACE_SOLVED: {
  70. Error body_result = analyzer->resolve_body();
  71. status = FULLY_SOLVED;
  72. if (result == OK) {
  73. result = body_result;
  74. }
  75. } break;
  76. case FULLY_SOLVED: {
  77. return result;
  78. }
  79. }
  80. if (result != OK) {
  81. if (parser != nullptr) {
  82. memdelete(parser);
  83. parser = nullptr;
  84. }
  85. if (analyzer != nullptr) {
  86. memdelete(analyzer);
  87. analyzer = nullptr;
  88. }
  89. return result;
  90. }
  91. }
  92. return result;
  93. }
  94. GDScriptParserRef::~GDScriptParserRef() {
  95. if (parser != nullptr) {
  96. memdelete(parser);
  97. }
  98. if (analyzer != nullptr) {
  99. memdelete(analyzer);
  100. }
  101. MutexLock lock(GDScriptCache::singleton->lock);
  102. GDScriptCache::singleton->parser_map.erase(path);
  103. }
  104. GDScriptCache *GDScriptCache::singleton = nullptr;
  105. void GDScriptCache::remove_script(const String &p_path) {
  106. MutexLock lock(singleton->lock);
  107. singleton->shallow_gdscript_cache.erase(p_path);
  108. singleton->full_gdscript_cache.erase(p_path);
  109. }
  110. Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptParserRef::Status p_status, Error &r_error, const String &p_owner) {
  111. MutexLock lock(singleton->lock);
  112. Ref<GDScriptParserRef> ref;
  113. if (p_owner != String()) {
  114. singleton->dependencies[p_owner].insert(p_path);
  115. }
  116. if (singleton->parser_map.has(p_path)) {
  117. ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
  118. } else {
  119. if (!FileAccess::exists(p_path)) {
  120. r_error = ERR_FILE_NOT_FOUND;
  121. return ref;
  122. }
  123. GDScriptParser *parser = memnew(GDScriptParser);
  124. ref.instance();
  125. ref->parser = parser;
  126. ref->path = p_path;
  127. singleton->parser_map[p_path] = ref.ptr();
  128. }
  129. r_error = ref->raise_status(p_status);
  130. return ref;
  131. }
  132. String GDScriptCache::get_source_code(const String &p_path) {
  133. Vector<uint8_t> source_file;
  134. Error err;
  135. FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err);
  136. if (err) {
  137. ERR_FAIL_COND_V(err, "");
  138. }
  139. int len = f->get_len();
  140. source_file.resize(len + 1);
  141. int r = f->get_buffer(source_file.ptrw(), len);
  142. f->close();
  143. ERR_FAIL_COND_V(r != len, "");
  144. source_file.write[len] = 0;
  145. String source;
  146. if (source.parse_utf8((const char *)source_file.ptr())) {
  147. 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.");
  148. }
  149. return source;
  150. }
  151. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, const String &p_owner) {
  152. MutexLock lock(singleton->lock);
  153. if (p_owner != String()) {
  154. singleton->dependencies[p_owner].insert(p_path);
  155. }
  156. if (singleton->full_gdscript_cache.has(p_path)) {
  157. return singleton->full_gdscript_cache[p_path];
  158. }
  159. if (singleton->shallow_gdscript_cache.has(p_path)) {
  160. return singleton->shallow_gdscript_cache[p_path];
  161. }
  162. Ref<GDScript> script;
  163. script.instance();
  164. script->set_path(p_path, true);
  165. script->set_script_path(p_path);
  166. script->load_source_code(p_path);
  167. singleton->shallow_gdscript_cache[p_path] = script.ptr();
  168. return script;
  169. }
  170. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner) {
  171. MutexLock lock(singleton->lock);
  172. if (p_owner != String()) {
  173. singleton->dependencies[p_owner].insert(p_path);
  174. }
  175. r_error = OK;
  176. if (singleton->full_gdscript_cache.has(p_path)) {
  177. return singleton->full_gdscript_cache[p_path];
  178. }
  179. Ref<GDScript> script = get_shallow_script(p_path);
  180. r_error = script->load_source_code(p_path);
  181. if (r_error) {
  182. return script;
  183. }
  184. r_error = script->reload();
  185. if (r_error) {
  186. return script;
  187. }
  188. singleton->full_gdscript_cache[p_path] = script.ptr();
  189. singleton->shallow_gdscript_cache.erase(p_path);
  190. return script;
  191. }
  192. Error GDScriptCache::finish_compiling(const String &p_owner) {
  193. // Mark this as compiled.
  194. Ref<GDScript> script = get_shallow_script(p_owner);
  195. singleton->full_gdscript_cache[p_owner] = script.ptr();
  196. singleton->shallow_gdscript_cache.erase(p_owner);
  197. Set<String> depends = singleton->dependencies[p_owner];
  198. Error err = OK;
  199. for (const Set<String>::Element *E = depends.front(); E != nullptr; E = E->next()) {
  200. Error this_err = OK;
  201. // No need to save the script. We assume it's already referenced in the owner.
  202. get_full_script(E->get(), this_err);
  203. if (this_err != OK) {
  204. err = this_err;
  205. }
  206. }
  207. singleton->dependencies.erase(p_owner);
  208. return err;
  209. }
  210. GDScriptCache::GDScriptCache() {
  211. singleton = this;
  212. }
  213. GDScriptCache::~GDScriptCache() {
  214. parser_map.clear();
  215. shallow_gdscript_cache.clear();
  216. full_gdscript_cache.clear();
  217. singleton = nullptr;
  218. }