gdscript_cache.cpp 12 KB

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