gdscript_cache.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 "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. script->reload(true);
  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. singleton->packed_scene_dependencies.clear();
  344. singleton->packed_scene_cache.clear();
  345. parser_map_refs.clear();
  346. singleton->parser_map.clear();
  347. singleton->shallow_gdscript_cache.clear();
  348. singleton->full_gdscript_cache.clear();
  349. singleton->packed_scene_cache.clear();
  350. singleton->packed_scene_dependencies.clear();
  351. }
  352. GDScriptCache::GDScriptCache() {
  353. singleton = this;
  354. }
  355. GDScriptCache::~GDScriptCache() {
  356. if (!cleared) {
  357. clear();
  358. }
  359. singleton = nullptr;
  360. }