file_access.cpp 27 KB

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