gdscript_cache.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. GDScriptParserRef::Status GDScriptParserRef::get_status() const {
  38. return status;
  39. }
  40. String GDScriptParserRef::get_path() const {
  41. return path;
  42. }
  43. uint32_t GDScriptParserRef::get_source_hash() const {
  44. return source_hash;
  45. }
  46. GDScriptParser *GDScriptParserRef::get_parser() {
  47. if (parser == nullptr) {
  48. parser = memnew(GDScriptParser);
  49. }
  50. return parser;
  51. }
  52. GDScriptAnalyzer *GDScriptParserRef::get_analyzer() {
  53. if (analyzer == nullptr) {
  54. analyzer = memnew(GDScriptAnalyzer(get_parser()));
  55. }
  56. return analyzer;
  57. }
  58. Error GDScriptParserRef::raise_status(Status p_new_status) {
  59. ERR_FAIL_COND_V(clearing, ERR_BUG);
  60. ERR_FAIL_COND_V(parser == nullptr && status != EMPTY, ERR_BUG);
  61. while (result == OK && p_new_status > status) {
  62. switch (status) {
  63. case EMPTY: {
  64. // Calling parse will clear the parser, which can destruct another GDScriptParserRef which can clear the last reference to the script with this path, calling remove_script, which clears this GDScriptParserRef.
  65. // It's ok if its the first thing done here.
  66. get_parser()->clear();
  67. status = PARSED;
  68. String remapped_path = ResourceLoader::path_remap(path);
  69. if (remapped_path.get_extension().to_lower() == "gdc") {
  70. Vector<uint8_t> tokens = GDScriptCache::get_binary_tokens(remapped_path);
  71. source_hash = hash_djb2_buffer(tokens.ptr(), tokens.size());
  72. result = get_parser()->parse_binary(tokens, path);
  73. } else {
  74. String source = GDScriptCache::get_source_code(remapped_path);
  75. source_hash = source.hash();
  76. result = get_parser()->parse(source, path, false);
  77. }
  78. } break;
  79. case PARSED: {
  80. status = INHERITANCE_SOLVED;
  81. result = get_analyzer()->resolve_inheritance();
  82. } break;
  83. case INHERITANCE_SOLVED: {
  84. status = INTERFACE_SOLVED;
  85. result = get_analyzer()->resolve_interface();
  86. } break;
  87. case INTERFACE_SOLVED: {
  88. status = BODY_SOLVED;
  89. result = get_analyzer()->resolve_body();
  90. } break;
  91. case BODY_SOLVED: {
  92. status = FULLY_SOLVED;
  93. result = get_analyzer()->resolve_dependencies();
  94. } break;
  95. case FULLY_SOLVED: {
  96. return result;
  97. }
  98. }
  99. }
  100. return result;
  101. }
  102. void GDScriptParserRef::clear() {
  103. if (clearing) {
  104. return;
  105. }
  106. clearing = true;
  107. GDScriptParser *lparser = parser;
  108. GDScriptAnalyzer *lanalyzer = analyzer;
  109. parser = nullptr;
  110. analyzer = nullptr;
  111. status = EMPTY;
  112. result = OK;
  113. source_hash = 0;
  114. clearing = false;
  115. if (lanalyzer != nullptr) {
  116. memdelete(lanalyzer);
  117. }
  118. if (lparser != nullptr) {
  119. memdelete(lparser);
  120. }
  121. }
  122. GDScriptParserRef::~GDScriptParserRef() {
  123. clear();
  124. GDScriptCache::remove_parser(path);
  125. }
  126. GDScriptCache *GDScriptCache::singleton = nullptr;
  127. void GDScriptCache::move_script(const String &p_from, const String &p_to) {
  128. if (singleton == nullptr || p_from == p_to) {
  129. return;
  130. }
  131. MutexLock lock(singleton->mutex);
  132. if (singleton->cleared) {
  133. return;
  134. }
  135. if (singleton->parser_map.has(p_from) && !p_from.is_empty()) {
  136. singleton->parser_map[p_to] = singleton->parser_map[p_from];
  137. }
  138. singleton->parser_map.erase(p_from);
  139. if (singleton->parser_inverse_dependencies.has(p_from) && !p_from.is_empty()) {
  140. singleton->parser_inverse_dependencies[p_to] = singleton->parser_inverse_dependencies[p_from];
  141. }
  142. singleton->parser_inverse_dependencies.erase(p_from);
  143. if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) {
  144. singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from];
  145. }
  146. singleton->shallow_gdscript_cache.erase(p_from);
  147. if (singleton->full_gdscript_cache.has(p_from) && !p_from.is_empty()) {
  148. singleton->full_gdscript_cache[p_to] = singleton->full_gdscript_cache[p_from];
  149. }
  150. singleton->full_gdscript_cache.erase(p_from);
  151. }
  152. void GDScriptCache::remove_script(const String &p_path) {
  153. if (singleton == nullptr) {
  154. return;
  155. }
  156. MutexLock lock(singleton->mutex);
  157. if (singleton->cleared) {
  158. return;
  159. }
  160. if (singleton->parser_map.has(p_path)) {
  161. singleton->parser_map[p_path]->clear();
  162. }
  163. remove_parser(p_path);
  164. singleton->dependencies.erase(p_path);
  165. singleton->shallow_gdscript_cache.erase(p_path);
  166. singleton->full_gdscript_cache.erase(p_path);
  167. }
  168. Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptParserRef::Status p_status, Error &r_error, const String &p_owner) {
  169. MutexLock lock(singleton->mutex);
  170. Ref<GDScriptParserRef> ref;
  171. if (!p_owner.is_empty()) {
  172. singleton->dependencies[p_owner].insert(p_path);
  173. singleton->parser_inverse_dependencies[p_path].insert(p_owner);
  174. }
  175. if (singleton->parser_map.has(p_path)) {
  176. ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
  177. if (ref.is_null()) {
  178. r_error = ERR_INVALID_DATA;
  179. return ref;
  180. }
  181. } else {
  182. String remapped_path = ResourceLoader::path_remap(p_path);
  183. if (!FileAccess::exists(remapped_path)) {
  184. r_error = ERR_FILE_NOT_FOUND;
  185. return ref;
  186. }
  187. ref.instantiate();
  188. ref->path = p_path;
  189. singleton->parser_map[p_path] = ref.ptr();
  190. }
  191. r_error = ref->raise_status(p_status);
  192. return ref;
  193. }
  194. bool GDScriptCache::has_parser(const String &p_path) {
  195. MutexLock lock(singleton->mutex);
  196. return singleton->parser_map.has(p_path);
  197. }
  198. void GDScriptCache::remove_parser(const String &p_path) {
  199. MutexLock lock(singleton->mutex);
  200. // Can't clear the parser because some other parser might be currently using it in the chain of calls.
  201. singleton->parser_map.erase(p_path);
  202. // Have to copy while iterating, because parser_inverse_dependencies is modified.
  203. HashSet<String> ideps = singleton->parser_inverse_dependencies[p_path];
  204. singleton->parser_inverse_dependencies.erase(p_path);
  205. for (String idep_path : ideps) {
  206. remove_parser(idep_path);
  207. }
  208. }
  209. String GDScriptCache::get_source_code(const String &p_path) {
  210. Vector<uint8_t> source_file;
  211. Error err;
  212. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  213. ERR_FAIL_COND_V(err, "");
  214. uint64_t len = f->get_length();
  215. source_file.resize(len + 1);
  216. uint64_t r = f->get_buffer(source_file.ptrw(), len);
  217. ERR_FAIL_COND_V(r != len, "");
  218. source_file.write[len] = 0;
  219. String source;
  220. if (source.parse_utf8((const char *)source_file.ptr()) != OK) {
  221. 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.");
  222. }
  223. return source;
  224. }
  225. Vector<uint8_t> GDScriptCache::get_binary_tokens(const String &p_path) {
  226. Vector<uint8_t> buffer;
  227. Error err = OK;
  228. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  229. ERR_FAIL_COND_V_MSG(err != OK, buffer, "Failed to open binary GDScript file '" + p_path + "'.");
  230. uint64_t len = f->get_length();
  231. buffer.resize(len);
  232. uint64_t read = f->get_buffer(buffer.ptrw(), buffer.size());
  233. ERR_FAIL_COND_V_MSG(read != len, Vector<uint8_t>(), "Failed to read binary GDScript file '" + p_path + "'.");
  234. return buffer;
  235. }
  236. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, Error &r_error, const String &p_owner) {
  237. MutexLock lock(singleton->mutex);
  238. if (!p_owner.is_empty()) {
  239. singleton->dependencies[p_owner].insert(p_path);
  240. }
  241. if (singleton->full_gdscript_cache.has(p_path)) {
  242. return singleton->full_gdscript_cache[p_path];
  243. }
  244. if (singleton->shallow_gdscript_cache.has(p_path)) {
  245. return singleton->shallow_gdscript_cache[p_path];
  246. }
  247. String remapped_path = ResourceLoader::path_remap(p_path);
  248. Ref<GDScript> script;
  249. script.instantiate();
  250. script->set_path(p_path, true);
  251. if (remapped_path.get_extension().to_lower() == "gdc") {
  252. Vector<uint8_t> buffer = get_binary_tokens(remapped_path);
  253. if (buffer.is_empty()) {
  254. r_error = ERR_FILE_CANT_READ;
  255. }
  256. script->set_binary_tokens_source(buffer);
  257. } else {
  258. r_error = script->load_source_code(remapped_path);
  259. }
  260. if (r_error) {
  261. return Ref<GDScript>(); // Returns null and does not cache when the script fails to load.
  262. }
  263. Ref<GDScriptParserRef> parser_ref = get_parser(p_path, GDScriptParserRef::PARSED, r_error);
  264. if (r_error == OK) {
  265. GDScriptCompiler::make_scripts(script.ptr(), parser_ref->get_parser()->get_tree(), true);
  266. }
  267. singleton->shallow_gdscript_cache[p_path] = script;
  268. return script;
  269. }
  270. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
  271. MutexLock lock(singleton->mutex);
  272. if (!p_owner.is_empty()) {
  273. singleton->dependencies[p_owner].insert(p_path);
  274. }
  275. Ref<GDScript> script;
  276. r_error = OK;
  277. if (singleton->full_gdscript_cache.has(p_path)) {
  278. script = singleton->full_gdscript_cache[p_path];
  279. if (!p_update_from_disk) {
  280. return script;
  281. }
  282. }
  283. if (script.is_null()) {
  284. script = get_shallow_script(p_path, r_error);
  285. // Only exit early if script failed to load, otherwise let reload report errors.
  286. if (script.is_null()) {
  287. return script;
  288. }
  289. }
  290. if (p_update_from_disk) {
  291. if (p_path.get_extension().to_lower() == "gdc") {
  292. Vector<uint8_t> buffer = get_binary_tokens(p_path);
  293. if (buffer.is_empty()) {
  294. r_error = ERR_FILE_CANT_READ;
  295. return script;
  296. }
  297. script->set_binary_tokens_source(buffer);
  298. } else {
  299. r_error = script->load_source_code(p_path);
  300. if (r_error) {
  301. return script;
  302. }
  303. }
  304. }
  305. r_error = script->reload(true);
  306. if (r_error) {
  307. return script;
  308. }
  309. singleton->full_gdscript_cache[p_path] = script;
  310. singleton->shallow_gdscript_cache.erase(p_path);
  311. return script;
  312. }
  313. Ref<GDScript> GDScriptCache::get_cached_script(const String &p_path) {
  314. MutexLock lock(singleton->mutex);
  315. if (singleton->full_gdscript_cache.has(p_path)) {
  316. return singleton->full_gdscript_cache[p_path];
  317. }
  318. if (singleton->shallow_gdscript_cache.has(p_path)) {
  319. return singleton->shallow_gdscript_cache[p_path];
  320. }
  321. return Ref<GDScript>();
  322. }
  323. Error GDScriptCache::finish_compiling(const String &p_owner) {
  324. MutexLock lock(singleton->mutex);
  325. // Mark this as compiled.
  326. Ref<GDScript> script = get_cached_script(p_owner);
  327. singleton->full_gdscript_cache[p_owner] = script;
  328. singleton->shallow_gdscript_cache.erase(p_owner);
  329. HashSet<String> depends = singleton->dependencies[p_owner];
  330. Error err = OK;
  331. for (const String &E : depends) {
  332. Error this_err = OK;
  333. // No need to save the script. We assume it's already referenced in the owner.
  334. get_full_script(E, this_err);
  335. if (this_err != OK) {
  336. err = this_err;
  337. }
  338. }
  339. singleton->dependencies.erase(p_owner);
  340. return err;
  341. }
  342. void GDScriptCache::add_static_script(Ref<GDScript> p_script) {
  343. ERR_FAIL_COND_MSG(p_script.is_null(), "Trying to cache empty script as static.");
  344. ERR_FAIL_COND_MSG(!p_script->is_valid(), "Trying to cache non-compiled script as static.");
  345. singleton->static_gdscript_cache[p_script->get_fully_qualified_name()] = p_script;
  346. }
  347. void GDScriptCache::remove_static_script(const String &p_fqcn) {
  348. singleton->static_gdscript_cache.erase(p_fqcn);
  349. }
  350. void GDScriptCache::clear() {
  351. if (singleton == nullptr) {
  352. return;
  353. }
  354. MutexLock lock(singleton->mutex);
  355. if (singleton->cleared) {
  356. return;
  357. }
  358. singleton->cleared = true;
  359. singleton->parser_inverse_dependencies.clear();
  360. RBSet<Ref<GDScriptParserRef>> parser_map_refs;
  361. for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) {
  362. parser_map_refs.insert(E.value);
  363. }
  364. singleton->parser_map.clear();
  365. for (Ref<GDScriptParserRef> &E : parser_map_refs) {
  366. if (E.is_valid()) {
  367. E->clear();
  368. }
  369. }
  370. parser_map_refs.clear();
  371. singleton->shallow_gdscript_cache.clear();
  372. singleton->full_gdscript_cache.clear();
  373. }
  374. GDScriptCache::GDScriptCache() {
  375. singleton = this;
  376. }
  377. GDScriptCache::~GDScriptCache() {
  378. if (!cleared) {
  379. clear();
  380. }
  381. singleton = nullptr;
  382. }