file_access_android.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************/
  2. /* file_access_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "file_access_android.h"
  30. #include "print_string.h"
  31. #ifdef ANDROID_NATIVE_ACTIVITY
  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_pos() 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 (r>=0) {
  102. pos+=r;
  103. if (pos>len) {
  104. pos=len;
  105. eof=true;
  106. }
  107. }
  108. return r;
  109. }
  110. Error FileAccessAndroid::get_error() const {
  111. return eof?ERR_FILE_EOF:OK; //not sure what else it may happen
  112. }
  113. void FileAccessAndroid::store_8(uint8_t p_dest) {
  114. ERR_FAIL();
  115. }
  116. bool FileAccessAndroid::file_exists(const String& p_path) {
  117. String path=fix_path(p_path).simplify_path();
  118. if (path.begins_with("/"))
  119. path=path.substr(1,path.length());
  120. else if (path.begins_with("res://"))
  121. path=path.substr(6,path.length());
  122. AAsset *at=AAssetManager_open(asset_manager,path.utf8().get_data(),AASSET_MODE_STREAMING);
  123. if (!at)
  124. return false;
  125. AAsset_close(at);
  126. return true;
  127. }
  128. FileAccessAndroid::FileAccessAndroid() {
  129. a=NULL;
  130. eof=false;
  131. }
  132. FileAccessAndroid::~FileAccessAndroid()
  133. {
  134. close();
  135. }
  136. #endif