pck_packer.cpp 5.1 KB

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