project_zip_packer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* project_zip_packer.cpp */
  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. #include "project_zip_packer.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. #include "core/os/os.h"
  35. #include "core/os/time.h"
  36. String ProjectZIPPacker::get_project_zip_safe_name() {
  37. // Name the downloaded ZIP file to contain the project name and download date for easier organization.
  38. // Replace characters not allowed (or risky) in Windows file names with safe characters.
  39. // In the project name, all invalid characters become an empty string so that a name
  40. // like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".
  41. const String project_name = GLOBAL_GET("application/config/name");
  42. const String project_name_safe = project_name.to_lower().replace_char(' ', '_');
  43. const String datetime_safe =
  44. Time::get_singleton()->get_datetime_string_from_system(false, true).replace_char(' ', '_');
  45. const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip", project_name_safe, datetime_safe));
  46. return output_name;
  47. }
  48. void ProjectZIPPacker::pack_project_zip(const String &p_path) {
  49. Ref<FileAccess> io_fa;
  50. zlib_filefunc_def io = zipio_create_io(&io_fa);
  51. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  52. const String base_path = resource_path.substr(0, resource_path.rfind_char('/')) + "/";
  53. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
  54. _zip_recursive(resource_path, base_path, zip);
  55. zipClose(zip, nullptr);
  56. }
  57. void ProjectZIPPacker::_zip_file(const String &p_path, const String &p_base_path, zipFile p_zip) {
  58. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  59. if (f.is_null()) {
  60. WARN_PRINT("Unable to open file for zipping: " + p_path);
  61. return;
  62. }
  63. Vector<uint8_t> data;
  64. uint64_t len = f->get_length();
  65. data.resize(len);
  66. f->get_buffer(data.ptrw(), len);
  67. String path = p_path.trim_prefix(p_base_path);
  68. zipOpenNewFileInZip4(p_zip,
  69. path.utf8().get_data(),
  70. nullptr,
  71. nullptr,
  72. 0,
  73. nullptr,
  74. 0,
  75. nullptr,
  76. Z_DEFLATED,
  77. Z_DEFAULT_COMPRESSION,
  78. 0,
  79. -MAX_WBITS,
  80. DEF_MEM_LEVEL,
  81. Z_DEFAULT_STRATEGY,
  82. nullptr,
  83. 0,
  84. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  85. 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
  86. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  87. zipCloseFileInZip(p_zip);
  88. }
  89. void ProjectZIPPacker::_zip_recursive(const String &p_path, const String &p_base_path, zipFile p_zip) {
  90. Ref<DirAccess> dir = DirAccess::open(p_path);
  91. if (dir.is_null()) {
  92. WARN_PRINT("Unable to open directory for zipping: " + p_path);
  93. return;
  94. }
  95. dir->list_dir_begin();
  96. String cur = dir->get_next();
  97. String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
  98. while (!cur.is_empty()) {
  99. String cs = p_path.path_join(cur);
  100. if (cur == "." || cur == ".." || cur == project_data_dir_name) {
  101. // Skip
  102. } else if (dir->current_is_dir()) {
  103. String path = cs.trim_prefix(p_base_path) + "/";
  104. zipOpenNewFileInZip4(p_zip,
  105. path.utf8().get_data(),
  106. nullptr,
  107. nullptr,
  108. 0,
  109. nullptr,
  110. 0,
  111. nullptr,
  112. Z_DEFLATED,
  113. Z_DEFAULT_COMPRESSION,
  114. 0,
  115. -MAX_WBITS,
  116. DEF_MEM_LEVEL,
  117. Z_DEFAULT_STRATEGY,
  118. nullptr,
  119. 0,
  120. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  121. 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
  122. zipCloseFileInZip(p_zip);
  123. _zip_recursive(cs, p_base_path, p_zip);
  124. } else {
  125. _zip_file(cs, p_base_path, p_zip);
  126. }
  127. cur = dir->get_next();
  128. }
  129. }