gdscript_cache.cpp 8.4 KB

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