dir_access_android.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* dir_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. #ifdef ANDROID_NATIVE_ACTIVITY
  30. #include "dir_access_android.h"
  31. #include "file_access_android.h"
  32. DirAccess *DirAccessAndroid::create_fs() {
  33. return memnew(DirAccessAndroid);
  34. }
  35. bool DirAccessAndroid::list_dir_begin() {
  36. list_dir_end();
  37. AAssetDir* aad = AAssetManager_openDir(FileAccessAndroid::asset_manager,current_dir.utf8().get_data());
  38. if (!aad)
  39. return true; //nothing
  40. return false;
  41. }
  42. String DirAccessAndroid::get_next(){
  43. const char* fn= AAssetDir_getNextFileName(aad);
  44. if (!fn)
  45. return "";
  46. String s;
  47. s.parse_utf8(fn);
  48. current=s;
  49. return s;
  50. }
  51. bool DirAccessAndroid::current_is_dir() const{
  52. String sd;
  53. if (current_dir=="")
  54. sd=current;
  55. else
  56. sd=current_dir+"/"+current;
  57. AAssetDir* aad2 = AAssetManager_openDir(FileAccessAndroid::asset_manager,sd.utf8().get_data());
  58. if (aad2) {
  59. AAssetDir_close(aad2);
  60. return true;
  61. }
  62. return false;
  63. }
  64. bool DirAccessAndroid::current_is_hidden() const{
  65. return current!="." && current!=".." && current.begins_with(".");
  66. }
  67. void DirAccessAndroid::list_dir_end(){
  68. if (aad==NULL)
  69. return;
  70. AAssetDir_close(aad);
  71. aad=NULL;
  72. }
  73. int DirAccessAndroid::get_drive_count(){
  74. return 0;
  75. }
  76. String DirAccessAndroid::get_drive(int p_drive){
  77. return "";
  78. }
  79. Error DirAccessAndroid::change_dir(String p_dir){
  80. p_dir=p_dir.simplify_path();
  81. if (p_dir=="" || p_dir=="." || (p_dir==".." && current_dir==""))
  82. return OK;
  83. String new_dir;
  84. if (p_dir.begins_with("/"))
  85. new_dir=p_dir.substr(1,p_dir.length());
  86. else if (p_dir.begins_with("res://"))
  87. new_dir=p_dir.substr(6,p_dir.length());
  88. else //relative
  89. new_dir=new_dir+"/"+p_dir;
  90. //test if newdir exists
  91. new_dir=new_dir.simplify_path();
  92. AAssetDir* aad = AAssetManager_openDir(FileAccessAndroid::asset_manager,new_dir.utf8().get_data());
  93. if (aad) {
  94. current_dir=new_dir;
  95. AAssetDir_close(aad);
  96. return OK;
  97. }
  98. return ERR_INVALID_PARAMETER;
  99. }
  100. String DirAccessAndroid::get_current_dir(){
  101. return "/"+current_dir;
  102. }
  103. bool DirAccessAndroid::file_exists(String p_file){
  104. String sd;
  105. if (current_dir=="")
  106. sd=p_file;
  107. else
  108. sd=current_dir+"/"+p_file;
  109. AAsset *a=AAssetManager_open(FileAccessAndroid::asset_manager,sd.utf8().get_data(),AASSET_MODE_STREAMING);
  110. if (a) {
  111. AAsset_close(a);
  112. return true;
  113. }
  114. return false;
  115. }
  116. Error DirAccessAndroid::make_dir(String p_dir){
  117. ERR_FAIL_V(ERR_UNAVAILABLE);
  118. }
  119. Error DirAccessAndroid::rename(String p_from, String p_to){
  120. ERR_FAIL_V(ERR_UNAVAILABLE);
  121. }
  122. Error DirAccessAndroid::remove(String p_name){
  123. ERR_FAIL_V(ERR_UNAVAILABLE);
  124. }
  125. //FileType get_file_type() const;
  126. size_t DirAccessAndroid::get_space_left() {
  127. return 0;
  128. }
  129. void DirAccessAndroid::make_default() {
  130. instance_func=create_fs;
  131. }
  132. DirAccessAndroid::DirAccessAndroid() {
  133. aad=NULL;
  134. }
  135. DirAccessAndroid::~DirAccessAndroid() {
  136. list_dir_end();;
  137. }
  138. #endif