| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  file_access_compressed.cpp                                           */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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-19 11:57:14 -03:00
										 |  |  | #include "file_access_compressed.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "core/print_string.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, int p_block_size) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	magic = p_magic.ascii().get_data(); | 
					
						
							|  |  |  | 	if (magic.length() > 4) | 
					
						
							|  |  |  | 		magic = magic.substr(0, 4); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		while (magic.length() < 4) | 
					
						
							|  |  |  | 			magic += " "; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	cmode = p_mode; | 
					
						
							|  |  |  | 	block_size = p_block_size; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 18:35:55 -03:00
										 |  |  | #define WRITE_FIT(m_bytes)                                  \
 | 
					
						
							|  |  |  | 	{                                                       \ | 
					
						
							|  |  |  | 		if (write_pos + (m_bytes) > write_max) {            \ | 
					
						
							|  |  |  | 			write_max = write_pos + (m_bytes);              \ | 
					
						
							|  |  |  | 		}                                                   \ | 
					
						
							|  |  |  | 		if (write_max > write_buffer_size) {                \ | 
					
						
							|  |  |  | 			write_buffer_size = next_power_of_2(write_max); \ | 
					
						
							|  |  |  | 			buffer.resize(write_buffer_size);               \ | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 			write_ptr = buffer.ptrw();                      \ | 
					
						
							| 
									
										
										
										
											2017-08-17 18:35:55 -03:00
										 |  |  | 		}                                                   \ | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Error FileAccessCompressed::open_after_magic(FileAccess *p_base) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	f = p_base; | 
					
						
							|  |  |  | 	cmode = (Compression::Mode)f->get_32(); | 
					
						
							|  |  |  | 	block_size = f->get_32(); | 
					
						
							| 
									
										
										
										
											2020-02-11 11:39:20 +08:00
										 |  |  | 	if (block_size == 0) { | 
					
						
							|  |  |  | 		f = NULL; // Let the caller to handle the FileAccess object if failed to open as compressed file.
 | 
					
						
							|  |  |  | 		ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Can't open compressed file '" + p_base->get_path() + "' with block size 0, it is corrupted."); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	read_total = f->get_32(); | 
					
						
							|  |  |  | 	int bc = (read_total / block_size) + 1; | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	int acc_ofs = f->get_position() + bc * 4; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int max_bs = 0; | 
					
						
							|  |  |  | 	for (int i = 0; i < bc; i++) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ReadBlock rb; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		rb.offset = acc_ofs; | 
					
						
							|  |  |  | 		rb.csize = f->get_32(); | 
					
						
							|  |  |  | 		acc_ofs += rb.csize; | 
					
						
							|  |  |  | 		max_bs = MAX(max_bs, rb.csize); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		read_blocks.push_back(rb); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	comp_buffer.resize(max_bs); | 
					
						
							|  |  |  | 	buffer.resize(block_size); | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 	read_ptr = buffer.ptrw(); | 
					
						
							|  |  |  | 	f->get_buffer(comp_buffer.ptrw(), read_blocks[0].csize); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	at_end = false; | 
					
						
							|  |  |  | 	read_eof = false; | 
					
						
							|  |  |  | 	read_block_count = bc; | 
					
						
							|  |  |  | 	read_block_size = read_blocks.size() == 1 ? read_total : block_size; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 	Compression::decompress(buffer.ptrw(), read_block_size, comp_buffer.ptr(), read_blocks[0].csize, cmode); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	read_block = 0; | 
					
						
							|  |  |  | 	read_pos = 0; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(p_mode_flags == READ_WRITE, ERR_UNAVAILABLE); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (f) | 
					
						
							|  |  |  | 		close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	f = FileAccess::open(p_path, p_mode_flags, &err); | 
					
						
							|  |  |  | 	if (err != OK) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		//not openable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		f = NULL; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_mode_flags & WRITE) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		buffer.clear(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		writing = true; | 
					
						
							|  |  |  | 		write_pos = 0; | 
					
						
							|  |  |  | 		write_buffer_size = 256; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		buffer.resize(256); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		write_max = 0; | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 		write_ptr = buffer.ptrw(); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//don't store anything else unless it's done saving!
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		char rmagic[5]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		f->get_buffer((uint8_t *)rmagic, 4); | 
					
						
							|  |  |  | 		rmagic[4] = 0; | 
					
						
							| 
									
										
										
										
											2020-02-11 11:39:20 +08:00
										 |  |  | 		if (magic != rmagic || open_after_magic(f) != OK) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			memdelete(f); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			f = NULL; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			return ERR_FILE_UNRECOGNIZED; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void FileAccessCompressed::close() { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!f) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (writing) { | 
					
						
							|  |  |  | 		//save block table and all compressed blocks
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		CharString mgc = magic.utf8(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //write header 4
 | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		f->store_32(cmode); //write compression mode 4
 | 
					
						
							|  |  |  | 		f->store_32(block_size); //write block size 4
 | 
					
						
							|  |  |  | 		f->store_32(write_max); //max amount of data written 4
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int bc = (write_max / block_size) + 1; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < bc; i++) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			f->store_32(0); //compressed sizes, will update later
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Vector<int> block_sizes; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < bc; i++) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			int bl = i == (bc - 1) ? write_max % block_size : block_size; | 
					
						
							|  |  |  | 			uint8_t *bp = &write_ptr[i * block_size]; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Vector<uint8_t> cblock; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode)); | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 			int s = Compression::compress(cblock.ptrw(), bp, bl, cmode); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			f->store_buffer(cblock.ptr(), s); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			block_sizes.push_back(s); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		f->seek(16); //ok write block sizes
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < bc; i++) | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			f->store_32(block_sizes[i]); | 
					
						
							|  |  |  | 		f->seek_end(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
 | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		buffer.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		comp_buffer.clear(); | 
					
						
							|  |  |  | 		buffer.clear(); | 
					
						
							|  |  |  | 		read_blocks.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memdelete(f); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	f = NULL; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | bool FileAccessCompressed::is_open() const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return f != NULL; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void FileAccessCompressed::seek(size_t p_position) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_MSG(!f, "File must be opened before use."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	if (writing) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ERR_FAIL_COND(p_position > write_max); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		write_pos = p_position; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ERR_FAIL_COND(p_position > read_total); | 
					
						
							|  |  |  | 		if (p_position == read_total) { | 
					
						
							|  |  |  | 			at_end = true; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-08-05 18:20:18 -07:00
										 |  |  | 			at_end = false; | 
					
						
							|  |  |  | 			read_eof = false; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			int block_idx = p_position / block_size; | 
					
						
							|  |  |  | 			if (block_idx != read_block) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				read_block = block_idx; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 				f->seek(read_blocks[read_block].offset); | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 				f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); | 
					
						
							|  |  |  | 				Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			read_pos = p_position % block_size; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void FileAccessCompressed::seek_end(int64_t p_position) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_MSG(!f, "File must be opened before use."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	if (writing) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		seek(write_max + p_position); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		seek(read_total + p_position); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | size_t FileAccessCompressed::get_position() const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	if (writing) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return write_pos; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		return read_block * block_size + read_pos; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | size_t FileAccessCompressed::get_len() const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	if (writing) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return write_max; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return read_total; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | bool FileAccessCompressed::eof_reached() const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	if (writing) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return read_eof; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | uint8_t FileAccessCompressed::get_8() const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (at_end) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		read_eof = true; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uint8_t ret = read_ptr[read_pos]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	read_pos++; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (read_pos >= read_block_size) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		read_block++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (read_block < read_block_count) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			//read another block of compressed data
 | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 			f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); | 
					
						
							|  |  |  | 			Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; | 
					
						
							|  |  |  | 			read_pos = 0; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			read_block--; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			at_end = true; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (at_end) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		read_eof = true; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < p_length; i++) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_dst[i] = read_ptr[read_pos]; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 		read_pos++; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (read_pos >= read_block_size) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 			read_block++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (read_block < read_block_count) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 				//read another block of compressed data
 | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 				f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); | 
					
						
							|  |  |  | 				Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; | 
					
						
							|  |  |  | 				read_pos = 0; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				read_block--; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				at_end = true; | 
					
						
							|  |  |  | 				if (i < p_length - 1) | 
					
						
							|  |  |  | 					read_eof = true; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 				return i; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return p_length; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error FileAccessCompressed::get_error() const { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return read_eof ? ERR_FILE_EOF : OK; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | void FileAccessCompressed::flush() { | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_MSG(!f, "File must be opened before use."); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// compressed files keep data in memory till close()
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void FileAccessCompressed::store_8(uint8_t p_dest) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_MSG(!f, "File must be opened before use."); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	WRITE_FIT(1); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	write_ptr[write_pos++] = p_dest; | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | bool FileAccessCompressed::file_exists(const String &p_name) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	FileAccess *fa = FileAccess::open(p_name, FileAccess::READ); | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 	if (!fa) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	memdelete(fa); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (f) | 
					
						
							|  |  |  | 		return f->get_modified_time(p_file); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-07 15:46:52 -03:00
										 |  |  | uint32_t FileAccessCompressed::_get_unix_permissions(const String &p_file) { | 
					
						
							|  |  |  | 	if (f) | 
					
						
							|  |  |  | 		return f->_get_unix_permissions(p_file); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error FileAccessCompressed::_set_unix_permissions(const String &p_file, uint32_t p_permissions) { | 
					
						
							|  |  |  | 	if (f) { | 
					
						
							|  |  |  | 		return f->_set_unix_permissions(p_file, p_permissions); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return FAILED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | FileAccessCompressed::FileAccessCompressed() : | 
					
						
							|  |  |  | 		cmode(Compression::MODE_ZSTD), | 
					
						
							|  |  |  | 		writing(false), | 
					
						
							|  |  |  | 		write_ptr(0), | 
					
						
							|  |  |  | 		write_buffer_size(0), | 
					
						
							|  |  |  | 		write_max(0), | 
					
						
							|  |  |  | 		block_size(0), | 
					
						
							|  |  |  | 		read_eof(false), | 
					
						
							|  |  |  | 		at_end(false), | 
					
						
							|  |  |  | 		read_ptr(NULL), | 
					
						
							|  |  |  | 		read_block(0), | 
					
						
							|  |  |  | 		read_block_count(0), | 
					
						
							|  |  |  | 		read_block_size(0), | 
					
						
							|  |  |  | 		read_pos(0), | 
					
						
							|  |  |  | 		read_total(0), | 
					
						
							|  |  |  | 		magic("GCMP"), | 
					
						
							|  |  |  | 		f(NULL) { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | FileAccessCompressed::~FileAccessCompressed() { | 
					
						
							| 
									
										
										
										
											2014-02-19 11:57:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (f) | 
					
						
							|  |  |  | 		close(); | 
					
						
							|  |  |  | } |