app_packager.h 5.1 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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #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/editor_export.h"
  42. #include "editor/editor_node.h"
  43. #include "thirdparty/minizip/unzip.h"
  44. #include "thirdparty/minizip/zip.h"
  45. #include <zlib.h>
  46. class AppxPackager {
  47. enum {
  48. FILE_HEADER_MAGIC = 0x04034b50,
  49. DATA_DESCRIPTOR_MAGIC = 0x08074b50,
  50. CENTRAL_DIR_MAGIC = 0x02014b50,
  51. END_OF_CENTRAL_DIR_MAGIC = 0x06054b50,
  52. ZIP64_END_OF_CENTRAL_DIR_MAGIC = 0x06064b50,
  53. ZIP64_END_DIR_LOCATOR_MAGIC = 0x07064b50,
  54. P7X_SIGNATURE = 0x58434b50,
  55. ZIP64_HEADER_ID = 0x0001,
  56. ZIP_VERSION = 20,
  57. ZIP_ARCHIVE_VERSION = 45,
  58. GENERAL_PURPOSE = 0x00,
  59. BASE_FILE_HEADER_SIZE = 30,
  60. DATA_DESCRIPTOR_SIZE = 24,
  61. BASE_CENTRAL_DIR_SIZE = 46,
  62. EXTRA_FIELD_LENGTH = 28,
  63. ZIP64_HEADER_SIZE = 24,
  64. ZIP64_END_OF_CENTRAL_DIR_SIZE = (56 - 12),
  65. END_OF_CENTRAL_DIR_SIZE = 42,
  66. BLOCK_SIZE = 65536,
  67. };
  68. struct BlockHash {
  69. String base64_hash;
  70. size_t compressed_size = 0;
  71. };
  72. struct FileMeta {
  73. String name;
  74. int lfh_size = 0;
  75. bool compressed = false;
  76. size_t compressed_size = 0;
  77. size_t uncompressed_size = 0;
  78. Vector<BlockHash> hashes;
  79. uLong file_crc32 = 0;
  80. ZPOS64_T zip_offset = 0;
  81. };
  82. String progress_task;
  83. FileAccess *package = nullptr;
  84. Set<String> mime_types;
  85. Vector<FileMeta> file_metadata;
  86. ZPOS64_T central_dir_offset;
  87. ZPOS64_T end_of_central_dir_offset;
  88. Vector<uint8_t> central_dir_data;
  89. String hash_block(const uint8_t *p_block_data, size_t p_block_len);
  90. void make_block_map(const String &p_path);
  91. void make_content_types(const String &p_path);
  92. _FORCE_INLINE_ unsigned int buf_put_int16(uint16_t p_val, uint8_t *p_buf) {
  93. for (int i = 0; i < 2; i++) {
  94. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  95. }
  96. return 2;
  97. }
  98. _FORCE_INLINE_ unsigned int buf_put_int32(uint32_t p_val, uint8_t *p_buf) {
  99. for (int i = 0; i < 4; i++) {
  100. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  101. }
  102. return 4;
  103. }
  104. _FORCE_INLINE_ unsigned int buf_put_int64(uint64_t p_val, uint8_t *p_buf) {
  105. for (int i = 0; i < 8; i++) {
  106. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  107. }
  108. return 8;
  109. }
  110. _FORCE_INLINE_ unsigned int buf_put_string(String p_val, uint8_t *p_buf) {
  111. for (int i = 0; i < p_val.length(); i++) {
  112. *p_buf++ = p_val.utf8().get(i);
  113. }
  114. return p_val.length();
  115. }
  116. Vector<uint8_t> make_file_header(FileMeta p_file_meta);
  117. void store_central_dir_header(const FileMeta &p_file, bool p_do_hash = true);
  118. Vector<uint8_t> make_end_of_central_record();
  119. String content_type(String p_extension);
  120. public:
  121. void set_progress_task(String p_task) { progress_task = p_task; }
  122. void init(FileAccess *p_fa);
  123. 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);
  124. void finish();
  125. AppxPackager();
  126. ~AppxPackager();
  127. };
  128. #endif