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