From b1fba2e013e0dc53dec7406e5db4fbc65ad17a94 Mon Sep 17 00:00:00 2001 From: J08nY Date: Sat, 1 Oct 2016 22:04:10 +0200 Subject: PCKPacker: moved from tools into core, fixes #4129 --- core/io/pck_packer.cpp | 191 +++++++++++++++++++++++++++++++++++++++++++ core/io/pck_packer.h | 59 +++++++++++++ core/register_core_types.cpp | 3 + main/main.cpp | 4 +- tools/SCsub | 1 - tools/pck/SCsub | 4 - tools/pck/pck_packer.cpp | 191 ------------------------------------------- tools/pck/pck_packer.h | 59 ------------- 8 files changed, 254 insertions(+), 258 deletions(-) create mode 100644 core/io/pck_packer.cpp create mode 100644 core/io/pck_packer.h delete mode 100644 tools/pck/SCsub delete mode 100644 tools/pck/pck_packer.cpp delete mode 100644 tools/pck/pck_packer.h diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp new file mode 100644 index 0000000000..04b88ea028 --- /dev/null +++ b/core/io/pck_packer.cpp @@ -0,0 +1,191 @@ +/*************************************************************************/ +/* pkc_packer.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#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); +}; + +static void _pad(FileAccess* p_file, int p_bytes) { + + for (int i=0; istore_8(0); + }; +}; + +void PCKPacker::_bind_methods() { + + 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); +}; + + +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); // MAGIC + file->store_32(0); // # version + file->store_32(0); // # major + file->store_32(0); // # minor + file->store_32(0); // # revision + + for (int i=0; i<16; i++) { + + file->store_32(0); // reserved + }; + + 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; + }; + + // write the index + + file->store_32(files.size()); + + for (int i=0; istore_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; + for (int i=0; i 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) { + printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100); + fflush(stdout); + }; + }; + }; + + if (p_verbose) + printf("\n"); + + file->close(); + + return OK; +}; + +PCKPacker::PCKPacker() { + + file = NULL; +}; + +PCKPacker::~PCKPacker() { + if (file != NULL) { + memdelete(file); + }; + file = NULL; +}; diff --git a/core/io/pck_packer.h b/core/io/pck_packer.h new file mode 100644 index 0000000000..b1182335e2 --- /dev/null +++ b/core/io/pck_packer.h @@ -0,0 +1,59 @@ +/*************************************************************************/ +/* pck_packer.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "core/reference.h" + +class FileAccess; + +class PCKPacker : public Reference { + + OBJ_TYPE(PCKPacker, Reference); + + FileAccess* file; + int alignment; + + static void _bind_methods(); + + struct File { + + String path; + String src_path; + int size; + uint64_t offset_offset; + }; + Vector files; + +public: + Error pck_start(const String& p_file, int p_alignment); + Error add_file(const String& p_file, const String& p_src); + Error flush(bool p_verbose = false); + + + PCKPacker(); + ~PCKPacker(); +}; diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 7e4ba039c5..c3a127afb9 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -49,6 +49,7 @@ #include "os/input.h" #include "core/io/xml_parser.h" #include "io/http_client.h" +#include "io/pck_packer.h" #include "packed_data_container.h" #include "func_ref.h" #include "input_map.h" @@ -146,6 +147,8 @@ void register_core_types() { ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::register_virtual_type(); ObjectTypeDB::register_type(); diff --git a/main/main.cpp b/main/main.cpp index d2ba38b094..cd6858fc24 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -57,7 +57,6 @@ #include "tools/editor/editor_node.h" #include "tools/editor/project_manager.h" -#include "tools/pck/pck_packer.h" #endif #include "io/file_access_network.h" @@ -996,7 +995,7 @@ Error Main::setup2() { #ifdef TOOLS_ENABLED ObjectTypeDB::set_current_api(ObjectTypeDB::API_EDITOR); EditorNode::register_editor_types(); - ObjectTypeDB::register_type(); // todo: move somewhere else + ObjectTypeDB::set_current_api(ObjectTypeDB::API_CORE); #endif @@ -1755,4 +1754,3 @@ void Main::cleanup() { } - diff --git a/tools/SCsub b/tools/SCsub index aaebab724a..67b1f20e72 100644 --- a/tools/SCsub +++ b/tools/SCsub @@ -117,7 +117,6 @@ if (env["tools"]!="no"): SConscript('editor/SCsub'); SConscript('collada/SCsub'); SConscript('doc/SCsub') - SConscript('pck/SCsub') lib = env.Library("tool",env.tool_sources) diff --git a/tools/pck/SCsub b/tools/pck/SCsub deleted file mode 100644 index cf98ae145d..0000000000 --- a/tools/pck/SCsub +++ /dev/null @@ -1,4 +0,0 @@ -Import('env') - -if env["tools"] == "yes": - env.add_source_files(env.tool_sources, "*.cpp") diff --git a/tools/pck/pck_packer.cpp b/tools/pck/pck_packer.cpp deleted file mode 100644 index 04b88ea028..0000000000 --- a/tools/pck/pck_packer.cpp +++ /dev/null @@ -1,191 +0,0 @@ -/*************************************************************************/ -/* pkc_packer.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ -/* */ -/* 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. */ -/*************************************************************************/ -#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); -}; - -static void _pad(FileAccess* p_file, int p_bytes) { - - for (int i=0; istore_8(0); - }; -}; - -void PCKPacker::_bind_methods() { - - 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); -}; - - -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); // MAGIC - file->store_32(0); // # version - file->store_32(0); // # major - file->store_32(0); // # minor - file->store_32(0); // # revision - - for (int i=0; i<16; i++) { - - file->store_32(0); // reserved - }; - - 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; - }; - - // write the index - - file->store_32(files.size()); - - for (int i=0; istore_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; - for (int i=0; i 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) { - printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100); - fflush(stdout); - }; - }; - }; - - if (p_verbose) - printf("\n"); - - file->close(); - - return OK; -}; - -PCKPacker::PCKPacker() { - - file = NULL; -}; - -PCKPacker::~PCKPacker() { - if (file != NULL) { - memdelete(file); - }; - file = NULL; -}; diff --git a/tools/pck/pck_packer.h b/tools/pck/pck_packer.h deleted file mode 100644 index b1182335e2..0000000000 --- a/tools/pck/pck_packer.h +++ /dev/null @@ -1,59 +0,0 @@ -/*************************************************************************/ -/* pck_packer.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ -/* */ -/* 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. */ -/*************************************************************************/ -#include "core/reference.h" - -class FileAccess; - -class PCKPacker : public Reference { - - OBJ_TYPE(PCKPacker, Reference); - - FileAccess* file; - int alignment; - - static void _bind_methods(); - - struct File { - - String path; - String src_path; - int size; - uint64_t offset_offset; - }; - Vector files; - -public: - Error pck_start(const String& p_file, int p_alignment); - Error add_file(const String& p_file, const String& p_src); - Error flush(bool p_verbose = false); - - - PCKPacker(); - ~PCKPacker(); -}; -- cgit v1.2.3