gdscript_cache.cpp 13 KB

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