Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication in FileAccess #92167

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 30 additions & 81 deletions core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,59 +223,44 @@ String FileAccess::fix_path(const String &p_path) const {
}

/* these are all implemented for ease of porting, then can later be optimized */
uint8_t FileAccess::get_8() const {
uint8_t data = 0;
get_buffer(&data, sizeof(uint8_t));

uint16_t FileAccess::get_16() const {
uint16_t res;
uint8_t a, b;
return data;
}

a = get_8();
b = get_8();
uint16_t FileAccess::get_16() const {
uint16_t data = 0;
get_buffer(reinterpret_cast<uint8_t *>(&data), sizeof(uint16_t));

if (big_endian) {
SWAP(a, b);
data = BSWAP16(data);
}

res = b;
res <<= 8;
res |= a;

return res;
return data;
}

uint32_t FileAccess::get_32() const {
uint32_t res;
uint16_t a, b;

a = get_16();
b = get_16();
uint32_t data = 0;
get_buffer(reinterpret_cast<uint8_t *>(&data), sizeof(uint32_t));

if (big_endian) {
SWAP(a, b);
data = BSWAP32(data);
}

res = b;
res <<= 16;
res |= a;

return res;
return data;
}

uint64_t FileAccess::get_64() const {
uint64_t res;
uint32_t a, b;

a = get_32();
b = get_32();
uint64_t data = 0;
get_buffer(reinterpret_cast<uint8_t *>(&data), sizeof(uint64_t));

if (big_endian) {
SWAP(a, b);
data = BSWAP64(data);
}

res = b;
res <<= 32;
res |= a;

return res;
return data;
}

float FileAccess::get_float() const {
Expand Down Expand Up @@ -465,17 +450,6 @@ String FileAccess::get_as_text(bool p_skip_cr) const {
return text;
}

uint64_t FileAccess::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);

uint64_t i = 0;
for (i = 0; i < p_length && !eof_reached(); i++) {
p_dst[i] = get_8();
}

return i;
}

