gdscript_cache.cpp 12 KB

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