file_access_android.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*************************************************************************/
  2. /* file_access_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. #include "file_access_android.h"
  31. #include "print_string.h"
  32. AAssetManager *FileAccessAndroid::asset_manager = NULL;
  33. /*void FileAccessAndroid::make_default() {
  34. create_func=create_android;
  35. }*/
  36. FileAccess *FileAccessAndroid::create_android() {
  37. return memnew(FileAccessAndroid);
  38. }
  39. Error FileAccessAndroid::_open(const String &p_path, int p_mode_flags) {
  40. String path = fix_path(p_path).simplify_path();
  41. if (path.begins_with("/"))
  42. path = path.substr(1, path.length());
  43. else if (path.begins_with("res://"))
  44. path = path.substr(6, path.length());
  45. ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, ERR_UNAVAILABLE); //can't write on android..
  46. a = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  47. if (!a)
  48. return ERR_CANT_OPEN;
  49. //ERR_FAIL_COND_V(!a,ERR_FILE_NOT_FOUND);
  50. len = AAsset_getLength(a);
  51. pos = 0;
  52. eof = false;
  53. return OK;
  54. }
  55. void FileAccessAndroid::close() {
  56. if (!a)
  57. return;
  58. AAsset_close(a);
  59. a = NULL;
  60. }
  61. bool FileAccessAndroid::is_open() const {
  62. return a != NULL;
  63. }
  64. void FileAccessAndroid::seek(size_t p_position) {
  65. ERR_FAIL_COND(!a);
  66. AAsset_seek(a, p_position, SEEK_SET);
  67. pos = p_position;
  68. if (pos > len) {
  69. pos = len;
  70. eof = true;
  71. } else {
  72. eof = false;
  73. }
  74. }
  75. void FileAccessAndroid::seek_end(int64_t p_position) {
  76. ERR_FAIL_COND(!a);
  77. AAsset_seek(a, p_position, SEEK_END);
  78. pos = len + p_position;
  79. }
  80. size_t FileAccessAndroid::get_position() const {
  81. return pos;
  82. }
  83. size_t FileAccessAndroid::get_len() const {
  84. return len;
  85. }
  86. bool FileAccessAndroid::eof_reached() const {
  87. return eof;
  88. }
  89. uint8_t FileAccessAndroid::get_8() const {
  90. if (pos >= len) {
  91. eof = true;
  92. return 0;
  93. }
  94. uint8_t byte;
  95. AAsset_read(a, &byte, 1);
  96. pos++;
  97. return byte;
  98. }
  99. int FileAccessAndroid::get_buffer(uint8_t *p_dst, int p_length) const {
  100. off_t r = AAsset_read(a, p_dst, p_length);
  101. if (pos + p_length > len) {
  102. eof = true;
  103. }
  104. if (r >= 0) {
  105. pos += r;
  106. if (pos > len) {
  107. pos = len;
  108. }
  109. }
  110. return r;
  111. }
  112. Error FileAccessAndroid::get_error() const {
  113. return eof ? ERR_FILE_EOF : OK; //not sure what else it may happen
  114. }
  115. void FileAccessAndroid::flush() {
  116. ERR_FAIL();
  117. }
  118. void FileAccessAndroid::store_8(uint8_t p_dest) {
  119. ERR_FAIL();
  120. }
  121. bool FileAccessAndroid::file_exists(const String &p_path) {
  122. String path = fix_path(p_path).simplify_path();
  123. if (path.begins_with("/"))
  124. path = path.substr(1, path.length());
  125. else if (path.begins_with("res://"))
  126. path = path.substr(6, path.length());
  127. AAsset *at = AAssetManager_open(asset_manager, path.utf8().get_data(), AASSET_MODE_STREAMING);
  128. if (!at)
  129. return false;
  130. AAsset_close(at);
  131. return true;
  132. }
  133. FileAccessAndroid::FileAccessAndroid() {
  134. a = NULL;
  135. eof = false;
  136. }
  137. FileAccessAndroid::~FileAccessAndroid() {
  138. close();
  139. }