gdscript_cache.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #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_NULL_V(parser, 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. r_error = script->load_source_code(p_path);
  220. if (r_error) {
  221. return Ref<GDScript>(); // Returns null and does not cache when the script fails to load.
  222. }
  223. Ref<GDScriptParserRef> parser_ref = get_parser(p_path, GDScriptParserRef::PARSED, r_error);
  224. if (r_error == OK) {
  225. GDScriptCompiler::make_scripts(script.ptr(), parser_ref->get_parser()->get_tree(), true);
  226. }
  227. singleton->shallow_gdscript_cache[p_path] = script;
  228. return script;
  229. }
  230. Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
  231. MutexLock lock(singleton->mutex);
  232. if (!p_owner.is_empty()) {
  233. singleton->dependencies[p_owner].insert(p_path);
  234. }
  235. Ref<GDScript> script;
  236. r_error = OK;
  237. if (singleton->full_gdscript_cache.has(p_path)) {
  238. script = singleton->full_gdscript_cache[p_path];
  239. if (!p_update_from_disk) {
  240. return script;
  241. }
  242. }
  243. if (script.is_null()) {
  244. script = get_shallow_script(p_path, r_error);
  245. // Only exit early if script failed to load, otherwise let reload report errors.
  246. if (script.is_null()) {
  247. return script;
  248. }
  249. }
  250. if (p_update_from_disk) {
  251. r_error = script->load_source_code(p_path);
  252. if (r_error) {
  253. return script;
  254. }
  255. }
  256. r_error = script->reload(true);
  257. if (r_error) {
  258. return script;
  259. }
  260. singleton->full_gdscript_cache[p_path] = script;
  261. singleton->shallow_gdscript_cache.erase(p_path);
  262. return script;
  263. }
  264. Ref<GDScript> GDScriptCache::get_cached_script(const String &p_path) {
  265. MutexLock lock(singleton->mutex);
  266. if (singleton->full_gdscript_cache.has(p_path)) {
  267. return singleton->full_gdscript_cache[p_path];
  268. }
  269. if (singleton->shallow_gdscript_cache.has(p_path)) {
  270. return singleton->shallow_gdscript_cache[p_path];
  271. }
  272. return Ref<GDScript>();
  273. }
  274. Error GDScriptCache::finish_compiling(const String &p_owner) {
  275. MutexLock lock(singleton->mutex);
  276. // Mark this as compiled.
  277. Ref<GDScript> script = get_cached_script(p_owner);
  278. singleton->full_gdscript_cache[p_owner] = script;
  279. singleton->shallow_gdscript_cache.erase(p_owner);
  280. HashSet<String> depends = singleton->dependencies[p_owner];
  281. Error err = OK;
  282. for (const String &E : depends) {
  283. Error this_err = OK;
  284. // No need to save the script. We assume it's already referenced in the owner.
  285. get_full_script(E, this_err);
  286. if (this_err != OK) {
  287. err = this_err;
  288. }
  289. }
  290. singleton->dependencies.erase(p_owner);
  291. return err;
  292. }
  293. void GDScriptCache::add_static_script(Ref<GDScript> p_script) {
  294. ERR_FAIL_COND_MSG(p_script.is_null(), "Trying to cache empty script as static.");
  295. ERR_FAIL_COND_MSG(!p_script->is_valid(), "Trying to cache non-compiled script as static.");
  296. singleton->static_gdscript_cache[p_script->get_fully_qualified_name()] = p_script;
  297. }
  298. void GDScriptCache::remove_static_script(const String &p_fqcn) {
  299. singleton->static_gdscript_cache.erase(p_fqcn);
  300. }
  301. Ref<PackedScene> GDScriptCache::get_packed_scene(const String &p_path, Error &r_error, const String &p_owner) {
  302. MutexLock lock(singleton->mutex);
  303. String path = p_path;
  304. if (path.begins_with("uid://")) {
  305. path = ResourceUID::get_singleton()->get_id_path(ResourceUID::get_singleton()->text_to_id(path));
  306. }
  307. if (singleton->packed_scene_cache.has(path)) {
  308. singleton->packed_scene_dependencies[path].insert(p_owner);
  309. return singleton->packed_scene_cache[path];
  310. }
  311. Ref<PackedScene> scene = ResourceCache::get_ref(path);
  312. if (scene.is_valid()) {
  313. singleton->packed_scene_cache[path] = scene;
  314. singleton->packed_scene_dependencies[path].insert(p_owner);
  315. return scene;
  316. }
  317. scene.instantiate();
  318. r_error = OK;
  319. if (path.is_empty()) {
  320. r_error = ERR_FILE_BAD_PATH;
  321. return scene;
  322. }
  323. scene->set_path(path);
  324. singleton->packed_scene_cache[path] = scene;
  325. singleton->packed_scene_dependencies[path].insert(p_owner);
  326. scene->reload_from_file();
  327. return scene;
  328. }
  329. void GDScriptCache::clear_unreferenced_packed_scenes() {
  330. if (singleton == nullptr) {
  331. return;
  332. }
  333. MutexLock lock(singleton->mutex);
  334. if (singleton->cleared) {
  335. return;
  336. }
  337. for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
  338. if (E.value.size() > 0 || !ResourceLoader::is_imported(E.key)) {
  339. continue;
  340. }
  341. singleton->packed_scene_dependencies.erase(E.key);
  342. singleton->packed_scene_cache.erase(E.key);
  343. }
  344. }
  345. void GDScriptCache::clear() {
  346. if (singleton == nullptr) {
  347. return;
  348. }
  349. MutexLock lock(singleton->mutex);
  350. if (singleton->cleared) {
  351. return;
  352. }
  353. singleton->cleared = true;
  354. RBSet<Ref<GDScriptParserRef>> parser_map_refs;
  355. for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) {
  356. parser_map_refs.insert(E.value);
  357. }
  358. for (Ref<GDScriptParserRef> &E : parser_map_refs) {
  359. if (E.is_valid())
  360. E->clear();
  361. }
  362. singleton->packed_scene_dependencies.clear();
  363. singleton->packed_scene_cache.clear();
  364. parser_map_refs.clear();
  365. singleton->parser_map.clear();
  366. singleton->shallow_gdscript_cache.clear();
  367. singleton->full_gdscript_cache.clear();
  368. singleton->packed_scene_cache.clear();
  369. singleton->packed_scene_dependencies.clear();
  370. }
  371. GDScriptCache::GDScriptCache() {
  372. singleton = this;
  373. }
  374. GDScriptCache::~GDScriptCache() {
  375. if (!cleared) {
  376. clear();
  377. }
  378. singleton = nullptr;
  379. }