file_access_patched.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /* file_access_patched.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 "file_access_patched.h"
  31. #include "file_access_pack.h"
  32. #include "core/io/delta_encoding.h"
  33. #include "core/os/os.h"
  34. Error FileAccessPatched::_apply_patch() const {
  35. ERR_FAIL_COND_V(!is_open(), FAILED);
  36. String path = old_file->get_path();
  37. Vector<PackedData::PackedFile> delta_patches = PackedData::get_singleton()->get_delta_patches(path);
  38. Vector<uint8_t> old_file_data = old_file->get_buffer(old_file->get_length());
  39. for (int i = 0; i < delta_patches.size(); ++i) {
  40. const PackedData::PackedFile &delta_patch = delta_patches[i];
  41. ERR_FAIL_COND_V(delta_patch.bundle, FAILED);
  42. Error err = OK;
  43. uint64_t total_usec_start = OS::get_singleton()->get_ticks_usec();
  44. uint64_t io_usec_start = OS::get_singleton()->get_ticks_usec();
  45. Ref<FileAccess> patch_file = FileAccess::open(delta_patch.pack, FileAccess::READ, &err);
  46. ERR_FAIL_COND_V(err != OK, err);
  47. patch_file->seek(delta_patch.offset);
  48. ERR_FAIL_COND_V(patch_file->get_error() != OK, patch_file->get_error());
  49. Vector<uint8_t> patch_data = patch_file->get_buffer(delta_patch.size);
  50. ERR_FAIL_COND_V(patch_data.is_empty(), ERR_FILE_CANT_READ);
  51. uint64_t io_usec_end = OS::get_singleton()->get_ticks_usec();
  52. uint64_t decode_usec_start = OS::get_singleton()->get_ticks_usec();
  53. Vector<uint8_t> new_file_data;
  54. err = DeltaEncoding::decode_delta(old_file_data, patch_data, new_file_data);
  55. ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to apply delta patch (%d of %d) to \"%s\".", i + 1, delta_patches.size(), path));
  56. uint64_t decode_usec_end = OS::get_singleton()->get_ticks_usec();
  57. old_file_data = new_file_data;
  58. uint64_t total_usec_end = OS::get_singleton()->get_ticks_usec();
  59. print_verbose(vformat(U"Applied delta patch to \"%s\" from \"%s\" in %d μs (%d μs I/O, %d μs decoding).", path, delta_patch.pack.get_file(), total_usec_end - total_usec_start, io_usec_end - io_usec_start, decode_usec_end - decode_usec_start));
  60. }
  61. patched_file_data = old_file_data;
  62. patched_file.instantiate();
  63. return patched_file->open_custom(patched_file_data.ptr(), patched_file_data.size());
  64. }
  65. bool FileAccessPatched::_try_apply_patch() const {
  66. if (last_error != OK) {
  67. return false;
  68. }
  69. if (patched_file.is_valid()) {
  70. return true;
  71. }
  72. last_error = _apply_patch();
  73. return last_error == OK;
  74. }
  75. Error FileAccessPatched::open_custom(const Ref<FileAccess> &p_old_file) {
  76. close();
  77. if (!p_old_file->is_open()) {
  78. last_error = ERR_FILE_CANT_OPEN;
  79. return last_error;
  80. }
  81. old_file = p_old_file;
  82. return OK;
  83. }
  84. bool FileAccessPatched::is_open() const {
  85. return old_file.is_valid() && old_file->is_open();
  86. }
  87. void FileAccessPatched::seek(uint64_t p_position) {
  88. if (!_try_apply_patch()) {
  89. return;
  90. }
  91. patched_file->seek(p_position);
  92. }
  93. void FileAccessPatched::seek_end(int64_t p_position) {
  94. if (!_try_apply_patch()) {
  95. return;
  96. }
  97. patched_file->seek_end(p_position);
  98. }
  99. uint64_t FileAccessPatched::get_position() const {
  100. if (!_try_apply_patch()) {
  101. return 0;
  102. }
  103. return patched_file->get_position();
  104. }
  105. uint64_t FileAccessPatched::get_length() const {
  106. if (!_try_apply_patch()) {
  107. return 0;
  108. }
  109. return patched_file->get_length();
  110. }
  111. bool FileAccessPatched::eof_reached() const {
  112. if (!_try_apply_patch()) {
  113. return true;
  114. }
  115. return patched_file->eof_reached();
  116. }
  117. Error FileAccessPatched::get_error() const {
  118. if (last_error != OK) {
  119. return last_error;
  120. }
  121. if (patched_file.is_valid()) {
  122. Error inner_error = patched_file->get_error();
  123. if (inner_error != OK) {
  124. return inner_error;
  125. }
  126. }
  127. return last_error;
  128. }
  129. bool FileAccessPatched::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  130. if (!_try_apply_patch()) {
  131. return false;
  132. }
  133. return patched_file->store_buffer(p_src, p_length);
  134. }
  135. uint64_t FileAccessPatched::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  136. if (!_try_apply_patch()) {
  137. return 0;
  138. }
  139. return patched_file->get_buffer(p_dst, p_length);
  140. }
  141. void FileAccessPatched::flush() {
  142. if (!_try_apply_patch()) {
  143. return;
  144. }
  145. patched_file->flush();
  146. }
  147. void FileAccessPatched::close() {
  148. old_file = Ref<FileAccess>();
  149. patched_file = Ref<FileAccessMemory>();
  150. patched_file_data.clear();
  151. last_error = OK;
  152. }
  153. bool FileAccessPatched::file_exists(const String &p_name) {
  154. ERR_FAIL_COND_V(old_file.is_null(), false);
  155. return old_file->file_exists(p_name);
  156. }