dir_access_jandroid.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*************************************************************************/
  2. /* dir_access_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "dir_access_jandroid.h"
  31. #include "core/string/print_string.h"
  32. #include "file_access_android.h"
  33. #include "string_android.h"
  34. #include "thread_jandroid.h"
  35. jobject DirAccessJAndroid::io = nullptr;
  36. jclass DirAccessJAndroid::cls = nullptr;
  37. jmethodID DirAccessJAndroid::_dir_open = nullptr;
  38. jmethodID DirAccessJAndroid::_dir_next = nullptr;
  39. jmethodID DirAccessJAndroid::_dir_close = nullptr;
  40. jmethodID DirAccessJAndroid::_dir_is_dir = nullptr;
  41. DirAccess *DirAccessJAndroid::create_fs() {
  42. return memnew(DirAccessJAndroid);
  43. }
  44. Error DirAccessJAndroid::list_dir_begin() {
  45. list_dir_end();
  46. JNIEnv *env = get_jni_env();
  47. jstring js = env->NewStringUTF(current_dir.utf8().get_data());
  48. int res = env->CallIntMethod(io, _dir_open, js);
  49. if (res <= 0) {
  50. return ERR_CANT_OPEN;
  51. }
  52. id = res;
  53. return OK;
  54. }
  55. String DirAccessJAndroid::get_next() {
  56. ERR_FAIL_COND_V(id == 0, "");
  57. JNIEnv *env = get_jni_env();
  58. jstring str = (jstring)env->CallObjectMethod(io, _dir_next, id);
  59. if (!str) {
  60. return "";
  61. }
  62. String ret = jstring_to_string((jstring)str, env);
  63. env->DeleteLocalRef((jobject)str);
  64. return ret;
  65. }
  66. bool DirAccessJAndroid::current_is_dir() const {
  67. JNIEnv *env = get_jni_env();
  68. return env->CallBooleanMethod(io, _dir_is_dir, id);
  69. }
  70. bool DirAccessJAndroid::current_is_hidden() const {
  71. return current != "." && current != ".." && current.begins_with(".");
  72. }
  73. void DirAccessJAndroid::list_dir_end() {
  74. if (id == 0) {
  75. return;
  76. }
  77. JNIEnv *env = get_jni_env();
  78. env->CallVoidMethod(io, _dir_close, id);
  79. id = 0;
  80. }
  81. int DirAccessJAndroid::get_drive_count() {
  82. return 0;
  83. }
  84. String DirAccessJAndroid::get_drive(int p_drive) {
  85. return "";
  86. }
  87. Error DirAccessJAndroid::change_dir(String p_dir) {
  88. JNIEnv *env = get_jni_env();
  89. if (p_dir.is_empty() || p_dir == "." || (p_dir == ".." && current_dir.is_empty())) {
  90. return OK;
  91. }
  92. String new_dir;
  93. if (p_dir != "res://" && p_dir.length() > 1 && p_dir.ends_with("/")) {
  94. p_dir = p_dir.substr(0, p_dir.length() - 1);
  95. }
  96. if (p_dir.begins_with("/")) {
  97. new_dir = p_dir.substr(1, p_dir.length());
  98. } else if (p_dir.begins_with("res://")) {
  99. new_dir = p_dir.substr(6, p_dir.length());
  100. } else if (current_dir.is_empty()) {
  101. new_dir = p_dir;
  102. } else {
  103. new_dir = current_dir.plus_file(p_dir);
  104. }
  105. //test if newdir exists
  106. new_dir = new_dir.simplify_path();
  107. jstring js = env->NewStringUTF(new_dir.utf8().get_data());
  108. int res = env->CallIntMethod(io, _dir_open, js);
  109. env->DeleteLocalRef(js);
  110. if (res <= 0) {
  111. return ERR_INVALID_PARAMETER;
  112. }
  113. env->CallVoidMethod(io, _dir_close, res);
  114. current_dir = new_dir;
  115. return OK;
  116. }
  117. String DirAccessJAndroid::get_current_dir(bool p_include_drive) {
  118. return "res://" + current_dir;
  119. }
  120. bool DirAccessJAndroid::file_exists(String p_file) {
  121. String sd;
  122. if (current_dir.is_empty()) {
  123. sd = p_file;
  124. } else {
  125. sd = current_dir.plus_file(p_file);
  126. }
  127. FileAccessAndroid *f = memnew(FileAccessAndroid);
  128. bool exists = f->file_exists(sd);
  129. memdelete(f);
  130. return exists;
  131. }
  132. bool DirAccessJAndroid::dir_exists(String p_dir) {
  133. JNIEnv *env = get_jni_env();
  134. String sd;
  135. if (current_dir.is_empty()) {
  136. sd = p_dir;
  137. } else {
  138. if (p_dir.is_relative_path()) {
  139. sd = current_dir.plus_file(p_dir);
  140. } else {
  141. sd = fix_path(p_dir);
  142. }
  143. }
  144. String path = sd.simplify_path();
  145. if (path.begins_with("/")) {
  146. path = path.substr(1, path.length());
  147. } else if (path.begins_with("res://")) {
  148. path = path.substr(6, path.length());
  149. }
  150. jstring js = env->NewStringUTF(path.utf8().get_data());
  151. int res = env->CallIntMethod(io, _dir_open, js);
  152. env->DeleteLocalRef(js);
  153. if (res <= 0) {
  154. return false;
  155. }
  156. env->CallVoidMethod(io, _dir_close, res);
  157. return true;
  158. }
  159. Error DirAccessJAndroid::make_dir(String p_dir) {
  160. ERR_FAIL_V(ERR_UNAVAILABLE);
  161. }
  162. Error DirAccessJAndroid::rename(String p_from, String p_to) {
  163. ERR_FAIL_V(ERR_UNAVAILABLE);
  164. }
  165. Error DirAccessJAndroid::remove(String p_name) {
  166. ERR_FAIL_V(ERR_UNAVAILABLE);
  167. }
  168. String DirAccessJAndroid::get_filesystem_type() const {
  169. return "APK";
  170. }
  171. uint64_t DirAccessJAndroid::get_space_left() {
  172. return 0;
  173. }
  174. void DirAccessJAndroid::setup(jobject p_io) {
  175. JNIEnv *env = get_jni_env();
  176. io = p_io;
  177. jclass c = env->GetObjectClass(io);
  178. cls = (jclass)env->NewGlobalRef(c);
  179. _dir_open = env->GetMethodID(cls, "dir_open", "(Ljava/lang/String;)I");
  180. _dir_next = env->GetMethodID(cls, "dir_next", "(I)Ljava/lang/String;");
  181. _dir_close = env->GetMethodID(cls, "dir_close", "(I)V");
  182. _dir_is_dir = env->GetMethodID(cls, "dir_is_dir", "(I)Z");
  183. //(*env)->CallVoidMethod(env,obj,aMethodID, myvar);
  184. }
  185. DirAccessJAndroid::DirAccessJAndroid() {
  186. id = 0;
  187. }
  188. DirAccessJAndroid::~DirAccessJAndroid() {
  189. list_dir_end();
  190. }