gdscript_cache.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
  114. if (E.value.has(p_from)) {
  115. E.value.insert(p_to);
  116. E.value.erase(p_from);
  117. }
  118. }
  119. if (singleton->parser_map.has(p_from) && !p_from.is_empty()) {
  120. singleton->parser_map[p_to] = singleton->parser_map[p_from];
  121. }
  122. singleton->parser_map.erase(p_from);
  123. if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) {
  124. singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from];
  125. }
  126. singleton->shallow_gdscript_cache.erase(p_from);
  127. if (singleton->full_gdscript_cache.has(p_from) && !p_from.is_empty()) {
  128. singleton->full_gdscript_cache[p_to] = singleton->full_gdscript_cache[p_from];
  129. }
  130. singleton->full_gdscript_cache.erase(p_from);
  131. }
  132. void GDScriptCache::remove_script(const String &p_path) {
  133. if (singleton == nullptr) {
  134. return;
  135. }
  136. MutexLock lock(singleton->mutex);
  137. for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
  138. if (!E.value.has(p_path)) {
  139. continue;
  140. }
  141. E.value.erase(p_path);
  142. }
  143. GDScriptCache::clear_unreferenced_packed_scenes();
  144. if (singleton->parser_map.has(p_path)) {
  145. singleton->parser_map[p_path]->clear();
  146. singleton->parser_map.erase(p_path);
  147. }
  148. singleton->dependencies.erase(p_path);
  149. singleton->shallow_gdscript_cache.erase(p_path);
  150. singleton->full_gdscript_cache.erase(p_path);
  151. }
  152. Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptParserRef::Status p_status, Error &r_error, const String &p_owner) {
  153. MutexLock lock(singleton->mutex);
  154. Ref<GDScriptParserRef> ref;
  155. if (!p_owner.is_empty()) {
  156. singleton->dependencies[p_owner].insert(p_path);
  157. }
  158. if (singleton->parser_map.has(p_path)) {
  159. ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
  160. if (ref.is_null()) {
  161. r_error = ERR_INVALID_DATA;
  162. return ref;
  163. }
  164. } else {
  165. if (!FileAccess::exists(p_path)) {
  166. r_error = ERR_FILE_NOT_FOUND;
  167. return ref;
  168. }
  169. GDScriptParser *parser = memnew(GDScriptParser);
  170. ref.instantiate();
  171. ref->parser = parser;
  172. ref->path = p_path;
  173. singleton->parser_map[p_path] = ref.ptr();
  174. }
  175. r_error = ref->raise_status(p_status);
  176. return ref;
  177. }
  178. String GDScriptCache::get_source_code(const String &p_path) {
  179. Vector<uint8_t> source_file;
  180. Error err;
  181. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  182. ERR_FAIL_COND_V(err, "");
  183. uint64_t len = f->get_length();
  184. source_file.resize(len + 1);
  185. uint64_t r = f->get_buffer(source_file.ptrw(), len);
  186. ERR_FAIL_COND_V(r != len, "");
  187. source_file.write[len] = 0;
  188. String source;
  189. if (source.parse_utf8((const char *)source_file.ptr()) != OK) {
  190. 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.");
  191. }
  192. return source;
  193. }
  194. Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, Error &r_error, const String &p_owner) {
  195. MutexLock lock(singleton->mutex);
  196. if (!p_owner.is_empty()) {
  197. singleton->dependencies[p_owner].insert(p_path);
  198. }
  199. if (singleton->full_gdscript_cache.has(p_path)) {
  200. return singleton->full_gdscript_cache[p_path];
  201. }
  202. if (singleton->shallow_gdscript_cache.has(p_path)) {
  203. return singleton->shallow_gdscript_cache[p_path];
  204. }
  205. Ref<GDScript> script;
  206. script.instantiate();
  207. script->set_path(p_path, true);
  208. script->load_source_code(p_path);
  209. Ref<GDScriptParserRef> parser_ref = get_parser(p_path, GDScriptParserRef::PARSED, r_error);
  210. if (r_error == OK) {
  211. GDScriptCompiler::make_scripts(script.ptr(), parser_ref->get_parser()->get_tree(), true);
  212. }
  213. singleton->shallow_gdscript_cache[p_path] = script;
  214. return script;
  215. }
  216. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
  217. MutexLock lock(singleton->mutex);
  218. if (!p_owner.is_empty()) {
  219. singleton->dependencies[p_owner].insert(p_path);
  220. }
  221. Ref<GDScript> script;
  222. r_error = OK;
  223. if (singleton->full_gdscript_cache.has(p_path)) {
  224. script = Ref<GDScript>(singleton->full_gdscript_cache[p_path]);
  225. if (!p_update_from_disk) {
  226. return script;
  227. }
  228. }
  229. if (script.is_null()) {
  230. script = get_shallow_script(p_path, r_error);
  231. if (r_error) {
  232. return script;
  233. }
  234. }
  235. if (p_update_from_disk) {
  236. r_error = script->load_source_code(p_path);
  237. }
  238. if (r_error) {
  239. return script;
  240. }
  241. singleton->full_gdscript_cache[p_path] = script;
  242. singleton->shallow_gdscript_cache.erase(p_path);
  243. r_error = script->reload(true);
  244. if (r_error) {
  245. singleton->shallow_gdscript_cache[p_path] = script;
  246. singleton->full_gdscript_cache.erase(p_path);
  247. return script;
  248. }
  249. return script;
  250. }
  251. Ref<GDScript> GDScriptCache::get_cached_script(const String &p_path) {
  252. MutexLock lock(singleton->mutex);
  253. if (singleton->full_gdscript_cache.has(p_path)) {
  254. return singleton->full_gdscript_cache[p_path];
  255. }
  256. if (singleton->shallow_gdscript_cache.has(p_path)) {
  257. return singleton->shallow_gdscript_cache[p_path];
  258. }
  259. return Ref<GDScript>();
  260. }
  261. Error GDScriptCache::finish_compiling(const String &p_owner) {
  262. MutexLock lock(singleton->mutex);
  263. // Mark this as compiled.
  264. Ref<GDScript> script = get_cached_script(p_owner);
  265. singleton->full_gdscript_cache[p_owner] = script;
  266. singleton->shallow_gdscript_cache.erase(p_owner);
  267. HashSet<String> depends = singleton->dependencies[p_owner];
  268. Error err = OK;
  269. for (const String &E : depends) {
  270. Error this_err = OK;
  271. // No need to save the script. We assume it's already referenced in the owner.
  272. get_full_script(E, this_err);
  273. if (this_err != OK) {
  274. err = this_err;
  275. }
  276. }
  277. singleton->dependencies.erase(p_owner);
  278. return err;
  279. }
  280. Ref<PackedScene> GDScriptCache::get_packed_scene(const String &p_path, Error &r_error, const String &p_owner) {
  281. MutexLock lock(singleton->mutex);
  282. if (singleton->packed_scene_cache.has(p_path)) {
  283. singleton->packed_scene_dependencies[p_path].insert(p_owner);
  284. return singleton->packed_scene_cache[p_path];
  285. }
  286. Ref<PackedScene> scene = ResourceCache::get_ref(p_path);
  287. if (scene.is_valid()) {
  288. singleton->packed_scene_cache[p_path] = scene;
  289. singleton->packed_scene_dependencies[p_path].insert(p_owner);
  290. return scene;
  291. }
  292. scene.instantiate();
  293. r_error = OK;
  294. if (p_path.is_empty()) {
  295. r_error = ERR_FILE_BAD_PATH;
  296. return scene;
  297. }
  298. scene->set_path(p_path);
  299. singleton->packed_scene_cache[p_path] = scene;
  300. singleton->packed_scene_dependencies[p_path].insert(p_owner);
  301. scene->recreate_state();
  302. scene->reload_from_file();
  303. return scene;
  304. }
  305. void GDScriptCache::clear_unreferenced_packed_scenes() {
  306. if (singleton == nullptr) {
  307. return;
  308. }
  309. MutexLock lock(singleton->mutex);
  310. for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
  311. if (E.value.size() > 0 || !ResourceLoader::is_imported(E.key)) {
  312. continue;
  313. }
  314. singleton->packed_scene_dependencies.erase(E.key);
  315. singleton->packed_scene_cache.erase(E.key);
  316. }
  317. }
  318. GDScriptCache::GDScriptCache() {
  319. singleton = this;
  320. }
  321. GDScriptCache::~GDScriptCache() {
  322. destructing = true;
  323. RBSet<Ref<GDScriptParserRef>> parser_map_refs;
  324. for (KeyValue<String, GDScriptParserRef *> &E : parser_map) {
  325. parser_map_refs.insert(E.value);
  326. }
  327. for (Ref<GDScriptParserRef> &E : parser_map_refs) {
  328. if (E.is_valid())
  329. E->clear();
  330. }
  331. parser_map_refs.clear();
  332. parser_map.clear();
  333. shallow_gdscript_cache.clear();
  334. full_gdscript_cache.clear();
  335. packed_scene_cache.clear();
  336. packed_scene_dependencies.clear();
  337. singleton = nullptr;
  338. }