2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_buffered_fa.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef FILE_ACCESS_BUFFERED_FA_H
|
|
|
|
#define FILE_ACCESS_BUFFERED_FA_H
|
|
|
|
|
|
|
|
#include "core/io/file_access_buffered.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
class FileAccessBufferedFA : public FileAccessBuffered {
|
|
|
|
|
|
|
|
T f;
|
|
|
|
|
|
|
|
int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!f.is_open(), -1);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
((T *)&f)->seek(p_offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (p_dest) {
|
|
|
|
|
|
|
|
f.get_buffer(p_dest, p_size);
|
|
|
|
return p_size;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
cache.offset = p_offset;
|
|
|
|
cache.buffer.resize(p_size);
|
|
|
|
|
2019-02-12 13:30:56 +01:00
|
|
|
// on PoolVector
|
2017-01-07 22:25:37 +01:00
|
|
|
//PoolVector<uint8_t>::Write write = cache.buffer.write();
|
2017-11-27 07:41:00 +01:00
|
|
|
//f.get_buffer(write.ptrw(), p_size);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// on vector
|
2017-11-27 07:41:00 +01:00
|
|
|
f.get_buffer(cache.buffer.ptrw(), p_size);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return p_size;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static FileAccess *create() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return memnew(FileAccessBufferedFA<T>());
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void _set_access_type(AccessType p_access) {
|
|
|
|
f._set_access_type(p_access);
|
|
|
|
FileAccessBuffered::_set_access_type(p_access);
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2017-09-22 07:56:02 +02:00
|
|
|
void flush() {
|
|
|
|
|
|
|
|
f.flush();
|
|
|
|
};
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void store_8(uint8_t p_dest) {
|
|
|
|
|
|
|
|
f.store_8(p_dest);
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void store_buffer(const uint8_t *p_src, int p_length) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f.store_buffer(p_src, p_length);
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool file_exists(const String &p_name) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return f.file_exists(p_name);
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error _open(const String &p_path, int p_mode_flags) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
|
|
Error ret = f._open(p_path, p_mode_flags);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ret != OK)
|
2014-02-10 02:10:30 +01:00
|
|
|
return ret;
|
|
|
|
//ERR_FAIL_COND_V( ret != OK, ret );
|
|
|
|
|
|
|
|
file.size = f.get_len();
|
|
|
|
file.offset = 0;
|
|
|
|
file.open = true;
|
|
|
|
file.name = p_path;
|
|
|
|
file.access_flags = p_mode_flags;
|
|
|
|
|
|
|
|
cache.buffer.resize(0);
|
|
|
|
cache.offset = 0;
|
|
|
|
|
|
|
|
return set_error(OK);
|
|
|
|
};
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
file.offset = 0;
|
|
|
|
file.size = 0;
|
|
|
|
file.open = false;
|
|
|
|
file.name = "";
|
|
|
|
|
|
|
|
cache.buffer.resize(0);
|
|
|
|
cache.offset = 0;
|
|
|
|
set_error(OK);
|
|
|
|
};
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
static void make_default() {
|
|
|
|
FileAccess::create_func = FileAccessBufferedFA<T>::create;
|
|
|
|
};
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual uint64_t _get_modified_time(const String &p_file) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return f._get_modified_time(p_file);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccessBufferedFA(){
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FILE_ACCESS_BUFFERED_FA_H
|