123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- /*************************************************************************/
- /* file_access.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "file_access.h"
- #include "core/io/file_access_pack.h"
- #include "core/io/marshalls.h"
- #include "core/os/os.h"
- #include "core/project_settings.h"
- #include "thirdparty/misc/md5.h"
- #include "thirdparty/misc/sha256.h"
- FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { 0, 0 };
- FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = NULL;
- bool FileAccess::backup_save = false;
- FileAccess *FileAccess::create(AccessType p_access) {
- ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, 0);
- FileAccess *ret = create_func[p_access]();
- ret->_set_access_type(p_access);
- return ret;
- }
- bool FileAccess::exists(const String &p_name) {
- if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name))
- return true;
- FileAccess *f = open(p_name, READ);
- if (!f)
- return false;
- memdelete(f);
- return true;
- }
- void FileAccess::_set_access_type(AccessType p_access) {
- _access_type = p_access;
- };
- FileAccess *FileAccess::create_for_path(const String &p_path) {
- FileAccess *ret = NULL;
- if (p_path.begins_with("res://")) {
- ret = create(ACCESS_RESOURCES);
- } else if (p_path.begins_with("user://")) {
- ret = create(ACCESS_USERDATA);
- } else {
- ret = create(ACCESS_FILESYSTEM);
- }
- return ret;
- }
- Error FileAccess::reopen(const String &p_path, int p_mode_flags) {
- return _open(p_path, p_mode_flags);
- };
- FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) {
- //try packed data first
- FileAccess *ret = NULL;
- if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
- ret = PackedData::get_singleton()->try_open_path(p_path);
- if (ret) {
- if (r_error)
- *r_error = OK;
- return ret;
- }
- }
- ret = create_for_path(p_path);
- Error err = ret->_open(p_path, p_mode_flags);
- if (r_error)
- *r_error = err;
- if (err != OK) {
- memdelete(ret);
- ret = NULL;
- }
- return ret;
- }
- FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
- return create_func[p_access];
- };
- String FileAccess::fix_path(const String &p_path) const {
- //helper used by file accesses that use a single filesystem
- String r_path = p_path.replace("\\", "/");
- switch (_access_type) {
- case ACCESS_RESOURCES: {
- if (ProjectSettings::get_singleton()) {
- if (r_path.begins_with("res://")) {
- String resource_path = ProjectSettings::get_singleton()->get_resource_path();
- if (resource_path != "") {
- return r_path.replace("res:/", resource_path);
- };
- return r_path.replace("res://", "");
- }
- }
- } break;
- case ACCESS_USERDATA: {
- if (r_path.begins_with("user://")) {
- String data_dir = OS::get_singleton()->get_user_data_dir();
- if (data_dir != "") {
- return r_path.replace("user:/", data_dir);
- };
- return r_path.replace("user://", "");
- }
- } break;
- case ACCESS_FILESYSTEM: {
- return r_path;
- } break;
- case ACCESS_MAX: break; // Can't happen, but silences warning
- }
- return r_path;
- }
- /* these are all implemented for ease of porting, then can later be optimized */
- uint16_t FileAccess::get_16() const {
- uint16_t res;
- uint8_t a, b;
- a = get_8();
- b = get_8();
- if (endian_swap) {
- SWAP(a, b);
- }
- res = b;
- res <<= 8;
- res |= a;
- return res;
- }
- uint32_t FileAccess::get_32() const {
- uint32_t res;
- uint16_t a, b;
- a = get_16();
- b = get_16();
- if (endian_swap) {
- SWAP(a, b);
- }
- res = b;
- res <<= 16;
- res |= a;
- return res;
- }
- uint64_t FileAccess::get_64() const {
- uint64_t res;
- uint32_t a, b;
- a = get_32();
- b = get_32();
- if (endian_swap) {
- SWAP(a, b);
- }
- res = b;
- res <<= 32;
- res |= a;
- return res;
- }
- float FileAccess::get_float() const {
- MarshallFloat m;
- m.i = get_32();
- return m.f;
- };
- real_t FileAccess::get_real() const {
- if (real_is_double)
- return get_double();
- else
- return get_float();
- }
- double FileAccess::get_double() const {
- MarshallDouble m;
- m.l = get_64();
- return m.d;
- };
- String FileAccess::get_token() const {
- CharString token;
- CharType c = get_8();
- while (!eof_reached()) {
- if (c <= ' ') {
- if (token.length())
- break;
- } else {
- token += c;
- }
- c = get_8();
- }
- return String::utf8(token.get_data());
- }
- class CharBuffer {
- Vector<char> vector;
- char stack_buffer[256];
- char *buffer;
- int capacity;
- int written;
- bool grow() {
- if (vector.resize(next_power_of_2(1 + written)) != OK) {
- return false;
- }
- if (buffer == stack_buffer) { // first chunk?
- for (int i = 0; i < written; i++) {
- vector.write[i] = stack_buffer[i];
- }
- }
- buffer = vector.ptrw();
- capacity = vector.size();
- ERR_FAIL_COND_V(written >= capacity, false);
- return true;
- }
- public:
- _FORCE_INLINE_ CharBuffer() :
- buffer(stack_buffer),
- capacity(sizeof(stack_buffer) / sizeof(char)),
- written(0) {
- }
- _FORCE_INLINE_ void push_back(char c) {
- if (written >= capacity) {
- ERR_FAIL_COND(!grow());
- }
- buffer[written++] = c;
- }
- _FORCE_INLINE_ const char *get_data() const {
- return buffer;
- }
- };
- String FileAccess::get_line() const {
- CharBuffer line;
- CharType c = get_8();
- while (!eof_reached()) {
- if (c == '\n' || c == '\0') {
- line.push_back(0);
- return String::utf8(line.get_data());
- } else if (c != '\r')
- line.push_back(c);
- c = get_8();
- }
- line.push_back(0);
- return String::utf8(line.get_data());
- }
- Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
- ERR_FAIL_COND_V(p_delim.length() != 1, Vector<String>());
- String l;
- int qc = 0;
- do {
- if (eof_reached())
- break;
- l += get_line() + "\n";
- qc = 0;
- for (int i = 0; i < l.length(); i++) {
- if (l[i] == '"')
- qc++;
- }
- } while (qc % 2);
- l = l.substr(0, l.length() - 1);
- Vector<String> strings;
- bool in_quote = false;
- String current;
- for (int i = 0; i < l.length(); i++) {
- CharType c = l[i];
- CharType s[2] = { 0, 0 };
- if (!in_quote && c == p_delim[0]) {
- strings.push_back(current);
- current = String();
- } else if (c == '"') {
- if (l[i + 1] == '"') {
- s[0] = '"';
- current += s;
- i++;
- } else {
- in_quote = !in_quote;
- }
- } else {
- s[0] = c;
- current += s;
- }
- }
- strings.push_back(current);
- return strings;
- }
- int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const {
- int i = 0;
- for (i = 0; i < p_length && !eof_reached(); i++)
- p_dst[i] = get_8();
- return i;
- }
- void FileAccess::store_16(uint16_t p_dest) {
- uint8_t a, b;
- a = p_dest & 0xFF;
- b = p_dest >> 8;
- if (endian_swap) {
- SWAP(a, b);
- }
- store_8(a);
- store_8(b);
- }
- void FileAccess::store_32(uint32_t p_dest) {
- uint16_t a, b;
- a = p_dest & 0xFFFF;
- b = p_dest >> 16;
- if (endian_swap) {
- SWAP(a, b);
- }
- store_16(a);
- store_16(b);
- }
- void FileAccess::store_64(uint64_t p_dest) {
- uint32_t a, b;
- a = p_dest & 0xFFFFFFFF;
- b = p_dest >> 32;
- if (endian_swap) {
- SWAP(a, b);
- }
- store_32(a);
- store_32(b);
- }
- void FileAccess::store_real(real_t p_real) {
- if (sizeof(real_t) == 4)
- store_float(p_real);
- else
- store_double(p_real);
- }
- void FileAccess::store_float(float p_dest) {
- MarshallFloat m;
- m.f = p_dest;
- store_32(m.i);
- };
- void FileAccess::store_double(double p_dest) {
- MarshallDouble m;
- m.d = p_dest;
- store_64(m.l);
- };
- uint64_t FileAccess::get_modified_time(const String &p_file) {
- if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file))
- return 0;
- FileAccess *fa = create_for_path(p_file);
- ERR_FAIL_COND_V(!fa, 0);
- uint64_t mt = fa->_get_modified_time(p_file);
- memdelete(fa);
- return mt;
- }
- void FileAccess::store_string(const String &p_string) {
- if (p_string.length() == 0)
- return;
- CharString cs = p_string.utf8();
- store_buffer((uint8_t *)&cs[0], cs.length());
- }
- void FileAccess::store_pascal_string(const String &p_string) {
- CharString cs = p_string.utf8();
- store_32(cs.length());
- store_buffer((uint8_t *)&cs[0], cs.length());
- };
- String FileAccess::get_pascal_string() {
- uint32_t sl = get_32();
- CharString cs;
- cs.resize(sl + 1);
- get_buffer((uint8_t *)cs.ptr(), sl);
- cs[sl] = 0;
- String ret;
- ret.parse_utf8(cs.ptr());
- return ret;
- };
- void FileAccess::store_line(const String &p_line) {
- store_string(p_line);
- store_8('\n');
- }
- void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
- ERR_FAIL_COND(p_delim.length() != 1);
- String line = "";
- int size = p_values.size();
- for (int i = 0; i < size; ++i) {
- String value = p_values[i];
- if (value.find("\"") != -1 || value.find(p_delim) != -1 || value.find("\n") != -1) {
- value = "\"" + value.replace("\"", "\"\"") + "\"";
- }
- if (i < size - 1) {
- value += p_delim;
- }
- line += value;
- }
- store_line(line);
- }
- void FileAccess::store_buffer(const uint8_t *p_src, int p_length) {
- for (int i = 0; i < p_length; i++)
- store_8(p_src[i]);
- }
- Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path) {
- FileAccess *f = FileAccess::open(p_path, READ);
- ERR_FAIL_COND_V(!f, Vector<uint8_t>());
- Vector<uint8_t> data;
- data.resize(f->get_len());
- f->get_buffer(data.ptrw(), data.size());
- memdelete(f);
- return data;
- }
- String FileAccess::get_md5(const String &p_file) {
- FileAccess *f = FileAccess::open(p_file, READ);
- if (!f)
- return String();
- MD5_CTX md5;
- MD5Init(&md5);
- unsigned char step[32768];
- while (true) {
- int br = f->get_buffer(step, 32768);
- if (br > 0) {
- MD5Update(&md5, step, br);
- }
- if (br < 4096)
- break;
- }
- MD5Final(&md5);
- String ret = String::md5(md5.digest);
- memdelete(f);
- return ret;
- }
- String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
- MD5_CTX md5;
- MD5Init(&md5);
- for (int i = 0; i < p_file.size(); i++) {
- FileAccess *f = FileAccess::open(p_file[i], READ);
- ERR_CONTINUE(!f);
- unsigned char step[32768];
- while (true) {
- int br = f->get_buffer(step, 32768);
- if (br > 0) {
- MD5Update(&md5, step, br);
- }
- if (br < 4096)
- break;
- }
- memdelete(f);
- }
- MD5Final(&md5);
- String ret = String::md5(md5.digest);
- return ret;
- }
- String FileAccess::get_sha256(const String &p_file) {
- FileAccess *f = FileAccess::open(p_file, READ);
- if (!f)
- return String();
- sha256_context sha256;
- sha256_init(&sha256);
- unsigned char step[32768];
- while (true) {
- int br = f->get_buffer(step, 32768);
- if (br > 0) {
- sha256_hash(&sha256, step, br);
- }
- if (br < 4096)
- break;
- }
- unsigned char hash[32];
- sha256_done(&sha256, hash);
- memdelete(f);
- return String::hex_encode_buffer(hash, 32);
- }
- FileAccess::FileAccess() {
- endian_swap = false;
- real_is_double = false;
- _access_type = ACCESS_FILESYSTEM;
- };
|