pck_packer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* pck_packer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/os/file_access.h"
  32. static uint64_t _align(uint64_t p_n, int p_alignment) {
  33. if (p_alignment == 0)
  34. return p_n;
  35. uint64_t rest = p_n % p_alignment;
  36. if (rest == 0)
  37. return p_n;
  38. else
  39. return p_n + (p_alignment - rest);
  40. };
  41. static void _pad(FileAccess *p_file, int p_bytes) {
  42. for (int i = 0; i < p_bytes; i++) {
  43. p_file->store_8(0);
  44. };
  45. };
  46. void PCKPacker::_bind_methods() {
  47. ObjectTypeDB::bind_method(_MD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start);
  48. ObjectTypeDB::bind_method(_MD("add_file", "pck_path", "source_path"), &PCKPacker::add_file);
  49. ObjectTypeDB::bind_method(_MD("flush", "verbose"), &PCKPacker::flush);
  50. };
  51. Error PCKPacker::pck_start(const String &p_file, int p_alignment) {
  52. file = FileAccess::open(p_file, FileAccess::WRITE);
  53. if (file == NULL) {
  54. return ERR_CANT_CREATE;
  55. };
  56. alignment = p_alignment;
  57. file->store_32(0x43504447); // MAGIC
  58. file->store_32(0); // # version
  59. file->store_32(0); // # major
  60. file->store_32(0); // # minor
  61. file->store_32(0); // # revision
  62. for (int i = 0; i < 16; i++) {
  63. file->store_32(0); // reserved
  64. };
  65. files.clear();
  66. return OK;
  67. };
  68. Error PCKPacker::add_file(const String &p_file, const String &p_src) {
  69. FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
  70. if (!f) {
  71. return ERR_FILE_CANT_OPEN;
  72. };
  73. File pf;
  74. pf.path = p_file;
  75. pf.src_path = p_src;
  76. pf.size = f->get_len();
  77. pf.offset_offset = 0;
  78. files.push_back(pf);
  79. f->close();
  80. memdelete(f);
  81. return OK;
  82. };
  83. Error PCKPacker::flush(bool p_verbose) {
  84. if (!file) {
  85. ERR_FAIL_COND_V(!file, ERR_INVALID_PARAMETER);
  86. return ERR_INVALID_PARAMETER;
  87. };
  88. // write the index
  89. file->store_32(files.size());
  90. for (int i = 0; i < files.size(); i++) {
  91. file->store_pascal_string(files[i].path);
  92. files[i].offset_offset = file->get_pos();
  93. file->store_64(0); // offset
  94. file->store_64(files[i].size); // size
  95. // # empty md5
  96. file->store_32(0);
  97. file->store_32(0);
  98. file->store_32(0);
  99. file->store_32(0);
  100. };
  101. uint64_t ofs = file->get_pos();
  102. ofs = _align(ofs, alignment);
  103. _pad(file, ofs - file->get_pos());
  104. const uint32_t buf_max = 65536;
  105. uint8_t *buf = memnew_arr(uint8_t, buf_max);
  106. int count = 0;
  107. for (int i = 0; i < files.size(); i++) {
  108. FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
  109. uint64_t to_write = files[i].size;
  110. while (to_write > 0) {
  111. int read = src->get_buffer(buf, MIN(to_write, buf_max));
  112. file->store_buffer(buf, read);
  113. to_write -= read;
  114. };
  115. uint64_t pos = file->get_pos();
  116. file->seek(files[i].offset_offset); // go back to store the file's offset
  117. file->store_64(ofs);
  118. file->seek(pos);
  119. ofs = _align(ofs + files[i].size, alignment);
  120. _pad(file, ofs - pos);
  121. src->close();
  122. memdelete(src);
  123. count += 1;
  124. if (p_verbose) {
  125. if (count % 100 == 0) {
  126. printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100);
  127. fflush(stdout);
  128. };
  129. };
  130. };
  131. if (p_verbose)
  132. printf("\n");
  133. file->close();
  134. memdelete(buf);
  135. return OK;
  136. };
  137. PCKPacker::PCKPacker() {
  138. file = NULL;
  139. };
  140. PCKPacker::~PCKPacker() {
  141. if (file != NULL) {
  142. memdelete(file);
  143. };
  144. file = NULL;
  145. };