file_access.cpp 27 KB

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