2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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"
|
2017-04-28 16:29:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "core/io/file_access_pack.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "core/io/marshalls.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "os/os.h"
|
2017-07-30 20:53:40 +00:00
|
|
|
#include "project_settings.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-28 16:29:15 +00:00
|
|
|
#include "thirdparty/misc/md5.h"
|
|
|
|
#include "thirdparty/misc/sha256.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { 0, 0 };
|
2016-06-13 13:10:50 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool FileAccess::backup_save = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *FileAccess::create(AccessType p_access) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!create_func, 0);
|
|
|
|
ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
FileAccess *ret = create_func[p_access]();
|
|
|
|
ret->_set_access_type(p_access);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool FileAccess::exists(const String &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-22 05:56:02 +00:00
|
|
|
if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name))
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = open(p_name, READ);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
memdelete(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccess::_set_access_type(AccessType p_access) {
|
|
|
|
|
|
|
|
_access_type = p_access;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *FileAccess::create_for_path(const String &p_path) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *ret = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error FileAccess::reopen(const String &p_path, int p_mode_flags) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return _open(p_path, p_mode_flags);
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//try packed data first
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *ret = NULL;
|
|
|
|
if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ret = PackedData::get_singleton()->try_open_path(p_path);
|
2014-05-24 04:35:47 +00:00
|
|
|
if (ret) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (r_error)
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ret = create_for_path(p_path);
|
|
|
|
Error err = ret->_open(p_path, p_mode_flags);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = err;
|
|
|
|
if (err != OK) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
memdelete(ret);
|
2017-03-05 15:44:50 +00:00
|
|
|
ret = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
|
|
|
|
|
|
|
|
return create_func[p_access];
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String FileAccess::fix_path(const String &p_path) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
//helper used by file accesses that use a single filesystem
|
|
|
|
|
|
|
|
String r_path = p_path.replace("\\", "/");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (_access_type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
case ACCESS_RESOURCES: {
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
if (ProjectSettings::get_singleton()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (r_path.begins_with("res://")) {
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (resource_path != "") {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return r_path.replace("res:/", resource_path);
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
return r_path.replace("res://", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACCESS_USERDATA: {
|
|
|
|
|
|
|
|
if (r_path.begins_with("user://")) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String data_dir = OS::get_singleton()->get_data_dir();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (data_dir != "") {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return r_path.replace("user:/", data_dir);
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
return r_path.replace("user://", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACCESS_FILESYSTEM: {
|
|
|
|
|
|
|
|
return r_path;
|
|
|
|
} break;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return r_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* these are all implemented for ease of porting, then can later be optimized */
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint16_t FileAccess::get_16() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
uint16_t res;
|
2017-03-05 15:44:50 +00:00
|
|
|
uint8_t a, b;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = get_8();
|
|
|
|
b = get_8();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (endian_swap) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
res = b;
|
|
|
|
res <<= 8;
|
|
|
|
res |= a;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t FileAccess::get_32() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t res;
|
2017-03-05 15:44:50 +00:00
|
|
|
uint16_t a, b;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = get_16();
|
|
|
|
b = get_16();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (endian_swap) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
res = b;
|
|
|
|
res <<= 16;
|
|
|
|
res |= a;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
return res;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
uint64_t FileAccess::get_64() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
uint64_t res;
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t a, b;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = get_32();
|
|
|
|
b = get_32();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (endian_swap) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
res = b;
|
|
|
|
res <<= 32;
|
|
|
|
res |= a;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
return res;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-05-29 00:46:48 +00:00
|
|
|
String FileAccess::get_token() const {
|
|
|
|
|
|
|
|
CharString token;
|
|
|
|
|
|
|
|
CharType c = get_8();
|
|
|
|
|
|
|
|
while (!eof_reached()) {
|
|
|
|
|
|
|
|
if (c <= ' ') {
|
|
|
|
if (!token.empty())
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
token.push_back(c);
|
|
|
|
}
|
|
|
|
c = get_8();
|
|
|
|
}
|
|
|
|
|
|
|
|
token.push_back(0);
|
|
|
|
return String::utf8(token.get_data());
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String FileAccess::get_line() const {
|
|
|
|
|
|
|
|
CharString line;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
CharType c = get_8();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (!eof_reached()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (c == '\n' || c == '\0') {
|
2014-02-10 01:10:30 +00:00
|
|
|
line.push_back(0);
|
|
|
|
return String::utf8(line.get_data());
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (c != '\r')
|
2014-02-10 01:10:30 +00:00
|
|
|
line.push_back(c);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c = get_8();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
line.push_back(0);
|
|
|
|
return String::utf8(line.get_data());
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:58:39 +00:00
|
|
|
Vector<String> FileAccess::get_csv_line(String delim) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(delim.length() != 1, Vector<String>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String l;
|
2017-03-05 15:44:50 +00:00
|
|
|
int qc = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
do {
|
2017-07-26 01:38:28 +00:00
|
|
|
ERR_FAIL_COND_V(eof_reached(), Vector<String>());
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l += get_line() + "\n";
|
|
|
|
qc = 0;
|
|
|
|
for (int i = 0; i < l.length(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (l[i] == '"')
|
2014-02-10 01:10:30 +00:00
|
|
|
qc++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} while (qc % 2);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.substr(0, l.length() - 1);
|
2016-04-16 12:12:29 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector<String> strings;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool in_quote = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
String current;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < l.length(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
CharType c = l[i];
|
2017-03-05 15:44:50 +00:00
|
|
|
CharType s[2] = { 0, 0 };
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!in_quote && c == delim[0]) {
|
2014-02-10 01:10:30 +00:00
|
|
|
strings.push_back(current);
|
2017-03-05 15:44:50 +00:00
|
|
|
current = String();
|
|
|
|
} else if (c == '"') {
|
|
|
|
if (l[i + 1] == '"') {
|
|
|
|
s[0] = '"';
|
|
|
|
current += s;
|
2014-02-10 01:10:30 +00:00
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
in_quote = !in_quote;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
s[0] = c;
|
|
|
|
current += s;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strings.push_back(current);
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < p_length && !eof_reached(); i++)
|
|
|
|
p_dst[i] = get_8();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccess::store_16(uint16_t p_dest) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint8_t a, b;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = p_dest & 0xFF;
|
|
|
|
b = p_dest >> 8;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (endian_swap) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
store_8(a);
|
|
|
|
store_8(b);
|
|
|
|
}
|
|
|
|
void FileAccess::store_32(uint32_t p_dest) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint16_t a, b;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = p_dest & 0xFFFF;
|
|
|
|
b = p_dest >> 16;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (endian_swap) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
store_16(a);
|
|
|
|
store_16(b);
|
|
|
|
}
|
|
|
|
void FileAccess::store_64(uint64_t p_dest) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t a, b;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = p_dest & 0xFFFFFFFF;
|
|
|
|
b = p_dest >> 32;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (endian_swap) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
store_32(a);
|
|
|
|
store_32(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileAccess::store_real(real_t p_real) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (sizeof(real_t) == 4)
|
2014-02-10 01:10:30 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint64_t FileAccess::get_modified_time(const String &p_file) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
FileAccess *fa = create_for_path(p_file);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!fa, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
uint64_t mt = fa->_get_modified_time(p_file);
|
|
|
|
memdelete(fa);
|
|
|
|
return mt;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FileAccess::store_string(const String &p_string) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_string.length() == 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
CharString cs = p_string.utf8();
|
|
|
|
store_buffer((uint8_t *)&cs[0], cs.length());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FileAccess::store_pascal_string(const String &p_string) {
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
CharString cs = p_string.utf8();
|
|
|
|
store_32(cs.length());
|
2017-03-05 15:44:50 +00:00
|
|
|
store_buffer((uint8_t *)&cs[0], cs.length());
|
2014-03-14 01:57:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
String FileAccess::get_pascal_string() {
|
|
|
|
|
|
|
|
uint32_t sl = get_32();
|
|
|
|
CharString cs;
|
2017-03-05 15:44:50 +00:00
|
|
|
cs.resize(sl + 1);
|
|
|
|
get_buffer((uint8_t *)cs.ptr(), sl);
|
|
|
|
cs[sl] = 0;
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
String ret;
|
|
|
|
ret.parse_utf8(cs.ptr());
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FileAccess::store_line(const String &p_line) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
store_string(p_line);
|
|
|
|
store_8('\n');
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void FileAccess::store_buffer(const uint8_t *p_src, int p_length) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_length; i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
store_8(p_src[i]);
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_path, READ);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!f, Vector<uint8_t>());
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector<uint8_t> data;
|
|
|
|
data.resize(f->get_len());
|
2017-03-05 15:44:50 +00:00
|
|
|
f->get_buffer(data.ptr(), data.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete(f);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String FileAccess::get_md5(const String &p_file) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_file, READ);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!f)
|
|
|
|
return String();
|
|
|
|
|
|
|
|
MD5_CTX md5;
|
|
|
|
MD5Init(&md5);
|
|
|
|
|
|
|
|
unsigned char step[32768];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int br = f->get_buffer(step, 32768);
|
|
|
|
if (br > 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MD5Update(&md5, step, br);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (br < 4096)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MD5Final(&md5);
|
|
|
|
|
|
|
|
String ret = String::md5(md5.digest);
|
|
|
|
|
|
|
|
memdelete(f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String FileAccess::get_sha256(const String &p_file) {
|
2016-06-17 07:55:16 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_file, READ);
|
2016-06-17 07:55:16 +00:00
|
|
|
if (!f)
|
|
|
|
return String();
|
|
|
|
|
|
|
|
sha256_context sha256;
|
|
|
|
sha256_init(&sha256);
|
|
|
|
|
|
|
|
unsigned char step[32768];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2016-06-17 07:55:16 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int br = f->get_buffer(step, 32768);
|
|
|
|
if (br > 0) {
|
2016-06-17 07:55:16 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
sha256_hash(&sha256, step, br);
|
2016-06-17 07:55:16 +00:00
|
|
|
}
|
|
|
|
if (br < 4096)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char hash[32];
|
|
|
|
|
|
|
|
sha256_done(&sha256, hash);
|
|
|
|
|
|
|
|
memdelete(f);
|
|
|
|
return String::hex_encode_buffer(hash, 32);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
FileAccess::FileAccess() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
endian_swap = false;
|
|
|
|
real_is_double = false;
|
|
|
|
_access_type = ACCESS_FILESYSTEM;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|