dir_access_android.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. void DirAccessAndroid::list_dir_end(){
  65. if (aad==NULL)
  66. return;
  67. AAssetDir_close(aad);
  68. aad=NULL;
  69. }
  70. int DirAccessAndroid::get_drive_count(){
  71. return 0;
  72. }
  73. String DirAccessAndroid::get_drive(int p_drive){
  74. return "";
  75. }
  76. Error DirAccessAndroid::change_dir(String p_dir){
  77. p_dir=p_dir.simplify_path();
  78. if (p_dir=="" || p_dir=="." || (p_dir==".." && current_dir==""))
  79. return OK;
  80. String new_dir;
  81. if (p_dir.begins_with("/"))
  82. new_dir=p_dir.substr(1,p_dir.length());
  83. else if (p_dir.begins_with("res://"))
  84. new_dir=p_dir.substr(6,p_dir.length());
  85. else //relative
  86. new_dir=new_dir+"/"+p_dir;
  87. //test if newdir exists
  88. new_dir=new_dir.simplify_path();
  89. AAssetDir* aad = AAssetManager_openDir(FileAccessAndroid::asset_manager,new_dir.utf8().get_data());
  90. if (aad) {
  91. current_dir=new_dir;
  92. AAssetDir_close(aad);
  93. return OK;
  94. }
  95. return ERR_INVALID_PARAMETER;
  96. }
  97. String DirAccessAndroid::get_current_dir(){
  98. return "/"+current_dir;
  99. }
  100. bool DirAccessAndroid::file_exists(String p_file){
  101. String sd;
  102. if (current_dir=="")
  103. sd=p_file;
  104. else
  105. sd=current_dir+"/"+p_file;
  106. AAsset *a=AAssetManager_open(FileAccessAndroid::asset_manager,sd.utf8().get_data(),AASSET_MODE_STREAMING);
  107. if (a) {
  108. AAsset_close(a);
  109. return true;
  110. }
  111. return false;
  112. }
  113. Error DirAccessAndroid::make_dir(String p_dir){
  114. ERR_FAIL_V(ERR_UNAVAILABLE);
  115. }
  116. Error DirAccessAndroid::rename(String p_from, String p_to){
  117. ERR_FAIL_V(ERR_UNAVAILABLE);
  118. }
  119. Error DirAccessAndroid::remove(String p_name){
  120. ERR_FAIL_V(ERR_UNAVAILABLE);
  121. }
  122. //FileType get_file_type() const;
  123. size_t DirAccessAndroid::get_space_left() {
  124. return 0;
  125. }
  126. void DirAccessAndroid::make_default() {
  127. instance_func=create_fs;
  128. }
  129. DirAccessAndroid::DirAccessAndroid() {
  130. aad=NULL;
  131. }
  132. DirAccessAndroid::~DirAccessAndroid() {
  133. list_dir_end();;
  134. }
  135. #endif