dir_access_android.cpp 5.0 KB

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