diff options
author | Marcel Admiraal <madmiraal@users.noreply.github.com> | 2021-06-08 17:43:10 +0100 |
---|---|---|
committer | Marcel Admiraal <madmiraal@users.noreply.github.com> | 2021-06-10 16:48:17 +0100 |
commit | b3a962945e82097fd472c6e1724785b11eaaa32f (patch) | |
tree | 71473572d7946b4eebf3650364404a07395ce037 /platform | |
parent | 9539e4e39e489e750c7ec99d84b93dce283fe721 (diff) |
Add OS.get_external_data_dir() to get Android external directory
Diffstat (limited to 'platform')
-rw-r--r-- | platform/android/java/lib/src/org/godotengine/godot/GodotIO.java | 4 | ||||
-rw-r--r-- | platform/android/java_godot_io_wrapper.cpp | 12 | ||||
-rw-r--r-- | platform/android/java_godot_io_wrapper.h | 2 | ||||
-rw-r--r-- | platform/android/os_android.cpp | 43 | ||||
-rw-r--r-- | platform/android/os_android.h | 1 |
5 files changed, 45 insertions, 17 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java index c7c7c1b40c..8a345de5ea 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java @@ -452,6 +452,10 @@ public class GodotIO { return activity.getFilesDir().getAbsolutePath(); } + public String getExternalDataDir() { + return activity.getExternalFilesDir(null).getAbsolutePath(); + } + public String getLocale() { return Locale.getDefault().toString(); } diff --git a/platform/android/java_godot_io_wrapper.cpp b/platform/android/java_godot_io_wrapper.cpp index ec3b6f8ac0..5e99135498 100644 --- a/platform/android/java_godot_io_wrapper.cpp +++ b/platform/android/java_godot_io_wrapper.cpp @@ -49,6 +49,7 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc _open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I"); _get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;"); + _get_external_data_dir = p_env->GetMethodID(cls, "getExternalDataDir", "()Ljava/lang/String;"); _get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;"); _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;"); _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I"); @@ -92,6 +93,17 @@ String GodotIOJavaWrapper::get_user_data_dir() { } } +String GodotIOJavaWrapper::get_external_data_dir() { + if (_get_external_data_dir) { + JNIEnv *env = get_jni_env(); + ERR_FAIL_COND_V(env == nullptr, String()); + jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_external_data_dir); + return jstring_to_string(s, env); + } else { + return String(); + } +} + String GodotIOJavaWrapper::get_locale() { if (_get_locale) { JNIEnv *env = get_jni_env(); diff --git a/platform/android/java_godot_io_wrapper.h b/platform/android/java_godot_io_wrapper.h index 394e97effa..e4c0a4b2c7 100644 --- a/platform/android/java_godot_io_wrapper.h +++ b/platform/android/java_godot_io_wrapper.h @@ -47,6 +47,7 @@ private: jmethodID _open_URI = 0; jmethodID _get_data_dir = 0; + jmethodID _get_external_data_dir = 0; jmethodID _get_locale = 0; jmethodID _get_model = 0; jmethodID _get_screen_DPI = 0; @@ -66,6 +67,7 @@ public: Error open_uri(const String &p_uri); String get_user_data_dir(); + String get_external_data_dir(); String get_locale(); String get_model(); int get_screen_dpi(); diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 422814dd50..222976d948 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -45,6 +45,23 @@ #include "java_godot_io_wrapper.h" #include "java_godot_wrapper.h" +String _remove_symlink(const String &dir) { + // Workaround for Android 6.0+ using a symlink. + // Save the current directory. + char current_dir_name[2048]; + getcwd(current_dir_name, 2048); + // Change directory to the external data directory. + chdir(dir.utf8().get_data()); + // Get the actual directory without the potential symlink. + char dir_name_wihout_symlink[2048]; + getcwd(dir_name_wihout_symlink, 2048); + // Convert back to a String. + String dir_without_symlink(dir_name_wihout_symlink); + // Restore original current directory. + chdir(current_dir_name); + return dir_without_symlink; +} + class AndroidLogger : public Logger { public: virtual void logv(const char *p_format, va_list p_list, bool p_err) { @@ -199,26 +216,18 @@ String OS_Android::get_user_data_dir() const { String data_dir = godot_io_java->get_user_data_dir(); if (data_dir != "") { - //store current dir - char real_current_dir_name[2048]; - getcwd(real_current_dir_name, 2048); - - //go to data dir - chdir(data_dir.utf8().get_data()); - - //get actual data dir, so we resolve potential symlink (Android 6.0+ seems to use symlink) - char data_current_dir_name[2048]; - getcwd(data_current_dir_name, 2048); - - //cache by parsing utf8 - data_dir_cache.parse_utf8(data_current_dir_name); - - //restore original dir so we don't mess things up - chdir(real_current_dir_name); - + data_dir_cache = _remove_symlink(data_dir); return data_dir_cache; } + return "."; +} +String OS_Android::get_external_data_dir() const { + String data_dir = godot_io_java->get_external_data_dir(); + if (data_dir != "") { + data_dir = _remove_symlink(data_dir); + return data_dir; + } return "."; } diff --git a/platform/android/os_android.h b/platform/android/os_android.h index dd14b69cf9..cfa1e4b1e7 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -111,6 +111,7 @@ public: virtual Error shell_open(String p_uri) override; virtual String get_user_data_dir() const override; + virtual String get_external_data_dir() const override; virtual String get_resource_dir() const override; virtual String get_locale() const override; virtual String get_model_name() const override; |