123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include "pck_packer.h"
- #include "core/os/file_access.h"
- #include "version.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);
- };
- static void _pad(FileAccess *p_file, int p_bytes) {
- for (int i = 0; i < p_bytes; i++) {
- p_file->store_8(0);
- };
- };
- void PCKPacker::_bind_methods() {
- ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start);
- ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path"), &PCKPacker::add_file);
- ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush);
- };
- Error PCKPacker::pck_start(const String &p_file, int p_alignment) {
- file = FileAccess::open(p_file, FileAccess::WRITE);
- if (file == NULL) {
- return ERR_CANT_CREATE;
- };
- alignment = p_alignment;
- file->store_32(0x43504447);
- file->store_32(1);
- file->store_32(VERSION_MAJOR);
- file->store_32(VERSION_MINOR);
- file->store_32(0);
- for (int i = 0; i < 16; i++) {
- file->store_32(0);
- };
- files.clear();
- return OK;
- };
- Error PCKPacker::add_file(const String &p_file, const String &p_src) {
- FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
- 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;
- };
-
- file->store_32(files.size());
- for (int i = 0; i < files.size(); i++) {
- file->store_pascal_string(files[i].path);
- files[i].offset_offset = file->get_position();
- file->store_64(0);
- file->store_64(files[i].size);
-
- file->store_32(0);
- file->store_32(0);
- file->store_32(0);
- file->store_32(0);
- };
- uint64_t ofs = file->get_position();
- ofs = _align(ofs, alignment);
- _pad(file, ofs - file->get_position());
- const uint32_t buf_max = 65536;
- uint8_t *buf = memnew_arr(uint8_t, buf_max);
- int count = 0;
- for (int i = 0; i < files.size(); i++) {
- FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
- 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_position();
- file->seek(files[i].offset_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) {
- printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100);
- fflush(stdout);
- };
- };
- };
- if (p_verbose)
- printf("\n");
- file->close();
- memdelete(buf);
- return OK;
- };
- PCKPacker::PCKPacker() {
- file = NULL;
- };
- PCKPacker::~PCKPacker() {
- if (file != NULL) {
- memdelete(file);
- };
- file = NULL;
- };
|