gdscript_cache.cpp 13 KB

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