| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | /*  pck_packer.cpp                                                       */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-13 09:45:09 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02: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-08-01 22:10:38 -03:00
										 |  |  | #include "pck_packer.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 17:22:22 +01:00
										 |  |  | #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
 | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | #include "core/os/file_access.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/version.h"
 | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static uint64_t _align(uint64_t p_n, int p_alignment) { | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (p_alignment == 0) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		return p_n; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	uint64_t rest = p_n % p_alignment; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (rest == 0) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		return p_n; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		return p_n + (p_alignment - rest); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | static void _pad(FileAccess *p_file, int p_bytes) { | 
					
						
							|  |  |  | 	for (int i = 0; i < p_bytes; i++) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		p_file->store_8(0); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PCKPacker::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2019-12-07 01:05:11 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start, DEFVAL(0)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path"), &PCKPacker::add_file); | 
					
						
							| 
									
										
										
										
											2019-12-07 01:05:11 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false)); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error PCKPacker::pck_start(const String &p_file, int p_alignment) { | 
					
						
							| 
									
										
										
										
											2021-05-04 16:00:45 +02:00
										 |  |  | 	if (file != nullptr) { | 
					
						
							| 
									
										
										
										
											2020-01-20 12:15:57 +08:00
										 |  |  | 		memdelete(file); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 	file = FileAccess::open(p_file, FileAccess::WRITE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 20:57:49 -06:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!file, ERR_CANT_CREATE, "Can't open file to write: " + String(p_file) + "."); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	alignment = p_alignment; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 17:22:22 +01:00
										 |  |  | 	file->store_32(PACK_HEADER_MAGIC); | 
					
						
							|  |  |  | 	file->store_32(PACK_FORMAT_VERSION); | 
					
						
							|  |  |  | 	file->store_32(VERSION_MAJOR); | 
					
						
							|  |  |  | 	file->store_32(VERSION_MINOR); | 
					
						
							|  |  |  | 	file->store_32(VERSION_PATCH); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < 16; i++) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		file->store_32(0); // reserved
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	files.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error PCKPacker::add_file(const String &p_file, const String &p_src) { | 
					
						
							|  |  |  | 	FileAccess *f = FileAccess::open(p_src, FileAccess::READ); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 	if (!f) { | 
					
						
							|  |  |  | 		return ERR_FILE_CANT_OPEN; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	File pf; | 
					
						
							|  |  |  | 	pf.path = p_file; | 
					
						
							|  |  |  | 	pf.src_path = p_src; | 
					
						
							|  |  |  | 	pf.size = f->get_len(); | 
					
						
							|  |  |  | 	pf.offset_offset = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	files.push_back(pf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f->close(); | 
					
						
							|  |  |  | 	memdelete(f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error PCKPacker::flush(bool p_verbose) { | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use."); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// write the index
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file->store_32(files.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < files.size(); i++) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		file->store_pascal_string(files[i].path); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		files.write[i].offset_offset = file->get_position(); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		file->store_64(0); // offset
 | 
					
						
							|  |  |  | 		file->store_64(files[i].size); // size
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// # empty md5
 | 
					
						
							|  |  |  | 		file->store_32(0); | 
					
						
							|  |  |  | 		file->store_32(0); | 
					
						
							|  |  |  | 		file->store_32(0); | 
					
						
							|  |  |  | 		file->store_32(0); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	uint64_t ofs = file->get_position(); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 	ofs = _align(ofs, alignment); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	_pad(file, ofs - file->get_position()); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const uint32_t buf_max = 65536; | 
					
						
							|  |  |  | 	uint8_t *buf = memnew_arr(uint8_t, buf_max); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int count = 0; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < files.size(); i++) { | 
					
						
							|  |  |  | 		FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		uint64_t to_write = files[i].size; | 
					
						
							|  |  |  | 		while (to_write > 0) { | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | 			uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max)); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 			file->store_buffer(buf, read); | 
					
						
							|  |  |  | 			to_write -= read; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 		uint64_t pos = file->get_position(); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		file->seek(files[i].offset_offset); // go back to store the file's offset
 | 
					
						
							|  |  |  | 		file->store_64(ofs); | 
					
						
							|  |  |  | 		file->seek(pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ofs = _align(ofs + files[i].size, alignment); | 
					
						
							|  |  |  | 		_pad(file, ofs - pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		src->close(); | 
					
						
							|  |  |  | 		memdelete(src); | 
					
						
							|  |  |  | 		count += 1; | 
					
						
							| 
									
										
										
										
											2020-03-21 09:50:19 +01:00
										 |  |  | 		if (p_verbose && files.size() > 0) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 			if (count % 100 == 0) { | 
					
						
							| 
									
										
										
										
											2015-05-06 00:46:02 +02:00
										 |  |  | 				printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100); | 
					
						
							| 
									
										
										
										
											2014-09-02 23:13:40 -03:00
										 |  |  | 				fflush(stdout); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (p_verbose) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		printf("\n"); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	file->close(); | 
					
						
							| 
									
										
										
										
											2019-01-22 20:02:19 +03:00
										 |  |  | 	memdelete_arr(buf); | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PCKPacker::PCKPacker() { | 
					
						
							| 
									
										
										
										
											2021-05-04 16:00:45 +02:00
										 |  |  | 	file = nullptr; | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PCKPacker::~PCKPacker() { | 
					
						
							| 
									
										
										
										
											2021-05-04 16:00:45 +02:00
										 |  |  | 	if (file != nullptr) { | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | 		memdelete(file); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2021-05-04 16:00:45 +02:00
										 |  |  | 	file = nullptr; | 
					
						
							| 
									
										
										
										
											2014-08-01 22:10:38 -03:00
										 |  |  | }; |