2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_buffered_fa.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef FILE_ACCESS_BUFFERED_FA_H
|
|
|
|
#define FILE_ACCESS_BUFFERED_FA_H
|
|
|
|
|
|
|
|
#include "core/io/file_access_buffered.h"
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
template <class T>
|
2014-02-10 01:10:30 +00: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-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!f.is_open(), -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
((T *)&f)->seek(p_offset);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_dest) {
|
|
|
|
|
|
|
|
f.get_buffer(p_dest, p_size);
|
|
|
|
return p_size;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
cache.offset = p_offset;
|
|
|
|
cache.buffer.resize(p_size);
|
|
|
|
|
|
|
|
// on dvector
|
|
|
|
//DVector<uint8_t>::Write write = cache.buffer.write();
|
|
|
|
//f.get_buffer(write.ptr(), p_size);
|
|
|
|
|
|
|
|
// on vector
|
|
|
|
f.get_buffer(cache.buffer.ptr(), p_size);
|
|
|
|
|
|
|
|
return p_size;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static FileAccess *create() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return memnew(FileAccessBufferedFA<T>());
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void _set_access_type(AccessType p_access) {
|
|
|
|
f._set_access_type(p_access);
|
|
|
|
FileAccessBuffered::_set_access_type(p_access);
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
void store_8(uint8_t p_dest) {
|
|
|
|
|
|
|
|
f.store_8(p_dest);
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void store_buffer(const uint8_t *p_src, int p_length) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
f.store_buffer(p_src, p_length);
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool file_exists(const String &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return f.file_exists(p_name);
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error _open(const String &p_path, int p_mode_flags) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
|
|
Error ret = f._open(p_path, p_mode_flags);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ret != OK)
|
2014-02-10 01:10:30 +00: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-03-18 23:36:26 +00:00
|
|
|
// static void make_default() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
//FileAccess::create_func = FileAccessBufferedFA<T>::create;
|
|
|
|
// };
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
virtual uint64_t _get_modified_time(const String &p_file) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return f._get_modified_time(p_file);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccessBufferedFA(){
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FILE_ACCESS_BUFFERED_FA_H
|