2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* pkc_packer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
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) */
|
2016-06-18 12:46:12 +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. */
|
|
|
|
/*************************************************************************/
|
2014-08-02 01:10:38 +00:00
|
|
|
#include "pck_packer.h"
|
|
|
|
|
|
|
|
#include "core/os/file_access.h"
|
|
|
|
|
|
|
|
static uint64_t _align(uint64_t p_n, int p_alignment) {
|
|
|
|
|
|
|
|
if (p_alignment == 0)
|
|
|
|
return p_n;
|
|
|
|
|
|
|
|
uint64_t rest = p_n % p_alignment;
|
|
|
|
if (rest == 0)
|
|
|
|
return p_n;
|
|
|
|
else
|
|
|
|
return p_n + (p_alignment - rest);
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static void _pad(FileAccess *p_file, int p_bytes) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_bytes; i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
p_file->store_8(0);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
void PCKPacker::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_file", "pck_path", "source_path"), &PCKPacker::add_file);
|
|
|
|
ObjectTypeDB::bind_method(_MD("flush", "verbose"), &PCKPacker::flush);
|
2014-08-02 01:10:38 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error PCKPacker::pck_start(const String &p_file, int p_alignment) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
file = FileAccess::open(p_file, FileAccess::WRITE);
|
|
|
|
if (file == NULL) {
|
|
|
|
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
};
|
|
|
|
|
|
|
|
alignment = p_alignment;
|
|
|
|
|
|
|
|
file->store_32(0x43504447); // MAGIC
|
|
|
|
file->store_32(0); // # version
|
|
|
|
file->store_32(0); // # major
|
|
|
|
file->store_32(0); // # minor
|
|
|
|
file->store_32(0); // # revision
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < 16; i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
file->store_32(0); // reserved
|
|
|
|
};
|
|
|
|
|
|
|
|
files.clear();
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error PCKPacker::add_file(const String &p_file, const String &p_src) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
|
2014-08-02 01:10:38 +00: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) {
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
ERR_FAIL_COND_V(!file, ERR_INVALID_PARAMETER);
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
};
|
|
|
|
|
|
|
|
// write the index
|
|
|
|
|
|
|
|
file->store_32(files.size());
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < files.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
file->store_pascal_string(files[i].path);
|
|
|
|
files[i].offset_offset = file->get_pos();
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t ofs = file->get_pos();
|
|
|
|
ofs = _align(ofs, alignment);
|
|
|
|
|
|
|
|
_pad(file, ofs - file->get_pos());
|
|
|
|
|
|
|
|
const uint32_t buf_max = 65536;
|
|
|
|
uint8_t *buf = memnew_arr(uint8_t, buf_max);
|
|
|
|
|
|
|
|
int count = 0;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < files.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
|
2014-08-02 01:10:38 +00:00
|
|
|
uint64_t to_write = files[i].size;
|
|
|
|
while (to_write > 0) {
|
|
|
|
|
|
|
|
int read = src->get_buffer(buf, MIN(to_write, buf_max));
|
|
|
|
file->store_buffer(buf, read);
|
|
|
|
to_write -= read;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t pos = file->get_pos();
|
|
|
|
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;
|
|
|
|
if (p_verbose) {
|
|
|
|
if (count % 100 == 0) {
|
2015-05-05 22:46:02 +00:00
|
|
|
printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100);
|
2014-09-03 02:13:40 +00:00
|
|
|
fflush(stdout);
|
2014-08-02 01:10:38 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (p_verbose)
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
file->close();
|
2017-04-06 11:18:41 +00:00
|
|
|
memdelete(buf);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
PCKPacker::PCKPacker() {
|
|
|
|
|
|
|
|
file = NULL;
|
|
|
|
};
|
|
|
|
|
|
|
|
PCKPacker::~PCKPacker() {
|
|
|
|
if (file != NULL) {
|
|
|
|
memdelete(file);
|
|
|
|
};
|
|
|
|
file = NULL;
|
|
|
|
};
|