gdscript_cache.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_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. status = PARSED;
  54. result = parser->parse(GDScriptCache::get_source_code(path), path, false);
  55. break;
  56. case PARSED: {
  57. analyzer = memnew(GDScriptAnalyzer(parser));
  58. status = INHERITANCE_SOLVED;
  59. Error inheritance_result = analyzer->resolve_inheritance();
  60. if (result == OK) {
  61. result = inheritance_result;
  62. }
  63. } break;
  64. case INHERITANCE_SOLVED: {
  65. status = INTERFACE_SOLVED;
  66. Error interface_result = analyzer->resolve_interface();
  67. if (result == OK) {
  68. result = interface_result;
  69. }
  70. } break;
  71. case INTERFACE_SOLVED: {
  72. status = FULLY_SOLVED;
  73. Error body_result = analyzer->resolve_body();
  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.is_empty()) {
  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. if (ref.is_null()) {
  113. r_error = ERR_INVALID_DATA;
  114. return ref;
  115. }
  116. } else {
  117. if (!FileAccess::exists(p_path)) {
  118. r_error = ERR_FILE_NOT_FOUND;
  119. return ref;
  120. }
  121. GDScriptParser *parser = memnew(GDScriptParser);
  122. ref.instantiate();
  123. ref->parser = parser;
  124. ref->path = p_path;
  125. singleton->parser_map[p_path] = ref.ptr();
  126. }
  127. r_error = ref->raise_status(p_status);
  128. return ref;
  129. }
  130. String GDScriptCache::get_source_code(const String &p_path) {
  131. Vector<uint8_t> source_file;
  132. Error err;
  133. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  134. ERR_FAIL_COND_V(err, "");
  135. uint64_t len = f->get_length();
  136. source_file.resize(len + 1);
  137. uint64_t r = f->get_buffer(source_file.ptrw(), len);
  138. ERR_FAIL_COND_V(r != len, "");
  139. source_file.write[len] = 0;
  140. String source;
  141. if (source.parse_utf8((const char *)source_file.ptr()) != OK) {
  142. 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.");
  143. }
  144. return source;
  145. }
  146. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, const String &p_owner) {
  147. MutexLock lock(singleton->lock);
  148. if (!p_owner.is_empty()) {
  149. singleton->dependencies[p_owner].insert(p_path);
  150. }
  151. if (singleton->full_gdscript_cache.has(p_path)) {
  152. return singleton->full_gdscript_cache[p_path];
  153. }
  154. if (singleton->shallow_gdscript_cache.has(p_path)) {
  155. return singleton->shallow_gdscript_cache[p_path];
  156. }
  157. Ref<GDScript> script;
  158. script.instantiate();
  159. script->set_path(p_path, true);
  160. script->set_script_path(p_path);
  161. script->load_source_code(p_path);
  162. singleton->shallow_gdscript_cache[p_path] = script.ptr();
  163. return script;
  164. }
  165. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
  166. MutexLock lock(singleton->lock);
  167. if (!p_owner.is_empty()) {
  168. singleton->dependencies[p_owner].insert(p_path);
  169. }
  170. Ref<GDScript> script;
  171. r_error = OK;
  172. if (singleton->full_gdscript_cache.has(p_path)) {
  173. script = Ref<GDScript>(singleton->full_gdscript_cache[p_path]);
  174. if (!p_update_from_disk) {
  175. return script;
  176. }
  177. }
  178. if (script.is_null()) {
  179. script = get_shallow_script(p_path);
  180. ERR_FAIL_COND_V(script.is_null(), Ref<GDScript>());
  181. }
  182. r_error = script->load_source_code(p_path);
  183. if (r_error) {
  184. return script;
  185. }
  186. r_error = script->reload();
  187. if (r_error) {
  188. return script;
  189. }
  190. singleton->full_gdscript_cache[p_path] = script.ptr();
  191. singleton->shallow_gdscript_cache.erase(p_path);
  192. return script;
  193. }
  194. Error GDScriptCache::finish_compiling(const String &p_owner) {
  195. // Mark this as compiled.
  196. Ref<GDScript> script = get_shallow_script(p_owner);
  197. singleton->full_gdscript_cache[p_owner] = script.ptr();
  198. singleton->shallow_gdscript_cache.erase(p_owner);
  199. HashSet<String> depends = singleton->dependencies[p_owner];
  200. Error err = OK;
  201. for (const String &E : depends) {
  202. Error this_err = OK;
  203. // No need to save the script. We assume it's already referenced in the owner.
  204. get_full_script(E, this_err);
  205. if (this_err != OK) {
  206. err = this_err;
  207. }
  208. }
  209. singleton->dependencies.erase(p_owner);
  210. return err;
  211. }
  212. GDScriptCache::GDScriptCache() {
  213. singleton = this;
  214. }
  215. GDScriptCache::~GDScriptCache() {
  216. parser_map.clear();
  217. shallow_gdscript_cache.clear();
  218. full_gdscript_cache.clear();
  219. singleton = nullptr;
  220. }