app_packager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**************************************************************************/
  2. /* app_packager.h */
  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. #ifndef UWP_APP_PACKAGER_H
  31. #define UWP_APP_PACKAGER_H
  32. #include "core/config/project_settings.h"
  33. #include "core/core_bind.h"
  34. #include "core/crypto/crypto_core.h"
  35. #include "core/io/dir_access.h"
  36. #include "core/io/file_access.h"
  37. #include "core/io/marshalls.h"
  38. #include "core/io/zip_io.h"
  39. #include "core/object/class_db.h"
  40. #include "core/version.h"
  41. #include "editor/export/editor_export_platform.h"
  42. #include "thirdparty/minizip/unzip.h"
  43. #include "thirdparty/minizip/zip.h"
  44. #include <zlib.h>
  45. class AppxPackager {
  46. enum {
  47. FILE_HEADER_MAGIC = 0x04034b50,
  48. DATA_DESCRIPTOR_MAGIC = 0x08074b50,
  49. CENTRAL_DIR_MAGIC = 0x02014b50,
  50. END_OF_CENTRAL_DIR_MAGIC = 0x06054b50,
  51. ZIP64_END_OF_CENTRAL_DIR_MAGIC = 0x06064b50,
  52. ZIP64_END_DIR_LOCATOR_MAGIC = 0x07064b50,
  53. P7X_SIGNATURE = 0x58434b50,
  54. ZIP64_HEADER_ID = 0x0001,
  55. ZIP_VERSION = 20,
  56. ZIP_ARCHIVE_VERSION = 45,
  57. GENERAL_PURPOSE = 0x00,
  58. BASE_FILE_HEADER_SIZE = 30,
  59. DATA_DESCRIPTOR_SIZE = 24,
  60. BASE_CENTRAL_DIR_SIZE = 46,
  61. EXTRA_FIELD_LENGTH = 28,
  62. ZIP64_HEADER_SIZE = 24,
  63. ZIP64_END_OF_CENTRAL_DIR_SIZE = (56 - 12),
  64. END_OF_CENTRAL_DIR_SIZE = 42,
  65. BLOCK_SIZE = 65536,
  66. };
  67. struct BlockHash {
  68. String base64_hash;
  69. size_t compressed_size = 0;
  70. };
  71. struct FileMeta {
  72. String name;
  73. int lfh_size = 0;
  74. bool compressed = false;
  75. size_t compressed_size = 0;
  76. size_t uncompressed_size = 0;
  77. Vector<BlockHash> hashes;
  78. uLong file_crc32 = 0;
  79. ZPOS64_T zip_offset = 0;
  80. };
  81. String progress_task;
  82. Ref<FileAccess> package;
  83. HashSet<String> mime_types;
  84. Vector<FileMeta> file_metadata;
  85. ZPOS64_T central_dir_offset = 0;
  86. ZPOS64_T end_of_central_dir_offset = 0;
  87. Vector<uint8_t> central_dir_data;
  88. String hash_block(const uint8_t *p_block_data, size_t p_block_len);
  89. void make_block_map(const String &p_path);
  90. void make_content_types(const String &p_path);
  91. _FORCE_INLINE_ unsigned int buf_put_int16(uint16_t p_val, uint8_t *p_buf) {
  92. for (int i = 0; i < 2; i++) {
  93. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  94. }
  95. return 2;
  96. }
  97. _FORCE_INLINE_ unsigned int buf_put_int32(uint32_t p_val, uint8_t *p_buf) {
  98. for (int i = 0; i < 4; i++) {
  99. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  100. }
  101. return 4;
  102. }
  103. _FORCE_INLINE_ unsigned int buf_put_int64(uint64_t p_val, uint8_t *p_buf) {
  104. for (int i = 0; i < 8; i++) {
  105. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  106. }
  107. return 8;
  108. }
  109. _FORCE_INLINE_ unsigned int buf_put_string(String p_val, uint8_t *p_buf) {
  110. for (int i = 0; i < p_val.length(); i++) {
  111. *p_buf++ = p_val.utf8().get(i);
  112. }
  113. return p_val.length();
  114. }
  115. Vector<uint8_t> make_file_header(FileMeta p_file_meta);
  116. void store_central_dir_header(const FileMeta &p_file, bool p_do_hash = true);
  117. Vector<uint8_t> make_end_of_central_record();
  118. String content_type(String p_extension);
  119. public:
  120. void set_progress_task(String p_task) { progress_task = p_task; }
  121. void init(Ref<FileAccess> p_fa);
  122. Error add_file(String p_file_name, const uint8_t *p_buffer, size_t p_len, int p_file_no, int p_total_files, bool p_compress = false);
  123. void finish();
  124. AppxPackager();
  125. ~AppxPackager();
  126. };
  127. #endif // UWP_APP_PACKAGER_H