project_zip_packer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.replace_first(p_base_path, "");
  68. zipOpenNewFileInZip(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. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  79. zipCloseFileInZip(p_zip);
  80. }
  81. void ProjectZIPPacker::_zip_recursive(const String &p_path, const String &p_base_path, zipFile p_zip) {
  82. Ref<DirAccess> dir = DirAccess::open(p_path);
  83. if (dir.is_null()) {
  84. WARN_PRINT("Unable to open directory for zipping: " + p_path);
  85. return;
  86. }
  87. dir->list_dir_begin();
  88. String cur = dir->get_next();
  89. String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
  90. while (!cur.is_empty()) {
  91. String cs = p_path.path_join(cur);
  92. if (cur == "." || cur == ".." || cur == project_data_dir_name) {
  93. // Skip
  94. } else if (dir->current_is_dir()) {
  95. String path = cs.replace_first(p_base_path, "") + "/";
  96. zipOpenNewFileInZip(p_zip,
  97. path.utf8().get_data(),
  98. nullptr,
  99. nullptr,
  100. 0,
  101. nullptr,
  102. 0,
  103. nullptr,
  104. Z_DEFLATED,
  105. Z_DEFAULT_COMPRESSION);
  106. zipCloseFileInZip(p_zip);
  107. _zip_recursive(cs, p_base_path, p_zip);
  108. } else {
  109. _zip_file(cs, p_base_path, p_zip);
  110. }
  111. cur = dir->get_next();
  112. }
  113. }