gdscript_cache.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************/
  2. /* gdscript_cache.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "gdscript.h"
  32. #include "gdscript_analyzer.h"
  33. #include "gdscript_compiler.h"
  34. #include "gdscript_parser.h"
  35. #include "core/io/file_access.h"
  36. #include "core/templates/vector.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. GDScriptAnalyzer *GDScriptParserRef::get_analyzer() {
  47. if (analyzer == nullptr) {
  48. analyzer = memnew(GDScriptAnalyzer(parser));
  49. }
  50. return analyzer;
  51. }
  52. Error GDScriptParserRef::raise_status(Status p_new_status) {
  53. ERR_FAIL_NULL_V(parser, ERR_INVALID_DATA);
  54. if (result != OK) {
  55. return result;
  56. }
  57. while (p_new_status > status) {
  58. switch (status) {
  59. case EMPTY:
  60. status = PARSED;
  61. result = parser->parse(GDScriptCache::get_source_code(path), path, false);
  62. break;
  63. case PARSED: {
  64. status = INHERITANCE_SOLVED;
  65. Error inheritance_result = get_analyzer()->resolve_inheritance();
  66. if (result == OK) {
  67. result = inheritance_result;
  68. }
  69. } break;
  70. case INHERITANCE_SOLVED: {
  71. status = INTERFACE_SOLVED;
  72. Error interface_result = get_analyzer()->resolve_interface();
  73. if (result == OK) {
  74. result = interface_result;
  75. }
  76. } break;
  77. case INTERFACE_SOLVED: {
  78. status = FULLY_SOLVED;
  79. Error body_result = get_analyzer()->resolve_body();
  80. if (result == OK) {
  81. result = body_result;
  82. }
  83. } break;
  84. case FULLY_SOLVED: {
  85. return result;
  86. }
  87. }
  88. if (result != OK) {
  89. return result;
  90. }
  91. }
  92. return result;
  93. }
  94. void GDScriptParserRef::clear() {
  95. if (cleared) {
  96. return;
  97. }
  98. cleared = true;
  99. if (parser != nullptr) {
  100. memdelete(parser);
  101. }
  102. if (analyzer != nullptr) {
  103. memdelete(analyzer);
  104. }
  105. }
  106. GDScriptParserRef::~GDScriptParserRef() {
  107. clear();
  108. MutexLock lock(GDScriptCache::singleton->mutex);
  109. GDScriptCache::singleton->parser_map.erase(path);
  110. }
  111. GDScriptCache *GDScriptCache::singleton = nullptr;
  112. void GDScriptCache::move_script(const String &p_from, const String &p_to) {
  113. if (singleton == nullptr || p_from == p_to) {
  114. return;
  115. }
  116. MutexLock lock(singleton->mutex);
  117. if (singleton->cleared) {
  118. return;
  119. }
  120. if (singleton->parser_map.has(p_from) && !p_from.is_empty()) {
  121. singleton->parser_map[p_to] = singleton->parser_map[p_from];
  122. }
  123. singleton->parser_map.erase(p_from);
  124. if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) {
  125. singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from];
  126. }
  127. singleton->shallow_gdscript_cache.erase(p_from);
  128. if (singleton->full_gdscript_cache.has(p_from) && !p_from.is_empty()) {
  129. singleton->full_gdscript_cache[p_to] = singleton->full_gdscript_cache[p_from];
  130. }
  131. singleton->full_gdscript_cache.erase(p_from);
  132. }
  133. void GDScriptCache::remove_script(const String &p_path) {
  134. if (singleton == nullptr) {
  135. return;
  136. }
  137. MutexLock lock(singleton->mutex);
  138. if (singleton->cleared) {
  139. return;
  140. }
  141. if (singleton->parser_map.has(p_path)) {
  142. singleton->parser_map[p_path]->clear();
  143. singleton->parser_map.erase(p_path);
  144. }
  145. singleton->dependencies.erase(p_path);
  146. singleton->shallow_gdscript_cache.erase(p_path);
  147. singleton->full_gdscript_cache.erase(p_path);
  148. }
  149. Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptParserRef::Status p_status, Error &r_error, const String &p_owner) {
  150. MutexLock lock(singleton->mutex);
  151. Ref<GDScriptParserRef> ref;
  152. if (!p_owner.is_empty()) {
  153. singleton->dependencies[p_owner].insert(p_path);
  154. }
  155. if (singleton->parser_map.has(p_path)) {
  156. ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
  157. if (ref.is_null()) {
  158. r_error = ERR_INVALID_DATA;
  159. return ref;
  160. }
  161. } else {
  162. if (!FileAccess::exists(p_path)) {
  163. r_error = ERR_FILE_NOT_FOUND;
  164. return ref;
  165. }
  166. GDScriptParser *parser = memnew(GDScriptParser);
  167. ref.instantiate();
  168. ref->parser = parser;
  169. ref->path = p_path;
  170. singleton->parser_map[p_path] = ref.ptr();
  171. }
  172. r_error = ref->raise_status(p_status);
  173. return ref;
  174. }
  175. String GDScriptCache::get_source_code(const String &p_path) {
  176. Vector<uint8_t> source_file;
  177. Error err;
  178. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  179. ERR_FAIL_COND_V(err, "");
  180. uint64_t len = f->get_length();
  181. source_file.resize(len + 1);
  182. uint64_t r = f->get_buffer(source_file.ptrw(), len);
  183. ERR_FAIL_COND_V(r != len, "");
  184. source_file.write[len] = 0;
  185. String source;
  186. if (source.parse_utf8((const char *)source_file.ptr()) != OK) {
  187. 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.");
  188. }
  189. return source;
  190. }
  191. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, Error &r_error, const String &p_owner) {
  192. MutexLock lock(singleton->mutex);
  193. if (!p_owner.is_empty()) {
  194. singleton->dependencies[p_owner].insert(p_path);
  195. }
  196. if (singleton->full_gdscript_cache.has(p_path)) {
  197. return singleton->full_gdscript_cache[p_path];
  198. }
  199. if (singleton->shallow_gdscript_cache.has(p_path)) {
  200. return singleton->shallow_gdscript_cache[p_path];
  201. }
  202. Ref<GDScript> script;
  203. script.instantiate();
  204. script->set_path(p_path, true);
  205. r_error = script->load_source_code(p_path);
  206. if (r_error) {
  207. return Ref<GDScript>(); // Returns null and does not cache when the script fails to load.
  208. }
  209. Ref<GDScriptParserRef> parser_ref = get_parser(p_path, GDScriptParserRef::PARSED, r_error);
  210. if (r_error == OK) {
  211. GDScriptCompiler::make_scripts(script.ptr(), parser_ref->get_parser()->get_tree(), true);
  212. }
  213. singleton->shallow_gdscript_cache[p_path] = script;
  214. return script;
  215. }
  216. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
  217. MutexLock lock(singleton->mutex);
  218. if (!p_owner.is_empty()) {
  219. singleton->dependencies[p_owner].insert(p_path);
  220. }
  221. Ref<GDScript> script;
  222. r_error = OK;
  223. if (singleton->full_gdscript_cache.has(p_path)) {
  224. script = singleton->full_gdscript_cache[p_path];
  225. if (!p_update_from_disk) {
  226. return script;
  227. }
  228. }
  229. if (script.is_null()) {
  230. script = get_shallow_script(p_path, r_error);
  231. // Only exit early if script failed to load, otherwise let reload report errors.
  232. if (script.is_null()) {
  233. return script;
  234. }
  235. }
  236. if (p_update_from_disk) {
  237. r_error = script->load_source_code(p_path);
  238. if (r_error) {
  239. return script;
  240. }
  241. }
  242. r_error = script->reload(true);
  243. if (r_error) {
  244. return script;
  245. }
  246. singleton->full_gdscript_cache[p_path] = script;
  247. singleton->shallow_gdscript_cache.erase(p_path);
  248. return script;
  249. }
  250. Ref<GDScript> GDScriptCache::get_cached_script(const String &p_path) {
  251. MutexLock lock(singleton->mutex);
  252. if (singleton->full_gdscript_cache.has(p_path)) {
  253. return singleton->full_gdscript_cache[p_path];
  254. }
  255. if (singleton->shallow_gdscript_cache.has(p_path)) {
  256. return singleton->shallow_gdscript_cache[p_path];
  257. }
  258. return Ref<GDScript>();
  259. }
  260. Error GDScriptCache::finish_compiling(const String &p_owner) {
  261. MutexLock lock(singleton->mutex);
  262. // Mark this as compiled.
  263. Ref<GDScript> script = get_cached_script(p_owner);
  264. singleton->full_gdscript_cache[p_owner] = script;
  265. singleton->shallow_gdscript_cache.erase(p_owner);
  266. HashSet<String> depends = singleton->dependencies[p_owner];
  267. Error err = OK;
  268. for (const String &E : depends) {
  269. Error this_err = OK;
  270. // No need to save the script. We assume it's already referenced in the owner.
  271. get_full_script(E, this_err);
  272. if (this_err != OK) {
  273. err = this_err;
  274. }
  275. }
  276. singleton->dependencies.erase(p_owner);
  277. return err;
  278. }
  279. void GDScriptCache::add_static_script(Ref<GDScript> p_script) {
  280. ERR_FAIL_COND_MSG(p_script.is_null(), "Trying to cache empty script as static.");
  281. ERR_FAIL_COND_MSG(!p_script->is_valid(), "Trying to cache non-compiled script as static.");
  282. singleton->static_gdscript_cache[p_script->get_fully_qualified_name()] = p_script;
  283. }
  284. void GDScriptCache::remove_static_script(const String &p_fqcn) {
  285. singleton->static_gdscript_cache.erase(p_fqcn);
  286. }
  287. void GDScriptCache::clear() {
  288. if (singleton == nullptr) {
  289. return;
  290. }
  291. MutexLock lock(singleton->mutex);
  292. if (singleton->cleared) {
  293. return;
  294. }
  295. singleton->cleared = true;
  296. RBSet<Ref<GDScriptParserRef>> parser_map_refs;
  297. for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) {
  298. parser_map_refs.insert(E.value);
  299. }
  300. for (Ref<GDScriptParserRef> &E : parser_map_refs) {
  301. if (E.is_valid())
  302. E->clear();
  303. }
  304. parser_map_refs.clear();
  305. singleton->parser_map.clear();
  306. singleton->shallow_gdscript_cache.clear();
  307. singleton->full_gdscript_cache.clear();
  308. }
  309. GDScriptCache::GDScriptCache() {
  310. singleton = this;
  311. }
  312. GDScriptCache::~GDScriptCache() {
  313. if (!cleared) {
  314. clear();
  315. }
  316. singleton = nullptr;
  317. }