| 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 | |
| 31 | #include "file_access.h" |
| 32 | #include "file_access.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/crypto/crypto_core.h" |
| 36 | #include "core/io/file_access_compressed.h" |
| 37 | #include "core/io/file_access_encrypted.h" |
| 38 | #include "core/io/file_access_pack.h" |
| 39 | #include "core/io/marshalls.h" |
| 40 | #include "core/io/resource_uid.h" |
| 41 | #include "core/os/os.h" |
| 42 | #include "core/os/time.h" |
| 43 | |
| 44 | Ref<FileAccess> FileAccess::create(AccessType p_access) { |
| 45 | ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr); |
| 46 | ERR_FAIL_NULL_V(create_func[p_access], nullptr); |
| 47 | |
| 48 | Ref<FileAccess> ret = create_func[p_access](); |
| 49 | ret->_set_access_type(p_access); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | bool FileAccess::exists(const String &p_name) { |
| 54 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_name)) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | // Using file_exists because it's faster than trying to open the file. |
| 59 | Ref<FileAccess> ret = create_for_path(p_name); |
| 60 | return ret->file_exists(p_name); |
| 61 | } |
| 62 | |
| 63 | void FileAccess::_set_access_type(AccessType p_access) { |
| 64 | _access_type = p_access; |
| 65 | } |
| 66 | |
| 67 | Ref<FileAccess> FileAccess::create_for_path(const String &p_path) { |
| 68 | Ref<FileAccess> ret; |
| 69 | if (p_path.begins_with("res://") || p_path.begins_with("uid://")) { |
| 70 | ret = create(ACCESS_RESOURCES); |
| 71 | } else if (p_path.begins_with("user://")) { |
| 72 | ret = create(ACCESS_USERDATA); |
| 73 | } else if (p_path.begins_with("pipe://")) { |
| 74 | ret = create(ACCESS_PIPE); |
| 75 | } else { |
| 76 | ret = create(ACCESS_FILESYSTEM); |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | Ref<FileAccess> FileAccess::create_temp(ModeFlags p_mode_flags, const String &p_prefix, const String &p_extension, bool p_keep, Error *r_error) { |
| 83 | const String ERROR_COMMON_PREFIX = "Error while creating temporary file"; |
| 84 | |
| 85 | if (!p_prefix.is_empty() && !p_prefix.is_valid_filename()) { |
| 86 | *r_error = ERR_FILE_BAD_PATH; |
| 87 | ERR_FAIL_V_MSG(Ref<FileAccess>(), vformat(R"(%s: "%s" is not a valid prefix.)", ERROR_COMMON_PREFIX, p_prefix)); |
| 88 | } |
| 89 | |
| 90 | if (!p_extension.is_empty() && !p_extension.is_valid_filename()) { |
| 91 | *r_error = ERR_FILE_BAD_PATH; |
| 92 | ERR_FAIL_V_MSG(Ref<FileAccess>(), vformat(R"(%s: "%s" is not a valid extension.)", ERROR_COMMON_PREFIX, p_extension)); |
| 93 | } |
| 94 | |
| 95 | const String TEMP_DIR = OS::get_singleton()->get_temp_path(); |
| 96 | String extension = p_extension.trim_prefix("."); |
| 97 | |
| 98 | uint32_t suffix_i = 0; |
| 99 | String path; |
| 100 | while (true) { |
| 101 | String datetime = Time::get_singleton()->get_datetime_string_from_system().remove_chars("-T:"); |
| 102 | datetime += itos(Time::get_singleton()->get_ticks_usec()); |
| 103 | String suffix = datetime + (suffix_i > 0 ? itos(suffix_i) : ""); |
| 104 | path = TEMP_DIR.path_join((p_prefix.is_empty() ? "" : p_prefix + "-") + suffix + (extension.is_empty() ? "" : "." + extension)); |
| 105 | if (!DirAccess::exists(path)) { |
| 106 | break; |
| 107 | } |
| 108 | suffix_i += 1; |
| 109 | } |
| 110 | |
| 111 | Error err; |
| 112 | { |
| 113 | // Create file first with WRITE mode. |
| 114 | // Otherwise, it would fail to open with a READ mode. |
| 115 | Ref<FileAccess> ret = FileAccess::open(path, FileAccess::ModeFlags::WRITE, &err); |
| 116 | if (err != OK) { |
| 117 | *r_error = err; |
| 118 | ERR_FAIL_V_MSG(Ref<FileAccess>(), vformat(R"(%s: could not create "%s".)", ERROR_COMMON_PREFIX, path)); |
| 119 | } |
| 120 | ret->flush(); |
| 121 | } |
| 122 | |
| 123 | // Open then the temp file with the correct mode flag. |
| 124 | Ref<FileAccess> ret = FileAccess::open(path, p_mode_flags, &err); |
| 125 | if (err != OK) { |
| 126 | *r_error = err; |
| 127 | ERR_FAIL_V_MSG(Ref<FileAccess>(), vformat(R"(%s: could not open "%s".)", ERROR_COMMON_PREFIX, path)); |
| 128 | } |
| 129 | if (ret.is_valid()) { |
| 130 | ret->_is_temp_file = true; |
| 131 | ret->_temp_keep_after_use = p_keep; |
| 132 | ret->_temp_path = ret->get_path_absolute(); |
| 133 | } |
| 134 | |
| 135 | *r_error = OK; |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | Ref<FileAccess> FileAccess::_create_temp(ModeFlags p_mode_flags, const String &p_prefix, const String &p_extension, bool p_keep) { |
| 140 | return create_temp(p_mode_flags, p_prefix, p_extension, p_keep, &last_file_open_error); |
| 141 | } |
| 142 | |
| 143 | void FileAccess::_delete_temp() { |
| 144 | if (!_is_temp_file || _temp_keep_after_use) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (!FileAccess::exists(_temp_path)) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | DirAccess::remove_absolute(_temp_path); |
| 153 | } |
| 154 | |
| 155 | Error FileAccess::reopen(const String &p_path, int p_mode_flags) { |
| 156 | return open_internal(p_path, p_mode_flags); |
| 157 | } |
| 158 | |
| 159 | Ref<FileAccess> FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) { |
| 160 | //try packed data first |
| 161 | |
| 162 | Ref<FileAccess> ret; |
| 163 | if (!(p_mode_flags & WRITE) && !(p_mode_flags & SKIP_PACK) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) { |
| 164 | ret = PackedData::get_singleton()->try_open_path(p_path); |
| 165 | if (ret.is_valid()) { |
| 166 | if (r_error) { |
| 167 | *r_error = OK; |
| 168 | } |
| 169 | return ret; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | ret = create_for_path(p_path); |
| 174 | Error err = ret->open_internal(p_path, p_mode_flags & ~SKIP_PACK); |
| 175 | |
| 176 | if (r_error) { |
| 177 | *r_error = err; |
| 178 | } |
| 179 | if (err != OK) { |
| 180 | ret.unref(); |
| 181 | } |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | Ref<FileAccess> FileAccess::_open(const String &p_path, ModeFlags p_mode_flags) { |
| 187 | Error err = OK; |
| 188 | Ref<FileAccess> fa = open(p_path, p_mode_flags, &err); |
| 189 | last_file_open_error = err; |
| 190 | if (err) { |
| 191 | return Ref<FileAccess>(); |
| 192 | } |
| 193 | return fa; |
| 194 | } |
| 195 | |
| 196 | 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) { |
| 197 | Ref<FileAccess> fa = _open(p_path, p_mode_flags); |
| 198 | if (fa.is_null()) { |
| 199 | return fa; |
| 200 | } |
| 201 | |
| 202 | Ref<FileAccessEncrypted> fae; |
| 203 | fae.instantiate(); |
| 204 | Error err = fae->open_and_parse(fa, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ, true, p_iv); |
| 205 | last_file_open_error = err; |
| 206 | if (err) { |
| 207 | return Ref<FileAccess>(); |
| 208 | } |
| 209 | return fae; |
| 210 | } |
| 211 | |
| 212 | Ref<FileAccess> FileAccess::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) { |
| 213 | Ref<FileAccess> fa = _open(p_path, p_mode_flags); |
| 214 | if (fa.is_null()) { |
| 215 | return fa; |
| 216 | } |
| 217 | |
| 218 | Ref<FileAccessEncrypted> fae; |
| 219 | fae.instantiate(); |
| 220 | Error err = fae->open_and_parse_password(fa, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ); |
| 221 | last_file_open_error = err; |
| 222 | if (err) { |
| 223 | return Ref<FileAccess>(); |
| 224 | } |
| 225 | return fae; |
| 226 | } |
| 227 | |
| 228 | Ref<FileAccess> FileAccess::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) { |
| 229 | Ref<FileAccessCompressed> fac; |
| 230 | fac.instantiate(); |
| 231 | fac->configure("GCPF", (Compression::Mode)p_compress_mode); |
| 232 | Error err = fac->open_internal(p_path, p_mode_flags); |
| 233 | last_file_open_error = err; |
| 234 | if (err) { |
| 235 | return Ref<FileAccess>(); |
| 236 | } |
| 237 | |
| 238 | return fac; |
| 239 | } |
| 240 | |
| 241 | Error FileAccess::get_open_error() { |
| 242 | return last_file_open_error; |
| 243 | } |
| 244 | |
| 245 | FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) { |
| 246 | return create_func[p_access]; |
| 247 | } |
| 248 | |
| 249 | FileAccess::AccessType FileAccess::get_access_type() const { |
| 250 | return _access_type; |
| 251 | } |
| 252 | |
| 253 | String FileAccess::fix_path(const String &p_path) const { |
| 254 | // Helper used by file accesses that use a single filesystem. |
| 255 | |
| 256 | String r_path = p_path.replace_char('\\', '/'); |
| 257 | |
| 258 | switch (_access_type) { |
| 259 | case ACCESS_RESOURCES: { |
| 260 | if (ProjectSettings::get_singleton()) { |
| 261 | if (r_path.begins_with("uid://")) { |
| 262 | ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(r_path); |
| 263 | if (ResourceUID::get_singleton()->has_id(uid)) { |
| 264 | r_path = ResourceUID::get_singleton()->get_id_path(uid); |
| 265 | } else { |
| 266 | r_path.clear(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (r_path.begins_with("res://")) { |
| 271 | String resource_path = ProjectSettings::get_singleton()->get_resource_path(); |
| 272 | if (!resource_path.is_empty()) { |
| 273 | return r_path.replace("res:/", resource_path); |
| 274 | } |
| 275 | return r_path.replace("res://", ""); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | } break; |
| 280 | case ACCESS_USERDATA: { |
| 281 | if (r_path.begins_with("user://")) { |
| 282 | String data_dir = OS::get_singleton()->get_user_data_dir(); |
| 283 | if (!data_dir.is_empty()) { |
| 284 | return r_path.replace("user:/", data_dir); |
| 285 | } |
| 286 | return r_path.replace("user://", ""); |
| 287 | } |
| 288 | |
| 289 | } break; |
| 290 | case ACCESS_PIPE: { |
| 291 | return r_path; |
| 292 | } break; |
| 293 | case ACCESS_FILESYSTEM: { |
| 294 | return r_path; |
| 295 | } break; |
| 296 | case ACCESS_MAX: |
| 297 | break; // Can't happen, but silences warning |
| 298 | } |
| 299 | |
| 300 | return r_path; |
| 301 | } |
| 302 | |
| 303 | /* these are all implemented for ease of porting, then can later be optimized */ |
| 304 | uint8_t FileAccess::get_8() const { |
| 305 | uint8_t data = 0; |
| 306 | get_buffer(&data, sizeof(uint8_t)); |
| 307 | |
| 308 | return data; |
| 309 | } |
| 310 | |
| 311 | uint16_t FileAccess::get_16() const { |
| 312 | uint16_t data = 0; |
| 313 | get_buffer(reinterpret_cast<uint8_t *>(&data), sizeof(uint16_t)); |
| 314 | |
| 315 | #ifdef BIG_ENDIAN_ENABLED |
| 316 | if (!big_endian) { |
| 317 | data = BSWAP16(data); |
| 318 | } |
| 319 | #else |
| 320 | if (big_endian) { |
| 321 | data = BSWAP16(data); |
| 322 | } |
| 323 | #endif |
| 324 | |
| 325 | return data; |
| 326 | } |
| 327 | |
| 328 | uint32_t FileAccess::get_32() const { |
| 329 | uint32_t data = 0; |
| 330 | get_buffer(reinterpret_cast<uint8_t *>(&data), sizeof(uint32_t)); |
| 331 | |
| 332 | #ifdef BIG_ENDIAN_ENABLED |
| 333 | if (!big_endian) { |
| 334 | data = BSWAP32(data); |
| 335 | } |
| 336 | #else |
| 337 | if (big_endian) { |
| 338 | data = BSWAP32(data); |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | return data; |
| 343 | } |
| 344 | |
| 345 | uint64_t FileAccess::get_64() const { |
| 346 | uint64_t data = 0; |
| 347 | get_buffer(reinterpret_cast<uint8_t *>(&data), sizeof(uint64_t)); |
| 348 | |
| 349 | #ifdef BIG_ENDIAN_ENABLED |
| 350 | if (!big_endian) { |
| 351 | data = BSWAP64(data); |
| 352 | } |
| 353 | #else |
| 354 | if (big_endian) { |
| 355 | data = BSWAP64(data); |
| 356 | } |
| 357 | #endif |
| 358 | |
| 359 | return data; |
| 360 | } |
| 361 | |
| 362 | float FileAccess::get_half() const { |
| 363 | return Math::half_to_float(get_16()); |
| 364 | } |
| 365 | |
| 366 | float FileAccess::get_float() const { |
| 367 | MarshallFloat m; |
| 368 | m.i = get_32(); |
| 369 | return m.f; |
| 370 | } |
| 371 | |
| 372 | real_t FileAccess::get_real() const { |
| 373 | if (real_is_double) { |
| 374 | return get_double(); |
| 375 | } else { |
| 376 | return get_float(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | Variant FileAccess::get_var(bool p_allow_objects) const { |
| 381 | uint32_t len = get_32(); |
| 382 | Vector<uint8_t> buff = get_buffer(len); |
| 383 | ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant()); |
| 384 | |
| 385 | const uint8_t *r = buff.ptr(); |
| 386 | |
| 387 | Variant v; |
| 388 | Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects); |
| 389 | ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant."); |
| 390 | |
| 391 | return v; |
| 392 | } |
| 393 | |
| 394 | double FileAccess::get_double() const { |
| 395 | MarshallDouble m; |
| 396 | m.l = get_64(); |
| 397 | return m.d; |
| 398 | } |
| 399 | |
| 400 | String FileAccess::get_token() const { |
| 401 | CharString token; |
| 402 | |
| 403 | uint8_t c = get_8(); |
| 404 | |
| 405 | while (!eof_reached()) { |
| 406 | if (c <= ' ') { |
| 407 | if (token.length()) { |
| 408 | break; |
| 409 | } |
| 410 | } else { |
| 411 | token += char(c); |
| 412 | } |
| 413 | c = get_8(); |
| 414 | } |
| 415 | |
| 416 | return String::utf8(token.get_data()); |
| 417 | } |
| 418 | |
| 419 | class CharBuffer { |
| 420 | Vector<char> vector; |
| 421 | char stack_buffer[256]; |
| 422 | |
| 423 | char *buffer = nullptr; |
| 424 | int64_t capacity = 0; |
| 425 | int64_t written = 0; |
| 426 | |
| 427 | bool grow() { |
| 428 | if (vector.resize(next_power_of_2((uint64_t)1 + (uint64_t)written)) != OK) { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | if (buffer == stack_buffer) { // first chunk? |
| 433 | |
| 434 | for (int64_t i = 0; i < written; i++) { |
| 435 | vector.write[i] = stack_buffer[i]; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | buffer = vector.ptrw(); |
| 440 | capacity = vector.size(); |
| 441 | ERR_FAIL_COND_V(written >= capacity, false); |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | public: |
| 447 | _FORCE_INLINE_ CharBuffer() : |
| 448 | buffer(stack_buffer), |
| 449 | capacity(std_size(stack_buffer)) { |
| 450 | } |
| 451 | |
| 452 | _FORCE_INLINE_ void push_back(char c) { |
| 453 | if (written >= capacity) { |
| 454 | ERR_FAIL_COND(!grow()); |
| 455 | } |
| 456 | |
| 457 | buffer[written++] = c; |
| 458 | } |
| 459 | |
| 460 | _FORCE_INLINE_ const char *get_data() const { |
| 461 | return buffer; |
| 462 | } |
| 463 | }; |
| 464 | |
| 465 | String FileAccess::get_line() const { |
| 466 | CharBuffer line; |
| 467 | |
| 468 | uint8_t c = get_8(); |
| 469 | |
| 470 | while (!eof_reached()) { |
| 471 | if (c == '\r' || c == '\n' || c == '\0' || get_error() != OK) { |
| 472 | if (c == '\r') { |
| 473 | // Check for Windows-style EOL. |
| 474 | const uint64_t prev_pos = get_position() - 1; |
| 475 | if (unlikely(get_8() != '\n')) { |
| 476 | // HACK: We can't simply check the next value in a vacuum, so we risk triggering |
| 477 | // an EOL false-positive in the unlikely event that this `\r` was the final |
| 478 | // value of the file. Unilaterally work around by re-reading the *previous* |
| 479 | // byte (the starting `\r`) to ensure `get_error()` returns `OK`. |
| 480 | const_cast<FileAccess *>(this)->seek(prev_pos); |
| 481 | get_8(); |
| 482 | } |
| 483 | } |
| 484 | line.push_back(0); |
| 485 | return String::utf8(line.get_data()); |
| 486 | } else { |
| 487 | line.push_back(char(c)); |
| 488 | } |
| 489 | |
| 490 | c = get_8(); |
| 491 | } |
| 492 | line.push_back(0); |
| 493 | return String::utf8(line.get_data()); |
| 494 | } |
| 495 | |
| 496 | Vector<String> FileAccess::get_csv_line(const String &p_delim) const { |
| 497 | ERR_FAIL_COND_V_MSG(p_delim.length() != 1, Vector<String>(), "Only single character delimiters are supported to parse CSV lines."); |
| 498 | ERR_FAIL_COND_V_MSG(p_delim[0] == '"', Vector<String>(), "The double quotation mark character (\") is not supported as a delimiter for CSV lines."); |
| 499 | |
| 500 | String line; |
| 501 | |
| 502 | // CSV can support entries with line breaks as long as they are enclosed |
| 503 | // in double quotes. So our "line" might be more than a single line in the |
| 504 | // text file. |
| 505 | int qc = 0; |
| 506 | do { |
| 507 | if (eof_reached()) { |
| 508 | break; |
| 509 | } |
| 510 | line += get_line() + "\n"; |
| 511 | qc = 0; |
| 512 | for (int i = 0; i < line.length(); i++) { |
| 513 | if (line[i] == '"') { |
| 514 | qc++; |
| 515 | } |
| 516 | } |
| 517 | } while (qc % 2); |
| 518 | |
| 519 | // Remove the extraneous newline we've added above. |
| 520 | line = line.substr(0, line.length() - 1); |
| 521 | |
| 522 | Vector<String> strings; |
| 523 | |
| 524 | bool in_quote = false; |
| 525 | String current; |
| 526 | for (int i = 0; i < line.length(); i++) { |
| 527 | char32_t c = line[i]; |
| 528 | // A delimiter ends the current entry, unless it's in a quoted string. |
| 529 | if (!in_quote && c == p_delim[0]) { |
| 530 | strings.push_back(current); |
| 531 | current = String(); |
| 532 | } else if (c == '"') { |
| 533 | // Doubled quotes are escapes for intentional quotes in the string. |
| 534 | if (line[i + 1] == '"' && in_quote) { |
| 535 | current += '"'; |
| 536 | i++; |
| 537 | } else { |
| 538 | in_quote = !in_quote; |
| 539 | } |
| 540 | } else { |
| 541 | current += c; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if (in_quote) { |
| 546 | WARN_PRINT(vformat("Reached end of file before closing '\"' in CSV file '%s'.", get_path())); |
| 547 | } |
| 548 | |
| 549 | strings.push_back(current); |
| 550 | |
| 551 | return strings; |
| 552 | } |
| 553 | |
| 554 | String FileAccess::get_as_text() const { |
| 555 | uint64_t original_pos = get_position(); |
| 556 | const_cast<FileAccess *>(this)->seek(0); |
| 557 | |
| 558 | String text = get_as_utf8_string(); |
| 559 | |
| 560 | const_cast<FileAccess *>(this)->seek(original_pos); |
| 561 | |
| 562 | return text; |
| 563 | } |
| 564 | |
| 565 | Vector<uint8_t> FileAccess::get_buffer(int64_t p_length) const { |
| 566 | Vector<uint8_t> data; |
| 567 | |
| 568 | ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0."); |
| 569 | if (p_length == 0) { |
| 570 | return data; |
| 571 | } |
| 572 | |
| 573 | data.reserve_exact(p_length); |
| 574 | Error err = data.resize(p_length); |
| 575 | ERR_FAIL_COND_V_MSG(err != OK, data, vformat("Can't resize data to %d elements.", p_length)); |
| 576 | |
| 577 | uint8_t *w = data.ptrw(); |
| 578 | int64_t len = get_buffer(w, p_length); |
| 579 | |
| 580 | if (len < p_length) { |
| 581 | data.resize(len); |
| 582 | } |
| 583 | |
| 584 | return data; |
| 585 | } |
| 586 | |
| 587 | String FileAccess::get_as_utf8_string() const { |
| 588 | Vector<uint8_t> sourcef; |
| 589 | uint64_t len = get_length(); |
| 590 | sourcef.resize(len + 1); |
| 591 | |
| 592 | uint8_t *w = sourcef.ptrw(); |
| 593 | uint64_t r = get_buffer(w, len); |
| 594 | ERR_FAIL_COND_V(r != len, String()); |
| 595 | w[len] = 0; |
| 596 | |
| 597 | String s; |
| 598 | s.append_utf8((const char *)w, len); |
| 599 | return s; |
| 600 | } |
| 601 | |
| 602 | bool FileAccess::store_8(uint8_t p_dest) { |
| 603 | return store_buffer(&p_dest, sizeof(uint8_t)); |
| 604 | } |
| 605 | |
| 606 | bool FileAccess::store_16(uint16_t p_dest) { |
| 607 | #ifdef BIG_ENDIAN_ENABLED |
| 608 | if (!big_endian) { |
| 609 | p_dest = BSWAP16(p_dest); |
| 610 | } |
| 611 | #else |
| 612 | if (big_endian) { |
| 613 | p_dest = BSWAP16(p_dest); |
| 614 | } |
| 615 | #endif |
| 616 | |
| 617 | return store_buffer(reinterpret_cast<uint8_t *>(&p_dest), sizeof(uint16_t)); |
| 618 | } |
| 619 | |
| 620 | bool FileAccess::store_32(uint32_t p_dest) { |
| 621 | #ifdef BIG_ENDIAN_ENABLED |
| 622 | if (!big_endian) { |
| 623 | p_dest = BSWAP32(p_dest); |
| 624 | } |
| 625 | #else |
| 626 | if (big_endian) { |
| 627 | p_dest = BSWAP32(p_dest); |
| 628 | } |
| 629 | #endif |
| 630 | |
| 631 | return store_buffer(reinterpret_cast<uint8_t *>(&p_dest), sizeof(uint32_t)); |
| 632 | } |
| 633 | |
| 634 | bool FileAccess::store_64(uint64_t p_dest) { |
| 635 | #ifdef BIG_ENDIAN_ENABLED |
| 636 | if (!big_endian) { |
| 637 | p_dest = BSWAP64(p_dest); |
| 638 | } |
| 639 | #else |
| 640 | if (big_endian) { |
| 641 | p_dest = BSWAP64(p_dest); |
| 642 | } |
| 643 | #endif |
| 644 | |
| 645 | return store_buffer(reinterpret_cast<uint8_t *>(&p_dest), sizeof(uint64_t)); |
| 646 | } |
| 647 | |
| 648 | bool FileAccess::store_real(real_t p_real) { |
| 649 | if constexpr (sizeof(real_t) == 4) { |
| 650 | return store_float(p_real); |
| 651 | } else { |
| 652 | return store_double(p_real); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | bool FileAccess::store_half(float p_dest) { |
| 657 | return store_16(Math::make_half_float(p_dest)); |
| 658 | } |
| 659 | |
| 660 | bool FileAccess::store_float(float p_dest) { |
| 661 | MarshallFloat m; |
| 662 | m.f = p_dest; |
| 663 | return store_32(m.i); |
| 664 | } |
| 665 | |
| 666 | bool FileAccess::store_double(double p_dest) { |
| 667 | MarshallDouble m; |
| 668 | m.d = p_dest; |
| 669 | return store_64(m.l); |
| 670 | } |
| 671 | |
| 672 | uint64_t FileAccess::get_modified_time(const String &p_file) { |
| 673 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | Ref<FileAccess> fa = create_for_path(p_file); |
| 678 | ERR_FAIL_COND_V_MSG(fa.is_null(), 0, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 679 | |
| 680 | return fa->_get_modified_time(p_file); |
| 681 | } |
| 682 | |
| 683 | uint64_t FileAccess::get_access_time(const String &p_file) { |
| 684 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | Ref<FileAccess> fa = create_for_path(p_file); |
| 689 | ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "Cannot create FileAccess for path '" + p_file + "'."); |
| 690 | |
| 691 | return fa->_get_access_time(p_file); |
| 692 | } |
| 693 | |
| 694 | int64_t FileAccess::get_size(const String &p_file) { |
| 695 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 696 | return PackedData::get_singleton()->get_size(p_file); |
| 697 | } |
| 698 | |
| 699 | Ref<FileAccess> fa = create_for_path(p_file); |
| 700 | ERR_FAIL_COND_V_MSG(fa.is_null(), -1, "Cannot create FileAccess for path '" + p_file + "'."); |
| 701 | |
| 702 | return fa->_get_size(p_file); |
| 703 | } |
| 704 | |
| 705 | BitField<FileAccess::UnixPermissionFlags> FileAccess::get_unix_permissions(const String &p_file) { |
| 706 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | Ref<FileAccess> fa = create_for_path(p_file); |
| 711 | ERR_FAIL_COND_V_MSG(fa.is_null(), 0, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 712 | |
| 713 | return fa->_get_unix_permissions(p_file); |
| 714 | } |
| 715 | |
| 716 | Error FileAccess::set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) { |
| 717 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 718 | return ERR_UNAVAILABLE; |
| 719 | } |
| 720 | |
| 721 | Ref<FileAccess> fa = create_for_path(p_file); |
| 722 | ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 723 | |
| 724 | Error err = fa->_set_unix_permissions(p_file, p_permissions); |
| 725 | return err; |
| 726 | } |
| 727 | |
| 728 | bool FileAccess::get_hidden_attribute(const String &p_file) { |
| 729 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | Ref<FileAccess> fa = create_for_path(p_file); |
| 734 | ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 735 | |
| 736 | return fa->_get_hidden_attribute(p_file); |
| 737 | } |
| 738 | |
| 739 | Error FileAccess::set_hidden_attribute(const String &p_file, bool p_hidden) { |
| 740 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 741 | return ERR_UNAVAILABLE; |
| 742 | } |
| 743 | |
| 744 | Ref<FileAccess> fa = create_for_path(p_file); |
| 745 | ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 746 | |
| 747 | Error err = fa->_set_hidden_attribute(p_file, p_hidden); |
| 748 | return err; |
| 749 | } |
| 750 | |
| 751 | bool FileAccess::get_read_only_attribute(const String &p_file) { |
| 752 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | Ref<FileAccess> fa = create_for_path(p_file); |
| 757 | ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 758 | |
| 759 | return fa->_get_read_only_attribute(p_file); |
| 760 | } |
| 761 | |
| 762 | Error FileAccess::set_read_only_attribute(const String &p_file, bool p_ro) { |
| 763 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 764 | return ERR_UNAVAILABLE; |
| 765 | } |
| 766 | |
| 767 | Ref<FileAccess> fa = create_for_path(p_file); |
| 768 | ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 769 | |
| 770 | Error err = fa->_set_read_only_attribute(p_file, p_ro); |
| 771 | return err; |
| 772 | } |
| 773 | |
| 774 | PackedByteArray FileAccess::get_extended_attribute(const String &p_file, const String &p_attribute_name) { |
| 775 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 776 | return PackedByteArray(); |
| 777 | } |
| 778 | |
| 779 | Ref<FileAccess> fa = create_for_path(p_file); |
| 780 | ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 781 | |
| 782 | return fa->_get_extended_attribute(p_file, p_attribute_name); |
| 783 | } |
| 784 | |
| 785 | String FileAccess::get_extended_attribute_string(const String &p_file, const String &p_attribute_name) { |
| 786 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 787 | return String(); |
| 788 | } |
| 789 | |
| 790 | Ref<FileAccess> fa = create_for_path(p_file); |
| 791 | ERR_FAIL_COND_V_MSG(fa.is_null(), String(), vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 792 | |
| 793 | PackedByteArray data = fa->_get_extended_attribute(p_file, p_attribute_name); |
| 794 | if (data.is_empty()) { |
| 795 | return String(); |
| 796 | } |
| 797 | return String::utf8((const char *)data.ptr(), data.size()); |
| 798 | } |
| 799 | |
| 800 | Error FileAccess::set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data) { |
| 801 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 802 | return ERR_UNAVAILABLE; |
| 803 | } |
| 804 | |
| 805 | Ref<FileAccess> fa = create_for_path(p_file); |
| 806 | ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 807 | |
| 808 | return fa->_set_extended_attribute(p_file, p_attribute_name, p_data); |
| 809 | } |
| 810 | |
| 811 | Error FileAccess::set_extended_attribute_string(const String &p_file, const String &p_attribute_name, const String &p_data) { |
| 812 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 813 | return ERR_UNAVAILABLE; |
| 814 | } |
| 815 | |
| 816 | Ref<FileAccess> fa = create_for_path(p_file); |
| 817 | ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 818 | |
| 819 | PackedByteArray data; |
| 820 | CharString cs = p_data.utf8(); |
| 821 | data.resize(cs.size()); |
| 822 | if (cs.size() > 0) { |
| 823 | memcpy(data.ptrw(), cs.get_data(), cs.size()); |
| 824 | } |
| 825 | |
| 826 | return fa->_set_extended_attribute(p_file, p_attribute_name, data); |
| 827 | } |
| 828 | |
| 829 | Error FileAccess::remove_extended_attribute(const String &p_file, const String &p_attribute_name) { |
| 830 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 831 | return ERR_UNAVAILABLE; |
| 832 | } |
| 833 | |
| 834 | Ref<FileAccess> fa = create_for_path(p_file); |
| 835 | ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 836 | |
| 837 | return fa->_remove_extended_attribute(p_file, p_attribute_name); |
| 838 | } |
| 839 | |
| 840 | PackedStringArray FileAccess::get_extended_attributes_list(const String &p_file) { |
| 841 | if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { |
| 842 | return PackedStringArray(); |
| 843 | } |
| 844 | |
| 845 | Ref<FileAccess> fa = create_for_path(p_file); |
| 846 | ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), vformat("Cannot create FileAccess for path '%s'.", p_file)); |
| 847 | |
| 848 | return fa->_get_extended_attributes_list(p_file); |
| 849 | } |
| 850 | |
| 851 | bool FileAccess::store_string(const String &p_string) { |
| 852 | if (p_string.length() == 0) { |
| 853 | return true; |
| 854 | } |
| 855 | |
| 856 | CharString cs = p_string.utf8(); |
| 857 | return store_buffer((uint8_t *)&cs[0], cs.length()); |
| 858 | } |
| 859 | |
| 860 | bool FileAccess::store_pascal_string(const String &p_string) { |
| 861 | CharString cs = p_string.utf8(); |
| 862 | return store_32(cs.length()) && store_buffer((uint8_t *)&cs[0], cs.length()); |
| 863 | } |
| 864 | |
| 865 | String FileAccess::get_pascal_string() { |
| 866 | uint32_t sl = get_32(); |
| 867 | CharString cs; |
| 868 | cs.resize_uninitialized(sl + 1); |
| 869 | get_buffer((uint8_t *)cs.ptr(), sl); |
| 870 | cs[sl] = 0; |
| 871 | |
| 872 | return String::utf8(cs.ptr(), sl); |
| 873 | } |
| 874 | |
| 875 | bool FileAccess::store_line(const String &p_line) { |
| 876 | return store_string(p_line) && store_8('\n'); |
| 877 | } |
| 878 | |
| 879 | bool FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) { |
| 880 | ERR_FAIL_COND_V(p_delim.length() != 1, false); |
| 881 | |
| 882 | String line = ""; |
| 883 | int size = p_values.size(); |
| 884 | for (int i = 0; i < size; ++i) { |
| 885 | String value = p_values[i]; |
| 886 | |
| 887 | if (value.contains_char('"') || value.contains(p_delim) || value.contains_char('\n')) { |
| 888 | value = "\"" + value.replace("\"", "\"\"") + "\""; |
| 889 | } |
| 890 | if (i < size - 1) { |
| 891 | value += p_delim; |
| 892 | } |
| 893 | |
| 894 | line += value; |
| 895 | } |
| 896 | |
| 897 | return store_line(line); |
| 898 | } |
| 899 | |
| 900 | bool FileAccess::store_buffer(const Vector<uint8_t> &p_buffer) { |
| 901 | uint64_t len = p_buffer.size(); |
| 902 | if (len == 0) { |
| 903 | return true; |
| 904 | } |
| 905 | |
| 906 | const uint8_t *r = p_buffer.ptr(); |
| 907 | |
| 908 | return store_buffer(r, len); |
| 909 | } |
| 910 | |
| 911 | bool FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) { |
| 912 | ERR_FAIL_COND_V(!p_src && p_length > 0, false); |
| 913 | for (uint64_t i = 0; i < p_length; i++) { |
| 914 | if (unlikely(!store_8(p_src[i]))) { |
| 915 | return false; |
| 916 | } |
| 917 | } |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | bool FileAccess::store_var(const Variant &p_var, bool p_full_objects) { |
| 922 | int len; |
| 923 | Error err = encode_variant(p_var, nullptr, len, p_full_objects); |
| 924 | ERR_FAIL_COND_V_MSG(err != OK, false, "Error when trying to encode Variant."); |
| 925 | |
| 926 | Vector<uint8_t> buff; |
| 927 | buff.resize(len); |
| 928 | |
| 929 | uint8_t *w = buff.ptrw(); |
| 930 | err = encode_variant(p_var, &w[0], len, p_full_objects); |
| 931 | ERR_FAIL_COND_V_MSG(err != OK, false, "Error when trying to encode Variant."); |
| 932 | |
| 933 | return store_32(uint32_t(len)) && store_buffer(buff); |
| 934 | } |
| 935 | |
| 936 | Vector<uint8_t> FileAccess::get_file_as_bytes(const String &p_path, Error *r_error) { |
| 937 | Ref<FileAccess> f = FileAccess::open(p_path, READ, r_error); |
| 938 | if (f.is_null()) { |
| 939 | if (r_error) { // if error requested, do not throw error |
| 940 | return Vector<uint8_t>(); |
| 941 | } |
| 942 | ERR_FAIL_V_MSG(Vector<uint8_t>(), vformat("Can't open file from path '%s'.", String(p_path))); |
| 943 | } |
| 944 | Vector<uint8_t> data; |
| 945 | data.reserve_exact(f->get_length()); |
| 946 | data.resize(f->get_length()); |
| 947 | f->get_buffer(data.ptrw(), data.size()); |
| 948 | return data; |
| 949 | } |
| 950 | |
| 951 | String FileAccess::get_file_as_string(const String &p_path, Error *r_error) { |
| 952 | Error err; |
| 953 | Vector<uint8_t> array = get_file_as_bytes(p_path, &err); |
| 954 | if (r_error) { |
| 955 | *r_error = err; |
| 956 | } |
| 957 | if (err != OK) { |
| 958 | if (r_error) { |
| 959 | return String(); |
| 960 | } |
| 961 | ERR_FAIL_V_MSG(String(), vformat("Can't get file as string from path '%s'.", String(p_path))); |
| 962 | } |
| 963 | |
| 964 | String ret; |
| 965 | ret.append_utf8((const char *)array.ptr(), array.size()); |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | String FileAccess::get_md5(const String &p_file) { |
| 970 | Ref<FileAccess> f = FileAccess::open(p_file, READ); |
| 971 | if (f.is_null()) { |
| 972 | return String(); |
| 973 | } |
| 974 | |
| 975 | CryptoCore::MD5Context ctx; |
| 976 | ctx.start(); |
| 977 | |
| 978 | unsigned char step[32768]; |
| 979 | |
| 980 | while (true) { |
| 981 | uint64_t br = f->get_buffer(step, 32768); |
| 982 | if (br > 0) { |
| 983 | ctx.update(step, br); |
| 984 | } |
| 985 | if (br < 4096) { |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | unsigned char hash[16]; |
| 991 | ctx.finish(hash); |
| 992 | |
| 993 | return String::md5(hash); |
| 994 | } |
| 995 | |
| 996 | String FileAccess::get_multiple_md5(const Vector<String> &p_file) { |
| 997 | CryptoCore::MD5Context ctx; |
| 998 | ctx.start(); |
| 999 | |
| 1000 | for (int i = 0; i < p_file.size(); i++) { |
| 1001 | Ref<FileAccess> f = FileAccess::open(p_file[i], READ); |
| 1002 | ERR_CONTINUE(f.is_null()); |
| 1003 | |
| 1004 | unsigned char step[32768]; |
| 1005 | |
| 1006 | while (true) { |
| 1007 | uint64_t br = f->get_buffer(step, 32768); |
| 1008 | if (br > 0) { |
| 1009 | ctx.update(step, br); |
| 1010 | } |
| 1011 | if (br < 4096) { |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | unsigned char hash[16]; |
| 1018 | ctx.finish(hash); |
| 1019 | |
| 1020 | return String::md5(hash); |
| 1021 | } |
| 1022 | |
| 1023 | String FileAccess::get_sha256(const String &p_file) { |
| 1024 | Ref<FileAccess> f = FileAccess::open(p_file, READ); |
| 1025 | if (f.is_null()) { |
| 1026 | return String(); |
| 1027 | } |
| 1028 | |
| 1029 | CryptoCore::SHA256Context ctx; |
| 1030 | ctx.start(); |
| 1031 | |
| 1032 | unsigned char step[32768]; |
| 1033 | |
| 1034 | while (true) { |
| 1035 | uint64_t br = f->get_buffer(step, 32768); |
| 1036 | if (br > 0) { |
| 1037 | ctx.update(step, br); |
| 1038 | } |
| 1039 | if (br < 4096) { |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | unsigned char hash[32]; |
| 1045 | ctx.finish(hash); |
| 1046 | |
| 1047 | return String::hex_encode_buffer(hash, 32); |
| 1048 | } |
| 1049 | |
| 1050 | void FileAccess::_bind_methods() { |
| 1051 | ClassDB::bind_static_method("FileAccess", D_METHOD("open", "path", "flags"), &FileAccess::_open); |
| 1052 | ClassDB::bind_static_method("FileAccess", D_METHOD("open_encrypted", "path", "mode_flags", "key", "iv"), &FileAccess::open_encrypted, DEFVAL(Vector<uint8_t>())); |
| 1053 | ClassDB::bind_static_method("FileAccess", D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &FileAccess::open_encrypted_pass); |
| 1054 | ClassDB::bind_static_method("FileAccess", D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &FileAccess::open_compressed, DEFVAL(0)); |
| 1055 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_open_error"), &FileAccess::get_open_error); |
| 1056 | ClassDB::bind_static_method("FileAccess", D_METHOD("create_temp", "mode_flags", "prefix", "extension", "keep"), &FileAccess::_create_temp, DEFVAL(""), DEFVAL(""), DEFVAL(false)); |
| 1057 | |
| 1058 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_file_as_bytes", "path"), &FileAccess::_get_file_as_bytes); |
| 1059 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_file_as_string", "path"), &FileAccess::_get_file_as_string); |
| 1060 | |
| 1061 | ClassDB::bind_method(D_METHOD("resize", "length"), &FileAccess::resize); |
| 1062 | ClassDB::bind_method(D_METHOD("flush"), &FileAccess::flush); |
| 1063 | ClassDB::bind_method(D_METHOD("get_path"), &FileAccess::get_path); |
| 1064 | ClassDB::bind_method(D_METHOD("get_path_absolute"), &FileAccess::get_path_absolute); |
| 1065 | ClassDB::bind_method(D_METHOD("is_open"), &FileAccess::is_open); |
| 1066 | ClassDB::bind_method(D_METHOD("seek", "position"), &FileAccess::seek); |
| 1067 | ClassDB::bind_method(D_METHOD("seek_end", "position"), &FileAccess::seek_end, DEFVAL(0)); |
| 1068 | ClassDB::bind_method(D_METHOD("get_position"), &FileAccess::get_position); |
| 1069 | ClassDB::bind_method(D_METHOD("get_length"), &FileAccess::get_length); |
| 1070 | ClassDB::bind_method(D_METHOD("eof_reached"), &FileAccess::eof_reached); |
| 1071 | ClassDB::bind_method(D_METHOD("get_8"), &FileAccess::get_8); |
| 1072 | ClassDB::bind_method(D_METHOD("get_16"), &FileAccess::get_16); |
| 1073 | ClassDB::bind_method(D_METHOD("get_32"), &FileAccess::get_32); |
| 1074 | ClassDB::bind_method(D_METHOD("get_64"), &FileAccess::get_64); |
| 1075 | ClassDB::bind_method(D_METHOD("get_half"), &FileAccess::get_half); |
| 1076 | ClassDB::bind_method(D_METHOD("get_float"), &FileAccess::get_float); |
| 1077 | ClassDB::bind_method(D_METHOD("get_double"), &FileAccess::get_double); |
| 1078 | ClassDB::bind_method(D_METHOD("get_real"), &FileAccess::get_real); |
| 1079 | ClassDB::bind_method(D_METHOD("get_buffer", "length"), (Vector<uint8_t> (FileAccess::*)(int64_t) const) & FileAccess::get_buffer); |
| 1080 | ClassDB::bind_method(D_METHOD("get_line"), &FileAccess::get_line); |
| 1081 | ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &FileAccess::get_csv_line, DEFVAL(",")); |
| 1082 | ClassDB::bind_method(D_METHOD("get_as_text"), &FileAccess::get_as_text); |
| 1083 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_md5", "path"), &FileAccess::get_md5); |
| 1084 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_sha256", "path"), &FileAccess::get_sha256); |
| 1085 | ClassDB::bind_method(D_METHOD("is_big_endian"), &FileAccess::is_big_endian); |
| 1086 | ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &FileAccess::set_big_endian); |
| 1087 | ClassDB::bind_method(D_METHOD("get_error"), &FileAccess::get_error); |
| 1088 | ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &FileAccess::get_var, DEFVAL(false)); |
| 1089 | |
| 1090 | ClassDB::bind_method(D_METHOD("store_8", "value"), &FileAccess::store_8); |
| 1091 | ClassDB::bind_method(D_METHOD("store_16", "value"), &FileAccess::store_16); |
| 1092 | ClassDB::bind_method(D_METHOD("store_32", "value"), &FileAccess::store_32); |
| 1093 | ClassDB::bind_method(D_METHOD("store_64", "value"), &FileAccess::store_64); |
| 1094 | ClassDB::bind_method(D_METHOD("store_half", "value"), &FileAccess::store_half); |
| 1095 | ClassDB::bind_method(D_METHOD("store_float", "value"), &FileAccess::store_float); |
| 1096 | ClassDB::bind_method(D_METHOD("store_double", "value"), &FileAccess::store_double); |
| 1097 | ClassDB::bind_method(D_METHOD("store_real", "value"), &FileAccess::store_real); |
| 1098 | ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), (bool (FileAccess::*)(const Vector<uint8_t> &))&FileAccess::store_buffer); |
| 1099 | ClassDB::bind_method(D_METHOD("store_line", "line"), &FileAccess::store_line); |
| 1100 | ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &FileAccess::store_csv_line, DEFVAL(",")); |
| 1101 | ClassDB::bind_method(D_METHOD("store_string", "string"), &FileAccess::store_string); |
| 1102 | ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &FileAccess::store_var, DEFVAL(false)); |
| 1103 | |
| 1104 | ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &FileAccess::store_pascal_string); |
| 1105 | ClassDB::bind_method(D_METHOD("get_pascal_string"), &FileAccess::get_pascal_string); |
| 1106 | |
| 1107 | ClassDB::bind_method(D_METHOD("close"), &FileAccess::close); |
| 1108 | |
| 1109 | ClassDB::bind_static_method("FileAccess", D_METHOD("file_exists", "path"), &FileAccess::exists); |
| 1110 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_modified_time", "file"), &FileAccess::get_modified_time); |
| 1111 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_access_time", "file"), &FileAccess::get_access_time); |
| 1112 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_size", "file"), &FileAccess::get_size); |
| 1113 | |
| 1114 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_unix_permissions", "file"), &FileAccess::get_unix_permissions); |
| 1115 | ClassDB::bind_static_method("FileAccess", D_METHOD("set_unix_permissions", "file", "permissions"), &FileAccess::set_unix_permissions); |
| 1116 | |
| 1117 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_hidden_attribute", "file"), &FileAccess::get_hidden_attribute); |
| 1118 | ClassDB::bind_static_method("FileAccess", D_METHOD("set_hidden_attribute", "file", "hidden"), &FileAccess::set_hidden_attribute); |
| 1119 | ClassDB::bind_static_method("FileAccess", D_METHOD("set_read_only_attribute", "file", "ro"), &FileAccess::set_read_only_attribute); |
| 1120 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_read_only_attribute", "file"), &FileAccess::get_read_only_attribute); |
| 1121 | |
| 1122 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_extended_attribute", "file", "attribute_name"), &FileAccess::get_extended_attribute); |
| 1123 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_extended_attribute_string", "file", "attribute_name"), &FileAccess::get_extended_attribute_string); |
| 1124 | ClassDB::bind_static_method("FileAccess", D_METHOD("set_extended_attribute", "file", "attribute_name", "data"), &FileAccess::set_extended_attribute); |
| 1125 | ClassDB::bind_static_method("FileAccess", D_METHOD("set_extended_attribute_string", "file", "attribute_name", "data"), &FileAccess::set_extended_attribute_string); |
| 1126 | ClassDB::bind_static_method("FileAccess", D_METHOD("remove_extended_attribute", "file", "attribute_name"), &FileAccess::remove_extended_attribute); |
| 1127 | ClassDB::bind_static_method("FileAccess", D_METHOD("get_extended_attributes_list", "file"), &FileAccess::get_extended_attributes_list); |
| 1128 | |
| 1129 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian"); |
| 1130 | |
| 1131 | BIND_ENUM_CONSTANT(READ); |
| 1132 | BIND_ENUM_CONSTANT(WRITE); |
| 1133 | BIND_ENUM_CONSTANT(READ_WRITE); |
| 1134 | BIND_ENUM_CONSTANT(WRITE_READ); |
| 1135 | |
| 1136 | BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ); |
| 1137 | BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE); |
| 1138 | BIND_ENUM_CONSTANT(COMPRESSION_ZSTD); |
| 1139 | BIND_ENUM_CONSTANT(COMPRESSION_GZIP); |
| 1140 | BIND_ENUM_CONSTANT(COMPRESSION_BROTLI); |
| 1141 | |
| 1142 | BIND_BITFIELD_FLAG(UNIX_READ_OWNER); |
| 1143 | BIND_BITFIELD_FLAG(UNIX_WRITE_OWNER); |
| 1144 | BIND_BITFIELD_FLAG(UNIX_EXECUTE_OWNER); |
| 1145 | BIND_BITFIELD_FLAG(UNIX_READ_GROUP); |
| 1146 | BIND_BITFIELD_FLAG(UNIX_WRITE_GROUP); |
| 1147 | BIND_BITFIELD_FLAG(UNIX_EXECUTE_GROUP); |
| 1148 | BIND_BITFIELD_FLAG(UNIX_READ_OTHER); |
| 1149 | BIND_BITFIELD_FLAG(UNIX_WRITE_OTHER); |
| 1150 | BIND_BITFIELD_FLAG(UNIX_EXECUTE_OTHER); |
| 1151 | BIND_BITFIELD_FLAG(UNIX_SET_USER_ID); |
| 1152 | BIND_BITFIELD_FLAG(UNIX_SET_GROUP_ID); |
| 1153 | BIND_BITFIELD_FLAG(UNIX_RESTRICTED_DELETE); |
| 1154 | } |
| 1155 | |
| 1156 | FileAccess::~FileAccess() { |
| 1157 | _delete_temp(); |
| 1158 | } |