Merge pull request #98397 from adamscott/add-tmp-support

Add temp utilities (alias `OS::get_temp_dir()`, `FileAccess::create_temp()`, and `DirAccess::create_temp()`)
This commit is contained in:
Thaddeus Crews 2024-12-03 14:40:59 -06:00
commit 156bc92282
No known key found for this signature in database
GPG key ID: 62181B86FE9E5D84
26 changed files with 331 additions and 7 deletions

View file

@ -132,6 +132,18 @@ public class GodotIO {
return activity.getCacheDir().getAbsolutePath();
}
public String getTempDir() {
File tempDir = new File(getCacheDir() + "/tmp");
if (!tempDir.exists()) {
if (!tempDir.mkdirs()) {
Log.e(TAG, "Unable to create temp dir");
}
}
return tempDir.getAbsolutePath();
}
public String getDataDir() {
return activity.getFilesDir().getAbsolutePath();
}

View file

@ -52,6 +52,7 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc
_open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
_get_cache_dir = p_env->GetMethodID(cls, "getCacheDir", "()Ljava/lang/String;");
_get_temp_dir = p_env->GetMethodID(cls, "getTempDir", "()Ljava/lang/String;");
_get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
_get_display_cutouts = p_env->GetMethodID(cls, "getDisplayCutouts", "()[I"),
_get_display_safe_area = p_env->GetMethodID(cls, "getDisplaySafeArea", "()[I"),
@ -106,6 +107,17 @@ String GodotIOJavaWrapper::get_cache_dir() {
}
}
String GodotIOJavaWrapper::get_temp_dir() {
if (_get_temp_dir) {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL_V(env, String());
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_temp_dir);
return jstring_to_string(s, env);
} else {
return String();
}
}
String GodotIOJavaWrapper::get_user_data_dir() {
if (_get_data_dir) {
JNIEnv *env = get_jni_env();

View file

@ -48,6 +48,7 @@ private:
jmethodID _open_URI = 0;
jmethodID _get_cache_dir = 0;
jmethodID _get_data_dir = 0;
jmethodID _get_temp_dir = 0;
jmethodID _get_display_cutouts = 0;
jmethodID _get_display_safe_area = 0;
jmethodID _get_locale = 0;
@ -71,6 +72,7 @@ public:
Error open_uri(const String &p_uri);
String get_cache_dir();
String get_temp_dir();
String get_user_data_dir();
String get_locale();
String get_model();

View file

@ -677,6 +677,19 @@ String OS_Android::get_cache_path() const {
return ".";
}
String OS_Android::get_temp_path() const {
if (!temp_dir_cache.is_empty()) {
return temp_dir_cache;
}
String temp_dir = godot_io_java->get_temp_dir();
if (!temp_dir.is_empty()) {
temp_dir_cache = _remove_symlink(temp_dir);
return temp_dir_cache;
}
return ".";
}
String OS_Android::get_unique_id() const {
String unique_id = godot_io_java->get_unique_id();
if (!unique_id.is_empty()) {

View file

@ -58,6 +58,7 @@ private:
mutable String data_dir_cache;
mutable String cache_dir_cache;
mutable String temp_dir_cache;
mutable String remote_fs_dir;
AudioDriverOpenSL audio_driver_android;
@ -148,6 +149,7 @@ public:
virtual String get_user_data_dir() const override;
virtual String get_data_path() const override;
virtual String get_cache_path() const override;
virtual String get_temp_path() const override;
virtual String get_resource_dir() const override;
virtual String get_locale() const override;
virtual String get_model_name() const override;

View file

@ -117,6 +117,7 @@ public:
virtual String get_user_data_dir() const override;
virtual String get_cache_path() const override;
virtual String get_temp_path() const override;
virtual String get_locale() const override;

View file

@ -336,6 +336,19 @@ String OS_IOS::get_cache_path() const {
return ret;
}
String OS_IOS::get_temp_path() const {
static String ret;
if (ret.is_empty()) {
NSURL *url = [NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES];
if (url) {
ret = String::utf8([url.path UTF8String]);
ret = ret.trim_prefix("file://");
}
}
return ret;
}
String OS_IOS::get_locale() const {
NSString *preferedLanguage = [NSLocale preferredLanguages].firstObject;

View file

@ -92,6 +92,7 @@ public:
virtual String get_config_path() const override;
virtual String get_data_path() const override;
virtual String get_cache_path() const override;
virtual String get_temp_path() const override;
virtual String get_bundle_resource_dir() const override;
virtual String get_bundle_icon_path() const override;
virtual String get_godot_dir_name() const override;

View file

@ -276,6 +276,19 @@ String OS_MacOS::get_cache_path() const {
return get_config_path();
}
String OS_MacOS::get_temp_path() const {
static String ret;
if (ret.is_empty()) {
NSURL *url = [NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES];
if (url) {
ret = String::utf8([url.path UTF8String]);
ret = ret.trim_prefix("file://");
}
}
return ret;
}
String OS_MacOS::get_bundle_resource_dir() const {
String ret;

View file

@ -2077,16 +2077,37 @@ String OS_Windows::get_cache_path() const {
if (has_environment("LOCALAPPDATA")) {
cache_path_cache = get_environment("LOCALAPPDATA").replace("\\", "/");
}
if (cache_path_cache.is_empty() && has_environment("TEMP")) {
cache_path_cache = get_environment("TEMP").replace("\\", "/");
}
if (cache_path_cache.is_empty()) {
cache_path_cache = get_config_path();
cache_path_cache = get_temp_path();
}
}
return cache_path_cache;
}
String OS_Windows::get_temp_path() const {
static String temp_path_cache;
if (temp_path_cache.is_empty()) {
{
Vector<WCHAR> temp_path;
// The maximum possible size is MAX_PATH+1 (261) + terminating null character.
temp_path.resize(MAX_PATH + 2);
DWORD temp_path_length = GetTempPathW(temp_path.size(), temp_path.ptrw());
if (temp_path_length > 0 && temp_path_length < temp_path.size()) {
temp_path_cache = String::utf16((const char16_t *)temp_path.ptr());
// Let's try to get the long path instead of the short path (with tildes ~).
DWORD temp_path_long_length = GetLongPathNameW(temp_path.ptr(), temp_path.ptrw(), temp_path.size());
if (temp_path_long_length > 0 && temp_path_long_length < temp_path.size()) {
temp_path_cache = String::utf16((const char16_t *)temp_path.ptr());
}
}
}
if (temp_path_cache.is_empty()) {
temp_path_cache = get_config_path();
}
}
return temp_path_cache;
}
// Get properly capitalized engine name for system paths
String OS_Windows::get_godot_dir_name() const {
return String(VERSION_SHORT_NAME).capitalize();

View file

@ -226,6 +226,7 @@ public:
virtual String get_config_path() const override;
virtual String get_data_path() const override;
virtual String get_cache_path() const override;
virtual String get_temp_path() const override;
virtual String get_godot_dir_name() const override;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;