gdscript_cache.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*************************************************************************/
  2. /* gdscript_cache.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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(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(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(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 = 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;
  128. ref->unreference();
  129. }
  130. r_error = ref->raise_status(p_status);
  131. return ref;
  132. }
  133. String GDScriptCache::get_source_code(const String &p_path) {
  134. Vector<uint8_t> source_file;
  135. Error err;
  136. FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err);
  137. if (err) {
  138. ERR_FAIL_COND_V(err, "");
  139. }
  140. int len = f->get_len();
  141. source_file.resize(len + 1);
  142. int r = f->get_buffer(source_file.ptrw(), len);
  143. f->close();
  144. ERR_FAIL_COND_V(r != len, "");
  145. source_file.write[len] = 0;
  146. String source;
  147. if (source.parse_utf8((const char *)source_file.ptr())) {
  148. 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.");
  149. }
  150. return source;
  151. }
  152. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, const String &p_owner) {
  153. MutexLock(singleton->lock);
  154. if (p_owner != String()) {
  155. singleton->dependencies[p_owner].insert(p_path);
  156. }
  157. if (singleton->full_gdscript_cache.has(p_path)) {
  158. return singleton->full_gdscript_cache[p_path];
  159. }
  160. if (singleton->shallow_gdscript_cache.has(p_path)) {
  161. return singleton->shallow_gdscript_cache[p_path];
  162. }
  163. Ref<GDScript> script;
  164. script.instance();
  165. script->set_path(p_path, true);
  166. script->set_script_path(p_path);
  167. script->load_source_code(p_path);
  168. singleton->shallow_gdscript_cache[p_path] = script;
  169. // The one in cache is not a hard reference: if the script dies somewhere else it's fine.
  170. // Scripts remove themselves from cache when they die.
  171. script->unreference();
  172. return script;
  173. }
  174. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner) {
  175. MutexLock(singleton->lock);
  176. if (p_owner != String()) {
  177. singleton->dependencies[p_owner].insert(p_path);
  178. }
  179. r_error = OK;
  180. if (singleton->full_gdscript_cache.has(p_path)) {
  181. return singleton->full_gdscript_cache[p_path];
  182. }
  183. Ref<GDScript> script = get_shallow_script(p_path);
  184. r_error = script->load_source_code(p_path);
  185. if (r_error) {
  186. return script;
  187. }
  188. r_error = script->reload();
  189. if (r_error) {
  190. return script;
  191. }
  192. singleton->full_gdscript_cache[p_path] = script;
  193. singleton->shallow_gdscript_cache.erase(p_path);
  194. return script;
  195. }
  196. Error GDScriptCache::finish_compiling(const String &p_owner) {
  197. // Mark this as compiled.
  198. Ref<GDScript> script = get_shallow_script(p_owner);
  199. singleton->full_gdscript_cache[p_owner] = script;
  200. singleton->shallow_gdscript_cache.erase(p_owner);
  201. Set<String> depends = singleton->dependencies[p_owner];
  202. Error err = OK;
  203. for (const Set<String>::Element *E = depends.front(); E != nullptr; E = E->next()) {
  204. Error this_err = OK;
  205. // No need to save the script. We assume it's already referenced in the owner.
  206. get_full_script(E->get(), this_err);
  207. if (this_err != OK) {
  208. err = this_err;
  209. }
  210. }
  211. singleton->dependencies.erase(p_owner);
  212. return err;
  213. }
  214. GDScriptCache::GDScriptCache() {
  215. singleton = this;
  216. }
  217. GDScriptCache::~GDScriptCache() {
  218. parser_map.clear();
  219. shallow_gdscript_cache.clear();
  220. full_gdscript_cache.clear();
  221. singleton = nullptr;
  222. }