file_access.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /**************************************************************************/
  2. /* file_access.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 "file_access.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/crypto/crypto_core.h"
  33. #include "core/io/file_access_compressed.h"
  34. #include "core/io/file_access_encrypted.h"
  35. #include "core/io/file_access_pack.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/os/os.h"
  38. FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { nullptr, nullptr };
  39. FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = nullptr;
  40. bool FileAccess::backup_save = false;
  41. thread_local Error FileAccess::last_file_open_error = OK;
  42. Ref<FileAccess> FileAccess::create(AccessType p_access) {
  43. ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr);
  44. Ref<FileAccess> ret = create_func[p_access]();
  45. ret->_set_access_type(p_access);
  46. return ret;
  47. }
  48. bool FileAccess::exists(const String &p_name) {
  49. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_name)) {
  50. return true;
  51. }
  52. Ref<FileAccess> f = open(p_name, READ);
  53. if (f.is_null()) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. void FileAccess::_set_access_type(AccessType p_access) {
  59. _access_type = p_access;
  60. }
  61. Ref<FileAccess> FileAccess::create_for_path(const String &p_path) {
  62. Ref<FileAccess> ret;
  63. if (p_path.begins_with("res://")) {
  64. ret = create(ACCESS_RESOURCES);
  65. } else if (p_path.begins_with("user://")) {
  66. ret = create(ACCESS_USERDATA);
  67. } else {
  68. ret = create(ACCESS_FILESYSTEM);
  69. }
  70. return ret;
  71. }
  72. Error FileAccess::reopen(const String &p_path, int p_mode_flags) {
  73. return open_internal(p_path, p_mode_flags);
  74. }
  75. Ref<FileAccess> FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) {
  76. //try packed data first
  77. Ref<FileAccess> ret;
  78. if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
  79. ret = PackedData::get_singleton()->try_open_path(p_path);
  80. if (ret.is_valid()) {
  81. if (r_error) {
  82. *r_error = OK;
  83. }
  84. return ret;
  85. }
  86. }
  87. ret = create_for_path(p_path);
  88. Error err = ret->open_internal(p_path, p_mode_flags);
  89. if (r_error) {
  90. *r_error = err;
  91. }
  92. if (err != OK) {
  93. ret.unref();
  94. }
  95. return ret;
  96. }
  97. Ref<FileAccess> FileAccess::_open(const String &p_path, ModeFlags p_mode_flags) {
  98. Error err = OK;
  99. Ref<FileAccess> fa = open(p_path, p_mode_flags, &err);
  100. last_file_open_error = err;
  101. if (err) {
  102. return Ref<FileAccess>();
  103. }
  104. return fa;
  105. }
  106. Ref<FileAccess> FileAccess::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  107. Ref<FileAccess> fa = _open(p_path, p_mode_flags);
  108. if (fa.is_null()) {
  109. return fa;
  110. }
  111. Ref<FileAccessEncrypted> fae;
  112. fae.instantiate();
  113. Error err = fae->open_and_parse(fa, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  114. last_file_open_error = err;
  115. if (err) {
  116. return Ref<FileAccess>();
  117. }
  118. return fae;
  119. }
  120. Ref<FileAccess> FileAccess::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  121. Ref<FileAccess> fa = _open(p_path, p_mode_flags);
  122. if (fa.is_null()) {
  123. return fa;
  124. }
  125. Ref<FileAccessEncrypted> fae;
  126. fae.instantiate();
  127. Error err = fae->open_and_parse_password(fa, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  128. last_file_open_error = err;
  129. if (err) {
  130. return Ref<FileAccess>();
  131. }
  132. return fae;
  133. }
  134. Ref<FileAccess> FileAccess::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  135. Ref<FileAccessCompressed> fac;
  136. fac.instantiate();
  137. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  138. Error err = fac->open_internal(p_path, p_mode_flags);
  139. last_file_open_error = err;
  140. if (err) {
  141. return Ref<FileAccess>();
  142. }
  143. return fac;
  144. }
  145. Error FileAccess::get_open_error() {
  146. return last_file_open_error;
  147. }
  148. FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
  149. return create_func[p_access];
  150. }
  151. FileAccess::AccessType FileAccess::get_access_type() const {
  152. return _access_type;
  153. }
  154. String FileAccess::fix_path(const String &p_path) const {
  155. //helper used by file accesses that use a single filesystem
  156. String r_path = p_path.replace("\\", "/");
  157. switch (_access_type) {
  158. case ACCESS_RESOURCES: {
  159. if (ProjectSettings::get_singleton()) {
  160. if (r_path.begins_with("res://")) {
  161. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  162. if (!resource_path.is_empty()) {
  163. return r_path.replace("res:/", resource_path);
  164. }
  165. return r_path.replace("res://", "");
  166. }
  167. }
  168. } break;
  169. case ACCESS_USERDATA: {
  170. if (r_path.begins_with("user://")) {
  171. String data_dir = OS::get_singleton()->get_user_data_dir();
  172. if (!data_dir.is_empty()) {
  173. return r_path.replace("user:/", data_dir);
  174. }
  175. return r_path.replace("user://", "");
  176. }
  177. } break;
  178. case ACCESS_FILESYSTEM: {
  179. return r_path;
  180. } break;
  181. case ACCESS_MAX:
  182. break; // Can't happen, but silences warning
  183. }
  184. return r_path;
  185. }
  186. /* these are all implemented for ease of porting, then can later be optimized */
  187. uint16_t FileAccess::get_16() const {
  188. uint16_t res;
  189. uint8_t a, b;
  190. a = get_8();
  191. b = get_8();
  192. if (big_endian) {
  193. SWAP(a, b);
  194. }
  195. res = b;
  196. res <<= 8;
  197. res |= a;
  198. return res;
  199. }
  200. uint32_t FileAccess::get_32() const {
  201. uint32_t res;
  202. uint16_t a, b;
  203. a = get_16();
  204. b = get_16();
  205. if (big_endian) {
  206. SWAP(a, b);
  207. }
  208. res = b;
  209. res <<= 16;
  210. res |= a;
  211. return res;
  212. }
  213. uint64_t FileAccess::get_64() const {
  214. uint64_t res;
  215. uint32_t a, b;
  216. a = get_32();
  217. b = get_32();
  218. if (big_endian) {
  219. SWAP(a, b);
  220. }
  221. res = b;
  222. res <<= 32;
  223. res |= a;
  224. return res;
  225. }
  226. float FileAccess::get_float() const {
  227. MarshallFloat m;
  228. m.i = get_32();
  229. return m.f;
  230. }
  231. real_t FileAccess::get_real() const {
  232. if (real_is_double) {
  233. return get_double();
  234. } else {
  235. return get_float();
  236. }
  237. }
  238. Variant FileAccess::get_var(bool p_allow_objects) const {
  239. uint32_t len = get_32();
  240. Vector<uint8_t> buff = get_buffer(len);
  241. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  242. const uint8_t *r = buff.ptr();
  243. Variant v;
  244. Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects);
  245. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant.");
  246. return v;
  247. }
  248. double FileAccess::get_double() const {
  249. MarshallDouble m;
  250. m.l = get_64();
  251. return m.d;
  252. }
  253. String FileAccess::get_token() const {
  254. CharString token;
  255. char32_t c = get_8();
  256. while (!eof_reached()) {
  257. if (c <= ' ') {
  258. if (token.length()) {
  259. break;
  260. }
  261. } else {
  262. token += c;
  263. }
  264. c = get_8();
  265. }
  266. return String::utf8(token.get_data());
  267. }
  268. class CharBuffer {
  269. Vector<char> vector;
  270. char stack_buffer[256];
  271. char *buffer = nullptr;
  272. int capacity = 0;
  273. int written = 0;
  274. bool grow() {
  275. if (vector.resize(next_power_of_2(1 + written)) != OK) {
  276. return false;
  277. }
  278. if (buffer == stack_buffer) { // first chunk?
  279. for (int i = 0; i < written; i++) {
  280. vector.write[i] = stack_buffer[i];
  281. }
  282. }
  283. buffer = vector.ptrw();
  284. capacity = vector.size();
  285. ERR_FAIL_COND_V(written >= capacity, false);
  286. return true;
  287. }
  288. public:
  289. _FORCE_INLINE_ CharBuffer() :
  290. buffer(stack_buffer),
  291. capacity(sizeof(stack_buffer) / sizeof(char)) {
  292. }
  293. _FORCE_INLINE_ void push_back(char c) {
  294. if (written >= capacity) {
  295. ERR_FAIL_COND(!grow());
  296. }
  297. buffer[written++] = c;
  298. }
  299. _FORCE_INLINE_ const char *get_data() const {
  300. return buffer;
  301. }
  302. };
  303. String FileAccess::get_line() const {
  304. CharBuffer line;
  305. char32_t c = get_8();
  306. while (!eof_reached()) {
  307. if (c == '\n' || c == '\0') {
  308. line.push_back(0);
  309. return String::utf8(line.get_data());
  310. } else if (c != '\r') {
  311. line.push_back(c);
  312. }
  313. c = get_8();
  314. }
  315. line.push_back(0);
  316. return String::utf8(line.get_data());
  317. }
  318. Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
  319. ERR_FAIL_COND_V_MSG(p_delim.length() != 1, Vector<String>(), "Only single character delimiters are supported to parse CSV lines.");
  320. ERR_FAIL_COND_V_MSG(p_delim[0] == '"', Vector<String>(), "The double quotation mark character (\") is not supported as a delimiter for CSV lines.");
  321. String line;
  322. // CSV can support entries with line breaks as long as they are enclosed
  323. // in double quotes. So our "line" might be more than a single line in the
  324. // text file.
  325. int qc = 0;
  326. do {
  327. if (eof_reached()) {
  328. break;
  329. }
  330. line += get_line() + "\n";
  331. qc = 0;
  332. for (int i = 0; i < line.length(); i++) {
  333. if (line[i] == '"') {
  334. qc++;
  335. }
  336. }
  337. } while (qc % 2);
  338. // Remove the extraneous newline we've added above.
  339. line = line.substr(0, line.length() - 1);
  340. Vector<String> strings;
  341. bool in_quote = false;
  342. String current;
  343. for (int i = 0; i < line.length(); i++) {
  344. char32_t c = line[i];
  345. // A delimiter ends the current entry, unless it's in a quoted string.
  346. if (!in_quote && c == p_delim[0]) {
  347. strings.push_back(current);
  348. current = String();
  349. } else if (c == '"') {
  350. // Doubled quotes are escapes for intentional quotes in the string.
  351. if (line[i + 1] == '"' && in_quote) {
  352. current += '"';
  353. i++;
  354. } else {
  355. in_quote = !in_quote;
  356. }
  357. } else {
  358. current += c;
  359. }
  360. }
  361. if (in_quote) {
  362. WARN_PRINT(vformat("Reached end of file before closing '\"' in CSV file '%s'.", get_path()));
  363. }
  364. strings.push_back(current);
  365. return strings;
  366. }
  367. String FileAccess::get_as_text(bool p_skip_cr) const {
  368. uint64_t original_pos = get_position();
  369. const_cast<FileAccess *>(this)->seek(0);
  370. String text = get_as_utf8_string(p_skip_cr);
  371. const_cast<FileAccess *>(this)->seek(original_pos);
  372. return text;
  373. }
  374. uint64_t FileAccess::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  375. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  376. uint64_t i = 0;
  377. for (i = 0; i < p_length && !eof_reached(); i++) {
  378. p_dst[i] = get_8();
  379. }
  380. return i;
  381. }
  382. Vector<uint8_t> FileAccess::get_buffer(int64_t p_length) const {
  383. Vector<uint8_t> data;
  384. ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0.");
  385. if (p_length == 0) {
  386. return data;
  387. }
  388. Error err = data.resize(p_length);
  389. ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");
  390. uint8_t *w = data.ptrw();
  391. int64_t len = get_buffer(&w[0], p_length);
  392. if (len < p_length) {
  393. data.resize(len);
  394. }
  395. return data;
  396. }
  397. String FileAccess::get_as_utf8_string(bool p_skip_cr) const {
  398. Vector<uint8_t> sourcef;
  399. uint64_t len = get_length();
  400. sourcef.resize(len + 1);
  401. uint8_t *w = sourcef.ptrw();
  402. uint64_t r = get_buffer(w, len);
  403. ERR_FAIL_COND_V(r != len, String());
  404. w[len] = 0;
  405. String s;
  406. s.parse_utf8((const char *)w, -1, p_skip_cr);
  407. return s;
  408. }
  409. void FileAccess::store_16(uint16_t p_dest) {
  410. uint8_t a, b;
  411. a = p_dest & 0xFF;
  412. b = p_dest >> 8;
  413. if (big_endian) {
  414. SWAP(a, b);
  415. }
  416. store_8(a);
  417. store_8(b);
  418. }
  419. void FileAccess::store_32(uint32_t p_dest) {
  420. uint16_t a, b;
  421. a = p_dest & 0xFFFF;
  422. b = p_dest >> 16;
  423. if (big_endian) {
  424. SWAP(a, b);
  425. }
  426. store_16(a);
  427. store_16(b);
  428. }
  429. void FileAccess::store_64(uint64_t p_dest) {
  430. uint32_t a, b;
  431. a = p_dest & 0xFFFFFFFF;
  432. b = p_dest >> 32;
  433. if (big_endian) {
  434. SWAP(a, b);
  435. }
  436. store_32(a);
  437. store_32(b);
  438. }
  439. void FileAccess::store_real(real_t p_real) {
  440. if constexpr (sizeof(real_t) == 4) {
  441. store_float(p_real);
  442. } else {
  443. store_double(p_real);
  444. }
  445. }
  446. void FileAccess::store_float(float p_dest) {
  447. MarshallFloat m;
  448. m.f = p_dest;
  449. store_32(m.i);
  450. }
  451. void FileAccess::store_double(double p_dest) {
  452. MarshallDouble m;
  453. m.d = p_dest;
  454. store_64(m.l);
  455. }
  456. uint64_t FileAccess::get_modified_time(const String &p_file) {
  457. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) {
  458. return 0;
  459. }
  460. Ref<FileAccess> fa = create_for_path(p_file);
  461. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "Cannot create FileAccess for path '" + p_file + "'.");
  462. uint64_t mt = fa->_get_modified_time(p_file);
  463. return mt;
  464. }
  465. uint32_t FileAccess::get_unix_permissions(const String &p_file) {
  466. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) {
  467. return 0;
  468. }
  469. Ref<FileAccess> fa = create_for_path(p_file);
  470. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "Cannot create FileAccess for path '" + p_file + "'.");
  471. uint32_t mt = fa->_get_unix_permissions(p_file);
  472. return mt;
  473. }
  474. Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  475. if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) {
  476. return ERR_UNAVAILABLE;
  477. }
  478. Ref<FileAccess> fa = create_for_path(p_file);
  479. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'.");
  480. Error err = fa->_set_unix_permissions(p_file, p_permissions);
  481. return err;
  482. }
  483. void FileAccess::store_string(const String &p_string) {
  484. if (p_string.length() == 0) {
  485. return;
  486. }
  487. CharString cs = p_string.utf8();
  488. store_buffer((uint8_t *)&cs[0], cs.length());
  489. }
  490. void FileAccess::store_pascal_string(const String &p_string) {
  491. CharString cs = p_string.utf8();
  492. store_32(cs.length());
  493. store_buffer((uint8_t *)&cs[0], cs.length());
  494. }
  495. String FileAccess::get_pascal_string() {
  496. uint32_t sl = get_32();
  497. CharString cs;
  498. cs.resize(sl + 1);
  499. get_buffer((uint8_t *)cs.ptr(), sl);
  500. cs[sl] = 0;
  501. String ret;
  502. ret.parse_utf8(cs.ptr());
  503. return ret;
  504. }
  505. void FileAccess::store_line(const String &p_line) {
  506. store_string(p_line);
  507. store_8('\n');
  508. }
  509. void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  510. ERR_FAIL_COND(p_delim.length() != 1);
  511. String line = "";
  512. int size = p_values.size();
  513. for (int i = 0; i < size; ++i) {
  514. String value = p_values[i];
  515. if (value.contains("\"") || value.contains(p_delim) || value.contains("\n")) {
  516. value = "\"" + value.replace("\"", "\"\"") + "\"";
  517. }
  518. if (i < size - 1) {
  519. value += p_delim;
  520. }
  521. line += value;
  522. }
  523. store_line(line);
  524. }
  525. void FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  526. ERR_FAIL_COND(!p_src && p_length > 0);
  527. for (uint64_t i = 0; i < p_length; i++) {
  528. store_8(p_src[i]);
  529. }
  530. }
  531. void FileAccess::store_buffer(const Vector<uint8_t> &p_buffer) {
  532. uint64_t len = p_buffer.size();
  533. if (len == 0) {
  534. return;
  535. }
  536. const uint8_t *r = p_buffer.ptr();
  537. store_buffer(&r[0], len);
  538. }
  539. void FileAccess::store_var(const Variant &p_var, bool p_full_objects) {
  540. int len;
  541. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  542. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  543. Vector<uint8_t> buff;
  544. buff.resize(len);
  545. uint8_t *w = buff.ptrw();
  546. err = encode_variant(p_var, &w[0], len, p_full_objects);
  547. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  548. store_32(len);
  549. store_buffer(buff);
  550. }
  551. Vector<uint8_t> FileAccess::get_file_as_bytes(const String &p_path, Error *r_error) {
  552. Ref<FileAccess> f = FileAccess::open(p_path, READ, r_error);
  553. if (f.is_null()) {
  554. if (r_error) { // if error requested, do not throw error
  555. return Vector<uint8_t>();
  556. }
  557. ERR_FAIL_V_MSG(Vector<uint8_t>(), "Can't open file from path '" + String(p_path) + "'.");
  558. }
  559. Vector<uint8_t> data;
  560. data.resize(f->get_length());
  561. f->get_buffer(data.ptrw(), data.size());
  562. return data;
  563. }
  564. String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
  565. Error err;
  566. Vector<uint8_t> array = get_file_as_bytes(p_path, &err);
  567. if (r_error) {
  568. *r_error = err;
  569. }
  570. if (err != OK) {
  571. if (r_error) {
  572. return String();
  573. }
  574. ERR_FAIL_V_MSG(String(), "Can't get file as string from path '" + String(p_path) + "'.");
  575. }
  576. String ret;
  577. ret.parse_utf8((const char *)array.ptr(), array.size());
  578. return ret;
  579. }
  580. String FileAccess::get_md5(const String &p_file) {
  581. Ref<FileAccess> f = FileAccess::open(p_file, READ);
  582. if (f.is_null()) {
  583. return String();
  584. }
  585. CryptoCore::MD5Context ctx;
  586. ctx.start();
  587. unsigned char step[32768];
  588. while (true) {
  589. uint64_t br = f->get_buffer(step, 32768);
  590. if (br > 0) {
  591. ctx.update(step, br);
  592. }
  593. if (br < 4096) {
  594. break;
  595. }
  596. }
  597. unsigned char hash[16];
  598. ctx.finish(hash);
  599. return String::md5(hash);
  600. }
  601. String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
  602. CryptoCore::MD5Context ctx;
  603. ctx.start();
  604. for (int i = 0; i < p_file.size(); i++) {
  605. Ref<FileAccess> f = FileAccess::open(p_file[i], READ);
  606. ERR_CONTINUE(f.is_null());
  607. unsigned char step[32768];
  608. while (true) {
  609. uint64_t br = f->get_buffer(step, 32768);
  610. if (br > 0) {
  611. ctx.update(step, br);
  612. }
  613. if (br < 4096) {
  614. break;
  615. }
  616. }
  617. }
  618. unsigned char hash[16];
  619. ctx.finish(hash);
  620. return String::md5(hash);
  621. }
  622. String FileAccess::get_sha256(const String &p_file) {
  623. Ref<FileAccess> f = FileAccess::open(p_file, READ);
  624. if (f.is_null()) {
  625. return String();
  626. }
  627. CryptoCore::SHA256Context ctx;
  628. ctx.start();
  629. unsigned char step[32768];
  630. while (true) {
  631. uint64_t br = f->get_buffer(step, 32768);
  632. if (br > 0) {
  633. ctx.update(step, br);
  634. }
  635. if (br < 4096) {
  636. break;
  637. }
  638. }
  639. unsigned char hash[32];
  640. ctx.finish(hash);
  641. return String::hex_encode_buffer(hash, 32);
  642. }
  643. void FileAccess::_bind_methods() {
  644. ClassDB::bind_static_method("FileAccess", D_METHOD("open", "path", "flags"), &FileAccess::_open);
  645. ClassDB::bind_static_method("FileAccess", D_METHOD("open_encrypted", "path", "mode_flags", "key"), &FileAccess::open_encrypted);
  646. ClassDB::bind_static_method("FileAccess", D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &FileAccess::open_encrypted_pass);
  647. ClassDB::bind_static_method("FileAccess", D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &FileAccess::open_compressed, DEFVAL(0));
  648. ClassDB::bind_static_method("FileAccess", D_METHOD("get_open_error"), &FileAccess::get_open_error);
  649. ClassDB::bind_static_method("FileAccess", D_METHOD("get_file_as_bytes", "path"), &FileAccess::_get_file_as_bytes);
  650. ClassDB::bind_static_method("FileAccess", D_METHOD("get_file_as_string", "path"), &FileAccess::_get_file_as_string);
  651. ClassDB::bind_method(D_METHOD("flush"), &FileAccess::flush);
  652. ClassDB::bind_method(D_METHOD("get_path"), &FileAccess::get_path);
  653. ClassDB::bind_method(D_METHOD("get_path_absolute"), &FileAccess::get_path_absolute);
  654. ClassDB::bind_method(D_METHOD("is_open"), &FileAccess::is_open);
  655. ClassDB::bind_method(D_METHOD("seek", "position"), &FileAccess::seek);
  656. ClassDB::bind_method(D_METHOD("seek_end", "position"), &FileAccess::seek_end, DEFVAL(0));
  657. ClassDB::bind_method(D_METHOD("get_position"), &FileAccess::get_position);
  658. ClassDB::bind_method(D_METHOD("get_length"), &FileAccess::get_length);
  659. ClassDB::bind_method(D_METHOD("eof_reached"), &FileAccess::eof_reached);
  660. ClassDB::bind_method(D_METHOD("get_8"), &FileAccess::get_8);
  661. ClassDB::bind_method(D_METHOD("get_16"), &FileAccess::get_16);
  662. ClassDB::bind_method(D_METHOD("get_32"), &FileAccess::get_32);
  663. ClassDB::bind_method(D_METHOD("get_64"), &FileAccess::get_64);
  664. ClassDB::bind_method(D_METHOD("get_float"), &FileAccess::get_float);
  665. ClassDB::bind_method(D_METHOD("get_double"), &FileAccess::get_double);
  666. ClassDB::bind_method(D_METHOD("get_real"), &FileAccess::get_real);
  667. ClassDB::bind_method(D_METHOD("get_buffer", "length"), (Vector<uint8_t>(FileAccess::*)(int64_t) const) & FileAccess::get_buffer);
  668. ClassDB::bind_method(D_METHOD("get_line"), &FileAccess::get_line);
  669. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &FileAccess::get_csv_line, DEFVAL(","));
  670. ClassDB::bind_method(D_METHOD("get_as_text", "skip_cr"), &FileAccess::get_as_text, DEFVAL(false));
  671. ClassDB::bind_static_method("FileAccess", D_METHOD("get_md5", "path"), &FileAccess::get_md5);
  672. ClassDB::bind_static_method("FileAccess", D_METHOD("get_sha256", "path"), &FileAccess::get_sha256);
  673. ClassDB::bind_method(D_METHOD("is_big_endian"), &FileAccess::is_big_endian);
  674. ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &FileAccess::set_big_endian);
  675. ClassDB::bind_method(D_METHOD("get_error"), &FileAccess::get_error);
  676. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &FileAccess::get_var, DEFVAL(false));
  677. ClassDB::bind_method(D_METHOD("store_8", "value"), &FileAccess::store_8);
  678. ClassDB::bind_method(D_METHOD("store_16", "value"), &FileAccess::store_16);
  679. ClassDB::bind_method(D_METHOD("store_32", "value"), &FileAccess::store_32);
  680. ClassDB::bind_method(D_METHOD("store_64", "value"), &FileAccess::store_64);
  681. ClassDB::bind_method(D_METHOD("store_float", "value"), &FileAccess::store_float);
  682. ClassDB::bind_method(D_METHOD("store_double", "value"), &FileAccess::store_double);
  683. ClassDB::bind_method(D_METHOD("store_real", "value"), &FileAccess::store_real);
  684. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), (void(FileAccess::*)(const Vector<uint8_t> &)) & FileAccess::store_buffer);
  685. ClassDB::bind_method(D_METHOD("store_line", "line"), &FileAccess::store_line);
  686. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &FileAccess::store_csv_line, DEFVAL(","));
  687. ClassDB::bind_method(D_METHOD("store_string", "string"), &FileAccess::store_string);
  688. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &FileAccess::store_var, DEFVAL(false));
  689. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &FileAccess::store_pascal_string);
  690. ClassDB::bind_method(D_METHOD("get_pascal_string"), &FileAccess::get_pascal_string);
  691. ClassDB::bind_method(D_METHOD("close"), &FileAccess::close);
  692. ClassDB::bind_static_method("FileAccess", D_METHOD("file_exists", "path"), &FileAccess::exists);
  693. ClassDB::bind_static_method("FileAccess", D_METHOD("get_modified_time", "file"), &FileAccess::get_modified_time);
  694. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian");
  695. BIND_ENUM_CONSTANT(READ);
  696. BIND_ENUM_CONSTANT(WRITE);
  697. BIND_ENUM_CONSTANT(READ_WRITE);
  698. BIND_ENUM_CONSTANT(WRITE_READ);
  699. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  700. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  701. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  702. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  703. BIND_ENUM_CONSTANT(COMPRESSION_BROTLI);
  704. }