pck_packer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**************************************************************************/
  2. /* pck_packer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "pck_packer.h"
  31. #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
  32. #include "core/os/file_access.h"
  33. #include "core/version.h"
  34. static uint64_t _align(uint64_t p_n, int p_alignment) {
  35. if (p_alignment == 0) {
  36. return p_n;
  37. }
  38. uint64_t rest = p_n % p_alignment;
  39. if (rest == 0) {
  40. return p_n;
  41. } else {
  42. return p_n + (p_alignment - rest);
  43. }
  44. };
  45. static void _pad(FileAccess *p_file, int p_bytes) {
  46. for (int i = 0; i < p_bytes; i++) {
  47. p_file->store_8(0);
  48. };
  49. };
  50. void PCKPacker::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start, DEFVAL(0));
  52. ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path"), &PCKPacker::add_file);
  53. ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false));
  54. };
  55. Error PCKPacker::pck_start(const String &p_file, int p_alignment) {
  56. if (file != nullptr) {
  57. memdelete(file);
  58. }
  59. file = FileAccess::open(p_file, FileAccess::WRITE);
  60. ERR_FAIL_COND_V_MSG(!file, ERR_CANT_CREATE, "Can't open file to write: " + String(p_file) + ".");
  61. alignment = p_alignment;
  62. file->store_32(PACK_HEADER_MAGIC);
  63. file->store_32(PACK_FORMAT_VERSION);
  64. file->store_32(VERSION_MAJOR);
  65. file->store_32(VERSION_MINOR);
  66. file->store_32(VERSION_PATCH);
  67. for (int i = 0; i < 16; i++) {
  68. file->store_32(0); // reserved
  69. };
  70. files.clear();
  71. return OK;
  72. };
  73. Error PCKPacker::add_file(const String &p_file, const String &p_src) {
  74. ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use.");
  75. FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
  76. if (!f) {
  77. return ERR_FILE_CANT_OPEN;
  78. };
  79. File pf;
  80. pf.path = p_file;
  81. pf.src_path = p_src;
  82. pf.size = f->get_len();
  83. pf.offset_offset = 0;
  84. files.push_back(pf);
  85. f->close();
  86. memdelete(f);
  87. return OK;
  88. };
  89. Error PCKPacker::flush(bool p_verbose) {
  90. ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use.");
  91. // write the index
  92. file->store_32(files.size());
  93. for (int i = 0; i < files.size(); i++) {
  94. file->store_pascal_string(files[i].path);
  95. files.write[i].offset_offset = file->get_position();
  96. file->store_64(0); // offset
  97. file->store_64(files[i].size); // size
  98. // # empty md5
  99. file->store_32(0);
  100. file->store_32(0);
  101. file->store_32(0);
  102. file->store_32(0);
  103. };
  104. uint64_t ofs = file->get_position();
  105. ofs = _align(ofs, alignment);
  106. _pad(file, ofs - file->get_position());
  107. const uint32_t buf_max = 65536;
  108. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  109. int count = 0;
  110. for (int i = 0; i < files.size(); i++) {
  111. FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
  112. uint64_t to_write = files[i].size;
  113. while (to_write > 0) {
  114. uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max));
  115. file->store_buffer(buf, read);
  116. to_write -= read;
  117. };
  118. uint64_t pos = file->get_position();
  119. file->seek(files[i].offset_offset); // go back to store the file's offset
  120. file->store_64(ofs);
  121. file->seek(pos);
  122. ofs = _align(ofs + files[i].size, alignment);
  123. _pad(file, ofs - pos);
  124. src->close();
  125. memdelete(src);
  126. count += 1;
  127. const int file_num = files.size();
  128. if (p_verbose && (file_num > 0)) {
  129. print_line(vformat("[%d/%d - %d%%] PCKPacker flush: %s -> %s", count, file_num, float(count) / file_num * 100, files[i].src_path, files[i].path));
  130. }
  131. }
  132. if (p_verbose) {
  133. printf("\n");
  134. }
  135. file->close();
  136. memdelete(file);
  137. file = nullptr;
  138. memdelete_arr(buf);
  139. return OK;
  140. };
  141. PCKPacker::PCKPacker() {
  142. file = nullptr;
  143. };
  144. PCKPacker::~PCKPacker() {
  145. if (file != nullptr) {
  146. memdelete(file);
  147. };
  148. file = nullptr;
  149. };