gdscript_cache.cpp 12 KB

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