Vector<uint8_t> FileAccess::get_buffer(int64_t p_length) const {
Vector<uint8_t> data;

Expand All @@ -488,7 +462,7 @@ Vector<uint8_t> FileAccess::get_buffer(int64_t p_length) const {
ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");

uint8_t *w = data.ptrw();
int64_t len = get_buffer(&w[0], p_length);
int64_t len = get_buffer(w, p_length);

if (len < p_length) {
data.resize(len);
Expand All @@ -512,46 +486,32 @@ String FileAccess::get_as_utf8_string(bool p_skip_cr) const {
return s;
}

void FileAccess::store_16(uint16_t p_dest) {
uint8_t a, b;

a = p_dest & 0xFF;
b = p_dest >> 8;
void FileAccess::store_8(uint8_t p_dest) {
store_buffer(&p_dest, sizeof(uint8_t));
}

void FileAccess::store_16(uint16_t p_dest) {
if (big_endian) {
SWAP(a, b);
p_dest = BSWAP16(p_dest);
}

store_8(a);
store_8(b);
store_buffer(reinterpret_cast<uint8_t *>(&p_dest), sizeof(uint16_t));
}

void FileAccess::store_32(uint32_t p_dest) {
uint16_t a, b;

a = p_dest & 0xFFFF;
b = p_dest >> 16;

if (big_endian) {
SWAP(a, b);
p_dest = BSWAP32(p_dest);
}

store_16(a);
store_16(b);
store_buffer(reinterpret_cast<uint8_t *>(&p_dest), sizeof(uint32_t));
}

void FileAccess::store_64(uint64_t p_dest) {
uint32_t a, b;

a = p_dest & 0xFFFFFFFF;
b = p_dest >> 32;

if (big_endian) {
SWAP(a, b);
p_dest = BSWAP64(p_dest);
}

store_32(a);
store_32(b);
store_buffer(reinterpret_cast<uint8_t *>(&p_dest), sizeof(uint64_t));
}

void FileAccess::store_real(real_t p_real) {
Expand Down Expand Up @@ -708,22 +668,11 @@ void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_
store_line(line);
}

void FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_COND(!p_src && p_length > 0);
for (uint64_t i = 0; i < p_length; i++) {
store_8(p_src[i]);
}
}

void FileAccess::store_buffer(const Vector<uint8_t> &p_buffer) {
uint64_t len = p_buffer.size();
if (len == 0) {
return;
}

const uint8_t *r = p_buffer.ptr();

store_buffer(&r[0], len);
store_buffer(r, len);
}

void FileAccess::store_var(const Variant &p_var, bool p_full_objects) {
Expand Down
8 changes: 4 additions & 4 deletions core/io/file_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FileAccess : public RefCounted {

virtual bool eof_reached() const = 0; ///< reading passed EOF

virtual uint8_t get_8() const = 0; ///< get a byte
virtual uint8_t get_8() const; ///< get a byte
virtual uint16_t get_16() const; ///< get 16 bits uint
virtual uint32_t get_32() const; ///< get 32 bits uint
virtual uint64_t get_64() const; ///< get 64 bits uint
Expand All @@ -148,7 +148,7 @@ class FileAccess : public RefCounted {

Variant get_var(bool p_allow_objects = false) const;

virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const; ///< get an array of bytes
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const = 0; ///< get an array of bytes, needs to be overwritten by children.
Vector<uint8_t> get_buffer(int64_t p_length) const;
virtual String get_line() const;
virtual String get_token() const;
Expand All @@ -168,7 +168,7 @@ class FileAccess : public RefCounted {

virtual Error resize(int64_t p_length) = 0;
virtual void flush() = 0;
virtual void store_8(uint8_t p_dest) = 0; ///< store a byte
virtual void store_8(uint8_t p_dest); ///< store a byte
virtual void store_16(uint16_t p_dest); ///< store 16 bits uint
virtual void store_32(uint32_t p_dest); ///< store 32 bits uint
virtual void store_64(uint64_t p_dest); ///< store 64 bits uint
Expand All @@ -184,7 +184,7 @@ class FileAccess : public RefCounted {
virtual void store_pascal_string(const String &p_string);
virtual String get_pascal_string();

virtual void store_buffer(const uint8_t *p_src, uint64_t p_length); ///< store an array of bytes
virtual void store_buffer(const uint8_t *p_src, uint64_t p_length) = 0; ///< store an array of bytes, needs to be overwritten by children.
void store_buffer(const Vector<uint8_t> &p_buffer);

void store_var(const Variant &p_var, bool p_full_objects = false);
Expand Down
39 changes: 4 additions & 35 deletions core/io/file_access_compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,38 +260,6 @@ bool FileAccessCompressed::eof_reached() const {
}
}

uint8_t FileAccessCompressed::get_8() const {
ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");
ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");

if (at_end) {
read_eof = true;
return 0;
}

uint8_t ret = read_ptr[read_pos];

read_pos++;
if (read_pos >= read_block_size) {
read_block++;

if (read_block < read_block_count) {
//read another block of compressed data
f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);
int total = Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
ERR_FAIL_COND_V_MSG(total == -1, 0, "Compressed file is corrupt.");
read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
read_pos = 0;

} else {
read_block--;
at_end = true;
}
}

return ret;
}

uint64_t FileAccessCompressed::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
ERR_FAIL_COND_V_MSG(f.is_null(), -1, "File must be opened before use.");
Expand Down Expand Up @@ -341,12 +309,13 @@ void FileAccessCompressed::flush() {
// compressed files keep data in memory till close()
}

void FileAccessCompressed::store_8(uint8_t p_dest) {
void FileAccessCompressed::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");

WRITE_FIT(1);
write_ptr[write_pos++] = p_dest;
WRITE_FIT(p_length);
memcpy(write_ptr + write_pos, p_src, p_length);
write_pos += p_length;
}

bool FileAccessCompressed::file_exists(const String &p_name) {
Expand Down
3 changes: 1 addition & 2 deletions core/io/file_access_compressed.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ class FileAccessCompressed : public FileAccess {

virtual bool eof_reached() const override; ///< reading passed EOF

virtual uint8_t get_8() const override; ///< get a byte
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;

virtual Error get_error() const override; ///< get last error

virtual Error resize(int64_t p_length) override { return ERR_UNAVAILABLE; }
virtual void flush() override;
virtual void store_8(uint8_t p_dest) override; ///< store a byte
virtual void store_buffer(const uint8_t *p_src, uint64_t p_length) override;

virtual bool file_exists(const String &p_name) override; ///< return true if a file exists

Expand Down
42 changes: 6 additions & 36 deletions core/io/file_access_encrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,13 @@ bool FileAccessEncrypted::eof_reached() const {
return eofed;
}

uint8_t FileAccessEncrypted::get_8() const {
ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");
if (pos >= get_length()) {
eofed = true;
return 0;
}

uint8_t b = data[pos];
pos++;
return b;
}

uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");

uint64_t to_copy = MIN(p_length, get_length() - pos);
for (uint64_t i = 0; i < to_copy; i++) {
p_dst[i] = data[pos++];
}
memcpy(p_dst, data.ptr() + pos, to_copy);
pos += to_copy;

if (to_copy < p_length) {
eofed = true;
Expand All @@ -242,17 +229,12 @@ void FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length)
ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
ERR_FAIL_COND(!p_src && p_length > 0);

if (pos < get_length()) {
for (uint64_t i = 0; i < p_length; i++) {
store_8(p_src[i]);
}
} else if (pos == get_length()) {
if (pos + p_length >= get_length()) {
data.resize(pos + p_length);
for (uint64_t i = 0; i < p_length; i++) {
data.write[pos + i] = p_src[i];
}
pos += p_length;
}

memcpy(data.ptrw() + pos, p_src, p_length);
pos += p_length;
}

void FileAccessEncrypted::flush() {
Expand All @@ -261,18 +243,6 @@ void FileAccessEncrypted::flush() {
// encrypted files keep data in memory till close()
}

void FileAccessEncrypted::store_8(uint8_t p_dest) {
ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");

if (pos < get_length()) {
data.write[pos] = p_dest;
pos++;
} else if (pos == get_length()) {
data.push_back(p_dest);
pos++;
}
}

bool FileAccessEncrypted::file_exists(const String &p_name) {
Ref<FileAccess> fa = FileAccess::open(p_name, FileAccess::READ);
if (fa.is_null()) {
Expand Down
2 changes: 0 additions & 2 deletions core/io/file_access_encrypted.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ class FileAccessEncrypted : public FileAccess {

virtual bool eof_reached() const override; ///< reading passed EOF

virtual uint8_t get_8() const override; ///< get a byte
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;

virtual Error get_error() const override; ///< get last error

virtual Error resize(int64_t p_length) override { return ERR_UNAVAILABLE; }
virtual void flush() override;
virtual void store_8(uint8_t p_dest) override; ///< store a byte
virtual void store_buffer(const uint8_t *p_src, uint64_t p_length) override; ///< store an array of bytes

virtual bool file_exists(const String &p_name) override; ///< return true if a file exists
Expand Down
Loading
Loading