pck_packer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* pck_packer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
  75. if (!f) {
  76. return ERR_FILE_CANT_OPEN;
  77. };
  78. File pf;
  79. pf.path = p_file;
  80. pf.src_path = p_src;
  81. pf.size = f->get_len();
  82. pf.offset_offset = 0;
  83. files.push_back(pf);
  84. f->close();
  85. memdelete(f);
  86. return OK;
  87. };
  88. Error PCKPacker::flush(bool p_verbose) {
  89. ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use.");
  90. // write the index
  91. file->store_32(files.size());
  92. for (int i = 0; i < files.size(); i++) {
  93. file->store_pascal_string(files[i].path);
  94. files.write[i].offset_offset = file->get_position();
  95. file->store_64(0); // offset
  96. file->store_64(files[i].size); // size
  97. // # empty md5
  98. file->store_32(0);
  99. file->store_32(0);
  100. file->store_32(0);
  101. file->store_32(0);
  102. };
  103. uint64_t ofs = file->get_position();
  104. ofs = _align(ofs, alignment);
  105. _pad(file, ofs - file->get_position());
  106. const uint32_t buf_max = 65536;
  107. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  108. int count = 0;
  109. for (int i = 0; i < files.size(); i++) {
  110. FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
  111. uint64_t to_write = files[i].size;
  112. while (to_write > 0) {
  113. uint64_t read = src->get_buffer(buf, MIN(to_write, buf_max));
  114. file->store_buffer(buf, read);
  115. to_write -= read;
  116. };
  117. uint64_t pos = file->get_position();
  118. file->seek(files[i].offset_offset); // go back to store the file's offset
  119. file->store_64(ofs);
  120. file->seek(pos);
  121. ofs = _align(ofs + files[i].size, alignment);
  122. _pad(file, ofs - pos);
  123. src->close();
  124. memdelete(src);
  125. count += 1;
  126. if (p_verbose && files.size() > 0) {
  127. if (count % 100 == 0) {
  128. printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100);
  129. fflush(stdout);
  130. };
  131. };
  132. };
  133. if (p_verbose) {
  134. printf("\n");
  135. }
  136. file->close();
  137. memdelete_arr(buf);
  138. return OK;
  139. };
  140. PCKPacker::PCKPacker() {
  141. file = nullptr;
  142. };
  143. PCKPacker::~PCKPacker() {
  144. if (file != nullptr) {
  145. memdelete(file);
  146. };
  147. file = nullptr;
  148. };