From 25f01cb09d6baa67c4ad1cb1133e040f9b3b441d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 22 Feb 2021 22:54:12 +0100 Subject: [PATCH] Implement get_cache_path() for iOS, and improve it for Android and Windows --- platform/android/os_android.cpp | 7 +++++-- platform/android/os_android.h | 1 + platform/iphone/app_delegate.mm | 7 +++++-- platform/iphone/godot_iphone.mm | 4 ++-- platform/iphone/os_iphone.h | 4 +++- platform/iphone/os_iphone.mm | 7 ++++++- platform/windows/os_windows.cpp | 29 +++++++++++++++++++---------- 7 files changed, 41 insertions(+), 18 deletions(-) diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index ec3d1210251..8088c2e850b 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -452,10 +452,13 @@ String OS_Android::get_user_data_dir() const { } String OS_Android::get_cache_path() const { + if (cache_dir_cache != String()) + return cache_dir_cache; + String cache_dir = godot_io_java->get_cache_dir(); if (cache_dir != "") { - cache_dir = _remove_symlink(cache_dir); - return cache_dir; + cache_dir_cache = _remove_symlink(cache_dir); + return cache_dir_cache; } return "."; } diff --git a/platform/android/os_android.h b/platform/android/os_android.h index 7f369d89334..6fd20c4b23c 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -50,6 +50,7 @@ class OS_Android : public OS_Unix { VisualServer *visual_server; mutable String data_dir_cache; + mutable String cache_dir_cache; AudioDriverOpenSL audio_driver_android; diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm index d7d815d835d..1a6381a1a7c 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -44,7 +44,7 @@ extern int gargc; extern char **gargv; -extern int iphone_main(int, char **, String); +extern int iphone_main(int, char **, String, String); extern void iphone_finish(); @implementation AppDelegate @@ -63,8 +63,11 @@ static ViewController *mainViewController = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; + paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, + NSUserDomainMask, YES); + NSString *cacheDirectory = [paths objectAtIndex:0]; - int err = iphone_main(gargc, gargv, String::utf8([documentsDirectory UTF8String])); + int err = iphone_main(gargc, gargv, String::utf8([documentsDirectory UTF8String]), String::utf8([cacheDirectory UTF8String])); if (err != 0) { // bail, things did not go very well for us, should probably output a message on screen with our error code... exit(0); diff --git a/platform/iphone/godot_iphone.mm b/platform/iphone/godot_iphone.mm index 78676fe0096..e030ae2df19 100644 --- a/platform/iphone/godot_iphone.mm +++ b/platform/iphone/godot_iphone.mm @@ -70,7 +70,7 @@ int add_cmdline(int p_argc, char **p_args) { return p_argc; } -int iphone_main(int argc, char **argv, String data_dir) { +int iphone_main(int argc, char **argv, String data_dir, String cache_dir) { size_t len = strlen(argv[0]); while (len--) { @@ -90,7 +90,7 @@ int iphone_main(int argc, char **argv, String data_dir) { char cwd[512]; getcwd(cwd, sizeof(cwd)); printf("cwd %s\n", cwd); - os = new OSIPhone(data_dir); + os = new OSIPhone(data_dir, cache_dir); char *fargv[64]; for (int i = 0; i < argc; i++) { diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index ad9a8eed72b..a7a70c33528 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -81,6 +81,7 @@ private: void set_data_dir(String p_dir); String data_dir; + String cache_dir; InputDefault *input; @@ -93,7 +94,7 @@ private: public: static OSIPhone *get_singleton(); - OSIPhone(String p_data_dir); + OSIPhone(String p_data_dir, String p_cache_dir); ~OSIPhone(); bool iterate(); @@ -115,6 +116,7 @@ public: Error shell_open(String p_uri); String get_user_data_dir() const; + String get_cache_path() const; String get_locale() const; diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 21fed4a187b..c115bc09976 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -494,6 +494,10 @@ String OSIPhone::get_clipboard() const { return String::utf8([text UTF8String]); } +String OSIPhone::get_cache_path() const { + return cache_dir; +} + String OSIPhone::get_model_name() const { String model = ios->get_model(); if (model != "") { @@ -668,7 +672,7 @@ void add_ios_init_callback(init_callback cb) { } } -OSIPhone::OSIPhone(String p_data_dir) { +OSIPhone::OSIPhone(String p_data_dir, String p_cache_dir) { for (int i = 0; i < ios_init_callbacks_count; ++i) { ios_init_callbacks[i](); } @@ -683,6 +687,7 @@ OSIPhone::OSIPhone(String p_data_dir) { // can't call set_data_dir from here, since it requires DirAccess // which is initialized in initialize_core data_dir = p_data_dir; + cache_dir = p_cache_dir; Vector loggers; loggers.push_back(memnew(SyslogLogger)); diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 5aa1f329e17..a6e1bc8c6f3 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -3378,18 +3378,27 @@ String OS_Windows::get_data_path() const { } String OS_Windows::get_cache_path() const { - // The XDG Base Directory specification technically only applies on Linux/*BSD, but it doesn't hurt to support it on Windows as well. - if (has_environment("XDG_CACHE_HOME")) { - if (get_environment("XDG_CACHE_HOME").is_abs_path()) { - return get_environment("XDG_CACHE_HOME").replace("\\", "/"); - } else { - WARN_PRINT_ONCE("`XDG_CACHE_HOME` is a relative path. Ignoring its value and falling back to `%TEMP%` or `get_config_path()` per the XDG Base Directory specification."); + static String cache_path_cache; + if (cache_path_cache == String()) { + // The XDG Base Directory specification technically only applies on Linux/*BSD, but it doesn't hurt to support it on Windows as well. + if (has_environment("XDG_CACHE_HOME")) { + if (get_environment("XDG_CACHE_HOME").is_abs_path()) { + cache_path_cache = get_environment("XDG_CACHE_HOME").replace("\\", "/"); + } else { + WARN_PRINT_ONCE("`XDG_CACHE_HOME` is a relative path. Ignoring its value and falling back to `%LOCALAPPDATA%\\cache`, `%TEMP%` or `get_config_path()` per the XDG Base Directory specification."); + } + } + if (cache_path_cache == String() && has_environment("LOCALAPPDATA")) { + cache_path_cache = get_environment("LOCALAPPDATA").replace("\\", "/"); + } + if (cache_path_cache == String() && has_environment("TEMP")) { + cache_path_cache = get_environment("TEMP").replace("\\", "/"); + } + if (cache_path_cache == String()) { + cache_path_cache = get_config_path(); } } - if (has_environment("TEMP")) { - return get_environment("TEMP").replace("\\", "/"); - } - return get_config_path(); + return cache_path_cache; } // Get properly capitalized engine name for system paths