Fixed a possible app crash when calling AndroidStorage::GetUserDirectory

This commit is contained in:
OpenSauce04 2026-03-07 21:40:15 +00:00 committed by OpenSauce
parent 96485a22f8
commit 8e1ffc1bdc
2 changed files with 22 additions and 7 deletions

View File

@ -698,7 +698,7 @@ object NativeLibrary {
val dirSep = "/"
val pathSegment = uri.lastPathSegment!!
val pathSegment = uri.lastPathSegment ?: return ""
val virtualPath = pathSegment.substringAfter(":")
if (pathSegment.startsWith("primary:")) { // User directory is located in primary storage
@ -723,7 +723,8 @@ object NativeLibrary {
fun getUserDirectory(): String {
val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext)
return getNativePath(preferences.getString("CITRA_DIRECTORY", "")!!.toUri())
val userDirectoryUri = preferences.getString("CITRA_DIRECTORY", "")!!.toUri()
return getNativePath(userDirectoryUri)
}
@Keep

View File

@ -163,13 +163,27 @@ std::optional<std::string> GetUserDirectory() {
throw std::runtime_error(
"Unable to locate user directory: Function with ID 'get_user_directory' is missing");
}
auto env = GetEnvForThread();
auto j_user_directory =
(jstring)(env->CallStaticObjectMethod(native_library, get_user_directory, nullptr));
auto result = env->GetStringUTFChars(j_user_directory, nullptr);
if (result == "") {
jstring j_user_directory =
(jstring)env->CallStaticObjectMethod(native_library, get_user_directory);
if (env->ExceptionCheck() || j_user_directory == nullptr) {
env->ExceptionClear();
return std::nullopt;
}
const char* chars = env->GetStringUTFChars(j_user_directory, nullptr);
std::string result = chars ? chars : "";
env->ReleaseStringUTFChars(j_user_directory, chars);
if (result.empty()) {
return std::nullopt;
}
return result;
}
@ -296,7 +310,7 @@ bool MoveAndRenameFile(const std::string& src_full_path, const std::string& dest
std::string TranslateFilePath(const std::string& filepath) {
std::optional<std::string> userDirLocation = GetUserDirectory();
if (userDirLocation) {
return *userDirLocation + "/" + filepath;
return *userDirLocation + filepath;
}
return "";
}